Search in sources :

Example 1 with NotMasterException

use of org.elasticsearch.cluster.NotMasterException 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 2 with NotMasterException

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

the class ZenDiscovery method joinElectedMaster.

/**
     * Join a newly elected master.
     *
     * @return true if successful
     */
private boolean joinElectedMaster(DiscoveryNode masterNode) {
    try {
        // first, make sure we can connect to the master
        transportService.connectToNode(masterNode);
    } catch (Exception e) {
        logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to connect to master [{}], retrying...", masterNode), e);
        return false;
    }
    // we retry on illegal state if the master is not yet ready
    int joinAttempt = 0;
    while (true) {
        try {
            logger.trace("joining master {}", masterNode);
            membership.sendJoinRequestBlocking(masterNode, clusterService.localNode(), joinTimeout);
            return true;
        } catch (Exception e) {
            final Throwable unwrap = ExceptionsHelper.unwrapCause(e);
            if (unwrap instanceof NotMasterException) {
                if (++joinAttempt == this.joinRetryAttempts) {
                    logger.info("failed to send join request to master [{}], reason [{}], tried [{}] times", masterNode, ExceptionsHelper.detailedMessage(e), joinAttempt);
                    return false;
                } else {
                    logger.trace("master {} failed with [{}]. retrying... (attempts done: [{}])", masterNode, ExceptionsHelper.detailedMessage(e), joinAttempt);
                }
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace((Supplier<?>) () -> new ParameterizedMessage("failed to send join request to master [{}]", masterNode), e);
                } else {
                    logger.info("failed to send join request to master [{}], reason [{}]", masterNode, ExceptionsHelper.detailedMessage(e));
                }
                return false;
            }
        }
        try {
            Thread.sleep(this.joinRetryDelay.millis());
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) NotMasterException(org.elasticsearch.cluster.NotMasterException) ElasticsearchException(org.elasticsearch.ElasticsearchException) TransportException(org.elasticsearch.transport.TransportException) NotMasterException(org.elasticsearch.cluster.NotMasterException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with NotMasterException

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

the class ShardStateActionTests method testMasterChannelException.

public void testMasterChannelException() throws InterruptedException {
    final String index = "test";
    setState(clusterService, ClusterStateCreationUtils.stateWithActivePrimary(index, true, randomInt(5)));
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger retries = new AtomicInteger();
    AtomicBoolean success = new AtomicBoolean();
    AtomicReference<Throwable> throwable = new AtomicReference<>();
    LongConsumer retryLoop = requestId -> {
        if (randomBoolean()) {
            transport.handleRemoteError(requestId, randomFrom(new NotMasterException("simulated"), new Discovery.FailedToCommitClusterStateException("simulated")));
        } else {
            if (randomBoolean()) {
                transport.handleLocalError(requestId, new NodeNotConnectedException(null, "simulated"));
            } else {
                transport.handleError(requestId, new NodeDisconnectedException(null, ShardStateAction.SHARD_FAILED_ACTION_NAME));
            }
        }
    };
    final int numberOfRetries = randomIntBetween(1, 256);
    setUpMasterRetryVerification(numberOfRetries, retries, latch, retryLoop);
    ShardRouting failedShard = getRandomShardRouting(index);
    shardStateAction.localShardFailed(failedShard, "test", getSimulatedFailure(), new ShardStateAction.Listener() {

        @Override
        public void onSuccess() {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onFailure(Exception e) {
            success.set(false);
            throwable.set(e);
            latch.countDown();
            assert false;
        }
    });
    final CapturingTransport.CapturedRequest[] capturedRequests = transport.getCapturedRequestsAndClear();
    assertThat(capturedRequests.length, equalTo(1));
    assertFalse(success.get());
    assertThat(retries.get(), equalTo(0));
    retryLoop.accept(capturedRequests[0].requestId);
    latch.await();
    assertNull(throwable.get());
    assertThat(retries.get(), equalTo(numberOfRetries));
    assertTrue(success.get());
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) BeforeClass(org.junit.BeforeClass) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) NotMasterException(org.elasticsearch.cluster.NotMasterException) ClusterService(org.elasticsearch.cluster.service.ClusterService) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportResponse(org.elasticsearch.transport.TransportResponse) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) ClusterStateObserver(org.elasticsearch.cluster.ClusterStateObserver) ClusterStateCreationUtils(org.elasticsearch.action.support.replication.ClusterStateCreationUtils) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) Before(org.junit.Before) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) AfterClass(org.junit.AfterClass) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Discovery(org.elasticsearch.discovery.Discovery) Predicate(java.util.function.Predicate) LongConsumer(java.util.function.LongConsumer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) RoutingService(org.elasticsearch.cluster.routing.RoutingService) Matchers.is(org.hamcrest.Matchers.is) TransportException(org.elasticsearch.transport.TransportException) ClusterServiceUtils.setState(org.elasticsearch.test.ClusterServiceUtils.setState) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) Discovery(org.elasticsearch.discovery.Discovery) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) NotMasterException(org.elasticsearch.cluster.NotMasterException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) TransportException(org.elasticsearch.transport.TransportException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(java.util.function.LongConsumer) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NotMasterException(org.elasticsearch.cluster.NotMasterException) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

NotMasterException (org.elasticsearch.cluster.NotMasterException)3 ExecutionException (java.util.concurrent.ExecutionException)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 CapturingTransport (org.elasticsearch.test.transport.CapturingTransport)2 TransportException (org.elasticsearch.transport.TransportException)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 LongConsumer (java.util.function.LongConsumer)1 Predicate (java.util.function.Predicate)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)1 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)1 ActionResponse (org.elasticsearch.action.ActionResponse)1 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)1