Skip to content

Commit b478b95

Browse files
committed
fix test by retaining old generations
1 parent ce88b2c commit b478b95

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

server/src/main/java/org/elasticsearch/repositories/FinalizeSnapshotContext.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import org.elasticsearch.snapshots.SnapshotInfo;
2020
import org.elasticsearch.snapshots.SnapshotsService;
2121

22+
import java.util.Collection;
2223
import java.util.Map;
2324
import java.util.Set;
25+
import java.util.stream.Collectors;
2426

2527
/**
2628
* Context for finalizing a snapshot.
@@ -106,8 +108,21 @@ public ClusterState updatedClusterState(ClusterState state) {
106108
final ClusterState updatedState = SnapshotsService.stateWithoutSnapshot(state, snapshotInfo.snapshot(), updatedShardGenerations);
107109
// Now that the updated cluster state may have changed in-progress shard snapshots' shard generations to the latest shard
108110
// 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();
109113
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))
111126
);
112127
return updatedState;
113128
}

server/src/test/java/org/elasticsearch/snapshots/SnapshotResiliencyTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,10 @@ public void testDeleteIndexBetweenSuccessAndFinalization() {
16291629
}
16301630
l.onResponse(null);
16311631
}));
1632+
})
1633+
.andThen(l -> {
1634+
// delete the snapshot that has the deleted index so it triggers cleanup of redundant shard generations
1635+
client.admin().cluster().prepareDeleteSnapshot(TEST_REQUEST_TIMEOUT, repoName, "first-snapshot").execute(l.map(r -> null));
16321636
});
16331637

16341638
deterministicTaskQueue.runAllRunnableTasks();

0 commit comments

Comments
 (0)