Skip to content

Commit

Permalink
Index Blocks: Add index.blocks.write, index.blocks.read, and index.bl…
Browse files Browse the repository at this point in the history
…ocks.metadata settings, closes elastic#1771.
  • Loading branch information
kimchy committed Mar 8, 2012
1 parent 42260c9 commit 5014d15
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> dynamicSettings() {
return dynamicSettings;
Expand Down Expand Up @@ -116,6 +122,9 @@ public static State fromString(String state) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,38 @@ public ClusterState execute(ClusterState currentState) {
}
}
}
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
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/elasticsearch/gateway/GatewayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ public ClusterState execute(ClusterState currentState) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,19 +38,20 @@

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() {
closeAllNodes();
}

@Test
public void verifyReadOnly() throws Exception {
public void verifyIndexAndClusterReadOnly() throws Exception {
Node node1 = startNode("node1");
Client client = node1.client();

Expand Down Expand Up @@ -96,6 +97,23 @@ public void verifyReadOnly() throws Exception {
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();
Expand Down

0 comments on commit 5014d15

Please sign in to comment.