|
| 1 | +package dev.openfeature.sdk.multiprovider; |
| 2 | + |
| 3 | +import dev.openfeature.sdk.EvaluationContext; |
| 4 | +import dev.openfeature.sdk.EventProvider; |
| 5 | +import dev.openfeature.sdk.FeatureProvider; |
| 6 | +import dev.openfeature.sdk.Metadata; |
| 7 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 8 | +import dev.openfeature.sdk.Value; |
| 9 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.Callable; |
| 18 | +import java.util.concurrent.ExecutorService; |
| 19 | +import java.util.concurrent.Executors; |
| 20 | +import java.util.concurrent.Future; |
| 21 | +import lombok.Getter; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | + |
| 24 | +/** |
| 25 | + * <b>Experimental:</b> Provider implementation for multi-provider. |
| 26 | + * |
| 27 | + * <p>This provider delegates flag evaluations to multiple underlying providers using a configurable |
| 28 | + * {@link Strategy}. It also exposes combined metadata containing the original metadata of each |
| 29 | + * underlying provider. |
| 30 | + */ |
| 31 | +@Slf4j |
| 32 | +public class MultiProvider extends EventProvider { |
| 33 | + |
| 34 | + @Getter |
| 35 | + private static final String NAME = "multiprovider"; |
| 36 | + |
| 37 | + public static final int INIT_THREADS_COUNT = 8; |
| 38 | + |
| 39 | + private final Map<String, FeatureProvider> providers; |
| 40 | + private final Strategy strategy; |
| 41 | + private MultiProviderMetadata metadata; |
| 42 | + |
| 43 | + /** |
| 44 | + * Constructs a MultiProvider with the given list of FeatureProviders, by default uses |
| 45 | + * {@link FirstMatchStrategy}. |
| 46 | + * |
| 47 | + * @param providers the list of FeatureProviders to initialize the MultiProvider with |
| 48 | + */ |
| 49 | + public MultiProvider(List<FeatureProvider> providers) { |
| 50 | + this(providers, null); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructs a MultiProvider with the given list of FeatureProviders and a strategy. |
| 55 | + * |
| 56 | + * @param providers the list of FeatureProviders to initialize the MultiProvider with |
| 57 | + * @param strategy the strategy (if {@code null}, {@link FirstMatchStrategy} is used) |
| 58 | + */ |
| 59 | + public MultiProvider(List<FeatureProvider> providers, Strategy strategy) { |
| 60 | + this.providers = buildProviders(providers); |
| 61 | + if (strategy != null) { |
| 62 | + this.strategy = strategy; |
| 63 | + } else { |
| 64 | + this.strategy = new FirstMatchStrategy(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + protected static Map<String, FeatureProvider> buildProviders(List<FeatureProvider> providers) { |
| 69 | + Map<String, FeatureProvider> providersMap = new LinkedHashMap<>(providers.size()); |
| 70 | + for (FeatureProvider provider : providers) { |
| 71 | + FeatureProvider prevProvider = |
| 72 | + providersMap.put(provider.getMetadata().getName(), provider); |
| 73 | + if (prevProvider != null) { |
| 74 | + log.warn("duplicated provider name: {}", provider.getMetadata().getName()); |
| 75 | + } |
| 76 | + } |
| 77 | + return Collections.unmodifiableMap(providersMap); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Initialize the provider. |
| 82 | + * |
| 83 | + * @param evaluationContext evaluation context |
| 84 | + * @throws Exception on error (e.g. wrapped {@link java.util.concurrent.ExecutionException} |
| 85 | + * from a failing provider) |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public void initialize(EvaluationContext evaluationContext) throws Exception { |
| 89 | + var metadataBuilder = MultiProviderMetadata.builder().name(NAME); |
| 90 | + HashMap<String, Metadata> providersMetadata = new HashMap<>(); |
| 91 | + |
| 92 | + if (providers.isEmpty()) { |
| 93 | + metadataBuilder.originalMetadata(Collections.unmodifiableMap(providersMetadata)); |
| 94 | + metadata = metadataBuilder.build(); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + ExecutorService executorService = Executors.newFixedThreadPool(Math.min(INIT_THREADS_COUNT, providers.size())); |
| 99 | + try { |
| 100 | + Collection<Callable<Void>> tasks = new ArrayList<>(providers.size()); |
| 101 | + for (FeatureProvider provider : providers.values()) { |
| 102 | + tasks.add(() -> { |
| 103 | + provider.initialize(evaluationContext); |
| 104 | + return null; |
| 105 | + }); |
| 106 | + Metadata providerMetadata = provider.getMetadata(); |
| 107 | + providersMetadata.put(providerMetadata.getName(), providerMetadata); |
| 108 | + } |
| 109 | + |
| 110 | + metadataBuilder.originalMetadata(Collections.unmodifiableMap(providersMetadata)); |
| 111 | + |
| 112 | + List<Future<Void>> results = executorService.invokeAll(tasks); |
| 113 | + for (Future<Void> result : results) { |
| 114 | + // This will re-throw any exception from the provider's initialize method, |
| 115 | + // wrapped in an ExecutionException. |
| 116 | + result.get(); |
| 117 | + } |
| 118 | + } catch (Exception e) { |
| 119 | + // If initialization fails for any provider, attempt to shut down all providers |
| 120 | + // to avoid a partial/limbo state. |
| 121 | + for (FeatureProvider provider : providers.values()) { |
| 122 | + try { |
| 123 | + provider.shutdown(); |
| 124 | + } catch (Exception shutdownEx) { |
| 125 | + log.error( |
| 126 | + "error shutting down provider {} after failed initialize", |
| 127 | + provider.getMetadata().getName(), |
| 128 | + shutdownEx); |
| 129 | + } |
| 130 | + } |
| 131 | + throw e; |
| 132 | + } finally { |
| 133 | + executorService.shutdown(); |
| 134 | + } |
| 135 | + |
| 136 | + metadata = metadataBuilder.build(); |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressFBWarnings(value = "EI_EXPOSE_REP") |
| 140 | + @Override |
| 141 | + public Metadata getMetadata() { |
| 142 | + return metadata; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 147 | + return strategy.evaluate( |
| 148 | + providers, key, defaultValue, ctx, p -> p.getBooleanEvaluation(key, defaultValue, ctx)); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 153 | + return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getStringEvaluation(key, defaultValue, ctx)); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 158 | + return strategy.evaluate( |
| 159 | + providers, key, defaultValue, ctx, p -> p.getIntegerEvaluation(key, defaultValue, ctx)); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 164 | + return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getDoubleEvaluation(key, defaultValue, ctx)); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 169 | + return strategy.evaluate(providers, key, defaultValue, ctx, p -> p.getObjectEvaluation(key, defaultValue, ctx)); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void shutdown() { |
| 174 | + log.debug("shutdown begin"); |
| 175 | + for (FeatureProvider provider : providers.values()) { |
| 176 | + try { |
| 177 | + provider.shutdown(); |
| 178 | + } catch (Exception e) { |
| 179 | + log.error("error shutdown provider {}", provider.getMetadata().getName(), e); |
| 180 | + } |
| 181 | + } |
| 182 | + log.debug("shutdown end"); |
| 183 | + // Important: ensure EventProvider's executor is also shut down |
| 184 | + super.shutdown(); |
| 185 | + } |
| 186 | +} |
0 commit comments