use of org.opensearch.cluster.block.ClusterBlock in project OpenSearch by opensearch-project.
the class MetadataIndexStateServiceTests method testCloseFailedIfBlockDisappeared.
public void testCloseFailedIfBlockDisappeared() {
ClusterState state = ClusterState.builder(new ClusterName("failedIfBlockDisappeared")).build();
Map<Index, ClusterBlock> blockedIndices = new HashMap<>();
int numIndices = between(1, 10);
Set<Index> disappearedIndices = new HashSet<>();
Map<Index, IndexResult> verifyResults = new HashMap<>();
for (int i = 0; i < numIndices; i++) {
String indexName = "test-" + i;
state = addOpenedIndex(indexName, randomIntBetween(1, 3), randomIntBetween(0, 3), state);
Index index = state.metadata().index(indexName).getIndex();
state = MetadataIndexStateService.addIndexClosedBlocks(new Index[] { index }, blockedIndices, state);
if (randomBoolean()) {
state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).removeIndexBlocks(indexName).build()).build();
disappearedIndices.add(index);
}
verifyResults.put(index, new IndexResult(index));
}
Collection<IndexResult> closingResults = MetadataIndexStateService.closeRoutingTable(state, blockedIndices, unmodifiableMap(verifyResults)).v2();
assertThat(closingResults, hasSize(numIndices));
Set<Index> failedIndices = closingResults.stream().filter(IndexResult::hasFailures).map(IndexResult::getIndex).collect(Collectors.toSet());
assertThat(failedIndices, equalTo(disappearedIndices));
}
use of org.opensearch.cluster.block.ClusterBlock in project OpenSearch by opensearch-project.
the class MetadataIndexStateServiceTests method testAddIndexClosedBlocksReusesBlocks.
public void testAddIndexClosedBlocksReusesBlocks() {
ClusterState state = ClusterState.builder(new ClusterName("testAddIndexClosedBlocksReuseBlocks")).build();
state = addOpenedIndex("test", randomIntBetween(1, 3), randomIntBetween(0, 3), state);
Index test = state.metadata().index("test").getIndex();
Index[] indices = new Index[] { test };
final Map<Index, ClusterBlock> blockedIndices = new HashMap<>();
state = MetadataIndexStateService.addIndexClosedBlocks(indices, blockedIndices, state);
assertTrue(blockedIndices.containsKey(test));
assertHasBlock(test.getName(), state, blockedIndices.get(test));
final Map<Index, ClusterBlock> blockedIndices2 = new HashMap<>();
state = MetadataIndexStateService.addIndexClosedBlocks(indices, blockedIndices2, state);
assertTrue(blockedIndices2.containsKey(test));
assertHasBlock(test.getName(), state, blockedIndices2.get(test));
assertEquals(blockedIndices.get(test), blockedIndices2.get(test));
}
use of org.opensearch.cluster.block.ClusterBlock in project OpenSearch by opensearch-project.
the class MetadataIndexStateServiceTests method assertIsClosed.
private static void assertIsClosed(final String indexName, final ClusterState clusterState) {
final IndexMetadata indexMetadata = clusterState.metadata().indices().get(indexName);
assertThat(indexMetadata.getState(), is(IndexMetadata.State.CLOSE));
final Settings indexSettings = indexMetadata.getSettings();
assertThat(indexSettings.hasValue(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey()), is(true));
assertThat(indexSettings.getAsBoolean(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey(), false), is(true));
assertThat(clusterState.blocks().hasIndexBlock(indexName, MetadataIndexStateService.INDEX_CLOSED_BLOCK), is(true));
assertThat("Index " + indexName + " must have only 1 block with [id=" + MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID + "]", clusterState.blocks().indices().getOrDefault(indexName, emptySet()).stream().filter(clusterBlock -> clusterBlock.id() == MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID).count(), equalTo(1L));
final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName);
assertThat(indexRoutingTable, notNullValue());
for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
assertThat(shardRoutingTable.shards().stream().allMatch(ShardRouting::unassigned), is(true));
assertThat(shardRoutingTable.shards().stream().map(ShardRouting::unassignedInfo).map(UnassignedInfo::getReason).allMatch(info -> info == UnassignedInfo.Reason.INDEX_CLOSED), is(true));
}
}
use of org.opensearch.cluster.block.ClusterBlock in project OpenSearch by opensearch-project.
the class ClusterState method toXContent.
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
// always provide the cluster_uuid as part of the top-level response (also part of the metadata response)
builder.field("cluster_uuid", metadata().clusterUUID());
if (metrics.contains(Metric.VERSION)) {
builder.field("version", version);
builder.field("state_uuid", stateUUID);
}
if (metrics.contains(Metric.MASTER_NODE)) {
builder.field("master_node", nodes().getMasterNodeId());
}
if (metrics.contains(Metric.BLOCKS)) {
builder.startObject("blocks");
if (!blocks().global().isEmpty()) {
builder.startObject("global");
for (ClusterBlock block : blocks().global()) {
block.toXContent(builder, params);
}
builder.endObject();
}
if (!blocks().indices().isEmpty()) {
builder.startObject("indices");
for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : blocks().indices()) {
builder.startObject(entry.key);
for (ClusterBlock block : entry.value) {
block.toXContent(builder, params);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
}
// nodes
if (metrics.contains(Metric.NODES)) {
builder.startObject("nodes");
for (DiscoveryNode node : nodes) {
node.toXContent(builder, params);
}
builder.endObject();
}
// meta data
if (metrics.contains(Metric.METADATA)) {
metadata.toXContent(builder, params);
}
// routing table
if (metrics.contains(Metric.ROUTING_TABLE)) {
builder.startObject("routing_table");
builder.startObject("indices");
for (IndexRoutingTable indexRoutingTable : routingTable()) {
builder.startObject(indexRoutingTable.getIndex().getName());
builder.startObject("shards");
for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
for (ShardRouting shardRouting : indexShardRoutingTable) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.endObject();
}
// routing nodes
if (metrics.contains(Metric.ROUTING_NODES)) {
builder.startObject("routing_nodes");
builder.startArray("unassigned");
for (ShardRouting shardRouting : getRoutingNodes().unassigned()) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
builder.startObject("nodes");
for (RoutingNode routingNode : getRoutingNodes()) {
builder.startArray(routingNode.nodeId() == null ? "null" : routingNode.nodeId());
for (ShardRouting shardRouting : routingNode) {
shardRouting.toXContent(builder, params);
}
builder.endArray();
}
builder.endObject();
builder.endObject();
}
if (metrics.contains(Metric.CUSTOMS)) {
for (ObjectObjectCursor<String, Custom> cursor : customs) {
builder.startObject(cursor.key);
cursor.value.toXContent(builder, params);
builder.endObject();
}
}
return builder;
}
use of org.opensearch.cluster.block.ClusterBlock in project OpenSearch by opensearch-project.
the class ClusterStateDiffIT method randomBlocks.
/**
* Randomly creates or removes cluster blocks
*/
private ClusterState.Builder randomBlocks(ClusterState clusterState) {
ClusterBlocks.Builder builder = ClusterBlocks.builder().blocks(clusterState.blocks());
int globalBlocksCount = clusterState.blocks().global().size();
if (globalBlocksCount > 0) {
List<ClusterBlock> blocks = randomSubsetOf(randomInt(globalBlocksCount - 1), clusterState.blocks().global().toArray(new ClusterBlock[globalBlocksCount]));
for (ClusterBlock block : blocks) {
builder.removeGlobalBlock(block);
}
}
int additionalGlobalBlocksCount = randomIntBetween(1, 3);
for (int i = 0; i < additionalGlobalBlocksCount; i++) {
builder.addGlobalBlock(randomGlobalBlock());
}
return ClusterState.builder(clusterState).blocks(builder);
}
Aggregations