-
Couldn't load subscription status.
- Fork 58
feat: flagd caching #168
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
Merged
Merged
feat: flagd caching #168
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cbf6ed3
feat!: implement caching of flagd provider
skyerus 182e5d9
caching testing & documentation
skyerus 3459479
Update providers/flagd/pom.xml
skyerus d2cb4e8
Update providers/flagd/pom.xml
skyerus 9af472b
amendments from code review
skyerus a0350dd
extracted caching implementation into FlagdCache class. consolidated …
skyerus 37d64b0
Update providers/flagd/src/main/java/dev/openfeature/contrib/provider…
skyerus 2584c0e
parse consolidated configuration_change event
skyerus 7edaef7
cache invalidation test & NPE check
skyerus 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
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
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
Submodule schemas
updated
from 910fa3 to aa5714
10 changes: 10 additions & 0 deletions
10
...ders/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/EventStreamCallback.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,10 @@ | ||
| package dev.openfeature.contrib.providers.flagd; | ||
|
|
||
| /** | ||
| * Defines behaviour required of event stream callbacks. | ||
| */ | ||
| interface EventStreamCallback { | ||
| void setEventStreamAlive(Boolean alive); | ||
|
|
||
| void restartEventStream() throws Exception; | ||
| } |
82 changes: 82 additions & 0 deletions
82
...ders/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/EventStreamObserver.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,82 @@ | ||
| package dev.openfeature.contrib.providers.flagd; | ||
|
|
||
| import java.util.Map; | ||
| import io.grpc.stub.StreamObserver; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import dev.openfeature.flagd.grpc.Schema.EventStreamResponse; | ||
| import com.google.protobuf.Value; | ||
|
|
||
| /** | ||
| * EventStreamObserver handles events emitted by flagd. | ||
| */ | ||
| @Slf4j | ||
| public class EventStreamObserver implements StreamObserver<EventStreamResponse> { | ||
skyerus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private EventStreamCallback callback; | ||
| private FlagdCache cache; | ||
|
|
||
| private static final String configurationChange = "configuration_change"; | ||
| private static final String providerReady = "provider_ready"; | ||
| private static final String flagsKey = "flags"; | ||
|
|
||
| EventStreamObserver(FlagdCache cache, EventStreamCallback callback) { | ||
| this.cache = cache; | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(EventStreamResponse value) { | ||
| switch (value.getType()) { | ||
| case configurationChange: | ||
| this.handleConfigurationChangeEvent(value); | ||
| break; | ||
| case providerReady: | ||
| this.handleProviderReadyEvent(); | ||
| break; | ||
| default: | ||
| log.debug("unhandled event type {}", value.getType()); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable t) { | ||
| log.error("event stream", t); | ||
| this.cache.clear(); | ||
| this.callback.setEventStreamAlive(false); | ||
| try { | ||
| this.callback.restartEventStream(); | ||
| } catch (Exception e) { | ||
| log.error("restart event stream", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onCompleted() { | ||
| this.cache.clear(); | ||
| this.callback.setEventStreamAlive(false); | ||
| } | ||
|
|
||
| private void handleConfigurationChangeEvent(EventStreamResponse value) { | ||
| if (!this.cache.getEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| Map<String, Value> data = value.getData().getFieldsMap(); | ||
| Value flagsValue = data.get(flagsKey); | ||
| if (flagsValue == null) { | ||
| this.cache.clear(); | ||
| return; | ||
| } | ||
|
|
||
| Map<String, Value> flags = flagsValue.getStructValue().getFieldsMap(); | ||
|
|
||
| for (String flagKey : flags.keySet()) { | ||
| this.cache.remove(flagKey); | ||
| } | ||
| } | ||
|
|
||
| private void handleProviderReadyEvent() { | ||
| this.cache.clear(); | ||
| this.callback.setEventStreamAlive(true); | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdCache.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,51 @@ | ||
| package dev.openfeature.contrib.providers.flagd; | ||
|
|
||
| import dev.openfeature.sdk.ProviderEvaluation; | ||
| import dev.openfeature.sdk.Value; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.commons.collections4.map.LRUMap; | ||
| import java.util.Collections; | ||
|
|
||
| /** | ||
| * Exposes caching mechanism for flag evaluations. | ||
| */ | ||
| public class FlagdCache { | ||
| private Map<String,ProviderEvaluation<Value>> store; | ||
| private Boolean enabled; | ||
|
|
||
| static final String LRU_CACHE = "lru"; | ||
| static final String DISABLED = "disabled"; | ||
|
|
||
| FlagdCache(String cache, int maxCacheSize) { | ||
| switch (cache) { | ||
| case DISABLED: | ||
| return; | ||
| case LRU_CACHE: | ||
| default: | ||
| this.store = Collections.synchronizedMap(new LRUMap<String, ProviderEvaluation<Value>>(maxCacheSize)); | ||
| } | ||
|
|
||
| this.enabled = true; | ||
| } | ||
|
|
||
| public Boolean getEnabled() { | ||
| return this.enabled; | ||
| } | ||
|
|
||
| public void put(String key, ProviderEvaluation<Value> value) { | ||
| this.store.put(key, value); | ||
| } | ||
|
|
||
| public ProviderEvaluation<Value> get(String key) { | ||
| return this.store.get(key); | ||
| } | ||
|
|
||
| public void remove(String key) { | ||
| this.store.remove(key); | ||
| } | ||
|
|
||
| public void clear() { | ||
| this.store.clear(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.