Search in sources :

Example 31 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class TransportMasterNodeActionTests method testMasterFailoverAfterStepDown.

public void testMasterFailoverAfterStepDown() throws ExecutionException, InterruptedException {
    Request request = new Request().masterNodeTimeout(TimeValue.timeValueHours(1));
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    final Response response = new Response();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected void masterOperation(Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
            // The other node has become master, simulate failures of this node while publishing cluster state through ZenDiscovery
            setState(clusterService, ClusterStateCreationUtils.state(localNode, remoteNode, allNodes));
            Exception failure = randomBoolean() ? new Discovery.FailedToCommitClusterStateException("Fake error") : new NotMasterException("Fake error");
            listener.onFailure(failure);
        }
    }.execute(request, listener);
    assertThat(transport.capturedRequests().length, equalTo(1));
    CapturingTransport.CapturedRequest capturedRequest = transport.capturedRequests()[0];
    assertTrue(capturedRequest.node.isMasterNode());
    assertThat(capturedRequest.request, equalTo(request));
    assertThat(capturedRequest.action, equalTo("testAction"));
    transport.handleResponse(capturedRequest.requestId, response);
    assertTrue(listener.isDone());
    assertThat(listener.get(), equalTo(response));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ElasticsearchException(org.elasticsearch.ElasticsearchException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException) ActionResponse(org.elasticsearch.action.ActionResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) NotMasterException(org.elasticsearch.cluster.NotMasterException)

Example 32 with ClusterState

use of org.elasticsearch.cluster.ClusterState 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 33 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class TransportMasterNodeActionTests method testLocalOperationWithoutBlocks.

public void testLocalOperationWithoutBlocks() throws ExecutionException, InterruptedException {
    final boolean masterOperationFailure = randomBoolean();
    Request request = new Request();
    PlainActionFuture<Response> listener = new PlainActionFuture<>();
    final Exception exception = new Exception();
    final Response response = new Response();
    setState(clusterService, ClusterStateCreationUtils.state(localNode, localNode, allNodes));
    new Action(Settings.EMPTY, "testAction", transportService, clusterService, threadPool) {

        @Override
        protected void masterOperation(Task task, Request request, ClusterState state, ActionListener<Response> listener) throws Exception {
            if (masterOperationFailure) {
                listener.onFailure(exception);
            } else {
                listener.onResponse(response);
            }
        }
    }.execute(request, listener);
    assertTrue(listener.isDone());
    if (masterOperationFailure) {
        try {
            listener.get();
            fail("Expected exception but returned proper result");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause(), equalTo(exception));
        }
    } else {
        assertThat(listener.get(), equalTo(response));
    }
}
Also used : ActionResponse(org.elasticsearch.action.ActionResponse) ClusterState(org.elasticsearch.cluster.ClusterState) Task(org.elasticsearch.tasks.Task) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ExecutionException(java.util.concurrent.ExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ExecutionException(java.util.concurrent.ExecutionException) ActionRequestValidationException(org.elasticsearch.action.ActionRequestValidationException)

Example 34 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class AutoCreateIndexTests method testAutoCreationConflictingPatternsFirstWins.

public void testAutoCreationConflictingPatternsFirstWins() {
    Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), "+test1,-test1,-test2,+test2").build();
    AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
    ClusterState clusterState = ClusterState.builder(new ClusterName("test")).metaData(MetaData.builder()).build();
    assertThat(autoCreateIndex.shouldAutoCreate("test1", clusterState), equalTo(true));
    expectForbidden(clusterState, autoCreateIndex, "test2", "-test2");
    expectNotMatch(clusterState, autoCreateIndex, "does_not_match" + randomAsciiOfLengthBetween(1, 5));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterName(org.elasticsearch.cluster.ClusterName) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 35 with ClusterState

use of org.elasticsearch.cluster.ClusterState in project elasticsearch by elastic.

the class UnassignedInfoTests method testDanglingIndexImported.

public void testDanglingIndexImported() {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(randomIntBetween(1, 3)).numberOfReplicas(randomIntBetween(0, 3))).build();
    ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(RoutingTable.builder().addAsFromDangling(metaData.index("test")).build()).build();
    for (ShardRouting shard : clusterState.getRoutingNodes().shardsWithState(UNASSIGNED)) {
        assertThat(shard.unassignedInfo().getReason(), equalTo(UnassignedInfo.Reason.DANGLING_INDEX_IMPORTED));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

ClusterState (org.elasticsearch.cluster.ClusterState)564 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)211 MetaData (org.elasticsearch.cluster.metadata.MetaData)179 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)150 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)123 Settings (org.elasticsearch.common.settings.Settings)100 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)85 ClusterName (org.elasticsearch.cluster.ClusterName)82 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)75 Matchers.containsString (org.hamcrest.Matchers.containsString)72 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)62 ShardId (org.elasticsearch.index.shard.ShardId)61 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)59 ArrayList (java.util.ArrayList)57 IOException (java.io.IOException)55 Index (org.elasticsearch.index.Index)53 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)49 CountDownLatch (java.util.concurrent.CountDownLatch)47 HashSet (java.util.HashSet)45 List (java.util.List)45