Skip to content

Commit

Permalink
Adding support for index create block in OpenSearch
Browse files Browse the repository at this point in the history
Signed-off-by: Rishav Sagar <[email protected]>
  • Loading branch information
Rishav Sagar committed Sep 27, 2022
1 parent bb47419 commit 6dee99c
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
6 changes: 6 additions & 0 deletions server/src/main/java/org/opensearch/OpenSearchException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends OpenSearchException> exceptionClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public enum ClusterBlockLevel {
READ,
WRITE,
METADATA_READ,
METADATA_WRITE;
METADATA_WRITE,
CREATE_NEW_INDEX;

public static final EnumSet<ClusterBlockLevel> ALL = EnumSet.allOf(ClusterBlockLevel.class);
public static final EnumSet<ClusterBlockLevel> READ_WRITE = EnumSet.of(READ, WRITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClusterBlock> globalLevelBlocks) {
super(globalLevelBlocks);
}

public IndexCreateBlockException(StreamInput in) throws IOException {
super(in);
}
}
17 changes: 17 additions & 0 deletions server/src/main/java/org/opensearch/cluster/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,30 @@ public interface Custom extends NamedDiffable<Custom>, 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<Boolean> SETTING_READ_ONLY_ALLOW_DELETE_SETTING = Setting.boolSetting(
"cluster.blocks.read_only_allow_delete",
false,
Property.Dynamic,
Property.NodeScope
);

public static final Setting<Boolean> 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)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 12 additions & 6 deletions server/src/test/java/org/opensearch/cluster/ClusterStateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 6dee99c

Please sign in to comment.