Search in sources :

Example 11 with ClusterBlock

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

the class ClusterStateChanges method closeIndices.

public ClusterState closeIndices(ClusterState state, CloseIndexRequest request) {
    final Index[] concreteIndices = Arrays.stream(request.indices()).map(index -> state.metadata().index(index).getIndex()).toArray(Index[]::new);
    final Map<Index, ClusterBlock> blockedIndices = new HashMap<>();
    ClusterState newState = MetadataIndexStateServiceUtils.addIndexClosedBlocks(concreteIndices, blockedIndices, state);
    newState = MetadataIndexStateServiceUtils.closeRoutingTable(newState, blockedIndices, blockedIndices.keySet().stream().collect(Collectors.toMap(Function.identity(), CloseIndexResponse.IndexResult::new)));
    return allocationService.reroute(newState, "indices closed");
}
Also used : Arrays(java.util.Arrays) CheckedFunction(org.opensearch.common.CheckedFunction) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) Transport(org.opensearch.transport.Transport) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) TransportVerifyShardIndexBlockAction(org.opensearch.action.admin.indices.readonly.TransportVerifyShardIndexBlockAction) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionResponse(org.opensearch.action.ActionResponse) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) AllocationDeciders(org.opensearch.cluster.routing.allocation.decider.AllocationDeciders) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) ExceptionsHelper(org.opensearch.ExceptionsHelper) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) TransportClusterRerouteAction(org.opensearch.action.admin.cluster.reroute.TransportClusterRerouteAction) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Mockito.mock(org.mockito.Mockito.mock) ThreadPool(org.opensearch.threadpool.ThreadPool) Priority(org.opensearch.common.Priority) TransportMasterNodeAction(org.opensearch.action.support.master.TransportMasterNodeAction) ArrayList(java.util.ArrayList) ClusterState(org.opensearch.cluster.ClusterState) Mockito.anyString(org.mockito.Mockito.anyString) TestGatewayAllocator(org.opensearch.test.gateway.TestGatewayAllocator) Environment(org.opensearch.env.Environment) RandomizedTest.getRandom(com.carrotsearch.randomizedtesting.RandomizedTest.getRandom) IndexService(org.opensearch.index.IndexService) SystemIndices(org.opensearch.indices.SystemIndices) TransportMasterNodeActionUtils(org.opensearch.action.support.master.TransportMasterNodeActionUtils) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) StartedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.StartedShardEntry) ClusterService(org.opensearch.cluster.service.ClusterService) DestructiveOperations(org.opensearch.action.support.DestructiveOperations) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) FailedShardEntry(org.opensearch.cluster.action.shard.ShardStateAction.FailedShardEntry) MetadataDeleteIndexService(org.opensearch.cluster.metadata.MetadataDeleteIndexService) ClusterRerouteRequest(org.opensearch.action.admin.cluster.reroute.ClusterRerouteRequest) TransportUpdateSettingsAction(org.opensearch.action.admin.indices.settings.put.TransportUpdateSettingsAction) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MetadataIndexUpgradeService(org.opensearch.cluster.metadata.MetadataIndexUpgradeService) Assert.assertThat(org.junit.Assert.assertThat) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) ClusterTasksResult(org.opensearch.cluster.ClusterStateTaskExecutor.ClusterTasksResult) RandomAllocationDeciderTests(org.opensearch.cluster.routing.allocation.RandomAllocationDeciderTests) BalancedShardsAllocator(org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) PATH_HOME_SETTING(org.opensearch.env.Environment.PATH_HOME_SETTING) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) SameShardAllocationDecider(org.opensearch.cluster.routing.allocation.decider.SameShardAllocationDecider) IndexEventListener(org.opensearch.index.shard.IndexEventListener) IndicesService(org.opensearch.indices.IndicesService) NodeRemovalClusterStateTaskExecutor(org.opensearch.cluster.coordination.NodeRemovalClusterStateTaskExecutor) Collectors(java.util.stream.Collectors) AliasValidator(org.opensearch.cluster.metadata.AliasValidator) List(java.util.List) FailedShard(org.opensearch.cluster.routing.allocation.FailedShard) EmptyClusterInfoService(org.opensearch.cluster.EmptyClusterInfoService) Mockito.any(org.mockito.Mockito.any) TestEnvironment(org.opensearch.env.TestEnvironment) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) MetadataUpdateSettingsService(org.opensearch.cluster.metadata.MetadataUpdateSettingsService) HashMap(java.util.HashMap) TransportVerifyShardBeforeCloseAction(org.opensearch.action.admin.indices.close.TransportVerifyShardBeforeCloseAction) Function(java.util.function.Function) TransportCloseIndexAction(org.opensearch.action.admin.indices.close.TransportCloseIndexAction) HashSet(java.util.HashSet) MetadataCreateIndexService(org.opensearch.cluster.metadata.MetadataCreateIndexService) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) JoinTaskExecutor(org.opensearch.cluster.coordination.JoinTaskExecutor) TransportCreateIndexAction(org.opensearch.action.admin.indices.create.TransportCreateIndexAction) UUIDs(org.opensearch.common.UUIDs) ClusterSettings(org.opensearch.common.settings.ClusterSettings) MetadataIndexStateServiceUtils(org.opensearch.cluster.metadata.MetadataIndexStateServiceUtils) ReplicaAfterPrimaryActiveAllocationDecider(org.opensearch.cluster.routing.allocation.decider.ReplicaAfterPrimaryActiveAllocationDecider) MetadataIndexStateService(org.opensearch.cluster.metadata.MetadataIndexStateService) TransportDeleteIndexAction(org.opensearch.action.admin.indices.delete.TransportDeleteIndexAction) Collections.emptyMap(java.util.Collections.emptyMap) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) Mockito.when(org.mockito.Mockito.when) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TransportOpenIndexAction(org.opensearch.action.admin.indices.open.TransportOpenIndexAction) EmptySnapshotsInfoService(org.opensearch.snapshots.EmptySnapshotsInfoService) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterState(org.opensearch.cluster.ClusterState) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) HashMap(java.util.HashMap) Index(org.opensearch.index.Index)

Example 12 with ClusterBlock

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

the class IndicesClusterStateServiceRandomUpdatesTests method randomlyUpdateClusterState.

public ClusterState randomlyUpdateClusterState(ClusterState state, Map<DiscoveryNode, IndicesClusterStateService> clusterStateServiceMap, Supplier<MockIndicesService> indicesServiceSupplier) {
    // randomly remove no_master blocks
    if (randomBoolean() && state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)) {
        state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).removeGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ID)).build();
    }
    // randomly add no_master blocks
    if (rarely() && state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID) == false) {
        ClusterBlock block = randomBoolean() ? NoMasterBlockService.NO_MASTER_BLOCK_ALL : NoMasterBlockService.NO_MASTER_BLOCK_WRITES;
        state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).addGlobalBlock(block)).build();
    }
    // if no_master block is in place, make no other cluster state changes
    if (state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)) {
        return state;
    }
    // randomly create new indices (until we have 200 max)
    for (int i = 0; i < randomInt(5); i++) {
        if (state.metadata().indices().size() > 200) {
            break;
        }
        String name = "index_" + randomAlphaOfLength(15).toLowerCase(Locale.ROOT);
        Settings.Builder settingsBuilder = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 3));
        if (randomBoolean()) {
            int min = randomInt(2);
            int max = min + randomInt(3);
            settingsBuilder.put(SETTING_AUTO_EXPAND_REPLICAS, randomBoolean() ? min + "-" + max : min + "-all");
        } else {
            settingsBuilder.put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        }
        CreateIndexRequest request = new CreateIndexRequest(name, settingsBuilder.build()).waitForActiveShards(ActiveShardCount.NONE);
        state = cluster.createIndex(state, request);
        assertTrue(state.metadata().hasIndex(name));
    }
    // randomly delete indices
    Set<String> indicesToDelete = new HashSet<>();
    int numberOfIndicesToDelete = randomInt(Math.min(2, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToDelete, state.metadata().indices().keys().toArray(String.class))) {
        indicesToDelete.add(state.metadata().index(index).getIndex().getName());
    }
    if (indicesToDelete.isEmpty() == false) {
        DeleteIndexRequest deleteRequest = new DeleteIndexRequest(indicesToDelete.toArray(new String[indicesToDelete.size()]));
        state = cluster.deleteIndices(state, deleteRequest);
        for (String index : indicesToDelete) {
            assertFalse(state.metadata().hasIndex(index));
        }
    }
    // randomly close indices
    int numberOfIndicesToClose = randomInt(Math.min(1, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToClose, state.metadata().indices().keys().toArray(String.class))) {
        CloseIndexRequest closeIndexRequest = new CloseIndexRequest(state.metadata().index(index).getIndex().getName());
        state = cluster.closeIndices(state, closeIndexRequest);
    }
    // randomly open indices
    int numberOfIndicesToOpen = randomInt(Math.min(1, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToOpen, state.metadata().indices().keys().toArray(String.class))) {
        OpenIndexRequest openIndexRequest = new OpenIndexRequest(state.metadata().index(index).getIndex().getName());
        state = cluster.openIndices(state, openIndexRequest);
    }
    // randomly update settings
    Set<String> indicesToUpdate = new HashSet<>();
    boolean containsClosedIndex = false;
    int numberOfIndicesToUpdate = randomInt(Math.min(2, state.metadata().indices().size()));
    for (String index : randomSubsetOf(numberOfIndicesToUpdate, state.metadata().indices().keys().toArray(String.class))) {
        indicesToUpdate.add(state.metadata().index(index).getIndex().getName());
        if (state.metadata().index(index).getState() == IndexMetadata.State.CLOSE) {
            containsClosedIndex = true;
        }
    }
    if (indicesToUpdate.isEmpty() == false) {
        UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indicesToUpdate.toArray(new String[indicesToUpdate.size()]));
        Settings.Builder settings = Settings.builder();
        if (containsClosedIndex == false) {
            settings.put(SETTING_NUMBER_OF_REPLICAS, randomInt(2));
        }
        settings.put("index.refresh_interval", randomIntBetween(1, 5) + "s");
        updateSettingsRequest.settings(settings.build());
        state = cluster.updateSettings(state, updateSettingsRequest);
    }
    // randomly reroute
    if (rarely()) {
        state = cluster.reroute(state, new ClusterRerouteRequest());
    }
    // randomly start and fail allocated shards
    final Map<ShardRouting, Long> startedShards = new HashMap<>();
    List<FailedShard> failedShards = new ArrayList<>();
    for (DiscoveryNode node : state.nodes()) {
        IndicesClusterStateService indicesClusterStateService = clusterStateServiceMap.get(node);
        MockIndicesService indicesService = (MockIndicesService) indicesClusterStateService.indicesService;
        for (MockIndexService indexService : indicesService) {
            for (MockIndexShard indexShard : indexService) {
                ShardRouting persistedShardRouting = indexShard.routingEntry();
                if (persistedShardRouting.initializing() && randomBoolean()) {
                    startedShards.put(persistedShardRouting, indexShard.term());
                } else if (rarely()) {
                    failedShards.add(new FailedShard(persistedShardRouting, "fake shard failure", new Exception(), randomBoolean()));
                }
            }
        }
    }
    state = cluster.applyFailedShards(state, failedShards);
    state = cluster.applyStartedShards(state, startedShards);
    // randomly add and remove nodes (except current master)
    if (rarely()) {
        if (randomBoolean()) {
            // add node
            if (state.nodes().getSize() < 10) {
                state = cluster.addNodes(state, Collections.singletonList(createNode()));
                updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
            }
        } else {
            // remove node
            if (state.nodes().getDataNodes().size() > 3) {
                DiscoveryNode discoveryNode = randomFrom(state.nodes().getNodes().values().toArray(DiscoveryNode.class));
                if (discoveryNode.equals(state.nodes().getMasterNode()) == false) {
                    state = cluster.removeNodes(state, Collections.singletonList(discoveryNode));
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
                if (randomBoolean()) {
                    // and add it back
                    state = cluster.addNodes(state, Collections.singletonList(discoveryNode));
                    updateNodes(state, clusterStateServiceMap, indicesServiceSupplier);
                }
            }
        }
    }
    return state;
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterRerouteRequest(org.opensearch.action.admin.cluster.reroute.ClusterRerouteRequest) Settings(org.opensearch.common.settings.Settings) HashSet(java.util.HashSet) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) FailedShard(org.opensearch.cluster.routing.allocation.FailedShard) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 13 with ClusterBlock

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

the class TransportInstanceSingleOperationActionTests method testGlobalBlock.

public void testGlobalBlock() {
    Request request = new Request();
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    ClusterBlocks.Builder block = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(1, "", false, true, false, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL));
    setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block));
    try {
        action.new AsyncSingleAction(request, listener).start();
        listener.get();
        fail("expected ClusterBlockException");
    } catch (Exception e) {
        if (ExceptionsHelper.unwrap(e, ClusterBlockException.class) == null) {
            logger.info("expected ClusterBlockException  but got ", e);
            fail("expected ClusterBlockException");
        }
    }
}
Also used : ActionResponse(org.opensearch.action.ActionResponse) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) IndicesRequest(org.opensearch.action.IndicesRequest) TimeoutException(java.util.concurrent.TimeoutException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) TransportException(org.opensearch.transport.TransportException)

Example 14 with ClusterBlock

use of org.opensearch.cluster.block.ClusterBlock 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 15 with ClusterBlock

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

the class MetadataIndexStateServiceTests method testCloseRoutingTableWithRestoredIndex.

public void testCloseRoutingTableWithRestoredIndex() {
    ClusterState state = ClusterState.builder(new ClusterName("testCloseRoutingTableWithRestoredIndex")).build();
    String indexName = "restored-index";
    ClusterBlock block = MetadataIndexStateService.createIndexClosingBlock();
    state = addRestoredIndex(indexName, randomIntBetween(1, 5), randomIntBetween(0, 5), state);
    state = ClusterState.builder(state).blocks(ClusterBlocks.builder().blocks(state.blocks()).addIndexBlock(indexName, block)).build();
    final Index index = state.metadata().index(indexName).getIndex();
    final ClusterState updatedState = MetadataIndexStateService.closeRoutingTable(state, singletonMap(index, block), singletonMap(index, new IndexResult(index))).v1();
    assertIsOpened(index.getName(), updatedState);
    assertThat(updatedState.blocks().hasIndexBlockWithId(index.getName(), INDEX_CLOSED_BLOCK_ID), is(true));
}
Also used : ClusterBlock(org.opensearch.cluster.block.ClusterBlock) ClusterState(org.opensearch.cluster.ClusterState) ClusterName(org.opensearch.cluster.ClusterName) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) Index(org.opensearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString)

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