-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CountDownActionListener (#92308)
- Loading branch information
Showing
11 changed files
with
311 additions
and
46 deletions.
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 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()) { | ||
super.onFailure(failure.get()); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.