From 8475a7a105e6e42790dfba132a187a03d083e48e Mon Sep 17 00:00:00 2001 From: Artem Prigoda Date: Wed, 8 Nov 2023 10:18:38 +0100 Subject: [PATCH] Verify we don't have indices with same name but different ids in a snapshot repo (#101389) I couldn't reproduce the test case from #97261 where we had multiple indices with the same name. Let's see if additional checks in ShardGenerations builder would show something suspicious. Fixes #97261 Co-authored-by: David Turner --- .../elasticsearch/repositories/ShardGenerations.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/repositories/ShardGenerations.java b/server/src/main/java/org/elasticsearch/repositories/ShardGenerations.java index 6e8ae1956f945..e42552d3e5f3c 100644 --- a/server/src/main/java/org/elasticsearch/repositories/ShardGenerations.java +++ b/server/src/main/java/org/elasticsearch/repositories/ShardGenerations.java @@ -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; @@ -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 shardIds = entry.getValue().keySet();