-
Notifications
You must be signed in to change notification settings - Fork 47
feat: Add multi-provider support #1500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
suvaidkhan
wants to merge
4
commits into
open-feature:main
Choose a base branch
from
suvaidkhan:suvaidkhan/add-multiprovider-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+831
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1b3db82
MultiProvider: Added strategies, unit tests and documentation
suvaidkhan dd809d2
MultiProvider: Added MultiProviderMetadata.java, refactored tests and…
suvaidkhan 5e30d12
Multiprovider: review comments incorporated
suvaidkhan 84bc45a
Multiprovider: fixed log messages and exception handling
suvaidkhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/dev/openfeature/sdk/multiprovider/FirstMatchStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dev.openfeature.sdk.multiprovider; | ||
|
||
import static dev.openfeature.sdk.ErrorCode.FLAG_NOT_FOUND; | ||
|
||
import dev.openfeature.sdk.ErrorCode; | ||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.FeatureProvider; | ||
import dev.openfeature.sdk.ProviderEvaluation; | ||
import dev.openfeature.sdk.exceptions.FlagNotFoundError; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* First match strategy. Return the first result returned by a provider. Skip providers that | ||
* indicate they had no value due to FLAG_NOT_FOUND. In all other cases, use the value returned by | ||
* the provider. If any provider returns an error result other than FLAG_NOT_FOUND, the whole | ||
* evaluation should error and “bubble up” the individual provider’s error in the result. As soon as | ||
* a value is returned by a provider, the rest of the operation should short-circuit and not call | ||
* the rest of the providers. | ||
*/ | ||
@Slf4j | ||
@NoArgsConstructor | ||
public class FirstMatchStrategy implements Strategy { | ||
|
||
/** | ||
* Represents a strategy that evaluates providers based on a first-match approach. Provides a | ||
* method to evaluate providers using a specified function and return the evaluation result. | ||
* | ||
* @param providerFunction provider function | ||
* @param <T> ProviderEvaluation type | ||
* @return the provider evaluation | ||
*/ | ||
@Override | ||
public <T> ProviderEvaluation<T> evaluate( | ||
Map<String, FeatureProvider> providers, | ||
String key, | ||
T defaultValue, | ||
EvaluationContext ctx, | ||
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction) { | ||
for (FeatureProvider provider : providers.values()) { | ||
try { | ||
ProviderEvaluation<T> res = providerFunction.apply(provider); | ||
if (!FLAG_NOT_FOUND.equals(res.getErrorCode())) { | ||
return res; | ||
} | ||
} catch (FlagNotFoundError e) { | ||
log.debug("flag not found {}", key, e); | ||
} | ||
} | ||
return ProviderEvaluation.<T>builder() | ||
.errorMessage("No provider successfully responded") | ||
.errorCode(ErrorCode.GENERAL) | ||
.build(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/dev/openfeature/sdk/multiprovider/FirstSuccessfulStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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.Map; | ||
import java.util.function.Function; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* First Successful Strategy. Similar to “First Match”, except that errors from evaluated providers | ||
* do not halt execution. Instead, it will return the first successful result from a provider. If no | ||
* provider successfully responds, it will throw an error result. | ||
*/ | ||
@Slf4j | ||
@NoArgsConstructor | ||
public class FirstSuccessfulStrategy implements Strategy { | ||
|
||
@Override | ||
public <T> ProviderEvaluation<T> evaluate( | ||
Map<String, FeatureProvider> providers, | ||
String key, | ||
T defaultValue, | ||
EvaluationContext ctx, | ||
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction) { | ||
for (FeatureProvider provider : providers.values()) { | ||
try { | ||
ProviderEvaluation<T> res = providerFunction.apply(provider); | ||
if (res.getErrorCode() == null) { | ||
return res; | ||
} | ||
} catch (Exception e) { | ||
log.debug("evaluation exception {}", key, e); | ||
} | ||
} | ||
|
||
return ProviderEvaluation.<T>builder() | ||
.errorMessage("No provider successfully responded") | ||
.errorCode(ErrorCode.GENERAL) | ||
.build(); | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
src/main/java/dev/openfeature/sdk/multiprovider/MultiProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
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 dev.openfeature.sdk.exceptions.GeneralError; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
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. */ | ||
@Slf4j | ||
public class MultiProvider extends EventProvider { | ||
|
||
@Getter | ||
private static final String NAME = "multiprovider"; | ||
|
||
public static final int INIT_THREADS_COUNT = 8; | ||
private final Map<String, FeatureProvider> providers; | ||
private final Strategy strategy; | ||
private MultiProviderMetadata metadata; | ||
|
||
/** | ||
* Constructs a MultiProvider with the given list of FeatureProviders, by default uses FirstMatchStrategy. | ||
* | ||
* @param providers the list of FeatureProviders to initialize the MultiProvider with | ||
*/ | ||
public MultiProvider(List<FeatureProvider> providers) { | ||
this(providers, null); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public MultiProvider(List<FeatureProvider> providers, Strategy strategy) { | ||
this.providers = buildProviders(providers); | ||
if (strategy != null) { | ||
this.strategy = strategy; | ||
} else { | ||
this.strategy = new FirstMatchStrategy(); | ||
} | ||
} | ||
|
||
protected static Map<String, FeatureProvider> buildProviders(List<FeatureProvider> providers) { | ||
Map<String, FeatureProvider> providersMap = new LinkedHashMap<>(providers.size()); | ||
for (FeatureProvider provider : providers) { | ||
FeatureProvider prevProvider = | ||
providersMap.put(provider.getMetadata().getName(), provider); | ||
if (prevProvider != null) { | ||
log.warn("duplicated provider name: {}", provider.getMetadata().getName()); | ||
} | ||
} | ||
return Collections.unmodifiableMap(providersMap); | ||
} | ||
|
||
/** | ||
* Initialize the provider. | ||
* | ||
* @param evaluationContext evaluation context | ||
* @throws Exception on error | ||
*/ | ||
@Override | ||
public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
var metadataBuilder = MultiProviderMetadata.builder(); | ||
metadataBuilder.name(NAME); | ||
HashMap<String, Metadata> providersMetadata = new HashMap<>(); | ||
ExecutorService executorService = Executors.newFixedThreadPool(Math.min(INIT_THREADS_COUNT, providers.size())); | ||
Collection<Callable<Boolean>> tasks = new ArrayList<>(providers.size()); | ||
for (FeatureProvider provider : providers.values()) { | ||
tasks.add(() -> { | ||
provider.initialize(evaluationContext); | ||
return true; | ||
}); | ||
Metadata providerMetadata = provider.getMetadata(); | ||
providersMetadata.put(providerMetadata.getName(), providerMetadata); | ||
} | ||
metadataBuilder.originalMetadata(providersMetadata); | ||
List<Future<Boolean>> results = executorService.invokeAll(tasks); | ||
for (Future<Boolean> result : results) { | ||
if (!result.get()) { | ||
executorService.shutdown(); | ||
throw new GeneralError("init failed"); | ||
} | ||
} | ||
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.values()) { | ||
try { | ||
provider.shutdown(); | ||
} catch (Exception e) { | ||
log.error("error shutdown provider {}", provider.getMetadata().getName(), e); | ||
} | ||
} | ||
log.debug("shutdown end"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/dev/openfeature/sdk/multiprovider/MultiProviderMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.openfeature.sdk.multiprovider; | ||
|
||
import dev.openfeature.sdk.Metadata; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
/** | ||
* Metadata class for Multiprovider. | ||
*/ | ||
@Data | ||
@Builder | ||
public class MultiProviderMetadata implements Metadata { | ||
String name; | ||
Map<String, Metadata> originalMetadata; | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/dev/openfeature/sdk/multiprovider/Strategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.openfeature.sdk.multiprovider; | ||
|
||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.FeatureProvider; | ||
import dev.openfeature.sdk.ProviderEvaluation; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
/** strategy. */ | ||
public interface Strategy { | ||
<T> ProviderEvaluation<T> evaluate( | ||
Map<String, FeatureProvider> providers, | ||
String key, | ||
T defaultValue, | ||
EvaluationContext ctx, | ||
Function<FeatureProvider, ProviderEvaluation<T>> providerFunction); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to shut down the executer service in this case