Search in sources :

Example 11 with ClusterBlocks

use of org.elasticsearch.cluster.block.ClusterBlocks in project crate by crate.

the class MetadataDeleteIndexService method deleteIndices.

/**
 * Delete some indices from the cluster state.
 */
public ClusterState deleteIndices(ClusterState currentState, Set<Index> indices) {
    final Metadata meta = currentState.metadata();
    final Set<Index> indicesToDelete = indices.stream().map(i -> meta.getIndexSafe(i).getIndex()).collect(toSet());
    // Check if index deletion conflicts with any running snapshots
    Set<Index> snapshottingIndices = SnapshotsService.snapshottingIndices(currentState, indicesToDelete);
    if (snapshottingIndices.isEmpty() == false) {
        throw new SnapshotInProgressException("Cannot delete indices that are being snapshotted: " + snapshottingIndices + ". Try again after snapshot finishes or cancel the currently running snapshot.");
    }
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder(currentState.routingTable());
    Metadata.Builder metadataBuilder = Metadata.builder(meta);
    ClusterBlocks.Builder clusterBlocksBuilder = ClusterBlocks.builder().blocks(currentState.blocks());
    final IndexGraveyard.Builder graveyardBuilder = IndexGraveyard.builder(metadataBuilder.indexGraveyard());
    final int previousGraveyardSize = graveyardBuilder.tombstones().size();
    for (final Index index : indices) {
        String indexName = index.getName();
        LOGGER.info("{} deleting index", index);
        routingTableBuilder.remove(indexName);
        clusterBlocksBuilder.removeIndexBlocks(indexName);
        metadataBuilder.remove(indexName);
    }
    // add tombstones to the cluster state for each deleted index
    final IndexGraveyard currentGraveyard = graveyardBuilder.addTombstones(indices).build(settings);
    // the new graveyard set on the metadata
    metadataBuilder.indexGraveyard(currentGraveyard);
    LOGGER.trace("{} tombstones purged from the cluster state. Previous tombstone size: {}. Current tombstone size: {}.", graveyardBuilder.getNumPurged(), previousGraveyardSize, currentGraveyard.getTombstones().size());
    Metadata newMetadata = metadataBuilder.build();
    ClusterBlocks blocks = clusterBlocksBuilder.build();
    // update snapshot restore entries
    ImmutableOpenMap<String, ClusterState.Custom> customs = currentState.getCustoms();
    final RestoreInProgress restoreInProgress = currentState.custom(RestoreInProgress.TYPE);
    if (restoreInProgress != null) {
        RestoreInProgress updatedRestoreInProgress = RestoreService.updateRestoreStateWithDeletedIndices(restoreInProgress, indices);
        if (updatedRestoreInProgress != restoreInProgress) {
            ImmutableOpenMap.Builder<String, ClusterState.Custom> builder = ImmutableOpenMap.builder(customs);
            builder.put(RestoreInProgress.TYPE, updatedRestoreInProgress);
            customs = builder.build();
        }
    }
    return allocationService.reroute(ClusterState.builder(currentState).routingTable(routingTableBuilder.build()).metadata(newMetadata).blocks(blocks).customs(customs).build(), "deleted indices [" + indices + "]");
}
Also used : Arrays(java.util.Arrays) AckedClusterStateUpdateTask(org.elasticsearch.cluster.AckedClusterStateUpdateTask) Priority(org.elasticsearch.common.Priority) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ClusterService(org.elasticsearch.cluster.service.ClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) Set(java.util.Set) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Index(org.elasticsearch.index.Index) RestoreService(org.elasticsearch.snapshots.RestoreService) Inject(org.elasticsearch.common.inject.Inject) DeleteIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest) ClusterState(org.elasticsearch.cluster.ClusterState) Logger(org.apache.logging.log4j.Logger) Settings(org.elasticsearch.common.settings.Settings) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ClusterStateUpdateResponse(org.elasticsearch.cluster.ack.ClusterStateUpdateResponse) LogManager(org.apache.logging.log4j.LogManager) ActionListener(org.elasticsearch.action.ActionListener) Collectors.toSet(java.util.stream.Collectors.toSet) SnapshotInProgressException(org.elasticsearch.snapshots.SnapshotInProgressException) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) Index(org.elasticsearch.index.Index) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) RestoreInProgress(org.elasticsearch.cluster.RestoreInProgress) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) SnapshotInProgressException(org.elasticsearch.snapshots.SnapshotInProgressException)

Aggregations

ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)11 ClusterState (org.elasticsearch.cluster.ClusterState)5 Settings (org.elasticsearch.common.settings.Settings)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)3 Index (org.elasticsearch.index.Index)3 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 Set (java.util.Set)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 ActionListener (org.elasticsearch.action.ActionListener)2 DeleteIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexClusterStateUpdateRequest)2 AckedClusterStateUpdateTask (org.elasticsearch.cluster.AckedClusterStateUpdateTask)2 RestoreInProgress (org.elasticsearch.cluster.RestoreInProgress)2 ClusterStateUpdateResponse (org.elasticsearch.cluster.ack.ClusterStateUpdateResponse)2 ClusterBlock (org.elasticsearch.cluster.block.ClusterBlock)2 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)2 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)2 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)2 Priority (org.elasticsearch.common.Priority)2 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)2