diff --git a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java index a11d88e49d9..9fa55bf90bb 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java +++ b/server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java @@ -335,10 +335,9 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus repository.initializeSnapshot( snapshot.snapshot().getSnapshotId(), snapshot.indices(), metadataForSnapshot(snapshot, clusterState.metadata())); + snapshotCreated = true; } - snapshotCreated = true; - logger.info("snapshot [{}] started", snapshot.snapshot()); final Version version = minCompatibleVersion(clusterState.nodes().getMinNodeVersion(), snapshot.repository(), repositoryData, null); @@ -408,7 +407,7 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus logger.warn(() -> new ParameterizedMessage("[{}] failed to create snapshot", snapshot.snapshot().getSnapshotId()), e); removeSnapshotFromClusterState(snapshot.snapshot(), null, e, - new CleanupAfterErrorListener(snapshot, true, userCreateSnapshotListener, e)); + new CleanupAfterErrorListener(snapshot, snapshotCreated, userCreateSnapshotListener, e)); } @Override @@ -471,9 +470,12 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus } @Override - public void onFailure(Exception e) { - e.addSuppressed(this.e); - cleanupAfterError(e); + public void onFailure(@Nullable Exception e) { + if (snapshotCreated) { + cleanupAfterError(ExceptionsHelper.useOrSuppress(e, this.e)); + } else { + userCreateSnapshotListener.onFailure(ExceptionsHelper.useOrSuppress(e, this.e)); + } } public void onNoLongerMaster() { @@ -482,29 +484,25 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus private void cleanupAfterError(Exception exception) { threadPool.generic().execute(() -> { - if (snapshotCreated) { - final Metadata metadata = clusterService.state().metadata(); - repositoriesService.repository(snapshot.snapshot().getRepository()) + final Metadata metadata = clusterService.state().metadata(); + repositoriesService.repository(snapshot.snapshot().getRepository()) .finalizeSnapshot(snapshot.snapshot().getSnapshotId(), - buildGenerations(snapshot, metadata), - snapshot.startTime(), - ExceptionsHelper.stackTrace(exception), - 0, - Collections.emptyList(), - snapshot.repositoryStateId(), - snapshot.includeGlobalState(), - metadataForSnapshot(snapshot, metadata), - snapshot.userMetadata(), - snapshot.version(), - ActionListener.runAfter(ActionListener.wrap(ignored -> { - }, inner -> { - inner.addSuppressed(exception); - logger.warn(() -> new ParameterizedMessage("[{}] failed to finalize snapshot in repository", - snapshot.snapshot()), inner); - }), () -> userCreateSnapshotListener.onFailure(e))); - } else { - userCreateSnapshotListener.onFailure(e); - } + buildGenerations(snapshot, metadata), + snapshot.startTime(), + ExceptionsHelper.stackTrace(exception), + 0, + Collections.emptyList(), + snapshot.repositoryStateId(), + snapshot.includeGlobalState(), + metadataForSnapshot(snapshot, metadata), + snapshot.userMetadata(), + snapshot.version(), + ActionListener.runAfter(ActionListener.wrap(ignored -> { + }, inner -> { + inner.addSuppressed(exception); + logger.warn(() -> new ParameterizedMessage("[{}] failed to finalize snapshot in repository", + snapshot.snapshot()), inner); + }), () -> userCreateSnapshotListener.onFailure(e))); }); } } @@ -1016,7 +1014,7 @@ public class SnapshotsService extends AbstractLifecycleComponent implements Clus endingSnapshots.remove(snapshot); } if (listener != null) { - listener.onResponse(snapshotInfo); + listener.onFailure(null); } } });