Skip to content

Commit 263e08b

Browse files
committed
Drop alloc filters on mount of searchable snapshot (#70007)
Today when a searchable snapshot is mounted it inherits many of the settings of the original index, including any index-level allocation filters that were in place when the snapshot was taken. These filters typically make no sense on the mounted index, so users must explicitly override or drop them. With this commit at mount time we drop all index-level allocation filters that existed on the original index, although we continue to respect any allocation filters that were specified as part of the mount request. Closes #69759
1 parent 15d6be4 commit 263e08b

File tree

2 files changed

+188
-4
lines changed

2 files changed

+188
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.searchablesnapshots;
9+
10+
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
11+
import org.elasticsearch.common.Nullable;
12+
import org.elasticsearch.common.Strings;
13+
import org.elasticsearch.common.settings.Setting;
14+
import org.elasticsearch.common.settings.Settings;
15+
import org.elasticsearch.index.IndexSettings;
16+
import org.elasticsearch.test.ESIntegTestCase;
17+
import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction;
18+
import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest;
19+
20+
import java.util.List;
21+
import java.util.Locale;
22+
23+
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_SETTING;
24+
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_SETTING;
25+
import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_SETTING;
26+
import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
27+
import static org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP_SETTING;
28+
import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING;
29+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
30+
import static org.hamcrest.Matchers.equalTo;
31+
32+
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0)
33+
public class AllocationFilteringIntegTests extends BaseSearchableSnapshotsIntegTestCase {
34+
35+
private MountSearchableSnapshotRequest prepareMountRequest(
36+
Settings.Builder originalIndexSettings,
37+
Settings.Builder mountedIndexSettings
38+
) throws InterruptedException {
39+
40+
final String fsRepoName = randomAlphaOfLength(10);
41+
final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
42+
final String snapshotName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
43+
44+
createRepository(fsRepoName, "fs", Settings.builder().put("location", randomRepoPath()));
45+
assertAcked(
46+
prepareCreate(
47+
indexName,
48+
Settings.builder()
49+
.put(INDEX_SOFT_DELETES_SETTING.getKey(), true)
50+
.put(SETTING_NUMBER_OF_REPLICAS, 0)
51+
.put(originalIndexSettings.build())
52+
)
53+
);
54+
populateIndex(indexName, 10);
55+
ensureGreen(indexName);
56+
createFullSnapshot(fsRepoName, snapshotName);
57+
assertAcked(client().admin().indices().prepareDelete(indexName));
58+
59+
final Settings.Builder indexSettingsBuilder = Settings.builder()
60+
.put(SearchableSnapshots.SNAPSHOT_CACHE_ENABLED_SETTING.getKey(), true)
61+
.put(IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(), Boolean.FALSE.toString())
62+
.put(mountedIndexSettings.build());
63+
64+
return new MountSearchableSnapshotRequest(
65+
indexName,
66+
fsRepoName,
67+
snapshotName,
68+
indexName,
69+
indexSettingsBuilder.build(),
70+
Strings.EMPTY_ARRAY,
71+
true,
72+
MountSearchableSnapshotRequest.Storage.FULL_COPY
73+
);
74+
}
75+
76+
public void testRemovesRequireFilter() throws InterruptedException {
77+
runTest(INDEX_ROUTING_REQUIRE_GROUP_SETTING, true, null, true);
78+
}
79+
80+
public void testRemovesIncludeFilter() throws InterruptedException {
81+
runTest(INDEX_ROUTING_INCLUDE_GROUP_SETTING, true, null, true);
82+
}
83+
84+
public void testRemovesExcludeFilter() throws InterruptedException {
85+
runTest(INDEX_ROUTING_EXCLUDE_GROUP_SETTING, false, null, true);
86+
}
87+
88+
public void testReplacesRequireFilter() throws InterruptedException {
89+
runTest(INDEX_ROUTING_REQUIRE_GROUP_SETTING, true, INDEX_ROUTING_REQUIRE_GROUP_SETTING, true);
90+
}
91+
92+
public void testReplacesIncludeFilter() throws InterruptedException {
93+
runTest(INDEX_ROUTING_INCLUDE_GROUP_SETTING, true, INDEX_ROUTING_INCLUDE_GROUP_SETTING, true);
94+
}
95+
96+
public void testReplacesExcludeFilter() throws InterruptedException {
97+
runTest(INDEX_ROUTING_EXCLUDE_GROUP_SETTING, false, INDEX_ROUTING_EXCLUDE_GROUP_SETTING, false);
98+
}
99+
100+
public void testReplacesIncludeFilterWithExcludeFilter() throws InterruptedException {
101+
runTest(INDEX_ROUTING_INCLUDE_GROUP_SETTING, true, INDEX_ROUTING_EXCLUDE_GROUP_SETTING, false);
102+
}
103+
104+
/**
105+
* Starts two nodes, allocates the original index on the first node but then excludes that node and verifies that the index can still be
106+
* mounted and allocated to the other node.
107+
*
108+
* @param indexSetting an allocation filter setting to apply to the original index
109+
* @param mountSetting an (optional) allocation filter setting to apply at mount time
110+
* @param indexSettingIsPositive whether {@code indexSetting} is positive (i.e. include/require) or negative (i.e. exclude)
111+
* @param mountSettingIsPositive whether {@code mountSetting} is positive (i.e. include/require) or negative (i.e. exclude)
112+
*/
113+
private void runTest(
114+
Setting.AffixSetting<String> indexSetting,
115+
boolean indexSettingIsPositive,
116+
@Nullable Setting.AffixSetting<String> mountSetting,
117+
boolean mountSettingIsPositive
118+
) throws InterruptedException {
119+
final List<String> nodes = internalCluster().startNodes(2);
120+
121+
// apply an index setting to restrict the original index to node 0
122+
final Settings.Builder indexSettings = Settings.builder()
123+
.put(indexSetting.getConcreteSettingForNamespace("_name").getKey(), nodes.get(indexSettingIsPositive ? 0 : 1));
124+
125+
final Settings.Builder mountSettings = Settings.builder();
126+
if (mountSetting != null) {
127+
// apply an index setting to restrict the mounted index to node 1
128+
mountSettings.put(mountSetting.getConcreteSettingForNamespace("_name").getKey(), nodes.get(mountSettingIsPositive ? 1 : 0));
129+
}
130+
131+
final MountSearchableSnapshotRequest mountRequest = prepareMountRequest(indexSettings, mountSettings);
132+
133+
// block allocation to node 0 at the cluster level
134+
assertAcked(
135+
client().admin()
136+
.cluster()
137+
.prepareUpdateSettings()
138+
.setPersistentSettings(
139+
Settings.builder()
140+
.put(CLUSTER_ROUTING_EXCLUDE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey(), nodes.get(0))
141+
)
142+
);
143+
144+
// mount snapshot and verify it is allocated as expected
145+
final RestoreSnapshotResponse restoreSnapshotResponse = client().execute(MountSearchableSnapshotAction.INSTANCE, mountRequest)
146+
.actionGet();
147+
assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
148+
ensureGreen(mountRequest.mountedIndexName());
149+
150+
final Settings mountedIndexSettings = client().admin()
151+
.indices()
152+
.prepareGetSettings(mountRequest.mountedIndexName())
153+
.get()
154+
.getIndexToSettings()
155+
.get(mountRequest.mountedIndexName());
156+
157+
if (mountSetting != null) {
158+
assertTrue(mountedIndexSettings.toString(), mountSetting.getConcreteSettingForNamespace("_name").exists(mountedIndexSettings));
159+
}
160+
161+
if (indexSetting != mountSetting) {
162+
assertFalse(mountedIndexSettings.toString(), indexSetting.getConcreteSettingForNamespace("_name").exists(mountedIndexSettings));
163+
}
164+
165+
assertAcked(
166+
client().admin()
167+
.cluster()
168+
.prepareUpdateSettings()
169+
.setPersistentSettings(
170+
Settings.builder().putNull(CLUSTER_ROUTING_EXCLUDE_GROUP_SETTING.getConcreteSettingForNamespace("_name").getKey())
171+
)
172+
);
173+
}
174+
175+
}

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/action/TransportMountSearchableSnapshotAction.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@
4242
import org.elasticsearch.xpack.searchablesnapshots.SearchableSnapshotsConstants;
4343

4444
import java.util.Arrays;
45+
import java.util.LinkedHashSet;
4546
import java.util.Locale;
4647
import java.util.Map;
4748
import java.util.Objects;
4849
import java.util.Optional;
50+
import java.util.Set;
4951

5052
import static org.elasticsearch.index.IndexModule.INDEX_RECOVERY_TYPE_SETTING;
5153
import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING;
@@ -178,9 +180,6 @@ protected void masterOperation(
178180
}
179181
final SnapshotId snapshotId = matchingSnapshotId.get();
180182

181-
final String[] ignoreIndexSettings = Arrays.copyOf(request.ignoreIndexSettings(), request.ignoreIndexSettings().length + 1);
182-
ignoreIndexSettings[ignoreIndexSettings.length - 1] = IndexMetadata.SETTING_DATA_PATH;
183-
184183
final IndexMetadata indexMetadata = repository.getSnapshotIndexMetaData(repoData, snapshotId, indexId);
185184
if (isSearchableSnapshotStore(indexMetadata.getSettings())) {
186185
throw new IllegalArgumentException(
@@ -200,6 +199,16 @@ protected void masterOperation(
200199
);
201200
}
202201

202+
final Set<String> ignoreIndexSettings = new LinkedHashSet<>(Arrays.asList(request.ignoreIndexSettings()));
203+
ignoreIndexSettings.add(IndexMetadata.SETTING_DATA_PATH);
204+
for (final String indexSettingKey : indexMetadata.getSettings().keySet()) {
205+
if (indexSettingKey.startsWith(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_PREFIX)
206+
|| indexSettingKey.startsWith(IndexMetadata.INDEX_ROUTING_INCLUDE_GROUP_PREFIX)
207+
|| indexSettingKey.startsWith(IndexMetadata.INDEX_ROUTING_EXCLUDE_GROUP_PREFIX)) {
208+
ignoreIndexSettings.add(indexSettingKey);
209+
}
210+
}
211+
203212
client.admin()
204213
.cluster()
205214
.restoreSnapshot(
@@ -222,7 +231,7 @@ protected void masterOperation(
222231
.build()
223232
)
224233
// Pass through ignored index settings
225-
.ignoreIndexSettings(ignoreIndexSettings)
234+
.ignoreIndexSettings(ignoreIndexSettings.toArray(new String[0]))
226235
// Don't include global state
227236
.includeGlobalState(false)
228237
// Don't include aliases

0 commit comments

Comments
 (0)