|
8 | 8 |
|
9 | 9 | package org.opensearch.cluster.block;
|
10 | 10 |
|
| 11 | +import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 12 | + |
| 13 | +import org.opensearch.Version; |
| 14 | +import org.opensearch.cluster.ClusterName; |
| 15 | +import org.opensearch.cluster.ClusterState; |
| 16 | +import org.opensearch.cluster.metadata.AliasMetadata; |
| 17 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 18 | +import org.opensearch.cluster.metadata.Metadata; |
11 | 19 | import org.opensearch.common.io.stream.BytesStreamOutput;
|
| 20 | +import org.opensearch.common.settings.Setting; |
| 21 | +import org.opensearch.common.settings.Settings; |
12 | 22 | import org.opensearch.core.common.io.stream.BufferedChecksumStreamOutput;
|
13 | 23 | import org.opensearch.core.common.io.stream.StreamInput;
|
| 24 | +import org.opensearch.index.IndexModule; |
| 25 | +import org.opensearch.index.IndexSettings; |
14 | 26 | import org.opensearch.test.OpenSearchTestCase;
|
15 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.Set; |
| 33 | + |
16 | 34 | import static org.opensearch.cluster.block.ClusterBlockTests.randomClusterBlock;
|
| 35 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_METADATA_BLOCK; |
| 36 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_BLOCK; |
| 37 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_ALLOW_DELETE_BLOCK; |
| 38 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK; |
| 39 | +import static org.opensearch.cluster.metadata.IndexMetadata.INDEX_WRITE_BLOCK; |
| 40 | +import static org.opensearch.cluster.metadata.IndexMetadata.REMOTE_READ_ONLY_ALLOW_DELETE; |
| 41 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_CREATION_DATE; |
| 42 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; |
| 43 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; |
| 44 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED; |
17 | 45 |
|
18 | 46 | public class ClusterBlocksTests extends OpenSearchTestCase {
|
19 | 47 |
|
@@ -52,4 +80,153 @@ public void testWriteVerifiableTo() throws Exception {
|
52 | 80 | clusterBlocks2.writeVerifiableTo(checksumOut2);
|
53 | 81 | assertEquals(checksumOut.getChecksum(), checksumOut2.getChecksum());
|
54 | 82 | }
|
| 83 | + |
| 84 | + public void testGlobalBlock() { |
| 85 | + String index = "test-000001"; |
| 86 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 87 | + Set<String> indices = new HashSet<>(); |
| 88 | + indices.add(index); |
| 89 | + |
| 90 | + // no global blocks |
| 91 | + { |
| 92 | + stateBuilder.blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK); |
| 93 | + ClusterState clusterState = stateBuilder.build(); |
| 94 | + clusterState.blocks(); |
| 95 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, clusterState)); |
| 96 | + } |
| 97 | + |
| 98 | + // has global block |
| 99 | + { |
| 100 | + for (ClusterBlock block : Arrays.asList( |
| 101 | + INDEX_READ_ONLY_BLOCK, |
| 102 | + INDEX_READ_BLOCK, |
| 103 | + INDEX_WRITE_BLOCK, |
| 104 | + INDEX_METADATA_BLOCK, |
| 105 | + INDEX_READ_ONLY_ALLOW_DELETE_BLOCK, |
| 106 | + REMOTE_READ_ONLY_ALLOW_DELETE |
| 107 | + )) { |
| 108 | + stateBuilder.blocks(ClusterBlocks.builder().addGlobalBlock(block).build()); |
| 109 | + ClusterState clusterState = stateBuilder.build(); |
| 110 | + clusterState.blocks(); |
| 111 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, clusterState)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public void testIndexWithBlock() { |
| 117 | + String index = "test-000001"; |
| 118 | + Set<String> indices = new HashSet<>(); |
| 119 | + indices.add(index); |
| 120 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 121 | + stateBuilder.blocks(ClusterBlocks.builder().addIndexBlock(index, IndexMetadata.INDEX_METADATA_BLOCK)); |
| 122 | + stateBuilder.metadata(Metadata.builder().put(createIndexMetadata(index, false, null, null), false)); |
| 123 | + ClusterState clusterState = stateBuilder.build(); |
| 124 | + clusterState.blocks(); |
| 125 | + assertNotNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, stateBuilder.build())); |
| 126 | + } |
| 127 | + |
| 128 | + public void testRemoteIndexBlock() { |
| 129 | + String remoteIndex = "remote_index"; |
| 130 | + Set<String> indices = new HashSet<>(); |
| 131 | + indices.add(remoteIndex); |
| 132 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 133 | + |
| 134 | + { |
| 135 | + IndexMetadata remoteSnapshotIndexMetadata = createIndexMetadata(remoteIndex, true, null, null); |
| 136 | + stateBuilder.metadata(Metadata.builder().put(remoteSnapshotIndexMetadata, false)); |
| 137 | + stateBuilder.blocks(ClusterBlocks.builder().addBlocks(remoteSnapshotIndexMetadata)); |
| 138 | + |
| 139 | + ClusterState clusterState = stateBuilder.build(); |
| 140 | + assertTrue(clusterState.blocks().hasIndexBlock(remoteIndex, IndexMetadata.REMOTE_READ_ONLY_ALLOW_DELETE)); |
| 141 | + clusterState.blocks(); |
| 142 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, clusterState)); |
| 143 | + } |
| 144 | + |
| 145 | + // searchable snapshot index with block |
| 146 | + { |
| 147 | + Setting<Boolean> setting = RandomizedTest.randomFrom(new ArrayList<>(ClusterBlocks.INDEX_DATA_READ_ONLY_BLOCK_SETTINGS)); |
| 148 | + IndexMetadata remoteSnapshotIndexMetadata = createIndexMetadata(remoteIndex, true, null, setting); |
| 149 | + stateBuilder.metadata(Metadata.builder().put(remoteSnapshotIndexMetadata, false)); |
| 150 | + stateBuilder.blocks(ClusterBlocks.builder().addBlocks(remoteSnapshotIndexMetadata)); |
| 151 | + ClusterState clusterState = stateBuilder.build(); |
| 152 | + clusterState.blocks(); |
| 153 | + assertNotNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, clusterState)); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void testRemoteIndexWithoutBlock() { |
| 158 | + String remoteIndex = "remote_index"; |
| 159 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 160 | + |
| 161 | + String alias = "alias1"; |
| 162 | + IndexMetadata remoteSnapshotIndexMetadata = createIndexMetadata(remoteIndex, true, alias, null); |
| 163 | + String index = "test-000001"; |
| 164 | + IndexMetadata indexMetadata = createIndexMetadata(index, false, alias, null); |
| 165 | + stateBuilder.metadata(Metadata.builder().put(remoteSnapshotIndexMetadata, false).put(indexMetadata, false)); |
| 166 | + |
| 167 | + Set<String> indices = new HashSet<>(); |
| 168 | + indices.add(remoteIndex); |
| 169 | + ClusterState clusterState = stateBuilder.build(); |
| 170 | + clusterState.blocks(); |
| 171 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(indices, clusterState)); |
| 172 | + } |
| 173 | + |
| 174 | + public void testRemoteIndexWithIndexBlock() { |
| 175 | + String index = "test-000001"; |
| 176 | + String remoteIndex = "remote_index"; |
| 177 | + String alias = "alias1"; |
| 178 | + { |
| 179 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 180 | + IndexMetadata remoteSnapshotIndexMetadata = createIndexMetadata(remoteIndex, true, alias, null); |
| 181 | + IndexMetadata indexMetadata = createIndexMetadata(index, false, alias, null); |
| 182 | + stateBuilder.metadata(Metadata.builder().put(remoteSnapshotIndexMetadata, false).put(indexMetadata, false)) |
| 183 | + .blocks(ClusterBlocks.builder().addBlocks(remoteSnapshotIndexMetadata)); |
| 184 | + ClusterState clusterState = stateBuilder.build(); |
| 185 | + clusterState.blocks(); |
| 186 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(Collections.singleton(index), clusterState)); |
| 187 | + clusterState.blocks(); |
| 188 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(Collections.singleton(remoteIndex), clusterState)); |
| 189 | + } |
| 190 | + |
| 191 | + { |
| 192 | + ClusterState.Builder stateBuilder = ClusterState.builder(new ClusterName("cluster")); |
| 193 | + Setting<Boolean> setting = RandomizedTest.randomFrom(new ArrayList<>(ClusterBlocks.INDEX_DATA_READ_ONLY_BLOCK_SETTINGS)); |
| 194 | + IndexMetadata remoteSnapshotIndexMetadata = createIndexMetadata(remoteIndex, true, alias, setting); |
| 195 | + IndexMetadata indexMetadata = createIndexMetadata(index, false, alias, null); |
| 196 | + stateBuilder.metadata(Metadata.builder().put(remoteSnapshotIndexMetadata, false).put(indexMetadata, false)) |
| 197 | + .blocks(ClusterBlocks.builder().addBlocks(remoteSnapshotIndexMetadata)); |
| 198 | + ClusterState clusterState = stateBuilder.build(); |
| 199 | + assertNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(Collections.singleton(index), clusterState)); |
| 200 | + assertNotNull(ClusterBlocks.indicesWithRemoteSnapshotBlockedException(Collections.singleton(remoteIndex), clusterState)); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private IndexMetadata createIndexMetadata(String index, boolean isRemoteIndex, String alias, Setting<Boolean> blockSetting) { |
| 205 | + IndexMetadata.Builder builder = IndexMetadata.builder(index).settings(createIndexSettingBuilder(isRemoteIndex, blockSetting)); |
| 206 | + if (alias != null) { |
| 207 | + AliasMetadata.Builder aliasBuilder = AliasMetadata.builder(alias); |
| 208 | + return builder.putAlias(aliasBuilder.build()).build(); |
| 209 | + } |
| 210 | + return builder.build(); |
| 211 | + } |
| 212 | + |
| 213 | + private Settings.Builder createIndexSettingBuilder(boolean isRemoteIndex, Setting<Boolean> blockSetting) { |
| 214 | + Settings.Builder builder = Settings.builder() |
| 215 | + .put(IndexMetadata.SETTING_INDEX_UUID, "abc") |
| 216 | + .put(SETTING_VERSION_CREATED, Version.CURRENT) |
| 217 | + .put(SETTING_CREATION_DATE, System.currentTimeMillis()) |
| 218 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 219 | + .put(SETTING_NUMBER_OF_REPLICAS, 1); |
| 220 | + |
| 221 | + if (isRemoteIndex) { |
| 222 | + builder.put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), IndexModule.Type.REMOTE_SNAPSHOT.getSettingsKey()) |
| 223 | + .put(IndexSettings.SEARCHABLE_SNAPSHOT_REPOSITORY.getKey(), "repo") |
| 224 | + .put(IndexSettings.SEARCHABLE_SNAPSHOT_ID_NAME.getKey(), "snapshot"); |
| 225 | + } |
| 226 | + if (blockSetting != null) { |
| 227 | + builder.put(blockSetting.getKey(), true); |
| 228 | + } |
| 229 | + |
| 230 | + return builder; |
| 231 | + } |
55 | 232 | }
|
0 commit comments