Search in sources :

Example 16 with ClusterBlock

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));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) HashMap(java.util.HashMap) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterName(org.opensearch.cluster.ClusterName) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) HashSet(java.util.HashSet)

Example 17 with ClusterBlock

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));
}
Also used : ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterState(org.opensearch.cluster.ClusterState) HashMap(java.util.HashMap) ClusterName(org.opensearch.cluster.ClusterName) Index(org.opensearch.index.Index)

Example 18 with ClusterBlock

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));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) INDEX_CLOSED_BLOCK_ID(org.opensearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID) INDEX_CLOSED_BLOCK(org.opensearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK) SETTING_VERSION_CREATED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED) Version(org.opensearch.Version) Strings(org.opensearch.common.Strings) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexId(org.opensearch.repositories.IndexId) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) SnapshotInfoTests(org.opensearch.snapshots.SnapshotInfoTests) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) SnapshotId(org.opensearch.snapshots.SnapshotId) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) Tuple(org.opensearch.common.collect.Tuple) List(java.util.List) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) CloseIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.close.CloseIndexClusterStateUpdateRequest) ClusterState(org.opensearch.cluster.ClusterState) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Collections.emptyMap(java.util.Collections.emptyMap) DeleteDataStreamRequestTests(org.opensearch.action.admin.indices.datastream.DeleteDataStreamRequestTests) Collections.emptySet(java.util.Collections.emptySet) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.when(org.mockito.Mockito.when) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) Snapshot(org.opensearch.snapshots.Snapshot) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) Collections(java.util.Collections) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Settings(org.opensearch.common.settings.Settings)

Example 19 with ClusterBlock

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;
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EnumSet(java.util.EnumSet) Set(java.util.Set) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 20 with ClusterBlock

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);
}
Also used : ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks)

Aggregations

ClusterBlock (org.opensearch.cluster.block.ClusterBlock)31 ClusterState (org.opensearch.cluster.ClusterState)19 ClusterBlocks (org.opensearch.cluster.block.ClusterBlocks)18 HashSet (java.util.HashSet)15 Index (org.opensearch.index.Index)14 HashMap (java.util.HashMap)12 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)11 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)11 Settings (org.opensearch.common.settings.Settings)11 ClusterName (org.opensearch.cluster.ClusterName)10 ClusterService (org.opensearch.cluster.service.ClusterService)10 Set (java.util.Set)9 IndexResult (org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult)9 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)9 IndexRoutingTable (org.opensearch.cluster.routing.IndexRoutingTable)9 ShardId (org.opensearch.index.shard.ShardId)9 RoutingTable (org.opensearch.cluster.routing.RoutingTable)8 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)8 List (java.util.List)7 ShardRouting (org.opensearch.cluster.routing.ShardRouting)7