|
1 | 1 | package com.spotify.confidence; |
2 | 2 |
|
| 3 | +import com.spotify.confidence.flags.resolver.v1.MaterializationMap; |
3 | 4 | import com.spotify.confidence.flags.resolver.v1.ResolveWithStickyRequest; |
4 | 5 | import com.spotify.confidence.flags.resolver.v1.ResolveWithStickyResponse; |
5 | 6 | import com.spotify.confidence.shaded.flags.resolver.v1.ResolveFlagsRequest; |
|
8 | 9 | import java.util.List; |
9 | 10 | import java.util.Map; |
10 | 11 | import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.ExecutionException; |
11 | 13 | import java.util.concurrent.atomic.AtomicReference; |
12 | 14 | import java.util.concurrent.locks.ReentrantLock; |
13 | 15 | import java.util.stream.Collectors; |
@@ -147,36 +149,46 @@ private ResolveWithStickyRequest handleMissingMaterializations( |
147 | 149 | missingItems.stream() |
148 | 150 | .collect( |
149 | 151 | Collectors.groupingBy( |
150 | | - ResolveWithStickyResponse.MissingMaterializationItem::getUnit, |
151 | | - Collectors.toMap( |
152 | | - ResolveWithStickyResponse.MissingMaterializationItem |
153 | | - ::getReadMaterialization, |
154 | | - item -> List.of(item.getRule()), |
155 | | - (existing, replacement) -> { |
156 | | - final var combined = new java.util.ArrayList<>(existing); |
157 | | - combined.addAll(replacement); |
158 | | - return combined; |
159 | | - }))); |
| 152 | + ResolveWithStickyResponse.MissingMaterializationItem::getUnit)); |
160 | 153 |
|
161 | | - // Load materialized assignments for all missing units |
162 | | - final var materializationContext = request.getMaterializationContext().toBuilder(); |
| 154 | + final HashMap<String, MaterializationMap> materializationPerUnitMap = new HashMap<>(); |
163 | 155 |
|
| 156 | + // Load materialized assignments for all missing units |
164 | 157 | missingByUnit.forEach( |
165 | | - (unit, materializationsToRules) -> { |
166 | | - try { |
167 | | - final var loadedAssignments = |
168 | | - repository.loadMaterializedAssignmentsForUnit(unit, materializationsToRules).get(); |
169 | | - loadedAssignments.forEach( |
170 | | - (materialization, info) -> |
171 | | - materializationContext.putUnitMaterializationInfo(unit, info.toProto())); |
172 | | - } catch (Exception e) { |
173 | | - throw new RuntimeException( |
174 | | - "Failed to load materialized assignments for unit: " + unit, e); |
175 | | - } |
| 158 | + (unit, materializationInfoItem) -> { |
| 159 | + materializationInfoItem.forEach( |
| 160 | + item -> { |
| 161 | + final Map<String, MaterializationInfo> loadedAssignments; |
| 162 | + try { |
| 163 | + loadedAssignments = |
| 164 | + repository |
| 165 | + .loadMaterializedAssignmentsForUnit(unit, item.getReadMaterialization()) |
| 166 | + .get(); |
| 167 | + } catch (InterruptedException | ExecutionException e) { |
| 168 | + throw new RuntimeException(e); |
| 169 | + } |
| 170 | + materializationPerUnitMap.put( |
| 171 | + unit, |
| 172 | + MaterializationMap.newBuilder() |
| 173 | + .putAllInfoMap( |
| 174 | + loadedAssignments.entrySet().stream() |
| 175 | + .collect( |
| 176 | + Collectors.toMap( |
| 177 | + Map.Entry::getKey, |
| 178 | + e -> { |
| 179 | + final var info = e.getValue(); |
| 180 | + return com.spotify.confidence.flags.resolver.v1 |
| 181 | + .MaterializationInfo.newBuilder() |
| 182 | + .setUnitInInfo(info.isUnitInMaterialization()) |
| 183 | + .putAllRuleToVariant(info.ruleToVariant()) |
| 184 | + .build(); |
| 185 | + }))) |
| 186 | + .build()); |
| 187 | + }); |
176 | 188 | }); |
177 | 189 |
|
178 | 190 | // Return new request with updated materialization context |
179 | | - return request.toBuilder().setMaterializationContext(materializationContext.build()).build(); |
| 191 | + return request.toBuilder().putAllMaterializationsPerUnit(materializationPerUnitMap).build(); |
180 | 192 | } |
181 | 193 |
|
182 | 194 | public ResolveFlagsResponse resolve(ResolveFlagsRequest request) { |
|
0 commit comments