|
19 | 19 | import org.elasticsearch.snapshots.SnapshotInfo; |
20 | 20 | import org.elasticsearch.snapshots.SnapshotsService; |
21 | 21 |
|
| 22 | +import java.util.Collection; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.Set; |
| 25 | +import java.util.stream.Collectors; |
24 | 26 |
|
25 | 27 | /** |
26 | 28 | * Context for finalizing a snapshot. |
@@ -106,8 +108,21 @@ public ClusterState updatedClusterState(ClusterState state) { |
106 | 108 | final ClusterState updatedState = SnapshotsService.stateWithoutSnapshot(state, snapshotInfo.snapshot(), updatedShardGenerations); |
107 | 109 | // Now that the updated cluster state may have changed in-progress shard snapshots' shard generations to the latest shard |
108 | 110 | // generation, let's mark any now unreferenced shard generations as obsolete and ready to be deleted. |
| 111 | + |
| 112 | + final Collection<IndexId> deletedIndices = updatedShardGenerations.deletedIndices.indices(); |
109 | 113 | obsoleteGenerations.set( |
110 | | - SnapshotsInProgress.get(updatedState).obsoleteGenerations(snapshotInfo.repository(), SnapshotsInProgress.get(state)) |
| 114 | + SnapshotsInProgress.get(updatedState) |
| 115 | + .obsoleteGenerations(snapshotInfo.repository(), SnapshotsInProgress.get(state)) |
| 116 | + .entrySet() |
| 117 | + .stream() |
| 118 | + // We want to keep both old and new generations for deleted indices, so we filter them out here to avoid deletion. |
| 119 | + // We need the old generations because they are what get recorded in the RepositoryData. |
| 120 | + // We also need the new generations a future finalization may build upon them. It may ends up not being used at all |
| 121 | + // when current batch of in-progress snapshots are completed and no new index of the same name is created. |
| 122 | + // That is also OK. It means we have some redundant shard generations in the repository and they will be deleted |
| 123 | + // when a snapshot deletion runs. |
| 124 | + .filter(e -> deletedIndices.contains(e.getKey().index()) == false) |
| 125 | + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)) |
111 | 126 | ); |
112 | 127 | return updatedState; |
113 | 128 | } |
|
0 commit comments