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;
}
Settings persistentSettingsBuilt = persistentSettings.build();
Settings transientSettingsBuilt = transientSettings.build();
MetaData.Builder metaData = MetaData.builder().metaData(currentState.metaData())
.persistentSettings(persistentSettingsBuilt)
.transientSettings(transientSettingsBuilt);
.persistentSettings(persistentSettings.build())
.transientSettings(transientSettings.build());
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) {
blocks.addGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
}
else {
} else {
blocks.removeGlobalBlock(MetaData.CLUSTER_READ_ONLY_BLOCK);
}

View File

@ -56,7 +56,7 @@ public class IndexMetaData {
.add(IndexMetaData.SETTING_READ_ONLY)
.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() {
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_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.read_only";
public static final String SETTING_READ_ONLY = "index.blocks.read_only";
private final String index;
@ -198,14 +195,6 @@ public class IndexMetaData {
return totalNumberOfShards();
}
public boolean readOnly() {
return settings.getAsBoolean(SETTING_READ_ONLY, false);
}
public boolean getreadOnly() {
return readOnly();
}
public Settings settings() {
return settings;
}

View File

@ -49,9 +49,9 @@ import static org.elasticsearch.common.settings.ImmutableSettings.*;
*
*/
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()
.add("cluster.read_only")
@ -718,11 +718,19 @@ public class MetaData implements Iterable<IndexMetaData> {
return this;
}
public Settings transientSettings() {
return this.transientSettings;
}
public Builder transientSettings(Settings settings) {
this.transientSettings = settings;
return this;
}
public Settings persistentSettings() {
return this.persistentSettings;
}
public Builder persistentSettings(Settings settings) {
this.persistentSettings = settings;
return this;

View File

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

View File

@ -17,9 +17,8 @@
* 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.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse;
@ -31,97 +30,77 @@ import org.elasticsearch.client.action.admin.indices.settings.UpdateSettingsRequ
import org.elasticsearch.client.action.index.IndexRequestBuilder;
import org.elasticsearch.cluster.block.ClusterBlockException;
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.NodeBuilder;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import static org.elasticsearch.node.NodeBuilder.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.HashMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
@Test
public class ClusterBlockTests {
@Test public void testClusterReadOnly() throws Exception {
Node node = newNode();
try {
Client client = node.client();
try {
// cluster.read_only = null: write and metadata not blocked
canCreateIndex(client, "test1");
canIndexDocument(client, "test1");
setIndexReadOnly(client, "test1", "false");
canIndexExists(client, "test1");
public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests {
// cluster.read_only = true: block write and metadata
setClusterReadOnly(client, "true");
canNotCreateIndex(client, "test2");
// even if index has index.read_only = false
canNotIndexDocument(client, "test1");
canNotIndexExists(client, "test1");
// cluster.read_only = false: removes the block
setClusterReadOnly(client, "false");
canCreateIndex(client, "test2");
canIndexDocument(client, "test2");
canIndexDocument(client, "test1");
canIndexExists(client, "test1");
}
finally {
client.close();
}
}
finally {
node.close();
}
@AfterMethod
public void closeNodes() {
closeAllNodes();
}
@Test public void testIndexReadOnly() throws Exception {
Node node = newNode();
try {
Client client = node.client();
try {
// newly created an index has no blocks
canCreateIndex(client, "ro");
canIndexDocument(client, "ro");
canIndexExists(client, "ro");
@Test
public void verifyReadOnly() throws Exception {
Node node1 = startNode("node1");
Client client = node1.client();
// adds index write and metadata block
setIndexReadOnly(client, "ro", "true");
canNotIndexDocument(client, "ro");
canNotIndexExists(client, "ro");
// cluster.read_only = null: write and metadata not blocked
canCreateIndex(client, "test1");
canIndexDocument(client, "test1");
setIndexReadOnly(client, "test1", "false");
canIndexExists(client, "test1");
// other indices not blocked
canCreateIndex(client, "rw");
canIndexDocument(client, "rw");
canIndexExists(client, "rw");
// cluster.read_only = true: block write and metadata
setClusterReadOnly(client, "true");
canNotCreateIndex(client, "test2");
// even if index has index.read_only = false
canNotIndexDocument(client, "test1");
canNotIndexExists(client, "test1");
// blocks can be removed
setIndexReadOnly(client, "ro", "false");
canIndexDocument(client, "ro");
canIndexExists(client, "ro");
}
finally {
client.close();
}
}
finally {
node.close();
}
}
// cluster.read_only = false: removes the block
setClusterReadOnly(client, "false");
canCreateIndex(client, "test2");
canIndexDocument(client, "test2");
canIndexDocument(client, "test1");
canIndexExists(client, "test1");
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();
// newly created an index has no blocks
canCreateIndex(client, "ro");
canIndexDocument(client, "ro");
canIndexExists(client, "ro");
// adds index write and metadata block
setIndexReadOnly(client, "ro", "true");
canNotIndexDocument(client, "ro");
canNotIndexExists(client, "ro");
// other indices not blocked
canCreateIndex(client, "rw");
canIndexDocument(client, "rw");
canIndexExists(client, "rw");
// blocks can be removed
setIndexReadOnly(client, "ro", "false");
canIndexDocument(client, "ro");
canIndexExists(client, "ro");
}
private void canCreateIndex(Client client, String index) {
try {
CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet();
assertThat(r, notNullValue());
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
assert false;
}
}
@ -130,8 +109,7 @@ public class ClusterBlockTests {
try {
client.admin().indices().prepareCreate(index).execute().actionGet();
assert false;
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
// all is well
}
}
@ -142,8 +120,7 @@ public class ClusterBlockTests {
builder.setSource("foo", "bar");
IndexResponse r = builder.execute().actionGet();
assertThat(r, notNullValue());
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
assert false;
}
}
@ -154,8 +131,7 @@ public class ClusterBlockTests {
builder.setSource("foo", "bar");
builder.execute().actionGet();
assert false;
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
// all is well
}
}
@ -164,8 +140,7 @@ public class ClusterBlockTests {
try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet();
assertThat(r, notNullValue());
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
assert false;
}
}
@ -174,8 +149,7 @@ public class ClusterBlockTests {
try {
IndicesExistsResponse r = client.admin().indices().prepareExists(index).execute().actionGet();
assert false;
}
catch (ClusterBlockException e) {
} catch (ClusterBlockException e) {
// all is well
}
}