Skip to content

Commit daca675

Browse files
author
Ali Beyad
committed
Removes completed snapshot from cluster state on master change (#24605)
Previously, if a master node updated the cluster state to reflect that a snapshot is completed, but subsequently failed before processing a cluster state to remove the snapshot from the cluster state, then the newly elected master would not know that it needed to clean up the leftover cluster state. This commit ensures that the newly elected master sees if there is a snapshot in the cluster state that is in the completed state but has not yet been removed from the cluster state. Closes #24452
1 parent 5ff0824 commit daca675

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ public void applyClusterState(ClusterChangedEvent event) {
634634
if (event.routingTableChanged()) {
635635
processStartedShards(event);
636636
}
637+
removeFinishedSnapshotFromClusterState(event);
637638
finalizeSnapshotDeletionFromPreviousMaster(event);
638639
}
639640
} catch (Exception e) {
@@ -663,6 +664,26 @@ private void finalizeSnapshotDeletionFromPreviousMaster(ClusterChangedEvent even
663664
}
664665
}
665666

667+
/**
668+
* Removes a finished snapshot from the cluster state. This can happen if the previous
669+
* master node processed a cluster state update that marked the snapshot as finished,
670+
* but the previous master node died before removing the snapshot in progress from the
671+
* cluster state. It is then the responsibility of the new master node to end the
672+
* snapshot and remove it from the cluster state.
673+
*/
674+
private void removeFinishedSnapshotFromClusterState(ClusterChangedEvent event) {
675+
if (event.localNodeMaster() && !event.previousState().nodes().isLocalNodeElectedMaster()) {
676+
SnapshotsInProgress snapshotsInProgress = event.state().custom(SnapshotsInProgress.TYPE);
677+
if (snapshotsInProgress != null && !snapshotsInProgress.entries().isEmpty()) {
678+
for (SnapshotsInProgress.Entry entry : snapshotsInProgress.entries()) {
679+
if (entry.state().completed()) {
680+
endSnapshot(entry);
681+
}
682+
}
683+
}
684+
}
685+
}
686+
666687
/**
667688
* Cleans up shard snapshots that were running on removed nodes
668689
*

0 commit comments

Comments
 (0)