clusterBlocks = currentState.blocks().indices().get(index.getName());
+ if (clusterBlocks != null) {
+ for (ClusterBlock clusterBlock : clusterBlocks) {
+ if (clusterBlock.id() == INDEX_CLOSED_BLOCK_ID) {
+ // Reuse the existing index closed block
+ indexBlock = clusterBlock;
+ break;
+ }
+ }
+ }
+ if (useDirectClose) {
+ logger.debug("closing index {} directly", index);
+ metadata.put(IndexMetaData.builder(indexToClose).state(IndexMetaData.State.CLOSE)); // increment version?
+ blocks.removeIndexBlockWithId(index.getName(), INDEX_CLOSED_BLOCK_ID);
+ routingTable.remove(index.getName());
+ indexBlock = INDEX_CLOSED_BLOCK;
+ } else {
+ if (indexBlock == null) {
+ // Create a new index closed block
+ indexBlock = createIndexClosingBlock();
+ }
+ assert Strings.hasLength(indexBlock.uuid()) : "Closing block should have a UUID";
+ }
+ blocks.addIndexBlock(index.getName(), indexBlock);
+ blockedIndices.put(index, indexBlock);
}
- ClusterState updatedState = ClusterState.builder(currentState).metaData(mdBuilder).blocks(blocksBuilder).build();
+ logger.info(() -> new ParameterizedMessage("closing indices {}",
+ blockedIndices.keySet().stream().map(Object::toString).collect(Collectors.joining(","))));
+ return ClusterState.builder(currentState).blocks(blocks).metaData(metadata).routingTable(routingTable.build()).build();
+ }
- RoutingTable.Builder rtBuilder = RoutingTable.builder(currentState.routingTable());
- for (IndexMetaData index : indicesToClose) {
- rtBuilder.remove(index.getIndex().getName());
+ /**
+ * Step 2 - Wait for indices to be ready for closing
+ *
+ * This step iterates over the indices previously blocked and sends a {@link TransportVerifyShardBeforeCloseAction} to each shard. If
+ * this action succeed then the shard is considered to be ready for closing. When all shards of a given index are ready for closing,
+ * the index is considered ready to be closed.
+ */
+ class WaitForClosedBlocksApplied extends AbstractRunnable {
+
+ private final Map blockedIndices;
+ private final CloseIndexClusterStateUpdateRequest request;
+ private final ActionListener