Skip to content

Commit a75c850

Browse files
committed
update all the way
1 parent 164d2f6 commit a75c850

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collection;
2323
import java.util.Map;
2424
import java.util.Set;
25-
import java.util.stream.Collectors;
2625

2726
/**
2827
* Context for finalizing a snapshot.
@@ -111,18 +110,7 @@ public ClusterState updatedClusterState(ClusterState state) {
111110

112111
final Collection<IndexId> deletedIndices = updatedShardGenerations.deletedIndices.indices();
113112
obsoleteGenerations.set(
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))
113+
SnapshotsInProgress.get(updatedState).obsoleteGenerations(snapshotInfo.repository(), SnapshotsInProgress.get(state))
126114
);
127115
return updatedState;
128116
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.index.IndexVersions;
2626
import org.elasticsearch.logging.LogManager;
2727
import org.elasticsearch.logging.Logger;
28+
import org.elasticsearch.repositories.FinalizeSnapshotContext.UpdatedShardGenerations;
2829
import org.elasticsearch.snapshots.SnapshotId;
2930
import org.elasticsearch.snapshots.SnapshotInfo;
3031
import org.elasticsearch.snapshots.SnapshotState;
@@ -405,7 +406,7 @@ public Map<IndexId, Collection<String>> indexMetaDataToRemoveAfterRemovingSnapsh
405406
*
406407
* @param snapshotId Id of the new snapshot
407408
* @param details Details of the new snapshot
408-
* @param shardGenerations Updated shard generations in the new snapshot. For each index contained in the snapshot an array of new
409+
* @param updatedShardGenerations Updated shard generations in the new snapshot. For each index contained in the snapshot an array of new
409410
* generations indexed by the shard id they correspond to must be supplied.
410411
* @param indexMetaBlobs Map of index metadata blob uuids
411412
* @param newIdentifiers Map of new index metadata blob uuids keyed by the identifiers of the
@@ -414,7 +415,7 @@ public Map<IndexId, Collection<String>> indexMetaDataToRemoveAfterRemovingSnapsh
414415
public RepositoryData addSnapshot(
415416
final SnapshotId snapshotId,
416417
final SnapshotDetails details,
417-
final ShardGenerations shardGenerations,
418+
final UpdatedShardGenerations updatedShardGenerations,
418419
@Nullable final Map<IndexId, String> indexMetaBlobs,
419420
@Nullable final Map<String, String> newIdentifiers
420421
) {
@@ -424,6 +425,7 @@ public RepositoryData addSnapshot(
424425
// the new master, so we make the operation idempotent
425426
return this;
426427
}
428+
final var shardGenerations = updatedShardGenerations.liveIndices();
427429
Map<String, SnapshotId> snapshots = new HashMap<>(snapshotIds);
428430
snapshots.put(snapshotId.getUUID(), snapshotId);
429431
Map<String, SnapshotDetails> newSnapshotDetails = new HashMap<>(snapshotsDetails);
@@ -459,7 +461,11 @@ public RepositoryData addSnapshot(
459461
snapshots,
460462
newSnapshotDetails,
461463
allIndexSnapshots,
462-
ShardGenerations.builder().putAll(this.shardGenerations).putAll(shardGenerations).build(),
464+
ShardGenerations.builder()
465+
.putAll(this.shardGenerations)
466+
.putAll(shardGenerations)
467+
.updateIfPresent(updatedShardGenerations.deletedIndices())
468+
.build(),
463469
newIndexMetaGenerations,
464470
clusterUUID
465471
);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ public Builder putAll(ShardGenerations shardGenerations) {
231231
return this;
232232
}
233233

234+
public Builder updateIfPresent(ShardGenerations shardGenerations) {
235+
shardGenerations.shardGenerations.forEach((indexId, gens) -> {
236+
if (generations.containsKey(indexId)) {
237+
for (int i = 0; i < gens.size(); i++) {
238+
final ShardGeneration gen = gens.get(i);
239+
if (gen != null) {
240+
generations.get(indexId).put(i, gen);
241+
}
242+
}
243+
}
244+
});
245+
return this;
246+
}
247+
234248
public Builder put(IndexId indexId, int shardId, SnapshotsInProgress.ShardSnapshotStatus status) {
235249
// only track generations for successful shard status values
236250
return put(indexId, shardId, status.state().failed() ? null : status.generation());

server/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ record RootBlobUpdateResult(RepositoryData oldRepositoryData, RepositoryData new
18671867
existingRepositoryData.addSnapshot(
18681868
snapshotId,
18691869
snapshotDetails,
1870-
shardGenerations,
1870+
finalizeSnapshotContext.updatedShardGenerations(),
18711871
metadataWriteResult.indexMetas(),
18721872
metadataWriteResult.indexMetaIdentifiers()
18731873
),

server/src/test/java/org/elasticsearch/repositories/RepositoryDataTests.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.common.util.Maps;
1717
import org.elasticsearch.index.IndexVersion;
1818
import org.elasticsearch.index.IndexVersions;
19+
import org.elasticsearch.repositories.FinalizeSnapshotContext.UpdatedShardGenerations;
1920
import org.elasticsearch.snapshots.SnapshotId;
2021
import org.elasticsearch.snapshots.SnapshotState;
2122
import org.elasticsearch.test.ESTestCase;
@@ -118,7 +119,7 @@ public void testAddSnapshots() {
118119
randomNonNegativeLong(),
119120
randomAlphaOfLength(10)
120121
),
121-
shardGenerations,
122+
new UpdatedShardGenerations(shardGenerations),
122123
indexLookup,
123124
indexLookup.values().stream().collect(Collectors.toMap(Function.identity(), ignored -> UUIDs.randomBase64UUID(random())))
124125
);
@@ -218,7 +219,7 @@ public void testGetSnapshotState() {
218219
randomNonNegativeLong(),
219220
randomAlphaOfLength(10)
220221
),
221-
ShardGenerations.EMPTY,
222+
UpdatedShardGenerations.EMPTY,
222223
Collections.emptyMap(),
223224
Collections.emptyMap()
224225
);
@@ -400,7 +401,13 @@ public void testIndexMetaDataToRemoveAfterRemovingSnapshotWithSharing() {
400401
randomNonNegativeLong(),
401402
randomAlphaOfLength(10)
402403
);
403-
final RepositoryData newRepoData = repositoryData.addSnapshot(newSnapshot, details, shardGenerations, indexLookup, newIdentifiers);
404+
final RepositoryData newRepoData = repositoryData.addSnapshot(
405+
newSnapshot,
406+
details,
407+
new UpdatedShardGenerations(shardGenerations),
408+
indexLookup,
409+
newIdentifiers
410+
);
404411
assertEquals(
405412
newRepoData.indexMetaDataToRemoveAfterRemovingSnapshots(Collections.singleton(newSnapshot)),
406413
newIndices.entrySet()
@@ -476,7 +483,7 @@ public static RepositoryData generateRandomRepoData() {
476483
randomNonNegativeLong(),
477484
randomAlphaOfLength(10)
478485
),
479-
builder.build(),
486+
new UpdatedShardGenerations(builder.build()),
480487
indexLookup,
481488
indexLookup.values().stream().collect(Collectors.toMap(Function.identity(), ignored -> UUIDs.randomBase64UUID(random())))
482489
);

server/src/test/java/org/elasticsearch/repositories/blobstore/BlobStoreRepositoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.elasticsearch.index.IndexVersion;
5252
import org.elasticsearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot;
5353
import org.elasticsearch.indices.recovery.RecoverySettings;
54+
import org.elasticsearch.repositories.FinalizeSnapshotContext.UpdatedShardGenerations;
5455
import org.elasticsearch.repositories.IndexId;
5556
import org.elasticsearch.repositories.RepositoriesService;
5657
import org.elasticsearch.repositories.RepositoryData;
@@ -510,7 +511,7 @@ private RepositoryData addRandomSnapshotsToRepoData(RepositoryData repoData, boo
510511
repoData = repoData.addSnapshot(
511512
snapshotId,
512513
details,
513-
shardGenerations,
514+
new UpdatedShardGenerations(shardGenerations),
514515
indexLookup,
515516
indexLookup.values().stream().collect(Collectors.toMap(Function.identity(), ignored -> UUIDs.randomBase64UUID(random())))
516517
);

x-pack/plugin/slm/src/test/java/org/elasticsearch/xpack/slm/TransportSLMGetExpiredSnapshotsActionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import org.elasticsearch.core.Nullable;
2222
import org.elasticsearch.core.Tuple;
2323
import org.elasticsearch.index.IndexVersion;
24+
import org.elasticsearch.repositories.FinalizeSnapshotContext.UpdatedShardGenerations;
2425
import org.elasticsearch.repositories.RepositoriesService;
2526
import org.elasticsearch.repositories.Repository;
2627
import org.elasticsearch.repositories.RepositoryData;
2728
import org.elasticsearch.repositories.RepositoryMissingException;
28-
import org.elasticsearch.repositories.ShardGenerations;
2929
import org.elasticsearch.snapshots.Snapshot;
3030
import org.elasticsearch.snapshots.SnapshotId;
3131
import org.elasticsearch.snapshots.SnapshotInfo;
@@ -299,7 +299,7 @@ private static Repository createMockRepository(ThreadPool threadPool, List<Snaps
299299
repositoryData = repositoryData.addSnapshot(
300300
snapshotInfo.snapshotId(),
301301
snapshotDetails,
302-
ShardGenerations.EMPTY,
302+
UpdatedShardGenerations.EMPTY,
303303
Map.of(),
304304
Map.of()
305305
);

0 commit comments

Comments
 (0)