-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Introduce RefCountingRunnable #92620
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
Introduce RefCountingRunnable #92620
Conversation
Today a `CountDownActionListener` which wraps a bare `Runnable` collects all the exceptions it receives only to drop them when finally completing the delegate action. Moreover callers must declare up-front the number of times the listener will be completed, which means they must put extra effort into computing this number ahead of time and/or supply an overestimate and then make up the difference with additional artificial completions. This commit introduces `RefCountingRunnable` which allows callers to acquire and release references as needed, executing the delegate `Runnable` once all references are released. It also refactors all the relevant call sites to use this new utility.
Pinging @elastic/es-core-infra (Team:Core/Infra) |
There's a few things I want to use this for in follow-up work (notably #92607 and #92373) so if you think any of the usages in this PR are questionable then I'd rather back things out to merge this sooner instead of spending too much time iterating on this PR. (ofc if you think the whole concept is questionable then that's worth discussing here 😁) |
server/src/main/java/org/elasticsearch/action/support/RefCountingRunnable.java
Show resolved
Hide resolved
server/src/main/java/org/elasticsearch/action/support/RefCountingRunnable.java
Show resolved
Hide resolved
It's a good idea, but I'm concerned about the syntax here. I think it's hiding too much behind I think it needs to be more explicit in what it's doing in the code, and not use |
@elasticmachine please run elasticsearch-ci/part-2 |
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.
Looks good, nice change
Thanks Simon! |
Similar to the `RefCountingRunnable` introduced in elastic#92620, this commit introduces `RefCountingListener` which wraps an `ActionListener<Void>` and allows callers to acquire a dynamic collection of subsidiary listeners. The `RefCountingListener` counts responses and collects exceptions passed to these subsidiary listeners, and completes the wrapped listener once all acquired listeners are complete. Unlike a `CountDownActionListener` or a `GroupedActionListener`, this mechanism avoids the need for callers to declare up-front the number of times the listener will be completed, saving effort in computing this number ahead of time and avoiding the need to sometimes supply an overestimate and then make up the difference with additional artificial completions.
Recent improvements to the primitives for writing async code (particularly elastic#92452 and elastic#92620) mean that we can enormously simplify `TransportNodesAction`. In particular, we can avoid accumulating an intermediate array of responses for later processing in favour of just accumulating the successes and failures into their final separate lists. We also no longer need to use a separate `NodesResponseTracker` to discard responses on cancellation.
Recent improvements to the primitives for writing async code (particularly #92452 and #92620) mean that we can enormously simplify `TransportNodesAction`. In particular, we can avoid accumulating an intermediate array of responses for later processing in favour of just accumulating the successes and failures into their final separate lists. We also no longer need to use a separate `NodesResponseTracker` to discard responses on cancellation.
Similarly to elastic#92987, recent improvements to the primitives for writing async code (particularly elastic#92452 and elastic#92620) mean that we can enormously simplify `TransportBroadcastByNodeAction`. In particular, we can avoid accumulating an intermediate array of responses for later processing in favour of just accumulating the successes and failures into their final separate lists. We also no longer need to use a separate `NodesResponseTracker` to discard responses on cancellation. Finally, we can now discard shard-level responses more promptly on cancellation.
Similarly to #92987, recent improvements to the primitives for writing async code (particularly #92452 and #92620) mean that we can enormously simplify `TransportBroadcastByNodeAction`. In particular, we can avoid accumulating an intermediate array of responses for later processing in favour of just accumulating the successes and failures into their final separate lists. We also no longer need to use a separate `NodesResponseTracker` to discard responses on cancellation. Finally, we can now discard shard-level responses more promptly on cancellation.
Today a
CountDownActionListener
which wraps a bareRunnable
collects all the exceptions it receives only to drop them when finally completing the delegate action. Moreover callers must declare up-front the number of times the listener will be completed, which means they must put extra effort into computing this number ahead of time and/or supply an overestimate and then make up the difference with additional artificial completions.This commit introduces
RefCountingRunnable
which allows callers to acquire and release references as needed, executing the delegateRunnable
once all references are released. It also refactors all the relevant call sites to use this new utility.