Skip to content

Commit 36889e8

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 bdef2ab commit 36889e8

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
@@ -28,8 +28,6 @@
2828
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2929
import org.elasticsearch.cluster.service.ClusterService;
3030
import org.elasticsearch.common.inject.Inject;
31-
import org.elasticsearch.snapshots.Snapshot;
32-
import org.elasticsearch.snapshots.SnapshotInfo;
3331
import org.elasticsearch.snapshots.SnapshotsService;
3432
import org.elasticsearch.threadpool.ThreadPool;
3533
import org.elasticsearch.transport.TransportService;
@@ -72,37 +70,13 @@ protected ClusterBlockException checkBlock(CreateSnapshotRequest request, Cluste
7270

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

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
@@ -67,16 +67,7 @@ protected ClusterBlockException checkBlock(DeleteSnapshotRequest request, Cluste
6767
@Override
6868
protected void masterOperation(final DeleteSnapshotRequest request, ClusterState state,
6969
final ActionListener<AcknowledgedResponse> listener) {
70-
snapshotsService.deleteSnapshot(request.repository(), request.snapshot(), new SnapshotsService.DeleteSnapshotListener() {
71-
@Override
72-
public void onResponse() {
73-
listener.onResponse(new AcknowledgedResponse(true));
74-
}
75-
76-
@Override
77-
public void onFailure(Exception e) {
78-
listener.onFailure(e);
79-
}
80-
}, false);
70+
snapshotsService.deleteSnapshot(request.repository(), request.snapshot(),
71+
ActionListener.wrap(v -> listener.onResponse(new AcknowledgedResponse(true)), listener::onFailure), false);
8172
}
8273
}

0 commit comments

Comments
 (0)