Skip to content

Commit e707e93

Browse files
committed
Index Blocks: Add index.blocks.write, index.blocks.read, and index.blocks.metadata settings, closes #1771.
1 parent 3789983 commit e707e93

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

src/main/java/org/elasticsearch/cluster/metadata/IndexMetaData.java

+9
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ public class IndexMetaData {
5656
.add(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)
5757
.add(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS)
5858
.add(IndexMetaData.SETTING_READ_ONLY)
59+
.add(IndexMetaData.SETTING_BLOCKS_READ)
60+
.add(IndexMetaData.SETTING_BLOCKS_WRITE)
61+
.add(IndexMetaData.SETTING_BLOCKS_METADATA)
5962
.build();
6063

6164
public static final ClusterBlock INDEX_READ_ONLY_BLOCK = new ClusterBlock(5, "index read-only (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE, ClusterBlockLevel.METADATA);
65+
public static final ClusterBlock INDEX_READ_BLOCK = new ClusterBlock(7, "index read (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.READ);
66+
public static final ClusterBlock INDEX_WRITE_BLOCK = new ClusterBlock(8, "index write (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.WRITE);
67+
public static final ClusterBlock INDEX_METADATA_BLOCK = new ClusterBlock(9, "index metadata (api)", false, false, RestStatus.FORBIDDEN, ClusterBlockLevel.METADATA);
6268

6369
public static ImmutableSet<String> dynamicSettings() {
6470
return dynamicSettings;
@@ -116,6 +122,9 @@ public static State fromString(String state) {
116122
public static final String SETTING_NUMBER_OF_REPLICAS = "index.number_of_replicas";
117123
public static final String SETTING_AUTO_EXPAND_REPLICAS = "index.auto_expand_replicas";
118124
public static final String SETTING_READ_ONLY = "index.blocks.read_only";
125+
public static final String SETTING_BLOCKS_READ = "index.blocks.read";
126+
public static final String SETTING_BLOCKS_WRITE = "index.blocks.write";
127+
public static final String SETTING_BLOCKS_METADATA = "index.blocks.metadata";
119128
public static final String SETTING_VERSION_CREATED = "index.version.created";
120129

121130
private final String index;

src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java

+32
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,38 @@ public ClusterState execute(ClusterState currentState) {
175175
}
176176
}
177177
}
178+
Boolean updateMetaDataBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, null);
179+
if (updateMetaDataBlock != null) {
180+
for (String index : actualIndices) {
181+
if (updateMetaDataBlock) {
182+
blocks.addIndexBlock(index, IndexMetaData.INDEX_METADATA_BLOCK);
183+
} else {
184+
blocks.removeIndexBlock(index, IndexMetaData.INDEX_METADATA_BLOCK);
185+
}
186+
}
187+
}
188+
189+
Boolean updateWriteBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, null);
190+
if (updateWriteBlock != null) {
191+
for (String index : actualIndices) {
192+
if (updateWriteBlock) {
193+
blocks.addIndexBlock(index, IndexMetaData.INDEX_WRITE_BLOCK);
194+
} else {
195+
blocks.removeIndexBlock(index, IndexMetaData.INDEX_WRITE_BLOCK);
196+
}
197+
}
198+
}
199+
200+
Boolean updateReadBlock = openSettings.getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, null);
201+
if (updateReadBlock != null) {
202+
for (String index : actualIndices) {
203+
if (updateReadBlock) {
204+
blocks.addIndexBlock(index, IndexMetaData.INDEX_READ_BLOCK);
205+
} else {
206+
blocks.removeIndexBlock(index, IndexMetaData.INDEX_READ_BLOCK);
207+
}
208+
}
209+
}
178210

179211
// allow to change any settings to a close index, and only allow dynamic settings to be changed
180212
// on an open index

src/main/java/org/elasticsearch/gateway/GatewayService.java

+9
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,15 @@ public ClusterState execute(ClusterState currentState) {
262262
if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_READ_ONLY, false)) {
263263
blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_ONLY_BLOCK);
264264
}
265+
if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_READ, false)) {
266+
blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_READ_BLOCK);
267+
}
268+
if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_WRITE, false)) {
269+
blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_WRITE_BLOCK);
270+
}
271+
if (indexMetaData.settings().getAsBoolean(IndexMetaData.SETTING_BLOCKS_METADATA, false)) {
272+
blocks.addIndexBlock(indexMetaData.index(), IndexMetaData.INDEX_METADATA_BLOCK);
273+
}
265274
}
266275

267276
// update the state to reflect the new metadata and routing
+21-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.test.integration.readonly;
20+
package org.elasticsearch.test.integration.blocks;
2121

2222
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequestBuilder;
2323
import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
@@ -38,19 +38,20 @@
3838

3939
import java.util.HashMap;
4040

41+
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
4142
import static org.hamcrest.MatcherAssert.assertThat;
4243
import static org.hamcrest.Matchers.notNullValue;
4344

4445
@Test
45-
public class ClusterAndIndexReaderOnlyTests extends AbstractNodesTests {
46+
public class SimpleBlocksTests extends AbstractNodesTests {
4647

4748
@AfterMethod
4849
public void closeNodes() {
4950
closeAllNodes();
5051
}
5152

5253
@Test
53-
public void verifyReadOnly() throws Exception {
54+
public void verifyIndexAndClusterReadOnly() throws Exception {
5455
Node node1 = startNode("node1");
5556
Client client = node1.client();
5657

@@ -96,6 +97,23 @@ public void verifyReadOnly() throws Exception {
9697
canIndexExists(client, "ro");
9798
}
9899

100+
@Test
101+
public void testIndexReadWriteMetaDataBlocks() {
102+
Node node1 = startNode("node1");
103+
Client client = node1.client();
104+
105+
canCreateIndex(client, "test1");
106+
canIndexDocument(client, "test1");
107+
client.admin().indices().prepareUpdateSettings("test1")
108+
.setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, true))
109+
.execute().actionGet();
110+
canNotIndexDocument(client, "test1");
111+
client.admin().indices().prepareUpdateSettings("test1")
112+
.setSettings(settingsBuilder().put(IndexMetaData.SETTING_BLOCKS_WRITE, false))
113+
.execute().actionGet();
114+
canIndexDocument(client, "test1");
115+
}
116+
99117
private void canCreateIndex(Client client, String index) {
100118
try {
101119
CreateIndexResponse r = client.admin().indices().prepareCreate(index).execute().actionGet();

0 commit comments

Comments
 (0)