Skip to content

Commit

Permalink
remove unused cluster blocks in create index
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzaharovits committed Aug 29, 2024
1 parent 5ac4d8c commit bb9e1a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.cluster.ack.ClusterStateUpdateRequest;
import org.elasticsearch.cluster.block.ClusterBlock;
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -43,8 +42,6 @@ public class CreateIndexClusterStateUpdateRequest extends ClusterStateUpdateRequ

private final Set<Alias> aliases = new HashSet<>();

private final Set<ClusterBlock> blocks = new HashSet<>();

private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;

private boolean performReroute = true;
Expand Down Expand Up @@ -125,10 +122,6 @@ public Set<Alias> aliases() {
return aliases;
}

public Set<ClusterBlock> blocks() {
return blocks;
}

public Index recoverFrom() {
return recoverFrom;
}
Expand Down Expand Up @@ -229,8 +222,6 @@ public String toString() {
+ settings
+ ", aliases="
+ aliases
+ ", blocks="
+ blocks
+ ", waitForActiveShards="
+ waitForActiveShards
+ ", systemDataStreamDescriptor="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.elasticsearch.cluster.AckedClusterStateUpdateTask;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateUpdateTask;
import org.elasticsearch.cluster.block.ClusterBlock;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.block.ClusterBlocks;
import org.elasticsearch.cluster.node.DiscoveryNodes;
Expand Down Expand Up @@ -514,7 +513,6 @@ private ClusterState applyCreateIndexWithTemporaryService(

ClusterState updated = clusterStateCreateIndex(
currentState,
request.blocks(),
indexMetadata,
metadataTransformer,
allocationService.getShardRoutingRoleStrategy()
Expand Down Expand Up @@ -1231,7 +1229,6 @@ public static List<AliasMetadata> resolveAndValidateAliases(
*/
static ClusterState clusterStateCreateIndex(
ClusterState currentState,
Set<ClusterBlock> clusterBlocks,
IndexMetadata indexMetadata,
BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer,
ShardRoutingRoleStrategy shardRoutingRoleStrategy
Expand All @@ -1245,14 +1242,13 @@ static ClusterState clusterStateCreateIndex(
newMetadata = currentState.metadata().withAddedIndex(indexMetadata);
}

String indexName = indexMetadata.getIndex().getName();
ClusterBlocks.Builder blocks = createClusterBlocksBuilder(currentState, indexName, clusterBlocks);
blocks.updateBlocks(indexMetadata);
var blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
blocksBuilder.updateBlocks(indexMetadata);

RoutingTable.Builder routingTableBuilder = RoutingTable.builder(shardRoutingRoleStrategy, currentState.routingTable())
.addAsNew(newMetadata.index(indexName));
var routingTableBuilder = RoutingTable.builder(shardRoutingRoleStrategy, currentState.routingTable())
.addAsNew(newMetadata.index(indexMetadata.getIndex().getName()));

return ClusterState.builder(currentState).blocks(blocks).metadata(newMetadata).routingTable(routingTableBuilder).build();
return ClusterState.builder(currentState).blocks(blocksBuilder).metadata(newMetadata).routingTable(routingTableBuilder).build();
}

static IndexMetadata buildIndexMetadata(
Expand Down Expand Up @@ -1325,16 +1321,6 @@ private static IndexMetadata.Builder createIndexMetadataBuilder(
return builder;
}

private static ClusterBlocks.Builder createClusterBlocksBuilder(ClusterState currentState, String index, Set<ClusterBlock> blocks) {
ClusterBlocks.Builder blocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
if (blocks.isEmpty() == false) {
for (ClusterBlock block : blocks) {
blocksBuilder.addIndexBlock(index, block);
}
}
return blocksBuilder;
}

private static void updateIndexMappingsAndBuildSortOrder(
IndexService indexService,
CreateIndexClusterStateUpdateRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,13 +967,7 @@ public void testClusterStateCreateIndexThrowsWriteIndexValidationException() thr
assertThat(
expectThrows(
IllegalStateException.class,
() -> clusterStateCreateIndex(
currentClusterState,
Set.of(),
newIndex,
null,
TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY
)
() -> clusterStateCreateIndex(currentClusterState, newIndex, null, TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY)
).getMessage(),
startsWith("alias [alias1] has more than one write index [")
);
Expand All @@ -991,7 +985,6 @@ public void testClusterStateCreateIndex() {

ClusterState updatedClusterState = clusterStateCreateIndex(
currentClusterState,
Set.of(INDEX_READ_ONLY_BLOCK),
newIndexMetadata,
null,
TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY
Expand Down Expand Up @@ -1037,7 +1030,6 @@ public void testClusterStateCreateIndexWithMetadataTransaction() {

ClusterState updatedClusterState = clusterStateCreateIndex(
currentClusterState,
Set.of(INDEX_READ_ONLY_BLOCK),
newIndexMetadata,
metadataTransformer,
TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY
Expand Down

0 comments on commit bb9e1a3

Please sign in to comment.