Search in sources :

Example 31 with ClusterBlock

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

the class MetadataIndexStateServiceTests method testCloseRoutingTable.

public void testCloseRoutingTable() {
    final Set<Index> nonBlockedIndices = new HashSet<>();
    final Map<Index, ClusterBlock> blockedIndices = new HashMap<>();
    final Map<Index, IndexResult> results = new HashMap<>();
    ClusterState state = ClusterState.builder(new ClusterName("testCloseRoutingTable")).build();
    for (int i = 0; i < randomIntBetween(1, 25); i++) {
        final String indexName = "index-" + i;
        if (randomBoolean()) {
            state = addOpenedIndex(indexName, randomIntBetween(1, 5), randomIntBetween(0, 5), state);
            nonBlockedIndices.add(state.metadata().index(indexName).getIndex());
        } else {
            final ClusterBlock closingBlock = MetadataIndexStateService.createIndexClosingBlock();
            state = addBlockedIndex(indexName, randomIntBetween(1, 5), randomIntBetween(0, 5), state, closingBlock);
            final Index index = state.metadata().index(indexName).getIndex();
            blockedIndices.put(index, closingBlock);
            if (randomBoolean()) {
                results.put(index, new CloseIndexResponse.IndexResult(index));
            } else {
                results.put(index, new CloseIndexResponse.IndexResult(index, new Exception("test")));
            }
        }
    }
    final ClusterState updatedState = MetadataIndexStateService.closeRoutingTable(state, blockedIndices, results).v1();
    assertThat(updatedState.metadata().indices().size(), equalTo(nonBlockedIndices.size() + blockedIndices.size()));
    for (Index nonBlockedIndex : nonBlockedIndices) {
        assertIsOpened(nonBlockedIndex.getName(), updatedState);
        assertThat(updatedState.blocks().hasIndexBlockWithId(nonBlockedIndex.getName(), INDEX_CLOSED_BLOCK_ID), is(false));
    }
    for (Index blockedIndex : blockedIndices.keySet()) {
        if (results.get(blockedIndex).hasFailures() == false) {
            assertIsClosed(blockedIndex.getName(), updatedState);
        } else {
            assertIsOpened(blockedIndex.getName(), updatedState);
            assertThat(updatedState.blocks().hasIndexBlockWithId(blockedIndex.getName(), INDEX_CLOSED_BLOCK_ID), is(true));
        }
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) HashMap(java.util.HashMap) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) ClusterName(org.opensearch.cluster.ClusterName) HashSet(java.util.HashSet)

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