Search in sources :

Example 1 with ClusterBlock

use of org.elasticsearch.cluster.block.ClusterBlock in project elasticsearch by elastic.

the class TransportReplicationActionTests method testBlocks.

public void testBlocks() throws ExecutionException, InterruptedException {
    Request request = new Request();
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    ReplicationTask task = maybeTask();
    TestAction action = new TestAction(Settings.EMPTY, "testActionWithBlocks", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected ClusterBlockLevel globalBlockLevel() {
            return ClusterBlockLevel.WRITE;
        }
    };
    ClusterBlocks.Builder block = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(1, "non retryable", false, true, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL));
    setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block));
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    assertListenerThrows("primary phase should fail operation", listener, ClusterBlockException.class);
    assertPhase(task, "failed");
    block = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(1, "retryable", true, true, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL));
    setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block));
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(task, new Request().timeout("5ms"), listener);
    reroutePhase.run();
    assertListenerThrows("failed to timeout on retryable block", listener, ClusterBlockException.class);
    assertPhase(task, "failed");
    assertFalse(request.isRetrySet.get());
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(task, request = new Request(), listener);
    reroutePhase.run();
    assertFalse("primary phase should wait on retryable block", listener.isDone());
    assertPhase(task, "waiting_for_retry");
    assertTrue(request.isRetrySet.get());
    block = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(1, "non retryable", false, true, RestStatus.SERVICE_UNAVAILABLE, ClusterBlockLevel.ALL));
    setState(clusterService, ClusterState.builder(clusterService.state()).blocks(block));
    assertListenerThrows("primary phase should fail operation when moving from a retryable block to a non-retryable one", listener, ClusterBlockException.class);
    assertIndexShardUninitialized();
    action = new TestAction(Settings.EMPTY, "testActionWithNoBlocks", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected ClusterBlockLevel globalBlockLevel() {
            return null;
        }
    };
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(task, new Request().timeout("5ms"), listener);
    reroutePhase.run();
    assertListenerThrows("should fail with an IndexNotFoundException when no blocks checked", listener, IndexNotFoundException.class);
}
Also used : ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture)

Example 2 with ClusterBlock

use of org.elasticsearch.cluster.block.ClusterBlock in project elasticsearch by elastic.

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, 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.elasticsearch.action.ActionResponse) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) IndicesRequest(org.elasticsearch.action.IndicesRequest) TimeoutException(java.util.concurrent.TimeoutException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) TransportException(org.elasticsearch.transport.TransportException)

Example 3 with ClusterBlock

use of org.elasticsearch.cluster.block.ClusterBlock in project elasticsearch by elastic.

the class TransportMasterNodeActionTests method testLocalOperationWithBlocks.

public void testLocalOperationWithBlocks() throws ExecutionException, InterruptedException {
    final boolean retryableBlock = randomBoolean();
    final boolean unblockBeforeTimeout = randomBoolean();
    Request request = new Request().masterNodeTimeout(TimeValue.timeValueSeconds(unblockBeforeTimeout ? 60 : 0));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    ClusterBlock block = new ClusterBlock(1, "", retryableBlock, true, randomFrom(RestStatus.values()), ClusterBlockLevel.ALL);
    ClusterState stateWithBlock = ClusterState.builder(ClusterStateCreationUtils.state(localNode, localNode, allNodes)).blocks(ClusterBlocks.builder().addGlobalBlock(block)).build();
    setState(clusterService, stateWithBlock);
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected ClusterBlockException checkBlock(Request request, ClusterState state) {
            Set<ClusterBlock> blocks = state.blocks().global();
            return blocks.isEmpty() ? null : new ClusterBlockException(blocks);
        }
    }.execute(request, listener);
    if (retryableBlock && unblockBeforeTimeout) {
        assertFalse(listener.isDone());
        setState(clusterService, ClusterState.builder(ClusterStateCreationUtils.state(localNode, localNode, allNodes)).blocks(ClusterBlocks.EMPTY_CLUSTER_BLOCK).build());
        assertTrue(listener.isDone());
        listener.get();
        return;
    }
    assertTrue(listener.isDone());
    if (retryableBlock) {
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause(), instanceOf(MasterNotDiscoveredException.class));
            assertThat(ex.getCause().getCause(), instanceOf(ClusterBlockException.class));
        }
    } else {
        assertListenerThrows("ClusterBlockException should be thrown", listener, ClusterBlockException.class);
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterState(org.elasticsearch.cluster.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ExecutionException(java.util.concurrent.ExecutionException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException)

Example 4 with ClusterBlock

use of org.elasticsearch.cluster.block.ClusterBlock in project elasticsearch by elastic.

the class MainActionTests method testMainActionClusterAvailable.

public void testMainActionClusterAvailable() {
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterName clusterName = new ClusterName("elasticsearch");
    final Settings settings = Settings.builder().put("node.name", "my-node").build();
    final boolean available = randomBoolean();
    ClusterBlocks blocks;
    if (available) {
        if (randomBoolean()) {
            blocks = ClusterBlocks.EMPTY_CLUSTER_BLOCK;
        } else {
            blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 400", randomBoolean(), randomBoolean(), RestStatus.BAD_REQUEST, ClusterBlockLevel.ALL)).build();
        }
    } else {
        blocks = ClusterBlocks.builder().addGlobalBlock(new ClusterBlock(randomIntBetween(1, 16), "test global block 503", randomBoolean(), randomBoolean(), 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, null, null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null);
    TransportMainAction action = new TransportMainAction(settings, mock(ThreadPool.class), transportService, mock(ActionFilters.class), mock(IndexNameExpressionResolver.class), clusterService);
    AtomicReference<MainResponse> responseRef = new AtomicReference<>();
    action.doExecute(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());
    assertEquals(available, responseRef.get().isAvailable());
    verify(clusterService, times(1)).state();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ThreadPool(org.elasticsearch.threadpool.ThreadPool) AtomicReference(java.util.concurrent.atomic.AtomicReference) ActionFilters(org.elasticsearch.action.support.ActionFilters) IOException(java.io.IOException) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) ClusterService(org.elasticsearch.cluster.service.ClusterService) TransportService(org.elasticsearch.transport.TransportService) ClusterName(org.elasticsearch.cluster.ClusterName) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.elasticsearch.common.settings.Settings)

Example 5 with ClusterBlock

use of org.elasticsearch.cluster.block.ClusterBlock in project elasticsearch by elastic.

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.elasticsearch.cluster.block.ClusterBlock) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks)

Aggregations

ClusterBlock (org.elasticsearch.cluster.block.ClusterBlock)15 ClusterBlocks (org.elasticsearch.cluster.block.ClusterBlocks)11 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)7 ClusterState (org.elasticsearch.cluster.ClusterState)7 Set (java.util.Set)6 Settings (org.elasticsearch.common.settings.Settings)6 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)5 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)5 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)5 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)5 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)5 ExecutionException (java.util.concurrent.ExecutionException)4 ClusterBlockLevel (org.elasticsearch.cluster.block.ClusterBlockLevel)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4 TransportResponse (org.elasticsearch.transport.TransportResponse)4 IOException (java.io.IOException)3 EnumSet (java.util.EnumSet)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ActionListener (org.elasticsearch.action.ActionListener)3