Search in sources :

Example 81 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project crate by crate.

the class IndexShardTestCase method recoverUnstartedReplica.

/**
 * Recovers a replica from the give primary, allow the user to supply a custom recovery target. A typical usage of a custom recovery
 * target is to assert things in the various stages of recovery.
 *
 * Note: this method keeps the shard in {@link IndexShardState#POST_RECOVERY} and doesn't start it.
 *
 * @param replica                the recovery target shard
 * @param primary                the recovery source shard
 * @param targetSupplier         supplies an instance of {@link RecoveryTarget}
 * @param markAsRecovering       set to {@code false} if the replica is marked as recovering
 */
protected final void recoverUnstartedReplica(final IndexShard replica, final IndexShard primary, final BiFunction<IndexShard, DiscoveryNode, RecoveryTarget> targetSupplier, final boolean markAsRecovering, final Set<String> inSyncIds, final IndexShardRoutingTable routingTable) throws IOException {
    final DiscoveryNode pNode = getFakeDiscoNode(primary.routingEntry().currentNodeId());
    final DiscoveryNode rNode = getFakeDiscoNode(replica.routingEntry().currentNodeId());
    if (markAsRecovering) {
        replica.markAsRecovering("remote", new RecoveryState(replica.routingEntry(), pNode, rNode));
    } else {
        assertEquals(replica.state(), IndexShardState.RECOVERING);
    }
    replica.prepareForIndexRecovery();
    final RecoveryTarget recoveryTarget = targetSupplier.apply(replica, pNode);
    final long startingSeqNo = recoveryTarget.indexShard().recoverLocallyUpToGlobalCheckpoint();
    final StartRecoveryRequest request = PeerRecoveryTargetService.getStartRecoveryRequest(logger, rNode, recoveryTarget, startingSeqNo);
    int fileChunkSizeInBytes = Math.toIntExact(randomBoolean() ? RecoverySettings.DEFAULT_CHUNK_SIZE.getBytes() : randomIntBetween(1, 10 * 1024 * 1024));
    final RecoverySourceHandler recovery = new RecoverySourceHandler(primary, new AsyncRecoveryTarget(recoveryTarget, threadPool.generic()), threadPool, request, fileChunkSizeInBytes, between(1, 8), between(1, 8));
    primary.updateShardState(primary.routingEntry(), primary.getPendingPrimaryTerm(), null, currentClusterStateVersion.incrementAndGet(), inSyncIds, routingTable);
    try {
        PlainActionFuture<RecoveryResponse> future = new PlainActionFuture<>();
        recovery.recoverToTarget(future);
        future.actionGet();
        recoveryTarget.markAsDone();
    } catch (Exception e) {
        recoveryTarget.fail(new RecoveryFailedException(request, e), false);
        throw e;
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) StartRecoveryRequest(org.elasticsearch.indices.recovery.StartRecoveryRequest) AsyncRecoveryTarget(org.elasticsearch.indices.recovery.AsyncRecoveryTarget) AsyncRecoveryTarget(org.elasticsearch.indices.recovery.AsyncRecoveryTarget) RecoveryTarget(org.elasticsearch.indices.recovery.RecoveryTarget) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) RecoveryFailedException(org.elasticsearch.indices.recovery.RecoveryFailedException) IOException(java.io.IOException) RecoveryResponse(org.elasticsearch.indices.recovery.RecoveryResponse) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) RecoverySourceHandler(org.elasticsearch.indices.recovery.RecoverySourceHandler) RecoveryFailedException(org.elasticsearch.indices.recovery.RecoveryFailedException) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 82 with PlainActionFuture

use of org.elasticsearch.action.support.PlainActionFuture in project crate by crate.

the class TcpTransportTests method testExceptionHandling.

private void testExceptionHandling(boolean startTransport, Exception exception, boolean expectClosed, MockLogAppender.LoggingExpectation... expectations) throws IllegalAccessException {
    final TestThreadPool testThreadPool = new TestThreadPool("test");
    MockLogAppender appender = new MockLogAppender();
    try {
        appender.start();
        Loggers.addAppender(LogManager.getLogger(TcpTransport.class), appender);
        for (MockLogAppender.LoggingExpectation expectation : expectations) {
            appender.addExpectation(expectation);
        }
        final Lifecycle lifecycle = new Lifecycle();
        if (startTransport) {
            lifecycle.moveToStarted();
        }
        final FakeTcpChannel channel = new FakeTcpChannel();
        final PlainActionFuture<Void> listener = new PlainActionFuture<>();
        channel.addCloseListener(listener);
        var logger = Loggers.getLogger(TcpTransport.class);
        TcpTransport.handleException(logger, channel, exception, lifecycle, new OutboundHandler(randomAlphaOfLength(10), Version.CURRENT, new StatsTracker(), testThreadPool, BigArrays.NON_RECYCLING_INSTANCE));
        if (expectClosed) {
            assertTrue(listener.isDone());
            assertThat(listener.actionGet(), nullValue());
        } else {
            assertFalse(listener.isDone());
        }
        appender.assertAllExpectationsMatched();
    } finally {
        Loggers.removeAppender(LogManager.getLogger(TcpTransport.class), appender);
        appender.stop();
        ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
    }
}
Also used : MockLogAppender(org.elasticsearch.test.MockLogAppender) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) Lifecycle(org.elasticsearch.common.component.Lifecycle) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool)

Aggregations

PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)82 ShardId (org.elasticsearch.index.shard.ShardId)37 ClusterState (org.elasticsearch.cluster.ClusterState)28 ExecutionException (java.util.concurrent.ExecutionException)27 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)25 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)21 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)18 ElasticsearchException (org.elasticsearch.ElasticsearchException)17 TransportRequest (org.elasticsearch.transport.TransportRequest)17 Matchers.anyString (org.mockito.Matchers.anyString)17 HashSet (java.util.HashSet)16 List (java.util.List)16 CloseIndexRequest (org.elasticsearch.action.admin.indices.close.CloseIndexRequest)16 IndexShard (org.elasticsearch.index.shard.IndexShard)16 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)15 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)13