-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Use RelaxedSingleResultDeduplicator in TransportGetAllocationStatsAction
#127121
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
JeremyDahlgren
wants to merge
3
commits into
elastic:main
from
JeremyDahlgren:fix/use-relaxed-single-result-deduplicator
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
server/src/main/java/org/elasticsearch/action/RelaxedSingleResultDeduplicator.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,72 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.action; | ||
|
|
||
| import org.elasticsearch.action.support.ContextPreservingActionListener; | ||
| import org.elasticsearch.action.support.SubscribableListener; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * Wraps an async action that consumes an {@link ActionListener} such that multiple invocations of {@link #execute(ActionListener)} can | ||
| * share the result from a single call to the wrapped action. This implementation is similar to {@link StrictSingleResultDeduplicator} but | ||
| * relaxed in the sense that it allows the result of a currently running computation to be used for listeners that queue up during that | ||
| * computation. | ||
| * | ||
| * @param <T> Result type | ||
| */ | ||
| public class RelaxedSingleResultDeduplicator<T> extends SingleResultDeduplicator<T> { | ||
|
|
||
| private ActionListenerList<T> waitingListeners; | ||
|
|
||
| public RelaxedSingleResultDeduplicator(ThreadContext threadContext, Consumer<ActionListener<T>> executeAction) { | ||
| super(threadContext, executeAction); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ActionListener<T> listener) { | ||
| final var wrappedListener = ContextPreservingActionListener.wrapPreservingContext(listener, threadContext); | ||
| synchronized (this) { | ||
| if (waitingListeners != null) { | ||
| waitingListeners.add(wrappedListener); | ||
| return; | ||
| } | ||
| waitingListeners = new ActionListenerList<>(); | ||
| waitingListeners.add(wrappedListener); | ||
| } | ||
| final var currentWaitingListeners = waitingListeners; | ||
| SubscribableListener.newForked(executeAction::accept).addListener(ActionListener.runBefore(currentWaitingListeners, () -> { | ||
| synchronized (this) { | ||
| waitingListeners = null; | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| private static class ActionListenerList<T> implements ActionListener<T> { | ||
| private final List<ActionListener<T>> listeners = new ArrayList<>(); | ||
|
|
||
| void add(ActionListener<T> listener) { | ||
| listeners.add(listener); | ||
| } | ||
|
|
||
| @Override | ||
| public void onResponse(T response) { | ||
| ActionListener.onResponse(listeners, response); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| ActionListener.onFailure(listeners, e); | ||
| } | ||
| } | ||
| } | ||
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
115 changes: 115 additions & 0 deletions
115
server/src/main/java/org/elasticsearch/action/StrictSingleResultDeduplicator.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,115 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.action; | ||
|
|
||
| import org.elasticsearch.action.support.ContextPreservingActionListener; | ||
| import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * | ||
| * Wraps an async action that consumes an {@link ActionListener} such that multiple invocations of {@link #execute(ActionListener)} can | ||
| * share the result from a single call to the wrapped action. This implementation is similar to {@link ResultDeduplicator} but offers | ||
| * stronger guarantees of not seeing a stale result ever. Concretely, every invocation of {@link #execute(ActionListener)} is guaranteed to | ||
| * be resolved with a response that has been computed at a time after the call to {@code execute} has been made. This allows this class to | ||
| * be used to deduplicate results from actions that produce results that change over time transparently. | ||
| * | ||
| * @param <T> Result type | ||
| */ | ||
| public final class StrictSingleResultDeduplicator<T> extends SingleResultDeduplicator<T> { | ||
|
|
||
| /** | ||
| * List of listeners waiting for the execution after the current in-progress execution. If {@code null} then no execution is in | ||
| * progress currently, otherwise an execution is in progress and will trigger another execution that will resolve any listeners queued | ||
| * up here once done. | ||
| */ | ||
| private List<ActionListener<T>> waitingListeners; | ||
| /** | ||
| * The threadContext associated with the first listener in the waitingListeners. This context will be restored right before | ||
| * we perform the {@code executeAction}. | ||
| */ | ||
| private ThreadContext.StoredContext waitingStoredContext; | ||
|
|
||
| public StrictSingleResultDeduplicator(ThreadContext threadContext, Consumer<ActionListener<T>> executeAction) { | ||
| super(threadContext, executeAction); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(ActionListener<T> listener) { | ||
| synchronized (this) { | ||
| if (waitingListeners == null) { | ||
| // no queued up listeners, just execute this one directly without deduplication and instantiate the list so that | ||
| // subsequent executions will wait | ||
| waitingListeners = new ArrayList<>(); | ||
| waitingStoredContext = null; | ||
| } else { | ||
| // already running an execution, queue this one up | ||
| if (waitingListeners.isEmpty()) { | ||
| // Only the first listener in queue needs the stored context which is used for running executeAction | ||
| assert waitingStoredContext == null; | ||
| waitingStoredContext = threadContext.newStoredContext(); | ||
| } | ||
| waitingListeners.add(ContextPreservingActionListener.wrapPreservingContext(listener, threadContext)); | ||
| return; | ||
| } | ||
| } | ||
| doExecute(ContextPreservingActionListener.wrapPreservingContext(listener, threadContext), null); | ||
| } | ||
|
|
||
| private void doExecute(ActionListener<T> listener, @Nullable ThreadContext.StoredContext storedContext) { | ||
| final ActionListener<T> wrappedListener = ActionListener.runBefore(listener, () -> { | ||
| final List<ActionListener<T>> listeners; | ||
| final ThreadContext.StoredContext thisStoredContext; | ||
| synchronized (this) { | ||
| if (waitingListeners.isEmpty()) { | ||
| // no listeners were queued up while this execution ran, so we just reset the state to not having a running execution | ||
| waitingListeners = null; | ||
| waitingStoredContext = null; | ||
| return; | ||
| } else { | ||
| // we have queued up listeners, so we create a fresh list for the next execution and execute once to handle the | ||
| // listeners currently queued up | ||
| listeners = waitingListeners; | ||
| thisStoredContext = waitingStoredContext; | ||
| // This batch of listeners will use the context of the first listener in this batch for the work execution | ||
| assert thisStoredContext != null : "stored context must not be null for the first listener in a batch"; | ||
| waitingListeners = new ArrayList<>(); | ||
| waitingStoredContext = null; | ||
| } | ||
| } | ||
|
|
||
| // Create a child threadContext so that the parent context remains unchanged when the child execution ends. | ||
| // This ensures the parent does not see response headers from the child execution. | ||
| try (var ignore = threadContext.newStoredContext()) { | ||
| doExecute(new ActionListener<>() { | ||
| @Override | ||
| public void onResponse(T response) { | ||
| ActionListener.onResponse(listeners, response); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| ActionListener.onFailure(listeners, e); | ||
| } | ||
| }, thisStoredContext); | ||
| } | ||
| }); | ||
| // Restore the given threadContext before proceed with the work execution. | ||
| // This ensures all executions begin execution with their own context. | ||
| if (storedContext != null) { | ||
| storedContext.restore(); | ||
| } | ||
| ActionListener.run(wrappedListener, executeAction::accept); | ||
| } | ||
| } |
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
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.
WDYT about just using
SubscribableListenerhere? It uses a linked list so slightly more expensive in general I guess but also more efficient in the common one-subscriber case.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.
Yes, makes sense. This is trimmed down quite a bit and as you mentioned this code could just be used directly as needed in
TransportGetAllocationStatsAction. I'm fine with closing this and not touchingSingleResultDeduplicator. I can create a separate PR to update the class javadoc forSingleResultDeduplicatorwith the expected usage pattern, or revert changes here and just make that javadoc update.