-
Notifications
You must be signed in to change notification settings - Fork 46
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
feat: Improve wait logic to a more elegant solution #1160 #1169
Open
chrfwow
wants to merge
18
commits into
open-feature:main
Choose a base branch
from
chrfwow:improve-wait-logic-to-a-more-elegant-solution-#1160
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.
Open
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4402e66
feat: improve wait logic to a more elegant solution #1160
chrfwow 99dda5c
Merge branch 'refs/heads/main' into improve-wait-logic-to-a-more-eleg…
chrfwow a41e980
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow 12e889f
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow 82b542b
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow 4208b9e
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow 15af42d
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow 03de275
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow fa75f25
fixup! feat: improve wait logic to a more elegant solution #1160
chrfwow bee7edd
Merge branch 'main' into improve-wait-logic-to-a-more-elegant-solutio…
chrfwow 7d2022d
merge with master branch
chrfwow 97bdaba
revert cucumber changes
chrfwow 9a32b22
increase sleep to "fix" flaky tests
chrfwow 29a4260
increase sleep to "fix" flaky tests
chrfwow 89f4d1e
increase sleep to "fix" flaky tests
chrfwow 619a67f
reintroduce names for providers
chrfwow 2c9c843
Merge branch 'main' into improve-wait-logic-to-a-more-elegant-solutio…
chrfwow c2024e7
merge with main
chrfwow 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
100 changes: 100 additions & 0 deletions
100
providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/EventsLock.java
This file contains 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,100 @@ | ||
package dev.openfeature.contrib.providers.flagd; | ||
|
||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.ImmutableContext; | ||
import dev.openfeature.sdk.ImmutableStructure; | ||
import dev.openfeature.sdk.ProviderEvent; | ||
import dev.openfeature.sdk.Structure; | ||
import dev.openfeature.sdk.exceptions.GeneralError; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
/** | ||
* Contains all fields we need to worry about locking, used as intrinsic lock | ||
* for sync blocks. | ||
*/ | ||
@Getter | ||
public class EventsLock { | ||
chrfwow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Setter | ||
private volatile ProviderEvent previousEvent = null; | ||
|
||
private volatile ImmutableStructure syncMetadata = new ImmutableStructure(); | ||
private volatile EvaluationContext enrichedContext = new ImmutableContext(); | ||
private volatile boolean initialized; | ||
private volatile boolean isShutDown; | ||
|
||
public void setSyncMetadata(Structure syncMetadata) { | ||
this.syncMetadata = new ImmutableStructure(syncMetadata.asMap()); | ||
} | ||
|
||
public void setEnrichedContext(EvaluationContext context) { | ||
this.enrichedContext = new ImmutableContext(context.asMap()); | ||
} | ||
|
||
/** | ||
* With this method called, it is suggested that initialization has been completed. It will wake up all threads that | ||
* wait for the initialization. Subsequent calls have no effect. | ||
* | ||
* @return true iff this was the first call to {@code initialize()} | ||
*/ | ||
public synchronized boolean initialize() { | ||
if (this.initialized) { | ||
return false; | ||
} | ||
this.initialized = true; | ||
this.notifyAll(); | ||
return true; | ||
} | ||
|
||
/** | ||
* Blocks the calling thread until either {@link EventsLock#initialize()} or {@link EventsLock#shutdown()} is called | ||
* or the deadline is exceeded, whatever happens first. If {@link EventsLock#initialize()} has been executed before | ||
* {@code waitForInitialization(long)} is called, it will return instantly. | ||
* If the deadline is exceeded, a GeneralError will be thrown. | ||
* If {@link EventsLock#shutdown()} is called in the meantime, an {@link IllegalStateException} will be thrown. | ||
* Otherwise, the method will return cleanly. | ||
* | ||
* @param deadline the maximum time in ms to wait | ||
* @throws GeneralError when the deadline is exceeded before {@link EventsLock#initialize()} is called on this | ||
* object | ||
* @throws IllegalStateException when {@link EventsLock#shutdown()} is called or has been called on this object | ||
*/ | ||
public void waitForInitialization(long deadline) { | ||
long start = System.currentTimeMillis(); | ||
long end = start + deadline; | ||
while (!initialized && !isShutDown) { | ||
long now = System.currentTimeMillis(); | ||
// if wait(0) is called, the thread would wait forever, so we abort when this would happen | ||
if (now >= end) { | ||
throw new GeneralError(String.format( | ||
"Deadline exceeded. Condition did not complete within the %d ms deadline", deadline)); | ||
} | ||
long remaining = end - now; | ||
synchronized (this) { | ||
if (initialized) { // might have changed in the meantime | ||
return; | ||
} | ||
if (isShutDown) { | ||
break; | ||
} | ||
try { | ||
this.wait(remaining); | ||
} catch (InterruptedException e) { | ||
// try again. Leave the continue to make PMD happy | ||
continue; | ||
} | ||
} | ||
} | ||
if (isShutDown) { | ||
throw new IllegalStateException("Already shut down"); | ||
} | ||
} | ||
|
||
/** | ||
* Signals a shutdown. Threads waiting for initialization will wake up and throw an {@link IllegalStateException}. | ||
*/ | ||
public synchronized void shutdown() { | ||
isShutDown = true; | ||
this.notifyAll(); | ||
} | ||
} |
This file contains 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
39 changes: 0 additions & 39 deletions
39
...ers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/common/Util.java
This file was deleted.
Oops, something went wrong.
This file contains 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 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.
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.
I moved this class out of the
FlagdProvider
to ensure no private fields are accessed and to keep the size of the file down.However, I feel like the name is not approrpaite anymore, so please feel free to suggest a better one. Also, this new class is strongly coupled to the
FlagdProvider
, if you have any suggestions to make it cleaner please let me knowThere 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.
maybe
FlagdProviderSyncState
or similar could be an appropriate name - or justSyncState
orSyncResource
- but to be fair, i am also not hundred percent happy with my suggestions