Skip to content

Commit f176c7a

Browse files
committed
Fix repo name at allocation time (#73669)
Today when allocating a previously-allocated searchable snapshot shard we simulate a recovery source which refers to the repository named in the index metadata. This doesn't work if the repository was renamed since the index was mounted, which particularly may occur if the index was itself restored from a snapshot. With this commit we instead set up the recovery source to refer to the repository whose UUID matches the one in the index metadata. We rely on such a repository existing at allocation time; if it does not then the shards are not allocated.
1 parent d19c365 commit f176c7a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsIntegTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,10 @@ public void testSnapshotOfSearchableSnapshotIncludesNoDataButCanBeRestored() thr
942942

943943
assertTotalHits(restoredIndexName, originalAllHits, originalBarHits);
944944

945+
internalCluster().fullRestart();
946+
ensureGreen(restoredIndexName);
947+
assertTotalHits(restoredIndexName, originalAllHits, originalBarHits);
948+
945949
final IllegalArgumentException remountException = expectThrows(IllegalArgumentException.class, () -> {
946950
try {
947951
mountSnapshot(

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/allocation/SearchableSnapshotAllocator.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.elasticsearch.client.Client;
1515
import org.elasticsearch.cluster.ClusterState;
1616
import org.elasticsearch.cluster.metadata.IndexMetadata;
17+
import org.elasticsearch.cluster.metadata.RepositoriesMetadata;
18+
import org.elasticsearch.cluster.metadata.RepositoryMetadata;
1719
import org.elasticsearch.cluster.node.DiscoveryNode;
1820
import org.elasticsearch.cluster.node.DiscoveryNodes;
1921
import org.elasticsearch.cluster.routing.RecoverySource;
@@ -31,6 +33,7 @@
3133
import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider;
3234
import org.elasticsearch.common.Nullable;
3335
import org.elasticsearch.common.Priority;
36+
import org.elasticsearch.common.Strings;
3437
import org.elasticsearch.common.collect.Tuple;
3538
import org.elasticsearch.common.settings.Settings;
3639
import org.elasticsearch.common.unit.ByteSizeValue;
@@ -59,12 +62,14 @@
5962
import java.util.stream.Collectors;
6063
import java.util.stream.StreamSupport;
6164

65+
import static java.util.Collections.emptyList;
6266
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_INDEX_ID_SETTING;
6367
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_INDEX_NAME_SETTING;
64-
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING;
6568
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_REPOSITORY_NAME_SETTING;
69+
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_REPOSITORY_UUID_SETTING;
6670
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_SNAPSHOT_ID_SETTING;
6771
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshots.SNAPSHOT_SNAPSHOT_NAME_SETTING;
72+
import static org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants.SNAPSHOT_PARTIAL_SETTING;
6873

6974
public class SearchableSnapshotAllocator implements ExistingShardsAllocator {
7075

@@ -144,8 +149,28 @@ public void allocateUnassigned(
144149
SNAPSHOT_SNAPSHOT_NAME_SETTING.get(indexSettings),
145150
SNAPSHOT_SNAPSHOT_ID_SETTING.get(indexSettings)
146151
);
147-
final String repository = SNAPSHOT_REPOSITORY_NAME_SETTING.get(indexSettings);
148-
final Snapshot snapshot = new Snapshot(repository, snapshotId);
152+
153+
final String repositoryUuid = SNAPSHOT_REPOSITORY_UUID_SETTING.get(indexSettings);
154+
final String repositoryName;
155+
if (Strings.hasLength(repositoryUuid) == false) {
156+
repositoryName = SNAPSHOT_REPOSITORY_NAME_SETTING.get(indexSettings);
157+
} else {
158+
final RepositoriesMetadata repoMetadata = allocation.metadata().custom(RepositoriesMetadata.TYPE);
159+
final List<RepositoryMetadata> repositories = repoMetadata == null ? emptyList() : repoMetadata.repositories();
160+
repositoryName = repositories.stream()
161+
.filter(r -> repositoryUuid.equals(r.uuid()))
162+
.map(RepositoryMetadata::name)
163+
.findFirst()
164+
.orElse(null);
165+
}
166+
167+
if (repositoryName == null) {
168+
// TODO if the repository we seek appears later, we will need to get its UUID (usually automatic) and then reroute
169+
unassignedAllocationHandler.removeAndIgnore(UnassignedInfo.AllocationStatus.DECIDERS_NO, allocation.changes());
170+
return;
171+
}
172+
173+
final Snapshot snapshot = new Snapshot(repositoryName, snapshotId);
149174

150175
shardRouting = unassignedAllocationHandler.updateUnassigned(
151176
shardRouting.unassignedInfo(),

0 commit comments

Comments
 (0)