From 8ea0930a7ab86ff35223e0df3e8219efe77a9fea Mon Sep 17 00:00:00 2001 From: Gordon Brown Date: Mon, 29 Nov 2021 13:01:31 -0700 Subject: [PATCH] Strip blocks from settings for reindex targets (#80887) (#81122) When migrating system features, we copy settings from old indices into the new indices we create before reindexing. However, if we happen to copy a write block, this causes the reindexing to fail. Here, we strip the index block settings before applying settings to new indices. Fixes Co-authored-by: William Brafford --- .../migration/FeatureMigrationIT.java | 24 +++++++++++++++++++ .../upgrades/SystemIndexMigrator.java | 10 +++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java index 451760c21a862..5b77ba4d35b0e 100644 --- a/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java +++ b/modules/reindex/src/internalClusterTest/java/org/elasticsearch/migration/FeatureMigrationIT.java @@ -243,6 +243,30 @@ public void testMigrateInternalManagedSystemIndex() throws Exception { ); } + public void testMigrateIndexWithWriteBlock() throws Exception { + createSystemIndexForDescriptor(INTERNAL_UNMANAGED); + + String indexName = Optional.ofNullable(INTERNAL_UNMANAGED.getPrimaryIndex()) + .orElse(INTERNAL_UNMANAGED.getIndexPattern().replace("*", "old")); + client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put("index.blocks.write", true)).get(); + + TestPlugin.preMigrationHook.set((state) -> Collections.emptyMap()); + TestPlugin.postMigrationHook.set((state, metadata) -> {}); + + ensureGreen(); + + client().execute(PostFeatureUpgradeAction.INSTANCE, new PostFeatureUpgradeRequest()).get(); + + assertBusy(() -> { + GetFeatureUpgradeStatusResponse statusResp = client().execute( + GetFeatureUpgradeStatusAction.INSTANCE, + new GetFeatureUpgradeStatusRequest() + ).get(); + logger.info(Strings.toString(statusResp)); + assertThat(statusResp.getUpgradeStatus(), equalTo(GetFeatureUpgradeStatusResponse.UpgradeStatus.NO_MIGRATION_NEEDED)); + }); + } + public void assertIndexHasCorrectProperties( Metadata metadata, String indexName, diff --git a/server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java b/server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java index 80ca2d7593c26..a514c86d12237 100644 --- a/server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java +++ b/server/src/main/java/org/elasticsearch/upgrades/SystemIndexMigrator.java @@ -47,6 +47,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Queue; import java.util.concurrent.atomic.AtomicReference; @@ -459,9 +460,16 @@ private void createIndex(SystemIndexMigrationInfo migrationInfo, ActionListener< migrationInfo.getNextIndexName() ); + Settings.Builder settingsBuilder = Settings.builder(); + if (Objects.nonNull(migrationInfo.getSettings())) { + settingsBuilder.put(migrationInfo.getSettings()); + settingsBuilder.remove("index.blocks.write"); + settingsBuilder.remove("index.blocks.read"); + settingsBuilder.remove("index.blocks.metadata"); + } createRequest.waitForActiveShards(ActiveShardCount.ALL) .mappings(Collections.singletonMap("_doc", migrationInfo.getMappings())) - .settings(migrationInfo.getSettings() == null ? Settings.EMPTY : migrationInfo.getSettings()); + .settings(migrationInfo.getSettings() == null ? Settings.EMPTY : settingsBuilder.build()); metadataCreateIndexService.createIndex(createRequest, listener); }