|
14 | 14 | import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
15 | 15 | import com.carrotsearch.randomizedtesting.annotations.TestCaseOrdering; |
16 | 16 |
|
| 17 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 18 | +import org.elasticsearch.common.settings.Settings; |
17 | 19 | import org.elasticsearch.test.cluster.util.Version; |
18 | 20 |
|
19 | 21 | import java.util.Comparator; |
20 | 22 | import java.util.List; |
21 | 23 | import java.util.stream.Stream; |
22 | 24 |
|
| 25 | +import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_READ_ONLY_BLOCK; |
| 26 | +import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_WRITE_BLOCK; |
| 27 | +import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK; |
| 28 | +import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING; |
| 29 | +import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING; |
23 | 30 | import static org.elasticsearch.test.cluster.util.Version.CURRENT; |
| 31 | +import static org.hamcrest.Matchers.allOf; |
| 32 | +import static org.hamcrest.Matchers.contains; |
| 33 | +import static org.hamcrest.Matchers.either; |
24 | 34 | import static org.hamcrest.Matchers.equalTo; |
| 35 | +import static org.hamcrest.Matchers.hasItem; |
25 | 36 | import static org.hamcrest.Matchers.hasSize; |
| 37 | +import static org.hamcrest.Matchers.is; |
| 38 | +import static org.hamcrest.Matchers.not; |
26 | 39 | import static org.hamcrest.Matchers.notNullValue; |
27 | 40 |
|
28 | 41 | /** |
@@ -86,6 +99,101 @@ protected void maybeUpgrade() throws Exception { |
86 | 99 | } |
87 | 100 | } |
88 | 101 |
|
| 102 | + protected void addAndAssertIndexBlocks(String index, boolean maybeClose) throws Exception { |
| 103 | + final var randomBlocks = randomFrom( |
| 104 | + List.of(IndexMetadata.APIBlock.WRITE, IndexMetadata.APIBlock.READ_ONLY), |
| 105 | + List.of(IndexMetadata.APIBlock.READ_ONLY), |
| 106 | + List.of(IndexMetadata.APIBlock.WRITE) |
| 107 | + ); |
| 108 | + for (var randomBlock : randomBlocks) { |
| 109 | + addIndexBlock(index, randomBlock); |
| 110 | + assertThat(indexBlocks(index), hasItem(randomBlock.getBlock())); |
| 111 | + } |
| 112 | + |
| 113 | + assertThat(indexBlocks(index), maybeClose ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK))); |
| 114 | + assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(maybeClose)); |
| 115 | + assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true)); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * assert that index has either a read-only block or write block, if index is closed also a cloded-block. |
| 121 | + * In case both blocks are present, randomly modify one of them. |
| 122 | + */ |
| 123 | + protected void assertAndModifyIndexBlocks(String index, boolean isClosed) throws Exception { |
| 124 | + logger.debug("--> upgraded index [{}] is now in [{}] state", index, isClosed ? "closed" : "open"); |
| 125 | + assertThat( |
| 126 | + indexBlocks(index), |
| 127 | + allOf( |
| 128 | + either(hasItem(INDEX_READ_ONLY_BLOCK)).or(hasItem(INDEX_WRITE_BLOCK)), |
| 129 | + isClosed ? hasItem(INDEX_CLOSED_BLOCK) : not(hasItem(INDEX_CLOSED_BLOCK)) |
| 130 | + ) |
| 131 | + ); |
| 132 | + assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed)); |
| 133 | + assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true)); |
| 134 | + |
| 135 | + var blocks = indexBlocks(index).stream().filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)).toList(); |
| 136 | + if (blocks.size() == 2) { |
| 137 | + switch (randomInt(2)) { |
| 138 | + case 0: |
| 139 | + updateIndexSettings( |
| 140 | + index, |
| 141 | + Settings.builder() |
| 142 | + .putNull(IndexMetadata.APIBlock.WRITE.settingName()) |
| 143 | + .put(IndexMetadata.APIBlock.READ_ONLY.settingName(), true) |
| 144 | + ); |
| 145 | + assertThat( |
| 146 | + indexBlocks(index), |
| 147 | + isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_READ_ONLY_BLOCK) : contains(INDEX_READ_ONLY_BLOCK) |
| 148 | + ); |
| 149 | + break; |
| 150 | + case 1: |
| 151 | + updateIndexSettings( |
| 152 | + index, |
| 153 | + Settings.builder() |
| 154 | + .putNull(IndexMetadata.APIBlock.READ_ONLY.settingName()) |
| 155 | + .put(IndexMetadata.APIBlock.WRITE.settingName(), true) |
| 156 | + ); |
| 157 | + assertThat( |
| 158 | + indexBlocks(index), |
| 159 | + isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK) |
| 160 | + ); |
| 161 | + break; |
| 162 | + case 2: |
| 163 | + updateIndexSettings(index, Settings.builder().put(IndexMetadata.APIBlock.READ_ONLY.settingName(), false)); |
| 164 | + assertThat( |
| 165 | + indexBlocks(index), |
| 166 | + isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK) |
| 167 | + ); |
| 168 | + break; |
| 169 | + default: |
| 170 | + throw new AssertionError(); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * ensure we have a write-block. If this is currently not the case, set it. |
| 177 | + */ |
| 178 | + protected void ensureWriteBlock(String index, boolean isClosed) throws Exception { |
| 179 | + List<org.elasticsearch.cluster.block.ClusterBlock> blocks = indexBlocks(index).stream() |
| 180 | + .filter(c -> c.equals(INDEX_WRITE_BLOCK) || c.equals(INDEX_READ_ONLY_BLOCK)) |
| 181 | + .toList(); |
| 182 | + if (blocks.contains(INDEX_READ_ONLY_BLOCK)) { |
| 183 | + logger.debug("--> read_only API block can be replaced by a write block (required for the remaining tests)"); |
| 184 | + updateIndexSettings( |
| 185 | + index, |
| 186 | + Settings.builder() |
| 187 | + .putNull(IndexMetadata.APIBlock.READ_ONLY.settingName()) |
| 188 | + .put(IndexMetadata.APIBlock.WRITE.settingName(), true) |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + assertIndexSetting(index, VERIFIED_READ_ONLY_SETTING, is(true)); |
| 193 | + assertIndexSetting(index, VERIFIED_BEFORE_CLOSE_SETTING, is(isClosed)); |
| 194 | + assertThat(indexBlocks(index), isClosed ? contains(INDEX_CLOSED_BLOCK, INDEX_WRITE_BLOCK) : contains(INDEX_WRITE_BLOCK)); |
| 195 | + } |
| 196 | + |
89 | 197 | /** |
90 | 198 | * Execute the test suite with the parameters provided by the {@link #parameters()} in nodes versions order. |
91 | 199 | */ |
|
0 commit comments