diff --git a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/AbstractIndexCompatibilityTestCase.java b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/AbstractIndexCompatibilityTestCase.java index 392f2037139a3..6078194ef180f 100644 --- a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/AbstractIndexCompatibilityTestCase.java +++ b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/AbstractIndexCompatibilityTestCase.java @@ -15,6 +15,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.core.Booleans; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.test.XContentTestUtils; @@ -37,6 +38,7 @@ import java.util.Map; import java.util.stream.IntStream; +import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING; import static org.elasticsearch.test.cluster.util.Version.CURRENT; import static org.elasticsearch.test.cluster.util.Version.fromString; import static org.elasticsearch.test.rest.ObjectPath.createFromResponse; @@ -259,13 +261,24 @@ protected static boolean isIndexClosed(String indexName) throws Exception { return IndexMetadata.State.fromString((String) state) == IndexMetadata.State.CLOSE; } - protected static void addIndexWriteBlock(String indexName) throws Exception { - assertAcknowledged(client().performRequest(new Request("PUT", Strings.format("/%s/_block/write", indexName)))); - } - protected static void forceMerge(String indexName, int maxNumSegments) throws Exception { var request = new Request("POST", '/' + indexName + "/_forcemerge"); request.addParameter("max_num_segments", String.valueOf(maxNumSegments)); assertOK(client().performRequest(request)); } + + protected static void markAsReadOnly(String indexName) throws Exception { + var block = randomFrom(IndexMetadata.APIBlock.READ_ONLY, IndexMetadata.APIBlock.WRITE); + var request = new Request("PUT", Strings.format("/%s/_block/%s", indexName, block.name().toLowerCase(Locale.ROOT))); + assertAcknowledged(client().performRequest(request)); + } + + protected void assertMarkedAsReadOnly(String indexName) throws Exception { + var indexSettings = getIndexSettingsAsMap(indexName); + assertThat(indexSettings.get(VERIFIED_READ_ONLY_SETTING.getKey()), equalTo(Boolean.TRUE.toString())); + + boolean readOnly = Booleans.parseBoolean((String) indexSettings.get(IndexMetadata.APIBlock.READ_ONLY.settingName()), false); + boolean write = Booleans.parseBoolean((String) indexSettings.get(IndexMetadata.APIBlock.WRITE.settingName()), false); + assertThat("Expect either `read_only` [" + readOnly + "] or `write` [" + write + "] block", readOnly ^ write, is(true)); + } } diff --git a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/FullClusterRestartLuceneIndexCompatibilityIT.java b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/FullClusterRestartLuceneIndexCompatibilityIT.java index 89fefde08b9a4..8456d586f493c 100644 --- a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/FullClusterRestartLuceneIndexCompatibilityIT.java +++ b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/FullClusterRestartLuceneIndexCompatibilityIT.java @@ -15,7 +15,6 @@ import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.test.cluster.util.Version; -import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING; import static org.hamcrest.Matchers.equalTo; public class FullClusterRestartLuceneIndexCompatibilityIT extends FullClusterRestartIndexCompatibilityTestCase { @@ -58,14 +57,8 @@ public void testIndexUpgrade() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - logger.debug("--> flushing [{}]", index); - flush(index, true); - - logger.debug("--> applying write block on [{}]", index); - addIndexWriteBlock(index); - - logger.debug("--> applying verified read-only setting on [{}]", index); - updateIndexSettings(index, Settings.builder().put(VERIFIED_READ_ONLY_SETTING.getKey(), true)); + logger.debug("--> marking index [{}] as read-only", index); + markAsReadOnly(index); return; } @@ -75,9 +68,7 @@ public void testIndexUpgrade() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - var indexSettings = getIndexSettingsAsMap(index); - assertThat(indexSettings.get(IndexMetadata.APIBlock.WRITE.settingName()), equalTo(Boolean.TRUE.toString())); - assertThat(indexSettings.get(VERIFIED_READ_ONLY_SETTING.getKey()), equalTo(Boolean.TRUE.toString())); + assertMarkedAsReadOnly(index); var numberOfReplicas = getNumberOfReplicas(index); if (0 < numberOfReplicas) { @@ -146,14 +137,8 @@ public void testRestoreIndex() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - logger.debug("--> flushing [{}]", index); - flush(index, true); - - logger.debug("--> applying write block on [{}]", index); - addIndexWriteBlock(index); - - logger.debug("--> applying verified read-only setting on [{}]", index); - updateIndexSettings(index, Settings.builder().put(VERIFIED_READ_ONLY_SETTING.getKey(), true)); + logger.debug("--> marking index [{}] as read-only", index); + markAsReadOnly(index); logger.debug("--> creating snapshot [{}]", snapshot); createSnapshot(client(), repository, snapshot, true); @@ -169,6 +154,7 @@ public void testRestoreIndex() throws Exception { restoreIndex(repository, snapshot, index, restoredIndex); ensureGreen(restoredIndex); + assertMarkedAsReadOnly(restoredIndex); assertThat(indexVersion(restoredIndex), equalTo(VERSION_MINUS_2)); assertDocCount(client(), restoredIndex, numDocs); @@ -233,14 +219,8 @@ public void testRestoreIndexOverClosedIndex() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - logger.debug("--> flushing [{}]", index); - flush(index, true); - - logger.debug("--> applying write block on [{}]", index); - addIndexWriteBlock(index); - - logger.debug("--> applying verified read-only setting on [{}]", index); - updateIndexSettings(index, Settings.builder().put(VERIFIED_READ_ONLY_SETTING.getKey(), true)); + logger.debug("--> marking index [{}] as read-only", index); + markAsReadOnly(index); logger.debug("--> creating snapshot [{}]", snapshot); createSnapshot(client(), repository, snapshot, true); @@ -255,10 +235,8 @@ public void testRestoreIndexOverClosedIndex() throws Exception { } if (isFullyUpgradedTo(VERSION_CURRENT)) { - var indexSettings = getIndexSettingsAsMap(index); - assertThat(indexSettings.get(IndexMetadata.APIBlock.WRITE.settingName()), equalTo(Boolean.TRUE.toString())); - assertThat(indexSettings.get(VERIFIED_READ_ONLY_SETTING.getKey()), equalTo(Boolean.TRUE.toString())); assertThat(isIndexClosed(index), equalTo(true)); + assertMarkedAsReadOnly(index); logger.debug("--> restoring index [{}] over existing closed index", index); restoreIndex(repository, snapshot, index, index); diff --git a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeLuceneIndexCompatibilityTestCase.java b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeLuceneIndexCompatibilityTestCase.java index c183ccc39cdea..621028ed87bc6 100644 --- a/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeLuceneIndexCompatibilityTestCase.java +++ b/qa/lucene-index-compatibility/src/javaRestTest/java/org/elasticsearch/lucene/RollingUpgradeLuceneIndexCompatibilityTestCase.java @@ -19,7 +19,6 @@ import java.util.List; -import static org.elasticsearch.cluster.metadata.MetadataIndexStateService.VERIFIED_READ_ONLY_SETTING; import static org.hamcrest.Matchers.equalTo; public class RollingUpgradeLuceneIndexCompatibilityTestCase extends RollingUpgradeIndexCompatibilityTestCase { @@ -62,21 +61,14 @@ public void testIndexUpgrade() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - logger.debug("--> flushing [{}]", index); - flush(index, true); - - logger.debug("--> applying write block on [{}]", index); - addIndexWriteBlock(index); - - logger.debug("--> applying verified read-only setting on [{}]", index); - updateIndexSettings(index, Settings.builder().put(VERIFIED_READ_ONLY_SETTING.getKey(), true)); + logger.debug("--> marking index [{}] as read-only", index); + markAsReadOnly(index); return; } if (nodesVersions().values().stream().anyMatch(v -> v.onOrAfter(VERSION_CURRENT))) { var indexSettings = getIndexSettingsAsMap(index); - assertThat(indexSettings.get(IndexMetadata.APIBlock.WRITE.settingName()), equalTo(Boolean.TRUE.toString())); - assertThat(indexSettings.get(VERIFIED_READ_ONLY_SETTING.getKey()), equalTo(Boolean.TRUE.toString())); + assertMarkedAsReadOnly(index); if (isIndexClosed(index)) { logger.debug("--> re-opening index [{}] after upgrade", index); @@ -133,14 +125,8 @@ public void testRestoreIndex() throws Exception { assertThat(indexVersion(index), equalTo(VERSION_MINUS_2)); assertDocCount(client(), index, numDocs); - logger.debug("--> flushing [{}]", index); - flush(index, true); - - logger.debug("--> applying write block on [{}]", index); - addIndexWriteBlock(index); - - logger.debug("--> applying verified read-only setting on [{}]", index); - updateIndexSettings(index, Settings.builder().put(VERIFIED_READ_ONLY_SETTING.getKey(), true)); + logger.debug("--> marking index [{}] as read-only", index); + markAsReadOnly(index); logger.debug("--> creating snapshot [{}]", snapshot); createSnapshot(client(), repository, snapshot, true); @@ -158,6 +144,7 @@ public void testRestoreIndex() throws Exception { restoreIndex(repository, snapshot, index, restoredIndex); ensureGreen(restoredIndex); + assertMarkedAsReadOnly(restoredIndex); assertThat(indexVersion(restoredIndex), equalTo(VERSION_MINUS_2)); assertDocCount(client(), restoredIndex, numDocs);