Skip to content

Commit 15af42d

Browse files
committed
fixup! feat: improve wait logic to a more elegant solution open-feature#1160
Signed-off-by: christian.lutnik <[email protected]>
1 parent 4208b9e commit 15af42d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProvider.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public List<Hook> getProviderHooks() {
117117

118118
@Override
119119
public void initialize(EvaluationContext evaluationContext) throws Exception {
120+
log.info("called initialize");
120121
synchronized (syncResources) {
121122
if (syncResources.isInitialized()) {
122123
return;
@@ -207,7 +208,8 @@ EvaluationContext getEnrichedContext() {
207208
private void onProviderEvent(FlagdProviderEvent flagdProviderEvent) {
208209

209210
synchronized (syncResources) {
210-
log.info("FlagdProviderEvent: {}", flagdProviderEvent);
211+
log.info("FlagdProviderEvent: {} of type {}", flagdProviderEvent, flagdProviderEvent.getClass().getName());
212+
log.info("FlagdProviderEvent event {} ", flagdProviderEvent.getEvent().toString());
211213
syncResources.setSyncMetadata(flagdProviderEvent.getSyncMetadata());
212214
if (flagdProviderEvent.getSyncMetadata() != null) {
213215
syncResources.setEnrichedContext(contextEnricher.apply(flagdProviderEvent.getSyncMetadata()));
@@ -228,6 +230,7 @@ private void onProviderEvent(FlagdProviderEvent flagdProviderEvent) {
228230
onConfigurationChanged(flagdProviderEvent);
229231
break;
230232
}
233+
log.info("config change not ready");
231234
// intentional fall through, a not-ready change will trigger a ready.
232235
case PROVIDER_READY:
233236
onReady();
@@ -248,13 +251,16 @@ private void onProviderEvent(FlagdProviderEvent flagdProviderEvent) {
248251
}
249252

250253
private void onConfigurationChanged(FlagdProviderEvent flagdProviderEvent) {
254+
log.info("FlagdProvider.onConfigurationChanged");
251255
this.emitProviderConfigurationChanged(ProviderEventDetails.builder()
252256
.flagsChanged(flagdProviderEvent.getFlagsChanged())
253257
.message("configuration changed")
254258
.build());
259+
log.info("post FlagdProvider.onConfigurationChanged");
255260
}
256261

257262
private void onReady() {
263+
log.info("on ready");
258264
if (syncResources.initialize()) {
259265
log.info("initialized FlagdProvider");
260266
}

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProviderSyncResources.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import dev.openfeature.sdk.exceptions.GeneralError;
99
import lombok.Getter;
1010
import lombok.Setter;
11+
import lombok.extern.slf4j.Slf4j;
1112

1213
/**
1314
* Contains all fields we need to worry about locking, used as intrinsic lock
1415
* for sync blocks in the {@link FlagdProvider}.
1516
*/
17+
@Slf4j
1618
@Getter
1719
class FlagdProviderSyncResources {
1820
@Setter
@@ -38,6 +40,7 @@ public void setEnrichedContext(EvaluationContext context) {
3840
* @return true iff this was the first call to {@code initialize()}
3941
*/
4042
public synchronized boolean initialize() {
43+
log.info("initialize in wait");
4144
if (this.initialized) {
4245
return false;
4346
}
@@ -47,20 +50,21 @@ public synchronized boolean initialize() {
4750
}
4851

4952
/**
50-
* Blocks the calling thread until either {@link EventsLock#initialize()} or {@link EventsLock#shutdown()} is called
51-
* or the deadline is exceeded, whatever happens first. If {@link EventsLock#initialize()} has been executed before
52-
* {@code waitForInitialization(long)} is called, it will return instantly.
53-
* If the deadline is exceeded, a GeneralError will be thrown.
54-
* If {@link EventsLock#shutdown()} is called in the meantime, an {@link IllegalStateException} will be thrown.
55-
* Otherwise, the method will return cleanly.
53+
* Blocks the calling thread until either {@link FlagdProviderSyncResources#initialize()} or
54+
* {@link FlagdProviderSyncResources#shutdown()} is called or the deadline is exceeded, whatever happens first. If
55+
* {@link FlagdProviderSyncResources#initialize()} has been executed before {@code waitForInitialization(long)} is
56+
* called, it will return instantly. If the deadline is exceeded, a GeneralError will be thrown.
57+
* If {@link FlagdProviderSyncResources#shutdown()} is called in the meantime, an {@link IllegalStateException} will
58+
* be thrown. Otherwise, the method will return cleanly.
5659
*
5760
* @param deadline the maximum time in ms to wait
58-
* @throws GeneralError when the deadline is exceeded before {@link EventsLock#initialize()} is called on
59-
* this
60-
* object
61-
* @throws IllegalStateException when {@link EventsLock#shutdown()} is called or has been called on this object
61+
* @throws GeneralError when the deadline is exceeded before
62+
* {@link FlagdProviderSyncResources#initialize()} is called on this object
63+
* @throws IllegalStateException when {@link FlagdProviderSyncResources#shutdown()} is called or has been called on
64+
* this object
6265
*/
6366
public void waitForInitialization(long deadline) {
67+
log.info("wait for init");
6468
long start = System.currentTimeMillis();
6569
long end = start + deadline;
6670
while (!initialized && !isShutDown) {
@@ -89,12 +93,14 @@ public void waitForInitialization(long deadline) {
8993
if (isShutDown) {
9094
throw new IllegalStateException("Already shut down");
9195
}
96+
log.info("post wait for init");
9297
}
9398

9499
/**
95100
* Signals a shutdown. Threads waiting for initialization will wake up and throw an {@link IllegalStateException}.
96101
*/
97102
public synchronized void shutdown() {
103+
log.info("shutdown in wait");
98104
isShutDown = true;
99105
this.notifyAll();
100106
}

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/process/InProcessResolver.java

+2
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ public void init() throws Exception {
6868
flagStore.getStateQueue().take();
6969
switch (storageStateChange.getStorageState()) {
7070
case OK:
71+
log.info("onConnectionEvent.accept ProviderEvent.PROVIDER_CONFIGURATION_CHANGED");
7172
onConnectionEvent.accept(new FlagdProviderEvent(
7273
ProviderEvent.PROVIDER_CONFIGURATION_CHANGED,
7374
storageStateChange.getChangedFlagsKeys(),
7475
storageStateChange.getSyncMetadata()));
76+
log.info("post onConnectionEvent.accept ProviderEvent.PROVIDER_CONFIGURATION_CHANGED");
7577
break;
7678
case ERROR:
7779
onConnectionEvent.accept(new FlagdProviderEvent(ProviderEvent.PROVIDER_ERROR));

0 commit comments

Comments
 (0)