From 6dee99cad0ff0e9a476fa9dff8ac18128405cc0e Mon Sep 17 00:00:00 2001 From: Rishav Sagar Date: Mon, 26 Sep 2022 19:31:17 +0530 Subject: [PATCH] Adding support for index create block in OpenSearch Signed-off-by: Rishav Sagar --- CHANGELOG.md | 1 + .../opensearch/blocks/CreateIndexBlockIT.java | 37 +++++++++++++++++++ .../org/opensearch/OpenSearchException.java | 6 +++ .../cluster/settings/SettingsUpdater.java | 7 ++++ .../create/TransportCreateIndexAction.java | 8 +++- .../cluster/block/ClusterBlockLevel.java | 3 +- .../cluster/block/ClusterBlocks.java | 7 ++++ .../block/IndexCreateBlockException.java | 30 +++++++++++++++ .../opensearch/cluster/metadata/Metadata.java | 17 +++++++++ .../common/settings/ClusterSettings.java | 1 + .../opensearch/cluster/ClusterStateTests.java | 18 ++++++--- 11 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 server/src/internalClusterTest/java/org/opensearch/blocks/CreateIndexBlockIT.java create mode 100644 server/src/main/java/org/opensearch/cluster/block/IndexCreateBlockException.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b6af9b7041db3..63242a1190871 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Add BWC version 2.3.1 ([#4513](https://github.com/opensearch-project/OpenSearch/pull/4513)) - [Segment Replication] Add snapshot and restore tests for segment replication feature ([#3993](https://github.com/opensearch-project/OpenSearch/pull/3993)) - Added missing javadocs for `:example-plugins` modules ([#4540](https://github.com/opensearch-project/OpenSearch/pull/4540)) +- Added support to apply index create block ([#4603](https://github.com/opensearch-project/OpenSearch/issues/4603)) ### Dependencies - Bumps `log4j-core` from 2.18.0 to 2.19.0 diff --git a/server/src/internalClusterTest/java/org/opensearch/blocks/CreateIndexBlockIT.java b/server/src/internalClusterTest/java/org/opensearch/blocks/CreateIndexBlockIT.java new file mode 100644 index 0000000000000..b9c97b5465644 --- /dev/null +++ b/server/src/internalClusterTest/java/org/opensearch/blocks/CreateIndexBlockIT.java @@ -0,0 +1,37 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.blocks; + +import org.opensearch.cluster.metadata.Metadata; +import org.opensearch.common.settings.Settings; +import org.opensearch.test.OpenSearchIntegTestCase; + +import static org.opensearch.test.OpenSearchIntegTestCase.client; +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertBlocked; + +public class CreateIndexBlockIT extends OpenSearchIntegTestCase { + + public void testBlockCreateIndex() { + try { + setCreateIndexBlock("true"); + assertBlocked(client().admin().indices().prepareCreate("diskguardrails1"), Metadata.CLUSTER_CREATE_INDEX_BLOCK); + } finally { + setCreateIndexBlock("false"); + assertAcked(client().admin().indices().prepareCreate("diskguardrails2").execute().actionGet()); + } + + } + + private void setCreateIndexBlock(String value) { + Settings settings = Settings.builder().put(Metadata.SETTING_CREATE_INDEX_BLOCK.getKey(), value).build(); + assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(settings).get()); + } + +} diff --git a/server/src/main/java/org/opensearch/OpenSearchException.java b/server/src/main/java/org/opensearch/OpenSearchException.java index 34d7509c7afb2..41e1f569176cd 100644 --- a/server/src/main/java/org/opensearch/OpenSearchException.java +++ b/server/src/main/java/org/opensearch/OpenSearchException.java @@ -1620,6 +1620,12 @@ private enum OpenSearchExceptionHandle { org.opensearch.cluster.decommission.NodeDecommissionedException::new, 164, V_3_0_0 + ), + INDEX_CREATE_BLOCK_EXCEPTION( + org.opensearch.cluster.block.IndexCreateBlockException.class, + org.opensearch.cluster.block.IndexCreateBlockException::new, + 165, + UNKNOWN_VERSION_ADDED ); final Class exceptionClass; diff --git a/server/src/main/java/org/opensearch/action/admin/cluster/settings/SettingsUpdater.java b/server/src/main/java/org/opensearch/action/admin/cluster/settings/SettingsUpdater.java index 340d868c25853..d5d7c4a771d75 100644 --- a/server/src/main/java/org/opensearch/action/admin/cluster/settings/SettingsUpdater.java +++ b/server/src/main/java/org/opensearch/action/admin/cluster/settings/SettingsUpdater.java @@ -138,6 +138,13 @@ synchronized ClusterState updateSettings( } else { blocks.removeGlobalBlock(Metadata.CLUSTER_READ_ONLY_ALLOW_DELETE_BLOCK); } + boolean createIndexBlocked = Metadata.SETTING_CREATE_INDEX_BLOCK.get(metadata.persistentSettings()) + || Metadata.SETTING_CREATE_INDEX_BLOCK.get(metadata.transientSettings()); + if (createIndexBlocked) { + blocks.addGlobalBlock(Metadata.CLUSTER_CREATE_INDEX_BLOCK); + } else { + blocks.removeGlobalBlock(Metadata.CLUSTER_CREATE_INDEX_BLOCK); + } clusterState = builder(currentState).metadata(metadata).blocks(blocks).build(); } else { clusterState = currentState; diff --git a/server/src/main/java/org/opensearch/action/admin/indices/create/TransportCreateIndexAction.java b/server/src/main/java/org/opensearch/action/admin/indices/create/TransportCreateIndexAction.java index daba4d0f167f8..3465b59de6e2d 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/create/TransportCreateIndexAction.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/create/TransportCreateIndexAction.java @@ -91,7 +91,13 @@ protected CreateIndexResponse read(StreamInput in) throws IOException { @Override protected ClusterBlockException checkBlock(CreateIndexRequest request, ClusterState state) { - return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.index()); + ClusterBlockException clusterBlockException = state.blocks() + .indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.index()); + + if (clusterBlockException == null) { + return state.blocks().createIndexBlockedException(ClusterBlockLevel.CREATE_NEW_INDEX); + } + return clusterBlockException; } @Override diff --git a/server/src/main/java/org/opensearch/cluster/block/ClusterBlockLevel.java b/server/src/main/java/org/opensearch/cluster/block/ClusterBlockLevel.java index 06181f809be22..780bdd5db8821 100644 --- a/server/src/main/java/org/opensearch/cluster/block/ClusterBlockLevel.java +++ b/server/src/main/java/org/opensearch/cluster/block/ClusterBlockLevel.java @@ -43,7 +43,8 @@ public enum ClusterBlockLevel { READ, WRITE, METADATA_READ, - METADATA_WRITE; + METADATA_WRITE, + CREATE_NEW_INDEX; public static final EnumSet ALL = EnumSet.allOf(ClusterBlockLevel.class); public static final EnumSet READ_WRITE = EnumSet.of(READ, WRITE); diff --git a/server/src/main/java/org/opensearch/cluster/block/ClusterBlocks.java b/server/src/main/java/org/opensearch/cluster/block/ClusterBlocks.java index f5dc0eb8fdb5e..aab426cafcbf1 100644 --- a/server/src/main/java/org/opensearch/cluster/block/ClusterBlocks.java +++ b/server/src/main/java/org/opensearch/cluster/block/ClusterBlocks.java @@ -204,6 +204,13 @@ public ClusterBlockException globalBlockedException(ClusterBlockLevel level) { return new ClusterBlockException(global(level)); } + public IndexCreateBlockException createIndexBlockedException(ClusterBlockLevel level) { + if (global(level).isEmpty()) { + return null; + } + return new IndexCreateBlockException(global(level)); + } + public void indexBlockedRaiseException(ClusterBlockLevel level, String index) throws ClusterBlockException { ClusterBlockException blockException = indexBlockedException(level, index); if (blockException != null) { diff --git a/server/src/main/java/org/opensearch/cluster/block/IndexCreateBlockException.java b/server/src/main/java/org/opensearch/cluster/block/IndexCreateBlockException.java new file mode 100644 index 0000000000000..3ca0ef5ade225 --- /dev/null +++ b/server/src/main/java/org/opensearch/cluster/block/IndexCreateBlockException.java @@ -0,0 +1,30 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.block; + +import org.opensearch.common.io.stream.StreamInput; + +import java.io.IOException; +import java.util.Set; + +/** + * Internal exception on obtaining an index create block + * + * @opensearch.internal + */ +public class IndexCreateBlockException extends ClusterBlockException { + + public IndexCreateBlockException(Set globalLevelBlocks) { + super(globalLevelBlocks); + } + + public IndexCreateBlockException(StreamInput in) throws IOException { + super(in); + } +} diff --git a/server/src/main/java/org/opensearch/cluster/metadata/Metadata.java b/server/src/main/java/org/opensearch/cluster/metadata/Metadata.java index eb5e8bbc2d49b..2b786b21357d3 100644 --- a/server/src/main/java/org/opensearch/cluster/metadata/Metadata.java +++ b/server/src/main/java/org/opensearch/cluster/metadata/Metadata.java @@ -179,6 +179,16 @@ public interface Custom extends NamedDiffable, ToXContentFragment, Clust EnumSet.of(ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA_WRITE) ); + public static final ClusterBlock CLUSTER_CREATE_INDEX_BLOCK = new ClusterBlock( + 10, + "cluster create-index blocked (api)", + false, + false, + false, + RestStatus.FORBIDDEN, + EnumSet.of(ClusterBlockLevel.CREATE_NEW_INDEX) + ); + public static final Setting SETTING_READ_ONLY_ALLOW_DELETE_SETTING = Setting.boolSetting( "cluster.blocks.read_only_allow_delete", false, @@ -186,6 +196,13 @@ public interface Custom extends NamedDiffable, ToXContentFragment, Clust Property.NodeScope ); + public static final Setting SETTING_CREATE_INDEX_BLOCK = Setting.boolSetting( + "cluster.blocks.create_index", + false, + Property.Dynamic, + Property.NodeScope + ); + public static final ClusterBlock CLUSTER_READ_ONLY_ALLOW_DELETE_BLOCK = new ClusterBlock( 13, "cluster read-only / allow delete (api)", diff --git a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java index 1665614c18496..39b8ad5792045 100644 --- a/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/opensearch/common/settings/ClusterSettings.java @@ -251,6 +251,7 @@ public void apply(Settings value, Settings current, Settings previous) { MappingUpdatedAction.INDICES_MAX_IN_FLIGHT_UPDATES_SETTING, Metadata.SETTING_READ_ONLY_SETTING, Metadata.SETTING_READ_ONLY_ALLOW_DELETE_SETTING, + Metadata.SETTING_CREATE_INDEX_BLOCK, ShardLimitValidator.SETTING_CLUSTER_MAX_SHARDS_PER_NODE, RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING, RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_STATE_SYNC_SETTING, diff --git a/server/src/test/java/org/opensearch/cluster/ClusterStateTests.java b/server/src/test/java/org/opensearch/cluster/ClusterStateTests.java index f5461054ca70c..45950468965ab 100644 --- a/server/src/test/java/org/opensearch/cluster/ClusterStateTests.java +++ b/server/src/test/java/org/opensearch/cluster/ClusterStateTests.java @@ -161,7 +161,8 @@ public void testToXContent() throws IOException { + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " },\n" @@ -174,7 +175,8 @@ public void testToXContent() throws IOException { + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " }\n" @@ -367,7 +369,8 @@ public void testToXContent_FlatSettingTrue_ReduceMappingFalse() throws IOExcepti + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " },\n" @@ -380,7 +383,8 @@ public void testToXContent_FlatSettingTrue_ReduceMappingFalse() throws IOExcepti + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " }\n" @@ -566,7 +570,8 @@ public void testToXContent_FlatSettingFalse_ReduceMappingTrue() throws IOExcepti + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " },\n" @@ -579,7 +584,8 @@ public void testToXContent_FlatSettingFalse_ReduceMappingTrue() throws IOExcepti + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" - + " \"metadata_write\"\n" + + " \"metadata_write\",\n" + + " \"create_new_index\"\n" + " ]\n" + " }\n" + " }\n"