Skip to content

Commit e9fdb25

Browse files
Remove Custom Listeners from SnapshotsService (#37629)
* Remove Custom Listeners from SnapshotsService Motivations: * Shorten the code some more * Use ActionListener#wrap to get easy to reason about behavior in failure scenarios * Remove duplication in the logic of handling snapshot completion listeners (listeners removing themselves and comparing snapshots to their targets) * Also here, move all listener handling into `SnapshotsService` and remove custom listener class by putting listeners in a map
1 parent f862423 commit e9fdb25

File tree

3 files changed

+72
-161
lines changed

3 files changed

+72
-161
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/TransportCreateSnapshotAction.java

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.elasticsearch.cluster.service.ClusterService;
3030
import org.elasticsearch.common.inject.Inject;
3131
import org.elasticsearch.common.settings.Settings;
32-
import org.elasticsearch.snapshots.Snapshot;
33-
import org.elasticsearch.snapshots.SnapshotInfo;
3432
import org.elasticsearch.snapshots.SnapshotsService;
3533
import org.elasticsearch.threadpool.ThreadPool;
3634
import org.elasticsearch.transport.TransportService;
@@ -73,37 +71,13 @@ protected ClusterBlockException checkBlock(CreateSnapshotRequest request, Cluste
7371

7472
@Override
7573
protected void masterOperation(final CreateSnapshotRequest request, ClusterState state,
76-
final ActionListener<CreateSnapshotResponse> listener) {
77-
snapshotsService.createSnapshot(request, new SnapshotsService.CreateSnapshotListener() {
78-
@Override
79-
public void onResponse(Snapshot snapshotCreated) {
80-
if (request.waitForCompletion()) {
81-
snapshotsService.addListener(new SnapshotsService.SnapshotCompletionListener() {
82-
@Override
83-
public void onSnapshotCompletion(Snapshot snapshot, SnapshotInfo snapshotInfo) {
84-
if (snapshotCreated.equals(snapshot)) {
85-
listener.onResponse(new CreateSnapshotResponse(snapshotInfo));
86-
snapshotsService.removeListener(this);
87-
}
88-
}
89-
90-
@Override
91-
public void onSnapshotFailure(Snapshot snapshot, Exception e) {
92-
if (snapshotCreated.equals(snapshot)) {
93-
listener.onFailure(e);
94-
snapshotsService.removeListener(this);
95-
}
96-
}
97-
});
98-
} else {
99-
listener.onResponse(new CreateSnapshotResponse());
100-
}
101-
}
102-
103-
@Override
104-
public void onFailure(Exception e) {
105-
listener.onFailure(e);
106-
}
107-
});
74+
final ActionListener<CreateSnapshotResponse> listener) {
75+
if (request.waitForCompletion()) {
76+
snapshotsService.executeSnapshot(request,
77+
ActionListener.wrap(snapshotInfo-> listener.onResponse(new CreateSnapshotResponse(snapshotInfo)), listener::onFailure));
78+
} else {
79+
snapshotsService.createSnapshot(request,
80+
ActionListener.wrap(snapshot -> listener.onResponse(new CreateSnapshotResponse()), listener::onFailure));
81+
}
10882
}
10983
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/delete/TransportDeleteSnapshotAction.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,7 @@ protected ClusterBlockException checkBlock(DeleteSnapshotRequest request, Cluste
6868
@Override
6969
protected void masterOperation(final DeleteSnapshotRequest request, ClusterState state,
7070
final ActionListener<AcknowledgedResponse> listener) {
71-
snapshotsService.deleteSnapshot(request.repository(), request.snapshot(), new SnapshotsService.DeleteSnapshotListener() {
72-
@Override
73-
public void onResponse() {
74-
listener.onResponse(new AcknowledgedResponse(true));
75-
}
76-
77-
@Override
78-
public void onFailure(Exception e) {
79-
listener.onFailure(e);
80-
}
81-
}, false);
71+
snapshotsService.deleteSnapshot(request.repository(), request.snapshot(),
72+
ActionListener.wrap(v -> listener.onResponse(new AcknowledgedResponse(true)), listener::onFailure), false);
8273
}
8374
}

0 commit comments

Comments
 (0)