|
| 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 | +} |
0 commit comments