-
Notifications
You must be signed in to change notification settings - Fork 40
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: events #476
Merged
Merged
feat: events #476
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8b2b616
feat: events
toddbaert 384b96e
fixup: fix bad import, fix test
toddbaert 72fb077
fixup: add more coverage of internal
toddbaert a8eea10
fixup: remove unused imports
toddbaert 8d78df7
fixup: unsed imports
toddbaert f527a13
fixup: make inner static
toddbaert 934e136
fixup: more tests, run ready immediately
toddbaert 92b8886
fixup: improve reliability of error tests
toddbaert cb7ede2
Update src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java
toddbaert 5491769
fixup: review feedback, add comments
toddbaert 208e58b
fixup: add provider repo tests, fitest warnings
toddbaert 5eee2c2
Update src/main/java/dev/openfeature/sdk/OpenFeatureAPI.java
toddbaert e232c00
fixup: shorten javadoc links, shutdown tasks, use methods refs
toddbaert 9a628ae
fixup: flaky test
toddbaert 9e23fb8
Update src/main/java/dev/openfeature/sdk/OpenFeatureClient.java
toddbaert 5568e87
fixup: feedback from justin
toddbaert 4473277
fixup: improve javadoc
toddbaert 51ef70a
Merge branch 'main' into feat/events
justinabrahms f0d716e
Merge branch 'main' into feat/events
toddbaert 7eebd01
fixup: test race condition fixes
toddbaert 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 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
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,28 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import lombok.Data; | ||
import lombok.experimental.SuperBuilder; | ||
|
||
/** | ||
* The details of a particular event. | ||
*/ | ||
@Data @SuperBuilder(toBuilder = true) | ||
public class EventDetails extends ProviderEventDetails { | ||
private String clientName; | ||
|
||
static EventDetails fromProviderEventDetails(ProviderEventDetails providerEventDetails) { | ||
return EventDetails.fromProviderEventDetails(providerEventDetails, null); | ||
} | ||
|
||
static EventDetails fromProviderEventDetails( | ||
ProviderEventDetails providerEventDetails, | ||
@Nullable String clientName) { | ||
return EventDetails.builder() | ||
.clientName(clientName) | ||
.flagsChanged(providerEventDetails.getFlagsChanged()) | ||
.eventMetadata(providerEventDetails.getEventMetadata()) | ||
.message(providerEventDetails.getMessage()) | ||
.build(); | ||
} | ||
} |
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,64 @@ | ||
package dev.openfeature.sdk; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the public interface for both the API and client events. |
||
|
||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Interface for attaching event handlers. | ||
*/ | ||
public interface EventHandling<T> { | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
justinabrahms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Add a handler for the {@link ProviderEvent#PROVIDER_READY} event. | ||
* Shorthand for {@link #on(ProviderEvent, Consumer)} | ||
* | ||
* @param handler behavior to add with this event | ||
* @return this | ||
*/ | ||
T onProviderReady(Consumer<EventDetails> handler); | ||
|
||
/** | ||
* Add a handler for the {@link ProviderEvent#PROVIDER_CONFIGURATION_CHANGED} event. | ||
* Shorthand for {@link #on(ProviderEvent, Consumer)} | ||
* | ||
* @param handler behavior to add with this event | ||
* @return this | ||
*/ | ||
T onProviderConfigurationChanged(Consumer<EventDetails> handler); | ||
|
||
/** | ||
* Add a handler for the {@link ProviderEvent#PROVIDER_STALE} event. | ||
* Shorthand for {@link #on(ProviderEvent, Consumer)} | ||
* | ||
* @param handler behavior to add with this event | ||
* @return this | ||
*/ | ||
T onProviderError(Consumer<EventDetails> handler); | ||
|
||
/** | ||
* Add a handler for the {@link ProviderEvent#PROVIDER_ERROR} event. | ||
* Shorthand for {@link #on(ProviderEvent, Consumer)} | ||
* | ||
* @param handler behavior to add with this event | ||
* @return this | ||
*/ | ||
T onProviderStale(Consumer<EventDetails> handler); | ||
|
||
/** | ||
* Add a handler for the specified {@link ProviderEvent}. | ||
* | ||
* @param event event type | ||
* @param handler behavior to add with this event | ||
* @return this | ||
*/ | ||
T on(ProviderEvent event, Consumer<EventDetails> handler); | ||
|
||
/** | ||
* Remove the previously attached handler by reference. | ||
* If the handler doesn't exists, no-op. | ||
* | ||
* @param event event type | ||
* @param handler to be removed | ||
* @return this | ||
*/ | ||
T removeHandler(ProviderEvent event, Consumer<EventDetails> handler); | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,83 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import dev.openfeature.sdk.internal.TriConsumer; | ||
|
||
/** | ||
* Abstract EventProvider. Providers must extend this class to support events. | ||
* Emit events with {@link #emit(ProviderEvent, ProviderEventDetails)}. Please | ||
* note that the SDK will automatically emit | ||
* {@link ProviderEvent#PROVIDER_READY } or | ||
* {@link ProviderEvent#PROVIDER_ERROR } accordingly when | ||
* {@link FeatureProvider#initialize(EvaluationContext)} completes successfully | ||
* or with error, so these events need not be emitted manually during | ||
* initialization. | ||
* | ||
* @see FeatureProvider | ||
*/ | ||
public abstract class EventProvider implements FeatureProvider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the class application authors must extend to use events. See javadoc above. |
||
|
||
private TriConsumer<EventProvider, ProviderEvent, ProviderEventDetails> onEmit = null; | ||
|
||
void detach() { | ||
this.onEmit = null; | ||
} | ||
|
||
void attach(TriConsumer<EventProvider, ProviderEvent, ProviderEventDetails> onEmit) { | ||
if (this.onEmit == null) { | ||
justinabrahms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.onEmit = onEmit; | ||
} | ||
} | ||
|
||
/** | ||
* Emit the specified {@link ProviderEvent}. | ||
* | ||
* @param event The event type | ||
* @param details The details of the event | ||
*/ | ||
public void emit(ProviderEvent event, ProviderEventDetails details) { | ||
if (this.onEmit != null) { | ||
this.onEmit.accept(this, event, details); | ||
} | ||
} | ||
|
||
/** | ||
* Emit a {@link ProviderEvent#PROVIDER_READY} event. | ||
* Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} | ||
* | ||
* @param details The details of the event | ||
*/ | ||
public void emitProviderReady(ProviderEventDetails details) { | ||
emit(ProviderEvent.PROVIDER_READY, details); | ||
} | ||
|
||
/** | ||
* Emit a | ||
* {@link ProviderEvent#PROVIDER_CONFIGURATION_CHANGED} | ||
* event. Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} | ||
* | ||
* @param details The details of the event | ||
*/ | ||
public void emitProviderConfigurationChanged(ProviderEventDetails details) { | ||
emit(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, details); | ||
} | ||
|
||
/** | ||
* Emit a {@link ProviderEvent#PROVIDER_STALE} event. | ||
* Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} | ||
* | ||
* @param details The details of the event | ||
*/ | ||
public void emitProviderStale(ProviderEventDetails details) { | ||
emit(ProviderEvent.PROVIDER_STALE, details); | ||
} | ||
|
||
/** | ||
* Emit a {@link ProviderEvent#PROVIDER_ERROR} event. | ||
* Shorthand for {@link #emit(ProviderEvent, ProviderEventDetails)} | ||
* | ||
* @param details The details of the event | ||
*/ | ||
public void emitProviderError(ProviderEventDetails details) { | ||
emit(ProviderEvent.PROVIDER_ERROR, details); | ||
} | ||
} |
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,115 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.function.Consumer; | ||
|
||
import edu.umd.cs.findbugs.annotations.Nullable; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Util class for storing and running handlers. | ||
*/ | ||
@Slf4j | ||
class EventSupport { | ||
|
||
// we use a v4 uuid as a "placeholder" for anonymous clients, since | ||
// ConcurrentHashMap doesn't support nulls | ||
private static final String defaultClientUuid = UUID.randomUUID().toString(); | ||
private static final ExecutorService taskExecutor = Executors.newCachedThreadPool(); | ||
private final Map<String, HandlerStore> handlerStores = new ConcurrentHashMap<>(); | ||
private final HandlerStore globalHandlerStore = new HandlerStore(); | ||
|
||
public void runClientHandlers(@Nullable String clientName, ProviderEvent event, EventDetails eventDetails) { | ||
justinabrahms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clientName = Optional.ofNullable(clientName) | ||
.orElse(defaultClientUuid); | ||
|
||
// run handlers if they exist | ||
Optional.ofNullable(handlerStores.get(clientName)) | ||
.filter(store -> Optional.of(store).isPresent()) | ||
.map(store -> store.handlerMap.get(event)) | ||
.ifPresent(handlers -> handlers | ||
.forEach(handler -> runHandler(handler, eventDetails))); | ||
} | ||
|
||
public void runGlobalHandlers(ProviderEvent event, EventDetails eventDetails) { | ||
globalHandlerStore.handlerMap.get(event) | ||
.forEach(handler -> { | ||
runHandler(handler, eventDetails); | ||
}); | ||
} | ||
|
||
public void addClientHandler(@Nullable String clientName, ProviderEvent event, Consumer<EventDetails> handler) { | ||
final String name = Optional.ofNullable(clientName) | ||
.orElse(defaultClientUuid); | ||
|
||
// lazily create and cache a HandlerStore if it doesn't exist | ||
HandlerStore store = Optional.ofNullable(this.handlerStores.get(name)) | ||
.orElseGet(() -> { | ||
HandlerStore newStore = new HandlerStore(); | ||
this.handlerStores.put(name, newStore); | ||
return newStore; | ||
}); | ||
store.addHandler(event, handler); | ||
} | ||
|
||
public void removeClientHandler(String clientName, ProviderEvent event, Consumer<EventDetails> handler) { | ||
clientName = Optional.ofNullable(clientName) | ||
.orElse(defaultClientUuid); | ||
this.handlerStores.get(clientName).removeHandler(event, handler); | ||
} | ||
|
||
public void addGlobalHandler(ProviderEvent event, Consumer<EventDetails> handler) { | ||
this.globalHandlerStore.addHandler(event, handler); | ||
} | ||
|
||
public void removeGlobalHandler(ProviderEvent event, Consumer<EventDetails> handler) { | ||
this.globalHandlerStore.removeHandler(event, handler); | ||
} | ||
|
||
public Set<String> getAllClientNames() { | ||
return this.handlerStores.keySet(); | ||
} | ||
|
||
public void runHandler(Consumer<EventDetails> handler, EventDetails eventDetails) { | ||
taskExecutor.submit(() -> { | ||
try { | ||
handler.accept(eventDetails); | ||
} catch (Exception e) { | ||
log.error("Exception in event handler {}", handler, e); | ||
} | ||
}); | ||
} | ||
|
||
public void shutdown() { | ||
taskExecutor.shutdown(); | ||
} | ||
|
||
static class HandlerStore { | ||
|
||
private final Map<ProviderEvent, List<Consumer<EventDetails>>> handlerMap; | ||
|
||
{ | ||
handlerMap = new ConcurrentHashMap<ProviderEvent, List<Consumer<EventDetails>>>(); | ||
handlerMap.put(ProviderEvent.PROVIDER_READY, new ArrayList<>()); | ||
handlerMap.put(ProviderEvent.PROVIDER_CONFIGURATION_CHANGED, new ArrayList<>()); | ||
handlerMap.put(ProviderEvent.PROVIDER_ERROR, new ArrayList<>()); | ||
handlerMap.put(ProviderEvent.PROVIDER_STALE, new ArrayList<>()); | ||
} | ||
|
||
void addHandler(ProviderEvent event, Consumer<EventDetails> handler) { | ||
handlerMap.get(event).add(handler); | ||
} | ||
|
||
void removeHandler(ProviderEvent event, Consumer<EventDetails> handler) { | ||
handlerMap.get(event).remove(handler); | ||
} | ||
} | ||
} |
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.
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.
As witnessed by @Kavindu-Dodan the test suite is actually 3x faster without any forking at all, possibly due to JVM startup time 🤷
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.
This observation can be explained with the surefire documentation [1]
forkCount
helped with creating N parallel forks but, given thatreuseForks
was false, tests did not reuse them. This means, while tests ran in parallel forks, new tests require a new process, adding a delay between executions.[1] - https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html#forked-test-execution
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.
Thanks!
I guessed it was something like that. It makes sense that starting up a bunch of JVMs would slow things down.
I did just try
<forkCount>1C</forkCount>
(which means 1 x number-of-cores), while allowing fork re-use, and the speed of the suite is unchanged from the default... so it doesn't slow things down but doesn't really help much.