-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathMultiProvider.java
More file actions
163 lines (142 loc) · 6.13 KB
/
MultiProvider.java
File metadata and controls
163 lines (142 loc) · 6.13 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
package dev.openfeature.sdk.multiprovider;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.EventProvider;
import dev.openfeature.sdk.FeatureProvider;
import dev.openfeature.sdk.Metadata;
import dev.openfeature.sdk.ProviderEvaluation;
import dev.openfeature.sdk.Value;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* <b>Experimental:</b> Provider implementation for multi-provider.
*
* <p>This provider delegates flag evaluations to multiple underlying providers using a configurable
* {@link Strategy}. It also exposes combined metadata containing the original metadata of each
* underlying provider.
*/
@Slf4j
public class MultiProvider extends EventProvider {
@Getter
private static final String NAME = "multiprovider";
// Use CPU count as upper bound for init threads.
public static final int INIT_THREADS_COUNT = Runtime.getRuntime().availableProcessors();
private final List<FeatureProvider> providers;
private final Strategy strategy;
private MultiProviderMetadata metadata;
/**
* Constructs a MultiProvider with the given list of FeatureProviders, by default uses
* {@link FirstMatchStrategy}.
*
* @param providers the list of FeatureProviders to initialize the MultiProvider with
*/
public MultiProvider(List<FeatureProvider> providers) {
this(providers, new FirstMatchStrategy());
}
/**
* Constructs a MultiProvider with the given list of FeatureProviders and a strategy.
*
* @param providers the list of FeatureProviders to initialize the MultiProvider with
* @param strategy the strategy (if {@code null}, {@link FirstMatchStrategy} is used)
*/
public MultiProvider(List<FeatureProvider> providers, Strategy strategy) {
this.providers = providers;
this.strategy = Objects.requireNonNull(strategy, "strategy must not be null");
}
/**
* Initialize the provider.
*
* @param evaluationContext evaluation context
* @throws Exception on error (e.g. wrapped {@link java.util.concurrent.ExecutionException}
* from a failing provider)
*/
@Override
public void initialize(EvaluationContext evaluationContext) throws Exception {
var metadataBuilder = MultiProviderMetadata.builder().name(NAME);
if (providers.isEmpty()) {
metadataBuilder.originalMetadata(Collections.emptyList());
metadata = metadataBuilder.build();
return;
}
List<Metadata> providersMetadata = new ArrayList<>(providers.size());
var executorService = Executors.newFixedThreadPool(Math.min(INIT_THREADS_COUNT, providers.size()));
try {
Collection<Callable<Void>> tasks = new ArrayList<>(providers.size());
for (FeatureProvider provider : providers) {
tasks.add(() -> {
provider.initialize(evaluationContext);
return null;
});
providersMetadata.add(provider.getMetadata());
}
metadataBuilder.originalMetadata(Collections.unmodifiableList(providersMetadata));
List<Future<Void>> results = executorService.invokeAll(tasks);
for (Future<Void> result : results) {
// This will re-throw any exception from the provider's initialize method,
// wrapped in an ExecutionException.
result.get();
}
} catch (Exception e) {
// If initialization fails for any provider, attempt to shut down via the
// standard shutdown path to avoid a partial/limbo state.
try {
shutdown();
} catch (Exception shutdownEx) {
log.error("error during shutdown after failed initialize", shutdownEx);
}
throw e;
} finally {
executorService.shutdown();
}
metadata = metadataBuilder.build();
}
@SuppressFBWarnings(value = "EI_EXPOSE_REP")
@Override
public Metadata getMetadata() {
return metadata;
}
@Override
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
return strategy.evaluate(
providers, key, defaultValue, ctx, p -> p.getBooleanEvaluation(key, defaultValue, ctx));
}
@Override
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getStringEvaluation(key, defaultValue, ctx));
}
@Override
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
return strategy.evaluate(
providers, key, defaultValue, ctx, p -> p.getIntegerEvaluation(key, defaultValue, ctx));
}
@Override
public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) {
return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getDoubleEvaluation(key, defaultValue, ctx));
}
@Override
public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) {
return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getObjectEvaluation(key, defaultValue, ctx));
}
@Override
public void shutdown() {
log.debug("shutdown begin");
for (FeatureProvider provider : providers) {
try {
provider.shutdown();
} catch (Exception e) {
log.error("error shutdown provider {}", provider.getMetadata().getName(), e);
}
}
log.debug("shutdown end");
// Important: ensure EventProvider's executor is also shut down
super.shutdown();
}
}