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;
}
}
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());
}
}
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");
}
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();
}
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);
}
}
Aggregations