Search in sources :

Example 1 with ClusterBlocks

use of org.opensearch.cluster.block.ClusterBlocks in project OpenSearch by opensearch-project.

the class Coordinator method clusterStateWithNoMasterBlock.

private ClusterState clusterStateWithNoMasterBlock(ClusterState clusterState) {
    if (clusterState.nodes().getMasterNodeId() != null) {
        // remove block if it already exists before adding new one
        assert clusterState.blocks().hasGlobalBlockWithId(NO_MASTER_BLOCK_ID) == false : "NO_MASTER_BLOCK should only be added by Coordinator";
        final ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(clusterState.blocks()).addGlobalBlock(noMasterBlockService.getNoMasterBlock()).build();
        final DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder(clusterState.nodes()).masterNodeId(null).build();
        return ClusterState.builder(clusterState).blocks(clusterBlocks).nodes(discoveryNodes).build();
    } else {
        return clusterState;
    }
}
Also used : ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 2 with ClusterBlocks

use of org.opensearch.cluster.block.ClusterBlocks in project OpenSearch by opensearch-project.

the class TransportVerifyShardIndexBlockAction method executeShardOperation.

private void executeShardOperation(final ShardRequest request, final IndexShard indexShard) {
    final ShardId shardId = indexShard.shardId();
    if (indexShard.getActiveOperationsCount() != IndexShard.OPERATIONS_BLOCKED) {
        throw new IllegalStateException("index shard " + shardId + " is not blocking all operations while waiting for block " + request.clusterBlock());
    }
    final ClusterBlocks clusterBlocks = clusterService.state().blocks();
    if (clusterBlocks.hasIndexBlock(shardId.getIndexName(), request.clusterBlock()) == false) {
        throw new IllegalStateException("index shard " + shardId + " has not applied block " + request.clusterBlock());
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks)

Example 3 with ClusterBlocks

use of org.opensearch.cluster.block.ClusterBlocks in project OpenSearch by opensearch-project.

the class MetadataCreateIndexService method clusterStateCreateIndex.

/**
 * Creates the index into the cluster state applying the provided blocks. The final cluster state will contain an updated routing
 * table based on the live nodes.
 */
static ClusterState clusterStateCreateIndex(ClusterState currentState, Set<ClusterBlock> clusterBlocks, IndexMetadata indexMetadata, BiFunction<ClusterState, String, ClusterState> rerouteRoutingTable, BiConsumer<Metadata.Builder, IndexMetadata> metadataTransformer) {
    Metadata.Builder builder = Metadata.builder(currentState.metadata()).put(indexMetadata, false);
    if (metadataTransformer != null) {
        metadataTransformer.accept(builder, indexMetadata);
    }
    Metadata newMetadata = builder.build();
    String indexName = indexMetadata.getIndex().getName();
    ClusterBlocks.Builder blocks = createClusterBlocksBuilder(currentState, indexName, clusterBlocks);
    blocks.updateBlocks(indexMetadata);
    ClusterState updatedState = ClusterState.builder(currentState).blocks(blocks).metadata(newMetadata).build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable()).addAsNew(updatedState.metadata().index(indexName));
    updatedState = ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build();
    return rerouteRoutingTable.apply(updatedState, "index [" + indexName + "] created");
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks)

Example 4 with ClusterBlocks

use of org.opensearch.cluster.block.ClusterBlocks in project OpenSearch by opensearch-project.

the class MainActionTests method testMainActionClusterAvailable.

public void testMainActionClusterAvailable() {
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterName clusterName = new ClusterName("opensearch");
    final Settings settings = Settings.builder().put("node.name", "my-node").build();
    when(clusterService.getClusterSettings()).thenReturn(new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    ClusterBlocks blocks;
    if (randomBoolean()) {
        if (randomBoolean()) {
            blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        } else {
            blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 400", randomBoolean(), randomBoolean(), false, RestStatus.BAD_REQUEST, ClusterBlockLevel.ALL)).build();
        }
    } else {
        blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 503", randomBoolean(), randomBoolean(), false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL)).build();
    }
    ClusterState state = ClusterState.builder(clusterName).blocks(blocks).build();
    when(clusterService.state()).thenReturn(state);
    TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    TransportMainAction action = new TransportMainAction(settings, transportService, mock(ActionFilters.class), clusterService);
    AtomicReference<MainResponse> responseRef = new AtomicReference<>();
    action.doExecute(mock(Task.class), new MainRequest(), new ActionListener<MainResponse>() {

        @Override
        public void onResponse(MainResponse mainResponse) {
            responseRef.set(mainResponse);
        }

        @Override
        public void onFailure(Exception e) {
            logger.error("unexpected error", e);
        }
    });
    assertNotNull(responseRef.get());
    verify(clusterService, times(1)).state();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Task(org.opensearch.tasks.Task) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) AtomicReference(java.util.concurrent.atomic.AtomicReference) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) ClusterName(org.opensearch.cluster.ClusterName) Transport(org.opensearch.transport.Transport) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Example 5 with ClusterBlocks

use of org.opensearch.cluster.block.ClusterBlocks in project OpenSearch by opensearch-project.

the class TransportVerifyShardBeforeCloseAction method executeShardOperation.

private void executeShardOperation(final ShardRequest request, final IndexShard indexShard) throws IOException {
    final ShardId shardId = indexShard.shardId();
    if (indexShard.getActiveOperationsCount() != IndexShard.OPERATIONS_BLOCKED) {
        throw new IllegalStateException("Index shard " + shardId + " is not blocking all operations during closing");
    }
    final ClusterBlocks clusterBlocks = clusterService.state().blocks();
    if (clusterBlocks.hasIndexBlock(shardId.getIndexName(), request.clusterBlock()) == false) {
        throw new IllegalStateException("Index shard " + shardId + " must be blocked by " + request.clusterBlock() + " before closing");
    }
    if (request.isPhase1()) {
        // in order to advance the global checkpoint to the maximum sequence number, the (persisted) local checkpoint needs to be
        // advanced first, which, when using async translog syncing, does not automatically hold at the time where we have acquired
        // all operation permits. Instead, this requires and explicit sync, which communicates the updated (persisted) local checkpoint
        // to the primary (we call this phase1), and phase2 can then use the fact that the global checkpoint has moved to the maximum
        // sequence number to pass the verifyShardBeforeIndexClosing check and create a safe commit where the maximum sequence number
        // is equal to the global checkpoint.
        indexShard.sync();
    } else {
        indexShard.verifyShardBeforeIndexClosing();
        indexShard.flush(new FlushRequest().force(true).waitIfOngoing(true));
        logger.trace("{} shard is ready for closing", shardId);
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest)

Aggregations

ClusterBlocks (org.opensearch.cluster.block.ClusterBlocks)9 RoutingTable (org.opensearch.cluster.routing.RoutingTable)4 HashSet (java.util.HashSet)3 ClusterBlock (org.opensearch.cluster.block.ClusterBlock)3 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)3 Settings (org.opensearch.common.settings.Settings)3 Index (org.opensearch.index.Index)3 HashMap (java.util.HashMap)2 ClusterState (org.opensearch.cluster.ClusterState)2 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)2 ShardId (org.opensearch.index.shard.ShardId)2 SnapshotInProgressException (org.opensearch.snapshots.SnapshotInProgressException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)1 ActionFilters (org.opensearch.action.support.ActionFilters)1 Client (org.opensearch.client.Client)1