|
@@ -68,6 +68,7 @@ import org.elasticsearch.index.shard.ShardId;
|
|
|
import org.elasticsearch.indices.IndicesService;
|
|
|
import org.elasticsearch.rest.RestStatus;
|
|
|
import org.elasticsearch.snapshots.RestoreService;
|
|
|
+import org.elasticsearch.snapshots.SnapshotInProgressException;
|
|
|
import org.elasticsearch.snapshots.SnapshotsService;
|
|
|
import org.elasticsearch.tasks.TaskId;
|
|
|
import org.elasticsearch.threadpool.ThreadPool;
|
|
@@ -86,6 +87,7 @@ import java.util.Set;
|
|
|
import java.util.function.Consumer;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import static java.util.Collections.singleton;
|
|
|
import static java.util.Collections.unmodifiableMap;
|
|
|
|
|
|
/**
|
|
@@ -230,11 +232,11 @@ public class MetaDataIndexStateService {
|
|
|
final ClusterState currentState) {
|
|
|
final MetaData.Builder metadata = MetaData.builder(currentState.metaData());
|
|
|
|
|
|
- final Set<IndexMetaData> indicesToClose = new HashSet<>();
|
|
|
+ final Set<Index> indicesToClose = new HashSet<>();
|
|
|
for (Index index : indices) {
|
|
|
final IndexMetaData indexMetaData = metadata.getSafe(index);
|
|
|
if (indexMetaData.getState() != IndexMetaData.State.CLOSE) {
|
|
|
- indicesToClose.add(indexMetaData);
|
|
|
+ indicesToClose.add(index);
|
|
|
} else {
|
|
|
logger.debug("index {} is already closed, ignoring", index);
|
|
|
assert currentState.blocks().hasIndexBlock(index.getName(), INDEX_CLOSED_BLOCK);
|
|
@@ -246,16 +248,22 @@ public class MetaDataIndexStateService {
|
|
|
}
|
|
|
|
|
|
// Check if index closing conflicts with any running restores
|
|
|
- RestoreService.checkIndexClosing(currentState, indicesToClose);
|
|
|
+ Set<Index> restoringIndices = RestoreService.restoringIndices(currentState, indicesToClose);
|
|
|
+ if (restoringIndices.isEmpty() == false) {
|
|
|
+ throw new IllegalArgumentException("Cannot close indices that are being restored: " + restoringIndices);
|
|
|
+ }
|
|
|
+
|
|
|
// Check if index closing conflicts with any running snapshots
|
|
|
- SnapshotsService.checkIndexClosing(currentState, indicesToClose);
|
|
|
+ Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, indicesToClose);
|
|
|
+ if (snapshottingIndices.isEmpty() == false) {
|
|
|
+ throw new SnapshotInProgressException("Cannot close indices that are being snapshotted: " + snapshottingIndices +
|
|
|
+ ". Try again after snapshot finishes or cancel the currently running snapshot.");
|
|
|
+ }
|
|
|
|
|
|
final ClusterBlocks.Builder blocks = ClusterBlocks.builder().blocks(currentState.blocks());
|
|
|
final RoutingTable.Builder routingTable = RoutingTable.builder(currentState.routingTable());
|
|
|
|
|
|
- for (IndexMetaData indexToClose : indicesToClose) {
|
|
|
- final Index index = indexToClose.getIndex();
|
|
|
-
|
|
|
+ for (Index index : indicesToClose) {
|
|
|
ClusterBlock indexBlock = null;
|
|
|
final Set<ClusterBlock> clusterBlocks = currentState.blocks().indices().get(index.getName());
|
|
|
if (clusterBlocks != null) {
|
|
@@ -453,6 +461,24 @@ public class MetaDataIndexStateService {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ // Check if index closing conflicts with any running restores
|
|
|
+ Set<Index> restoringIndices = RestoreService.restoringIndices(currentState, singleton(index));
|
|
|
+ if (restoringIndices.isEmpty() == false) {
|
|
|
+ closingResults.put(result.getKey(), new IndexResult(result.getKey(), new IllegalStateException(
|
|
|
+ "verification of shards before closing " + index + " succeeded but index is being restored in the meantime")));
|
|
|
+ logger.debug("verification of shards before closing {} succeeded but index is being restored in the meantime", index);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if index closing conflicts with any running snapshots
|
|
|
+ Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, singleton(index));
|
|
|
+ if (snapshottingIndices.isEmpty() == false) {
|
|
|
+ closingResults.put(result.getKey(), new IndexResult(result.getKey(), new IllegalStateException(
|
|
|
+ "verification of shards before closing " + index + " succeeded but index is being snapshot in the meantime")));
|
|
|
+ logger.debug("verification of shards before closing {} succeeded but index is being snapshot in the meantime", index);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
blocks.removeIndexBlockWithId(index.getName(), INDEX_CLOSED_BLOCK_ID);
|
|
|
blocks.addIndexBlock(index.getName(), INDEX_CLOSED_BLOCK);
|
|
|
final IndexMetaData.Builder updatedMetaData = IndexMetaData.builder(indexMetaData).state(IndexMetaData.State.CLOSE);
|