-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add CountDownActionListener #92308
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
Merged
Merged
Add CountDownActionListener #92308
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db37bf0
Add a CountDownActionListener
joegallo 4979121
Use CountDownActionListener where feasible
joegallo f6a5dbf
Add a convenience constructor that wraps a Runnable
joegallo b3595d0
Add stricter validation
joegallo 40a10ee
Rewrite in terms of AtomicInteger
joegallo 4cf5d95
Add validation tests
joegallo 48dc722
Merge branch 'main' into count-down-action-listener
joegallo a29aae9
Prefer assertions over exceptions for over-invoking
joegallo a50f699
Merge branch 'main' into count-down-action-listener
joegallo 155392f
Merge branch 'main' into count-down-action-listener
joegallo 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
82 changes: 82 additions & 0 deletions
82
server/src/main/java/org/elasticsearch/action/support/CountDownActionListener.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,82 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
package org.elasticsearch.action.support; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Wraps another listener and adds a counter -- each invocation of this listener will decrement the counter, and when the counter has been | ||
* exhausted the final invocation of this listener will delegate to the wrapped listener. Similar to {@link GroupedActionListener}, but for | ||
* the cases where tracking individual results is not useful. | ||
*/ | ||
public final class CountDownActionListener extends ActionListener.Delegating<Void, Void> { | ||
|
||
private final AtomicInteger countDown; | ||
private final AtomicReference<Exception> failure = new AtomicReference<>(); | ||
|
||
/** | ||
* Creates a new listener | ||
* @param groupSize the group size | ||
* @param delegate the delegate listener | ||
*/ | ||
public CountDownActionListener(int groupSize, ActionListener<Void> delegate) { | ||
super(Objects.requireNonNull(delegate)); | ||
if (groupSize <= 0) { | ||
assert false : "illegal group size [" + groupSize + "]"; | ||
throw new IllegalArgumentException("groupSize must be greater than 0 but was " + groupSize); | ||
} | ||
countDown = new AtomicInteger(groupSize); | ||
} | ||
|
||
/** | ||
* Creates a new listener | ||
* @param groupSize the group size | ||
* @param runnable the runnable | ||
*/ | ||
public CountDownActionListener(int groupSize, Runnable runnable) { | ||
this(groupSize, ActionListener.wrap(Objects.requireNonNull(runnable))); | ||
} | ||
|
||
private boolean countDown() { | ||
final var result = countDown.getAndUpdate(current -> Math.max(0, current - 1)); | ||
assert result > 0; | ||
return result == 1; | ||
} | ||
|
||
@Override | ||
public void onResponse(Void element) { | ||
if (countDown()) { | ||
if (failure.get() != null) { | ||
super.onFailure(failure.get()); | ||
} else { | ||
delegate.onResponse(element); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
if (failure.compareAndSet(null, e) == false) { | ||
failure.accumulateAndGet(e, (current, update) -> { | ||
// we have to avoid self-suppression! | ||
if (update != current) { | ||
current.addSuppressed(update); | ||
} | ||
return current; | ||
}); | ||
} | ||
if (countDown()) { | ||
thecoop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super.onFailure(failure.get()); | ||
} | ||
} | ||
|
||
} |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.