Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
feat: Add a start wait timeout for initialization#61
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b5ce6a1
feat: Add a start wait timeout for initialization
devin-ai-integration[bot] a56a6f3
fix: Preserve configured Java start wait
devin-ai-integration[bot] 294a4a5
refactor: Reuse the wrapper config helper in the start wait constructor
devin-ai-integration[bot] b8e23e1
docs: Recommend bounded initialization waits
devin-ai-integration[bot] 095f4ea
fix: Resolve initialization immediately when a start wait was used
devin-ai-integration[bot] 70082f7
chore: Restore the generated version constant
devin-ai-integration[bot] 8862fe7
fix: Distinguish a permanent initialization failure from a lapsed sta…
devin-ai-integration[bot] a40a6d7
Merge remote-tracking branch 'origin/main' into devin/1787767004-java…
devin-ai-integration[bot] 0576d74
docs: Mark initialization as supported in the feature matrix
devin-ai-integration[bot] 3955f1d
docs: Document initialization behavior for setProvider and setProvide…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
58 changes: 52 additions & 6 deletions
58 src/main/java/com/launchdarkly/openfeature/serverprovider/Provider.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 |
|---|---|---|
| @@ -11,6 +11,7 @@ | ||
| import dev.openfeature.sdk.*; | ||
| import java.io.IOException; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.Future; | ||
| @@ -46,6 +47,7 @@ public String getName() { | ||
| private final EvaluationContextConverter evaluationContextConverter; | ||
| private final LDClientInterface client; | ||
| private final Duration startWait; | ||
| private ProviderState state = ProviderState.NOT_READY; | ||
| @@ -57,28 +59,65 @@ public String getName() { | ||
| * Create a provider with the specified SDK and default configuration. | ||
| * <p> | ||
| * If you need to specify any configuration use {@link Provider#Provider(String, LDConfig)} instead. | ||
| * Initialization waits indefinitely; using {@link Provider#Provider(String, LDConfig, Duration)} with a non-zero | ||
| * duration is strongly recommended. | ||
| * | ||
| * @param sdkKey the SDK key for your LaunchDarkly environment | ||
| */ | ||
| public Provider(String sdkKey) { | ||
| this(sdkKey, new LDConfig.Builder().build()); | ||
| this(new LDClient(sdkKey, withWrapper(new LDConfig.Builder().build())), Duration.ZERO); | ||
| } | ||
| /** | ||
| * Crate a provider with the specified SDK key and configuration. | ||
| * Create a provider with the specified SDK key and configuration. | ||
devin-ai-integration[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * Initialization waits indefinitely; using {@link Provider#Provider(String, LDConfig, Duration)} with a non-zero | ||
| * duration is strongly recommended. | ||
| * | ||
| * @param sdkKey the SDK key for your LaunchDarkly environment | ||
| * @param config a client configuration object | ||
| * @param config a client configuration object; its start wait setting is preserved, and provider initialization | ||
| * waits indefinitely | ||
| */ | ||
| public Provider(String sdkKey, LDConfig config) { | ||
| this(new LDClient(sdkKey, LDConfig.Builder.fromConfig(config) | ||
| this(new LDClient(sdkKey, withWrapper(config)), Duration.ZERO); | ||
| } | ||
| /** | ||
| * Create a provider with the specified SDK key, configuration, and start wait duration. | ||
| * <p> | ||
| * The duration is applied to the LaunchDarkly SDK as its start wait, and it bounds the whole of initialization: | ||
| * the constructor blocks for up to that long, and initialization then reports that outcome rather than waiting | ||
| * again. A non-zero duration is strongly recommended. A duration of zero applies no deadline: the constructor does | ||
| * not block, and initialization waits until the LaunchDarkly data source becomes valid or fails permanently, which | ||
| * may be indefinitely. | ||
| * <p> | ||
| * When the provider is registered with {@code setProviderAndWait}, initialization runs on the calling thread, so | ||
| * that call blocks for up to the duration and throws if the client did not become ready. When it is registered | ||
| * with {@code setProvider}, initialization runs in the background and a failure is reported as a | ||
| * {@code PROVIDER_ERROR} event instead, with evaluations before readiness returning their default value. | ||
| * | ||
| * @param sdkKey the SDK key for your LaunchDarkly environment | ||
| * @param config a client configuration object | ||
| * @param startWait the maximum duration to wait for initialization; zero applies no deadline | ||
| */ | ||
| public Provider(String sdkKey, LDConfig config, Duration startWait) { | ||
| this(new LDClient(sdkKey, | ||
| withWrapper(LDConfig.Builder.fromConfig(config).startWait(startWait).build())), startWait); | ||
| } | ||
| private static LDConfig withWrapper(LDConfig config) { | ||
| return LDConfig.Builder.fromConfig(config) | ||
| .wrapper(Components.wrapperInfo() | ||
| .wrapperName("open-feature-java-server") | ||
| .wrapperVersion(Version.SDK_VERSION)).build())); | ||
| .wrapperVersion(Version.SDK_VERSION)).build(); | ||
| } | ||
| Provider(LDClientInterface client) { | ||
| this(client, Duration.ZERO); | ||
| } | ||
| Provider(LDClientInterface client, Duration startWait) { | ||
| this.client = client; | ||
| this.startWait = startWait; | ||
| logger = client.getLogger(); | ||
| evaluationContextConverter = new EvaluationContextConverter(logger); | ||
| evaluationDetailConverter = new EvaluationDetailConverter(logger); | ||
| @@ -174,12 +213,19 @@ public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
| boolean successfullyInitialized; | ||
| try { | ||
| handleDataSourceStatus(client.getDataSourceStatusProvider().getStatus(), completer); | ||
| // With a start wait the client constructor has already waited, so the data source has either become valid, | ||
| // failed permanently, or run out of time; the outcome is whatever it is now. | ||
| if (!startWait.isZero() && !completer.isDone()) { | ||
| setState(ProviderState.ERROR); | ||
| throw new RuntimeException("The client did not initialize within the start wait duration."); | ||
| } | ||
| successfullyInitialized = completer.get(); | ||
| } finally { | ||
| setInitializing(false); | ||
| } | ||
devin-ai-integration[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if(!successfullyInitialized) { | ||
| if(!successfullyInitialized) { | ||
| throw new RuntimeException("Failed to initialize LaunchDarkly client."); | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106 src/test/java/com/launchdarkly/openfeature/serverprovider/LifeCycleTest.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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think Duration.Zero meaning wait indefinitely is not consistent with Duration.Zero passed to other SDKs startup/init/wait APIs. I think Duration.Zero is usually interpreted as don't wait at all.
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.
Zero here came from OFP 4.3.4.2 — "If the configured start wait time is zero, the provider MUST NOT apply an initialization timeout" — with the rationale that zero means the application does not want to block on initialization and leaves how long to wait up to the caller.
Worth separating the two layers, because I think my javadoc is what actually reads wrong:
LDConfig.startWait, so theLDClientconstructor returns immediately. That is the "don't wait at all" behavior you'd expect.initializeis invoked asynchronously by the OpenFeature API, so "no timeout" is not the application blocking forever — it means the provider does not fail initialization on a clock, and instead settles when the data source becomes valid or permanently fails. That is also the behavior onmaintoday for every existing caller.So the semantics follow the spec, but "waits indefinitely" is a misleading way to describe it, and if zero reads as "don't wait" to you it will read that way to users. Options, happy to take direction:
null, or a negative duration) and let zero mean fail immediately if not already ready — this diverges from OFP 4.3.4.2, so it would want a spec change rather than just a provider change.I'd lean towards 1 plus better docs, since the spec is cross-SDK, but you know the intent behind the wording better than I do. Which do you prefer?
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.
So, rephrasing it, we are saying:
This method will return immediately when the timeout is 0.
The time for the open feature initialized event itself is unbounded. It will be emitted when the SDK initializes or fails to initialize. If a timeout is provided, then an event will be emitted when the timeout lapses?
Or is it distinct from that?
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.
Close, with one correction on the first line — it's the constructor, not
initialize, that returns immediately.With
Duration.ZERO:Providerconstructor returns immediately. Zero goes toLDConfig.startWait, and the SDK checksisZero()/isNegative()and skips its wait on the data system future entirely.initializeis unbounded: it blocks on the data source status untilVALID(emitsPROVIDER_READY) orOFF(emitsPROVIDER_ERRORand throws). Nothing is emitted on a clock. Whether that blocks your thread is the OpenFeature API's choice, not the provider's —setProviderrunsinitializeon a background thread,setProviderAndWaitblocks.With a positive duration, both layers get it: the SDK constructor blocks up to that long, and
initializeadditionally stops waiting when it lapses, sets provider state toERROR, and throws — the OpenFeature SDK wraps that in aGeneralErrorand emitsPROVIDER_ERROR. So yes, an event on timeout, but as a failure rather than a separate timeout signal.One consequence worth a decision: the status listener stays registered after that throw, so if the data source becomes valid later, the provider still emits
PROVIDER_READYeven though initialization already failed. I think that's desirable — it's how the SDK recovers on its own — but it does mean the timeout bounds initialization, not the provider's lifetime. Say the word if you'd rather a lapsed timeout be terminal.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.
Following up on this after 095f4ea, since the behavior I described has changed: a positive duration is now spent once rather than twice. The SDK constructor blocks for up to
startWait, andinitializethen reports whatever the outcome already is instead of starting its own timed wait — so a two second start wait can no longer add up to four seconds of waiting. Zero still means no provider-applied timeout, per OFP 4.3.4.2, andinitializewaits until the data source is valid or permanently fails.The consequence I flagged above is unchanged: the status listener stays registered after a failed initialization, so a data source that becomes valid later still emits
PROVIDER_READY. Still happy to make a lapsed start wait terminal instead if that's what you'd prefer.