-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathComparisonStrategy.java
More file actions
248 lines (230 loc) · 9.64 KB
/
ComparisonStrategy.java
File metadata and controls
248 lines (230 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package dev.openfeature.sdk.multiprovider;
import dev.openfeature.sdk.ErrorCode;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.FeatureProvider;
import dev.openfeature.sdk.ProviderEvaluation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import lombok.Getter;
/**
* Comparison strategy.
*
* <p>Evaluates all providers in parallel and compares successful results.
* If all providers agree on the value, the fallback provider's result is returned.
* If providers disagree, the optional {@code onMismatch} callback is invoked
* and the fallback provider's result is returned.
* If any provider returns an error, all errors are collected and a
* {@link ErrorCode#GENERAL} error is returned.
*/
public class ComparisonStrategy implements Strategy {
private static final long DEFAULT_TIMEOUT_MS = 30_000;
@Getter
private final String fallbackProvider;
private final BiConsumer<String, Map<String, ProviderEvaluation<?>>> onMismatch;
private final ExecutorService executorService;
private final boolean ownsExecutorService;
private final long timeoutMs;
/**
* Constructs a comparison strategy with a fallback provider.
*
* <p>Uses a shared {@link ForkJoinPool#commonPool()} for parallel evaluation.
*
* @param fallbackProvider provider name to use as fallback when successful
* providers disagree
*/
public ComparisonStrategy(String fallbackProvider) {
this(fallbackProvider, null);
}
/**
* Constructs a comparison strategy with fallback provider and mismatch callback.
*
* <p>Uses a shared {@link ForkJoinPool#commonPool()} for parallel evaluation.
*
* @param fallbackProvider provider name to use as fallback when successful
* providers disagree
* @param onMismatch callback invoked with all successful evaluations
* when they disagree
*/
public ComparisonStrategy(
String fallbackProvider,
BiConsumer<String, Map<String, ProviderEvaluation<?>>> onMismatch) {
this(fallbackProvider, onMismatch, ForkJoinPool.commonPool(),
false, DEFAULT_TIMEOUT_MS);
}
/**
* Constructs a comparison strategy with a caller-supplied executor.
*
* @param fallbackProvider provider name to use as fallback when successful
* providers disagree
* @param onMismatch callback invoked with all successful evaluations
* when they disagree (may be {@code null})
* @param executorService executor to use for parallel evaluation
* @param timeoutMs maximum time in milliseconds to wait for all
* providers to complete
*/
public ComparisonStrategy(
String fallbackProvider,
BiConsumer<String, Map<String, ProviderEvaluation<?>>> onMismatch,
ExecutorService executorService,
long timeoutMs) {
this(fallbackProvider, onMismatch, executorService,
false, timeoutMs);
}
private ComparisonStrategy(
String fallbackProvider,
BiConsumer<String, Map<String, ProviderEvaluation<?>>> onMismatch,
ExecutorService executorService,
boolean ownsExecutorService,
long timeoutMs) {
this.fallbackProvider = Objects.requireNonNull(
fallbackProvider, "fallbackProvider must not be null");
this.onMismatch = onMismatch;
this.executorService = Objects.requireNonNull(
executorService, "executorService must not be null");
this.ownsExecutorService = ownsExecutorService;
this.timeoutMs = timeoutMs;
}
@Override
public <T> ProviderEvaluation<T> evaluate(
Map<String, FeatureProvider> providers,
String key,
T defaultValue,
EvaluationContext ctx,
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction) {
if (providers.isEmpty()) {
return ProviderEvaluation.<T>builder()
.errorCode(ErrorCode.GENERAL)
.errorMessage("No providers configured")
.build();
}
if (!providers.containsKey(fallbackProvider)) {
throw new IllegalArgumentException(
"fallbackProvider not found in providers: "
+ fallbackProvider);
}
Map<String, ProviderEvaluation<T>> successfulResults =
new ConcurrentHashMap<>(providers.size());
Map<String, String> providerErrors =
new ConcurrentHashMap<>(providers.size());
try {
List<Callable<Void>> tasks = new ArrayList<>(providers.size());
for (Map.Entry<String, FeatureProvider> entry
: providers.entrySet()) {
String providerName = entry.getKey();
FeatureProvider provider = entry.getValue();
tasks.add(() -> {
try {
ProviderEvaluation<T> evaluation =
providerFunction.apply(provider);
if (evaluation == null) {
providerErrors.put(
providerName, "null evaluation");
} else if (evaluation.getErrorCode() == null) {
successfulResults.put(
providerName, evaluation);
} else {
providerErrors.put(
providerName,
evaluation.getErrorCode() + ": "
+ evaluation.getErrorMessage());
}
} catch (Exception e) {
providerErrors.put(
providerName,
e.getClass().getSimpleName() + ": "
+ e.getMessage());
}
return null;
});
}
List<Future<Void>> futures =
executorService.invokeAll(
tasks, timeoutMs, TimeUnit.MILLISECONDS);
for (Future<Void> future : futures) {
if (future.isCancelled()) {
return ProviderEvaluation.<T>builder()
.errorCode(ErrorCode.GENERAL)
.errorMessage(
"Comparison strategy timed out after "
+ timeoutMs + "ms")
.build();
}
future.get();
}
} catch (Exception e) {
return ProviderEvaluation.<T>builder()
.errorCode(ErrorCode.GENERAL)
.errorMessage("Comparison strategy failed: "
+ e.getMessage())
.build();
}
if (!providerErrors.isEmpty()) {
return ProviderEvaluation.<T>builder()
.errorCode(ErrorCode.GENERAL)
.errorMessage("Provider errors: "
+ buildErrorSummary(providerErrors))
.build();
}
ProviderEvaluation<T> fallbackResult =
successfulResults.get(fallbackProvider);
if (fallbackResult == null) {
return ProviderEvaluation.<T>builder()
.errorCode(ErrorCode.GENERAL)
.errorMessage(
"Fallback provider did not return a successful "
+ "evaluation: " + fallbackProvider)
.build();
}
if (allEvaluationsMatch(successfulResults)) {
return fallbackResult;
}
if (onMismatch != null) {
Map<String, ProviderEvaluation<?>> mismatchPayload =
new LinkedHashMap<>(successfulResults);
onMismatch.accept(
key, Collections.unmodifiableMap(mismatchPayload));
}
return fallbackResult;
}
private String buildErrorSummary(Map<String, String> providerErrors) {
StringBuilder builder = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : providerErrors.entrySet()) {
if (!first) {
builder.append("; ");
}
first = false;
builder.append(entry.getKey())
.append(" -> ")
.append(entry.getValue());
}
return builder.toString();
}
private <T> boolean allEvaluationsMatch(
Map<String, ProviderEvaluation<T>> results) {
ProviderEvaluation<T> baseline = null;
for (ProviderEvaluation<T> evaluation : results.values()) {
if (baseline == null) {
baseline = evaluation;
continue;
}
if (!Objects.equals(
baseline.getValue(), evaluation.getValue())) {
return false;
}
}
return true;
}
}