diff --git a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java index e4ab12b3941..18ec6e0e9a2 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java @@ -56,9 +56,15 @@ public class IndexMetaData { .add(IndexMetaData.SETTING_NUMBER_OF_REPLICAS) .add(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS) .add(IndexMetaData.SETTING_READ_ONLY) + .add(IndexMetaData.SETTING_BLOCKS_READ) + .add(IndexMetaData.SETTING_BLOCKS_WRITE) + .add(IndexMetaData.SETTING_BLOCKS_METADATA) .build(); public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA); + public static final ClusterBlock INDEX_READ_BLOCK = new ClusterBlock(7, "index read (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.READ); + public static final ClusterBlock INDEX_WRITE_BLOCK = new ClusterBlock(8, "index write (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE); + public static final ClusterBlock INDEX_METADATA_BLOCK = new ClusterBlock(9, "index metadata (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.METADATA); public static ImmutableSet dynamicSettings() { return dynamicSettings; @@ -116,6 +122,9 @@ public class IndexMetaData { public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas"; public static final String SETTING_AUTO_EXPAND_REPLICAS = "index.auto_expand_replicas"; public static final String SETTING_READ_ONLY = "index.blocks.read_only"; + public static final String SETTING_BLOCKS_READ = "index.blocks.read"; + public static final String SETTING_BLOCKS_WRITE = "index.blocks.write"; + public static final String SETTING_BLOCKS_METADATA = "index.blocks.metadata"; public static final String SETTING_VERSION_CREATED = "index.version.created"; private final String index; diff --git a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java index e486310354c..599ac0f7197 100644 --- a/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java +++ b/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java @@ -175,6 +175,38 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements } } } + Boolean updateMetaDataBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, null); + if (updateMetaDataBlock != null) { + for (String index : actualIndices) { + if (updateMetaDataBlock) { + blocks.addIndexBlock(index, IndexMetaData.INDEX_METADATA_BLOCK); + } else { + blocks.removeIndexBlock(index, IndexMetaData.INDEX_METADATA_BLOCK); + } + } + } + + Boolean updateWriteBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, null); + if (updateWriteBlock != null) { + for (String index : actualIndices) { + if (updateWriteBlock) { + blocks.addIndexBlock(index, IndexMetaData.INDEX_WRITE_BLOCK); + } else { + blocks.removeIndexBlock(index, IndexMetaData.INDEX_WRITE_BLOCK); + } + } + } + + Boolean updateReadBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, null); + if (updateReadBlock != null) { + for (String index : actualIndices) { + if (updateReadBlock) { + blocks.addIndexBlock(index, IndexMetaData.INDEX_READ_BLOCK); + } else { + blocks.removeIndexBlock(index, IndexMetaData.INDEX_READ_BLOCK); + } + } + } // allow to change any settings to a close index, and only allow dynamic settings to be changed // on an open index diff --git a/src/main/java/org/elasticsearch/gateway/GatewayService.java b/src/main/java/org/elasticsearch/gateway/GatewayService.java index 429b39296f4..a732225af97 100644 --- a/src/main/java/org/elasticsearch/gateway/GatewayService.java +++ b/src/main/java/org/elasticsearch/gateway/GatewayService.java @@ -262,6 +262,15 @@ public class GatewayService extends AbstractLifecycleComponent i if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_READ_ONLY, false)) { blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_ONLY_BLOCK); } + if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, false)) { + blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_BLOCK); + } + if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, false)) { + blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_WRITE_BLOCK); + } + if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, false)) { + blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_METADATA_BLOCK); + } } // update the state to reflect the new metadata and routing diff --git a/src/test/java/org/elasticsearch/test/integration/readonly/ClusterAndIndexReaderOnlyTests.java b/src/test/java/org/elasticsearch/test/integration/blocks/SimpleBlocksTests.java similarity index 87% rename from src/test/java/org/elasticsearch/test/integration/readonly/ClusterAndIndexReaderOnlyTests.java rename to src/test/java/org/elasticsearch/test/integration/blocks/SimpleBlocksTests.java index 2b0b1d3716f..b6c27871e79 100644 --- a/src/test/java/org/elasticsearch/test/integration/readonly/ClusterAndIndexReaderOnlyTests.java +++ b/src/test/java/org/elasticsearch/test/integration/blocks/SimpleBlocksTests.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.test.integration.readonly; +package org.elasticsearch.test.integration.blocks; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequestBuilder; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; @@ -38,11 +38,12 @@ import org.testng.annotations.Test; import java.util.HashMap; +import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; @Test -public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests { +public class SimpleBlocksTests extends AbstractNodesTests { @AfterMethod public void closeNodes() { @@ -50,7 +51,7 @@ public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests { } @Test - public void verifyReadOnly() throws Exception { + public void verifyIndexAndClusterReadOnly() throws Exception { Node node1 = startNode("node1"); Client client = node1.client(); @@ -96,6 +97,23 @@ public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests { canIndexExists(client, "ro"); } + @Test + public void testIndexReadWriteMetaDataBlocks() { + Node node1 = startNode("node1"); + Client client = node1.client(); + + canCreateIndex(client, "test1"); + canIndexDocument(client, "test1"); + client.admin().indices().prepareUpdateSettings("test1") + .setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true)) + .execute().actionGet(); + canNotIndexDocument(client, "test1"); + client.admin().indices().prepareUpdateSettings("test1") + .setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, false)) + .execute().actionGet(); + canIndexDocument(client, "test1"); + } + private void canCreateIndex(Client client, String index) { try { CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet();