Skip to content

Commit 8413466

Browse files
committed
Add global and index level blocks to IndexSettings
1 parent 6d4a3f8 commit 8413466

15 files changed

+249
-27
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,13 @@ public ClusterState execute(ClusterState currentState) throws Exception {
447447
"]: cannot be greater than number of shard copies [" +
448448
(tmpImd.getNumberOfReplicas() + 1) + "]");
449449
}
450+
451+
final ClusterBlocks clusterBlocks = currentState.blocks();
452+
final Set<ClusterBlock> globalBlocks = clusterBlocks.global();
453+
final Set<ClusterBlock> indexBlocks = clusterBlocks.indices().get(tmpImd.getIndex().getName());
454+
450455
// create the index here (on the master) to validate it can be created, as well as adding the mapping
451-
final IndexService indexService = indicesService.createIndex(tmpImd, Collections.emptyList());
456+
final IndexService indexService = indicesService.createIndex(tmpImd, globalBlocks, indexBlocks, Collections.emptyList());
452457
createdIndex = indexService.index();
453458
// now add the mappings
454459
MapperService mapperService = indexService.mapperService();

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexAliasesService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
2626
import org.elasticsearch.cluster.ClusterState;
2727
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
28+
import org.elasticsearch.cluster.block.ClusterBlock;
29+
import org.elasticsearch.cluster.block.ClusterBlocks;
2830
import org.elasticsearch.cluster.metadata.AliasAction.NewAliasValidator;
2931
import org.elasticsearch.cluster.service.ClusterService;
3032
import org.elasticsearch.common.Priority;
@@ -124,6 +126,11 @@ ClusterState innerExecute(ClusterState currentState, Iterable<AliasAction> actio
124126
if (index == null) {
125127
throw new IndexNotFoundException(action.getIndex());
126128
}
129+
130+
final ClusterBlocks clusterBlocks = currentState.blocks();
131+
final Set<ClusterBlock> globalBlocks = clusterBlocks.global();
132+
final Set<ClusterBlock> indexBlocks = clusterBlocks.indices().get(index.getIndex().getName());
133+
127134
NewAliasValidator newAliasValidator = (alias, indexRouting, filter, writeIndex) -> {
128135
/* It is important that we look up the index using the metadata builder we are modifying so we can remove an
129136
* index and replace it with an alias. */
@@ -136,7 +143,7 @@ ClusterState innerExecute(ClusterState currentState, Iterable<AliasAction> actio
136143
if (indexService == null) {
137144
// temporarily create the index and add mappings so we can parse the filter
138145
try {
139-
indexService = indicesService.createIndex(index, emptyList());
146+
indexService = indicesService.createIndex(index, globalBlocks, indexBlocks, emptyList());
140147
indicesToClose.add(index.getIndex());
141148
} catch (IOException e) {
142149
throw new ElasticsearchException("Failed to create temporary index for parsing the alias", e);

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private static void validateAndAddTemplate(final PutRequest request, IndexTempla
232232
.build();
233233

234234
final IndexMetaData tmpIndexMetadata = IndexMetaData.builder(temporaryIndexName).settings(dummySettings).build();
235-
IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, Collections.emptyList());
235+
IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, null, null, Collections.emptyList());
236236
createdIndex = dummyIndexService.index();
237237

238238
templateBuilder.order(request.order);

server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.logging.log4j.LogManager;
2323
import org.apache.logging.log4j.Logger;
2424
import org.apache.logging.log4j.message.ParameterizedMessage;
25+
import org.elasticsearch.cluster.block.ClusterBlock;
26+
import org.elasticsearch.cluster.block.ClusterBlocks;
2527
import org.elasticsearch.core.internal.io.IOUtils;
2628
import org.elasticsearch.action.ActionListener;
2729
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest;
@@ -52,6 +54,7 @@
5254
import java.util.HashMap;
5355
import java.util.List;
5456
import java.util.Map;
57+
import java.util.Set;
5558

5659
import static org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED;
5760

@@ -144,8 +147,12 @@ ClusterState executeRefresh(final ClusterState currentState, final List<RefreshT
144147
boolean removeIndex = false;
145148
IndexService indexService = indicesService.indexService(indexMetaData.getIndex());
146149
if (indexService == null) {
150+
final ClusterBlocks clusterBlocks = currentState.blocks();
151+
final Set<ClusterBlock> globalBlocks = clusterBlocks.global();
152+
final Set<ClusterBlock> indexBlocks = clusterBlocks.indices().get(indexMetaData.getIndex().getName());
153+
147154
// we need to create the index here, and add the current mapping to it, so we can merge
148-
indexService = indicesService.createIndex(indexMetaData, Collections.emptyList());
155+
indexService = indicesService.createIndex(indexMetaData, globalBlocks, indexBlocks, Collections.emptyList());
149156
removeIndex = true;
150157
indexService.mapperService().merge(indexMetaData, MergeReason.MAPPING_RECOVERY);
151158
}

server/src/main/java/org/elasticsearch/index/IndexService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.Version;
2929
import org.elasticsearch.action.ActionListener;
3030
import org.elasticsearch.client.Client;
31+
import org.elasticsearch.cluster.block.ClusterBlock;
3132
import org.elasticsearch.cluster.metadata.IndexMetaData;
3233
import org.elasticsearch.cluster.routing.ShardRouting;
3334
import org.elasticsearch.common.Nullable;
@@ -679,6 +680,11 @@ public boolean isForceExecution() {
679680
}
680681
}
681682

683+
@Override
684+
public void updateBlocks(@Nullable final Set<ClusterBlock> globalBlocks, @Nullable final Set<ClusterBlock> indexBlocks) {
685+
indexSettings.updateIndexBlocks(globalBlocks, indexBlocks);
686+
}
687+
682688
private void rescheduleFsyncTask(Translog.Durability durability) {
683689
try {
684690
if (fsyncTask != null) {

server/src/main/java/org/elasticsearch/index/IndexSettings.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import org.apache.logging.log4j.Logger;
2222
import org.apache.lucene.index.MergePolicy;
2323
import org.elasticsearch.Version;
24+
import org.elasticsearch.cluster.block.ClusterBlock;
25+
import org.elasticsearch.cluster.block.ClusterBlocks;
2426
import org.elasticsearch.cluster.metadata.IndexMetaData;
27+
import org.elasticsearch.common.Nullable;
2528
import org.elasticsearch.common.logging.Loggers;
2629
import org.elasticsearch.common.settings.IndexScopedSettings;
2730
import org.elasticsearch.common.settings.Setting;
@@ -37,6 +40,7 @@
3740
import java.util.Collections;
3841
import java.util.List;
3942
import java.util.Locale;
43+
import java.util.Set;
4044
import java.util.concurrent.TimeUnit;
4145
import java.util.function.Consumer;
4246
import java.util.function.Function;
@@ -327,6 +331,11 @@ public final class IndexSettings {
327331
private volatile String defaultPipeline;
328332
private volatile boolean searchThrottled;
329333

334+
/**
335+
* A {@link ClusterBlocks} containing global level and index level blocks
336+
*/
337+
private volatile ClusterBlocks indexBlocks;
338+
330339
/**
331340
* The maximum number of refresh listeners allows on this shard.
332341
*/
@@ -397,8 +406,27 @@ public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSetti
397406
*
398407
* @param indexMetaData the index metadata this settings object is associated with
399408
* @param nodeSettings the nodes settings this index is allocated on.
409+
* @param indexScopedSettings the index level settings
400410
*/
401411
public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSettings, IndexScopedSettings indexScopedSettings) {
412+
this(indexMetaData, nodeSettings, indexScopedSettings, null, null);
413+
}
414+
415+
/**
416+
* Creates a new {@link IndexSettings} instance. The given node settings will be merged with the settings in the metadata
417+
* while index level settings will overwrite node settings.
418+
*
419+
* @param indexMetaData the index metadata this settings object is associated with
420+
* @param nodeSettings the nodes settings this index is allocated on.
421+
* @param indexScopedSettings the index level settings
422+
* @param globalBlocks the global level blocks
423+
* @param indexBlocks the index level blocks
424+
*/
425+
public IndexSettings(final IndexMetaData indexMetaData,
426+
final Settings nodeSettings,
427+
final IndexScopedSettings indexScopedSettings,
428+
@Nullable final Set<ClusterBlock> globalBlocks,
429+
@Nullable final Set<ClusterBlock> indexBlocks) {
402430
scopedSettings = indexScopedSettings.copy(nodeSettings, indexMetaData);
403431
this.nodeSettings = nodeSettings;
404432
this.settings = Settings.builder().put(nodeSettings).put(indexMetaData.getSettings()).build();
@@ -408,7 +436,7 @@ public IndexSettings(final IndexMetaData indexMetaData, final Settings nodeSetti
408436
nodeName = Node.NODE_NAME_SETTING.get(settings);
409437
this.indexMetaData = indexMetaData;
410438
numberOfShards = settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, null);
411-
439+
this.indexBlocks = buildBlocks(globalBlocks, indexBlocks);
412440
this.searchThrottled = INDEX_SEARCH_THROTTLED.get(settings);
413441
this.queryStringLenient = QUERY_STRING_LENIENT_SETTING.get(settings);
414442
this.queryStringAnalyzeWildcard = QUERY_STRING_ANALYZE_WILDCARD.get(nodeSettings);
@@ -626,6 +654,36 @@ public static boolean same(final Settings left, final Settings right) {
626654
.equals(right.filter(IndexScopedSettings.INDEX_SETTINGS_KEY_PREDICATE));
627655
}
628656

657+
/**
658+
* Updates the global level and index level blocks.
659+
*
660+
* @param globalBlocks the global level blocks
661+
* @param indexBlocks the index level blocks
662+
*/
663+
public synchronized void updateIndexBlocks(@Nullable final Set<ClusterBlock> globalBlocks,
664+
@Nullable final Set<ClusterBlock> indexBlocks) {
665+
this.indexBlocks = buildBlocks(globalBlocks, indexBlocks);
666+
}
667+
668+
private ClusterBlocks buildBlocks(@Nullable final Set<ClusterBlock> globalBlocks,
669+
@Nullable final Set<ClusterBlock> indexBlocks) {
670+
final ClusterBlocks.Builder builder = ClusterBlocks.builder();
671+
if (globalBlocks != null) {
672+
globalBlocks.forEach(builder::addGlobalBlock);
673+
}
674+
if (indexBlocks != null) {
675+
indexBlocks.forEach(block -> builder.addIndexBlock(index.getName(), block));
676+
}
677+
return builder.build();
678+
}
679+
680+
/**
681+
* @return the current global level and index level blocks
682+
*/
683+
public ClusterBlocks getIndexBlocks() {
684+
return indexBlocks;
685+
}
686+
629687
/**
630688
* Returns the translog durability for this index.
631689
*/

server/src/main/java/org/elasticsearch/indices/IndicesService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.elasticsearch.action.search.SearchType;
3737
import org.elasticsearch.client.Client;
3838
import org.elasticsearch.cluster.ClusterState;
39+
import org.elasticsearch.cluster.block.ClusterBlock;
3940
import org.elasticsearch.cluster.metadata.IndexMetaData;
4041
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
4142
import org.elasticsearch.cluster.metadata.MetaData;
@@ -448,8 +449,10 @@ public IndexService indexServiceSafe(Index index) {
448449
* @throws ResourceAlreadyExistsException if the index already exists.
449450
*/
450451
@Override
451-
public synchronized IndexService createIndex(
452-
final IndexMetaData indexMetaData, final List<IndexEventListener> builtInListeners) throws IOException {
452+
public synchronized IndexService createIndex(final IndexMetaData indexMetaData,
453+
@Nullable final Set<ClusterBlock> globalBlocks,
454+
@Nullable final Set<ClusterBlock> indexBlocks,
455+
final List<IndexEventListener> builtInListeners) throws IOException {
453456
ensureChangesAllowed();
454457
if (indexMetaData.getIndexUUID().equals(IndexMetaData.INDEX_UUID_NA_VALUE)) {
455458
throw new IllegalArgumentException("index must have a real UUID found value: [" + indexMetaData.getIndexUUID() + "]");
@@ -471,6 +474,8 @@ public void onStoreClosed(ShardId shardId) {
471474
createIndexService(
472475
"create index",
473476
indexMetaData,
477+
globalBlocks,
478+
indexBlocks,
474479
indicesQueryCache,
475480
indicesFieldDataCache,
476481
finalListeners,
@@ -493,11 +498,13 @@ public void onStoreClosed(ShardId shardId) {
493498
*/
494499
private synchronized IndexService createIndexService(final String reason,
495500
IndexMetaData indexMetaData,
501+
@Nullable final Set<ClusterBlock> globalBlocks,
502+
@Nullable final Set<ClusterBlock> indexBlocks,
496503
IndicesQueryCache indicesQueryCache,
497504
IndicesFieldDataCache indicesFieldDataCache,
498505
List<IndexEventListener> builtInListeners,
499506
IndexingOperationListener... indexingOperationListeners) throws IOException {
500-
final IndexSettings idxSettings = new IndexSettings(indexMetaData, settings, indexScopedSettings);
507+
final IndexSettings idxSettings = new IndexSettings(indexMetaData, settings, indexScopedSettings, globalBlocks, indexBlocks);
501508
// we ignore private settings since they are not registered settings
502509
indexScopedSettings.validate(indexMetaData.getSettings(), true, true, true);
503510
logger.debug("creating Index [{}], shards [{}]/[{}] - reason [{}]",
@@ -587,7 +594,7 @@ public synchronized void verifyIndexMetadata(IndexMetaData metaData, IndexMetaDa
587594
closeables.add(indicesQueryCache);
588595
// this will also fail if some plugin fails etc. which is nice since we can verify that early
589596
final IndexService service =
590-
createIndexService("metadata verification", metaData, indicesQueryCache, indicesFieldDataCache, emptyList());
597+
createIndexService("metadata verification", metaData, null, null, indicesQueryCache, indicesFieldDataCache, emptyList());
591598
closeables.add(() -> service.close("metadata verification", false));
592599
service.mapperService().merge(metaData, MapperService.MergeReason.MAPPING_RECOVERY);
593600
if (metaData.equals(metaDataUpdate) == false) {

server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.elasticsearch.cluster.ClusterStateApplier;
3131
import org.elasticsearch.cluster.action.index.NodeMappingRefreshAction;
3232
import org.elasticsearch.cluster.action.shard.ShardStateAction;
33+
import org.elasticsearch.cluster.block.ClusterBlock;
34+
import org.elasticsearch.cluster.block.ClusterBlocks;
3335
import org.elasticsearch.cluster.metadata.IndexMetaData;
3436
import org.elasticsearch.cluster.node.DiscoveryNode;
3537
import org.elasticsearch.cluster.node.DiscoveryNodes;
@@ -457,7 +459,11 @@ private void createIndices(final ClusterState state) {
457459

458460
AllocatedIndex<? extends Shard> indexService = null;
459461
try {
460-
indexService = indicesService.createIndex(indexMetaData, buildInIndexListener);
462+
final ClusterBlocks clusterBlocks = state.blocks();
463+
final Set<ClusterBlock> globalBlocks = clusterBlocks.global();
464+
final Set<ClusterBlock> indexBlocks = clusterBlocks.indices().get(index.getName());
465+
466+
indexService = indicesService.createIndex(indexMetaData, globalBlocks, indexBlocks, buildInIndexListener);
461467
if (indexService.updateMapping(null, indexMetaData) && sendRefreshMapping) {
462468
nodeMappingRefreshAction.nodeMappingRefresh(state.nodes().getMasterNode(),
463469
new NodeMappingRefreshAction.NodeMappingRefreshRequest(indexMetaData.getIndex().getName(),
@@ -479,8 +485,8 @@ private void createIndices(final ClusterState state) {
479485
}
480486
}
481487

482-
private void updateIndices(ClusterChangedEvent event) {
483-
if (!event.metaDataChanged()) {
488+
private void updateIndices(final ClusterChangedEvent event) {
489+
if (event.metaDataChanged() == false && event.blocksChanged() == false) {
484490
return;
485491
}
486492
final ClusterState state = event.state();
@@ -512,6 +518,11 @@ private void updateIndices(ClusterChangedEvent event) {
512518
}
513519
}
514520
}
521+
522+
if (event.blocksChanged()) {
523+
final ClusterBlocks clusterBlocks = state.blocks();
524+
indexService.updateBlocks(clusterBlocks.global(), clusterBlocks.indices().get(index.getName()));
525+
}
515526
}
516527
}
517528

@@ -780,6 +791,14 @@ public interface AllocatedIndex<T extends Shard> extends Iterable<T>, IndexCompo
780791
*/
781792
void updateMetaData(IndexMetaData currentIndexMetaData, IndexMetaData newIndexMetaData);
782793

794+
/**
795+
* Updates the global level and index level blocks of this index. Changes become visible through {@link #getIndexSettings()}.
796+
*
797+
* @param globalBlocks the global level blocks
798+
* @param indexBlocks the index level blocks
799+
*/
800+
void updateBlocks(@Nullable Set<ClusterBlock> globalBlocks, @Nullable Set<ClusterBlock> indexBlocks);
801+
783802
/**
784803
* Checks if index requires refresh from master.
785804
*/
@@ -801,12 +820,16 @@ public interface AllocatedIndices<T extends Shard, U extends AllocatedIndex<T>>
801820
/**
802821
* Creates a new {@link IndexService} for the given metadata.
803822
*
804-
* @param indexMetaData the index metadata to create the index for
805-
* @param builtInIndexListener a list of built-in lifecycle {@link IndexEventListener} that should should be used along side with
806-
* the per-index listeners
823+
* @param indexMetaData the index metadata to create the index for
824+
* @param globalBlocks the global level blocks
825+
* @param indexBlocks the index level blocks
826+
* @param builtInIndexListener a list of built-in lifecycle {@link IndexEventListener} that should should be used along side with
827+
* the per-index listeners
807828
* @throws ResourceAlreadyExistsException if the index already exists.
808829
*/
809830
U createIndex(IndexMetaData indexMetaData,
831+
@Nullable Set<ClusterBlock> globalBlocks,
832+
@Nullable Set<ClusterBlock> indexBlocks,
810833
List<IndexEventListener> builtInIndexListener) throws IOException;
811834

812835
/**

server/src/test/java/org/elasticsearch/cluster/metadata/IndexCreationTaskTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,6 @@ private void setupIndicesService() throws Exception {
457457
when(service.getIndexSortSupplier()).thenReturn(supplier);
458458
when(service.getIndexEventListener()).thenReturn(mock(IndexEventListener.class));
459459

460-
when(indicesService.createIndex(anyObject(), anyObject())).thenReturn(service);
460+
when(indicesService.createIndex(anyObject(), anyObject(), anyObject(), anyObject())).thenReturn(service);
461461
}
462462
}

0 commit comments

Comments
 (0)