Skip to content

Conversation

@DaveCTurner
Copy link
Contributor

As of #53261 today PlainListenableActionFuture is the sole
implementation of ListenableActionFuture, the reason for the
Plain... prefix is now lost. It also heavily uses variable shadowing
in its implementation which makes it quite hard to read, uses a mix of
volatile fields and mutexes, and keeps hold of the deferred listeners
even after completion. Finally it does not have many tests.

This commit drops the unnecessary interface and renames the class to
simply ListenableActionFuture. It reworks the implementation to avoid
shadowing variables, drops the deferred listeners on completion, and
moves to an entirely mutex-based implementation. Finally, it adds
another test that it works as advertised.

As of elastic#53261 today `PlainListenableActionFuture` is the sole implementation of `ListenableActionFuture`, the reason for the `Plain...` prefix is now lost. It also heavily uses variable shadowing in its implementation which makes it quite hard to read, uses a mix of `volatile` fields and mutexes, and keeps hold of the deferred listeners even after completion. Finally it does not have many tests. This commit drops the unnecessary interface and renames the class to simply `ListenableActionFuture`. It reworks the implementation to avoid shadowing variables, drops the deferred listeners on completion, and moves to an entirely mutex-based implementation. Finally, it adds another test that it works as advertised.
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Feb 9, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

synchronized (this) {
executedListeners = true;
}
Object listeners = this.listeners;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only volatile read outside of the mutex, but we just had the mutex anyway, may as well move this up a line and drop the volatile.

if (executedListeners) {
executeImmediate = true;
} else {
Object listeners = this.listeners;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked this to have better names and to use final variables throughout, which introduces one extra local variable but makes it all a lot clearer.


@Override
public void addListener(final ActionListener<T> listener) {
internalAddListener(listener);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only caller of internalAddListener, inlined it.

Copy link
Contributor

@henningandersen henningandersen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup, LGTM.

@DaveCTurner DaveCTurner merged commit ccf283e into elastic:master Feb 9, 2021
@DaveCTurner DaveCTurner deleted the 2021-02-09-PlainListenableActionFuture-refactoring branch February 9, 2021 15:34
DaveCTurner added a commit that referenced this pull request Feb 9, 2021
As of #53261 today `PlainListenableActionFuture` is the sole implementation of `ListenableActionFuture`, the reason for the `Plain...` prefix is now lost. It also heavily uses variable shadowing in its implementation which makes it quite hard to read, uses a mix of `volatile` fields and mutexes, and keeps hold of the deferred listeners even after completion. Finally it does not have many tests. This commit drops the unnecessary interface and renames the class to simply `ListenableActionFuture`. It reworks the implementation to avoid shadowing variables, drops the deferred listeners on completion, and moves to an entirely mutex-based implementation. Finally, it adds another test that it works as advertised.
DaveCTurner added a commit that referenced this pull request Feb 9, 2021
dakrone pushed a commit that referenced this pull request Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Core/Infra/Core Core issues without another label >refactoring Team:Core/Infra Meta label for core/infra team v7.12.0 v8.0.0-alpha1

4 participants