Skip to content

Commit

Permalink
Verify we don't have indices with same name but different ids in a sn…
Browse files Browse the repository at this point in the history
…apshot repo (elastic#101389)

I couldn't reproduce the test case from elastic#97261 where we had multiple indices with the same name. Let's see if additional checks in ShardGenerations builder would show something suspicious.

Fixes elastic#97261

Co-authored-by: David Turner <[email protected]>
  • Loading branch information
arteam and DaveCTurner authored Nov 8, 2023
1 parent 5ef9f76 commit 8475a7a
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.repositories;

import org.elasticsearch.cluster.SnapshotsInProgress;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.snapshots.IndexShardSnapshotStatus;

Expand Down Expand Up @@ -219,12 +220,22 @@ public Builder put(IndexId indexId, int shardId, SnapshotsInProgress.ShardSnapsh
}

public Builder put(IndexId indexId, int shardId, ShardGeneration generation) {
assert noDuplicateIndicesWithSameName(indexId);
ShardGeneration existingGeneration = generations.computeIfAbsent(indexId, i -> new HashMap<>()).put(shardId, generation);
assert generation != null || existingGeneration == null
: "must not overwrite existing generation with null generation [" + existingGeneration + "]";
return this;
}

private boolean noDuplicateIndicesWithSameName(IndexId newId) {
for (IndexId id : generations.keySet()) {
if (id.getName().equals(newId.getName()) && id.equals(newId) == false) {
assert false : Strings.format("Unable to add: %s. There's another index id with the same name: %s", newId, id);
}
}
return true;
}

public ShardGenerations build() {
return new ShardGenerations(generations.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> {
final Set<Integer> shardIds = entry.getValue().keySet();
Expand Down

0 comments on commit 8475a7a

Please sign in to comment.