Set an index / indices to read only, or make the cluster read only, closes #1573.

This commit is contained in:
Shay Banon 2011-12-27 20:35:07 +02:00
parent b41166a78a
commit 5049f60b6c
6 changed files with 77 additions and 110 deletions

View File

@ -112,18 +112,15 @@ public class TransportClusterUpdateSettingsAction extends TransportMasterNodeOpe
return currentState; return currentState;
} }
Settings persistentSettingsBuilt = persistentSettings.build();
Settings transientSettingsBuilt = transientSettings.build();
MetaData.Builder metaData = MetaData.builder().metaData(currentState.metaData()) MetaData.Builder metaData = MetaData.builder().metaData(currentState.metaData())
.persistentSettings(persistentSettingsBuilt) .persistentSettings(persistentSettings.build())
.transientSettings(transientSettingsBuilt); .transientSettings(transientSettings.build());
ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks()); ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
Boolean updatedReadOnly = persistentSettingsBuilt.getAsBoolean(MetaData.SETTING_READ_ONLY, false) || transientSettingsBuilt.getAsBoolean(MetaData.SETTING_READ_ONLY, false); boolean updatedReadOnly = metaData.persistentSettings().getAsBoolean(MetaData.SETTING_READ_ONLY, false) || metaData.transientSettings().getAsBoolean(MetaData.SETTING_READ_ONLY, false);
if (updatedReadOnly) { if (updatedReadOnly) {
blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK); blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
} } else {
else {
blocks.removeGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK); blocks.removeGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
} }

View File

@ -56,7 +56,7 @@ public class IndexMetaData {
.add(IndexMetaData.SETTING_READ_ONLY) .add(IndexMetaData.SETTING_READ_ONLY)
.build(); .build();
public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only", false, false, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA); public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only (api)", false, false, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA);
public static ImmutableSet<String> dynamicSettings() { public static ImmutableSet<String> dynamicSettings() {
return dynamicSettings; return dynamicSettings;
@ -111,12 +111,9 @@ public class IndexMetaData {
} }
public static final String SETTING_NUMBER_OF_SHARDS = "index.number_of_shards"; public static final String SETTING_NUMBER_OF_SHARDS = "index.number_of_shards";
public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas"; 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_AUTO_EXPAND_REPLICAS = "index.auto_expand_replicas";
public static final String SETTING_READ_ONLY = "index.blocks.read_only";
public static final String SETTING_READ_ONLY = "index.read_only";
private final String index; private final String index;
@ -198,14 +195,6 @@ public class IndexMetaData {
return totalNumberOfShards(); return totalNumberOfShards();
} }
public boolean readOnly() {
return settings.getAsBoolean(SETTING_READ_ONLY, false);
}
public boolean getreadOnly() {
return readOnly();
}
public Settings settings() { public Settings settings() {
return settings; return settings;
} }

View File

@ -49,9 +49,9 @@ import static org.elasticsearch.common.settings.ImmutableSettings.*;
* *
*/ */
public class MetaData implements Iterable<IndexMetaData> { public class MetaData implements Iterable<IndexMetaData> {
public static final String SETTING_READ_ONLY = "cluster.read_only"; public static final String SETTING_READ_ONLY = "cluster.blocks.read_only";
public static final ClusterBlock CLUSTER_READ_ONLY_BLOCK = new ClusterBlock(6, "cluster read-only", false, false, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA); public static final ClusterBlock CLUSTER_READ_ONLY_BLOCK = new ClusterBlock(6, "cluster read-only (api)", false, false, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA);
private static ImmutableSet<String> dynamicSettings = ImmutableSet.<String>builder() private static ImmutableSet<String> dynamicSettings = ImmutableSet.<String>builder()
.add("cluster.read_only") .add("cluster.read_only")
@ -718,11 +718,19 @@ public class MetaData implements Iterable<IndexMetaData> {
return this; return this;
} }
public Settings transientSettings() {
return this.transientSettings;
}
public Builder transientSettings(Settings settings) { public Builder transientSettings(Settings settings) {
this.transientSettings = settings; this.transientSettings = settings;
return this; return this;
} }
public Settings persistentSettings() {
return this.persistentSettings;
}
public Builder persistentSettings(Settings settings) { public Builder persistentSettings(Settings settings) {
this.persistentSettings = settings; this.persistentSettings = settings;
return this; return this;

View File

@ -170,8 +170,7 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements
for (String index : actualIndices) { for (String index : actualIndices) {
if (updatedReadOnly) { if (updatedReadOnly) {
blocks.addIndexBlock(index, IndexMetaData.INDEX_READ_ONLY_BLOCK); blocks.addIndexBlock(index, IndexMetaData.INDEX_READ_ONLY_BLOCK);
} } else {
else {
blocks.removeIndexBlock(index, IndexMetaData.INDEX_READ_ONLY_BLOCK); blocks.removeIndexBlock(index, IndexMetaData.INDEX_READ_ONLY_BLOCK);
} }
} }

View File

@ -268,7 +268,7 @@ public class GatewayService extends AbstractLifecycleComponent<GatewayService> i
if (indexMetaData.state() == IndexMetaData.State.CLOSE) { if (indexMetaData.state() == IndexMetaData.State.CLOSE) {
blocks.addIndexBlock(indexMetaData.index(), MetaDataStateIndexService.INDEX_CLOSED_BLOCK); blocks.addIndexBlock(indexMetaData.index(), MetaDataStateIndexService.INDEX_CLOSED_BLOCK);
} }
if (indexMetaData.readOnly()) { if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_READ_ONLY, false)) {
blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_ONLY_BLOCK); blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_ONLY_BLOCK);
} }
} }

View File

@ -17,9 +17,8 @@
* under the License. * under the License.
*/ */
package org.elasticsearch.cluster.metadata; package org.elasticsearch.test.integration.readonly;
import java.util.HashMap;
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse; import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse;
@ -31,22 +30,30 @@ import org.elasticsearch.client.action.admin.indices.settings.UpdateSettingsRequ
import org.elasticsearch.client.action.index.IndexRequestBuilder; import org.elasticsearch.client.action.index.IndexRequestBuilder;
import org.elasticsearch.cluster.block.ClusterBlockException; import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.node.Node; import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.elasticsearch.node.NodeBuilder.*; import java.util.HashMap;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
@Test @Test
public class ClusterBlockTests { public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests {
@Test public void testClusterReadOnly() throws Exception {
Node node = newNode(); @AfterMethod
try { public void closeNodes() {
Client client = node.client(); closeAllNodes();
try { }
@Test
public void verifyReadOnly() throws Exception {
Node node1 = startNode("node1");
Client client = node1.client();
// cluster.read_only = null: write and metadata not blocked // cluster.read_only = null: write and metadata not blocked
canCreateIndex(client, "test1"); canCreateIndex(client, "test1");
canIndexDocument(client, "test1"); canIndexDocument(client, "test1");
@ -66,21 +73,8 @@ public class ClusterBlockTests {
canIndexDocument(client, "test2"); canIndexDocument(client, "test2");
canIndexDocument(client, "test1"); canIndexDocument(client, "test1");
canIndexExists(client, "test1"); canIndexExists(client, "test1");
}
finally {
client.close();
}
}
finally {
node.close();
}
}
@Test public void testIndexReadOnly() throws Exception {
Node node = newNode();
try {
Client client = node.client();
try {
// newly created an index has no blocks // newly created an index has no blocks
canCreateIndex(client, "ro"); canCreateIndex(client, "ro");
canIndexDocument(client, "ro"); canIndexDocument(client, "ro");
@ -101,27 +95,12 @@ public class ClusterBlockTests {
canIndexDocument(client, "ro"); canIndexDocument(client, "ro");
canIndexExists(client, "ro"); canIndexExists(client, "ro");
} }
finally {
client.close();
}
}
finally {
node.close();
}
}
private Node newNode() {
ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder().put("gateway.type", "none");
NodeBuilder nodeBuilder = nodeBuilder().local(true).loadConfigSettings(false).clusterName("ClusterBlockTests").settings(settingsBuilder);
return nodeBuilder.node();
}
private void canCreateIndex(Client client, String index) { private void canCreateIndex(Client client, String index) {
try { try {
CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet(); CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
assert false; assert false;
} }
} }
@ -130,8 +109,7 @@ public class ClusterBlockTests {
try { try {
client.admin().indices().prepareCreate(index).execute().actionGet(); client.admin().indices().prepareCreate(index).execute().actionGet();
assert false; assert false;
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
// all is well // all is well
} }
} }
@ -142,8 +120,7 @@ public class ClusterBlockTests {
builder.setSource("foo", "bar"); builder.setSource("foo", "bar");
IndexResponse r = builder.execute().actionGet(); IndexResponse r = builder.execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
assert false; assert false;
} }
} }
@ -154,8 +131,7 @@ public class ClusterBlockTests {
builder.setSource("foo", "bar"); builder.setSource("foo", "bar");
builder.execute().actionGet(); builder.execute().actionGet();
assert false; assert false;
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
// all is well // all is well
} }
} }
@ -164,8 +140,7 @@ public class ClusterBlockTests {
try { try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet(); IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet();
assertThat(r, notNullValue()); assertThat(r, notNullValue());
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
assert false; assert false;
} }
} }
@ -174,8 +149,7 @@ public class ClusterBlockTests {
try { try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet(); IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet();
assert false; assert false;
} } catch (ClusterBlockException e) {
catch (ClusterBlockException e) {
// all is well // all is well
} }
} }