diff --git a/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java b/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java index c786f57029d..d3a656c59fc 100644 --- a/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java +++ b/core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java @@ -191,39 +191,29 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent> iterator = failedShards.entrySet().iterator(); iterator.hasNext(); ) { + Map.Entry entry = iterator.next(); + ShardId failedShardId = entry.getKey(); + ShardRouting failedShardRouting = entry.getValue(); + IndexRoutingTable indexRoutingTable = routingTable.index(failedShardId.getIndex()); + if (indexRoutingTable == null) { + iterator.remove(); + continue; } - } - - Set hasAllocations = new HashSet<>(); - for (ShardRouting routing : event.state().getRoutingNodes().node(event.state().nodes().localNodeId())) { - hasAllocations.add(routing.index()); - } - for (IndexService indexService : indicesService) { - Index index = indexService.index(); - if (hasAllocations.contains(index) == false) { - assert indexService.shardIds().isEmpty() : - "no locally assigned shards, but index wasn't emptied by applyDeletedShards." - + " index " + index + ", shards: " + indexService.shardIds(); - if (logger.isDebugEnabled()) { - logger.debug("{} cleaning index (no shards allocated)", index); - } - // clean the index - removeIndex(index, "removing index (no shards allocated)"); + IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(failedShardId.id()); + if (shardRoutingTable == null) { + iterator.remove(); + continue; + } + if (shardRoutingTable.assignedShards().stream().noneMatch(shr -> shr.isSameAllocation(failedShardRouting))) { + iterator.remove(); } } } @@ -305,23 +295,39 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent hasAllocations = new HashSet<>(); + for (ShardRouting routing : event.state().getRoutingNodes().node(event.state().nodes().localNodeId())) { + hasAllocations.add(routing.index()); + } + for (IndexService indexService : indicesService) { + Index index = indexService.index(); + if (hasAllocations.contains(index) == false) { + assert indexService.shardIds().isEmpty() : + "no locally assigned shards, but index wasn't emptied by applyDeletedShards." + + " index " + index + ", shards: " + indexService.shardIds(); if (logger.isDebugEnabled()) { - logger.debug("[{}] creating index", indexMetaData.getIndex()); - } - try { - indicesService.createIndex(nodeServicesProvider, indexMetaData, buildInIndexListener); - } catch (Throwable e) { - sendFailShard(shard, "failed to create index", e); + logger.debug("{} cleaning index (no shards allocated)", index); } + // clean the index + removeIndex(index, "removing index (no shards allocated)"); } } } @@ -349,6 +355,26 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent> iterator = failedShards.entrySet().iterator(); iterator.hasNext(); ) { - Map.Entry entry = iterator.next(); - ShardId failedShardId = entry.getKey(); - ShardRouting failedShardRouting = entry.getValue(); - IndexRoutingTable indexRoutingTable = routingTable.index(failedShardId.getIndex()); - if (indexRoutingTable == null) { - iterator.remove(); - continue; - } - IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(failedShardId.id()); - if (shardRoutingTable == null) { - iterator.remove(); - continue; - } - if (shardRoutingTable.assignedShards().stream().noneMatch(shr -> shr.isSameAllocation(failedShardRouting))) { - iterator.remove(); - } - } - } - private void applyInitializingShard(final ClusterState state, final IndexMetaData indexMetaData, IndexService indexService, final ShardRouting shardRouting) { final RoutingTable routingTable = state.routingTable(); final DiscoveryNodes nodes = state.getNodes();