Skip to content

Commit

Permalink
Use Add Index Block API in N-2 upgrade tests
Browse files Browse the repository at this point in the history
Now the Add Index Block API automatically synchronizes
the translog, flushes the shard and sets the verified
setting, we can use it in the N-2 upgrade tests instead
of explicit calls.

Relates elastic#119743
Relates elastic#120522
  • Loading branch information
tlrx committed Jan 21, 2025
1 parent 04358fa commit 4187d1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit 4187d1c

Please sign in to comment.