Search in sources :

Example 71 with TransportService

use of org.elasticsearch.transport.TransportService in project crate by crate.

the class IndexRecoveryIT method testDoNotInfinitelyWaitForMapping.

@Test
public void testDoNotInfinitelyWaitForMapping() {
    internalCluster().ensureAtLeastNumDataNodes(3);
    execute("CREATE ANALYZER test_analyzer (" + " TOKENIZER standard," + " TOKEN_FILTERS (test_token_filter)" + ")");
    execute("CREATE TABLE test (test_field TEXT INDEX USING FULLTEXT WITH (ANALYZER='test_analyzer'))" + " CLUSTERED INTO 1 SHARDS WITH (number_of_replicas = 0)");
    int numDocs = between(1, 10);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { Integer.toString(i) };
    }
    execute("INSERT INTO test (test_field) VALUES (?)", args);
    Semaphore recoveryBlocked = new Semaphore(1);
    for (DiscoveryNode node : clusterService().state().nodes()) {
        MockTransportService transportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node.getName());
        transportService.addSendBehavior((connection, requestId, action, request, options) -> {
            if (action.equals(PeerRecoverySourceService.Actions.START_RECOVERY)) {
                if (recoveryBlocked.tryAcquire()) {
                    PluginsService pluginService = internalCluster().getInstance(PluginsService.class, node.getName());
                    for (TestAnalysisPlugin plugin : pluginService.filterPlugins(TestAnalysisPlugin.class)) {
                        plugin.throwParsingError.set(true);
                    }
                }
            }
            connection.sendRequest(requestId, action, request, options);
        });
    }
    execute("ALTER TABLE test SET (number_of_replicas = 1)");
    ensureGreen();
    refresh();
    var searchResponse = execute("SELECT COUNT(*) FROM test");
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
}
Also used : PluginsService(org.elasticsearch.plugins.PluginsService) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 72 with TransportService

use of org.elasticsearch.transport.TransportService in project crate by crate.

the class IndexRecoveryIT method testRecoverLocallyUpToGlobalCheckpoint.

@Test
public void testRecoverLocallyUpToGlobalCheckpoint() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(2);
    List<String> nodes = randomSubsetOf(2, StreamSupport.stream(clusterService().state().nodes().getDataNodes().spliterator(), false).map(node -> node.value.getName()).collect(Collectors.toSet()));
    String indexName = "test";
    execute("CREATE TABLE doc.test (num INT)" + " CLUSTERED INTO 1 SHARDS" + " WITH (" + "  number_of_replicas = 1," + "  \"global_checkpoint_sync.interval\"='24h'," + "  \"routing.allocation.include._name\"='" + String.join(",", nodes) + "'" + " )");
    ensureGreen(indexName);
    int numDocs = randomIntBetween(1, 100);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { i };
    }
    execute("INSERT INTO doc.test (num) VALUES (?)", args);
    refresh(indexName);
    String failingNode = randomFrom(nodes);
    PlainActionFuture<StartRecoveryRequest> startRecoveryRequestFuture = new PlainActionFuture<>();
    // Peer recovery fails if the primary does not see the recovering replica in the replication group (when the cluster state
    // update on the primary is delayed). To verify the local recovery stats, we have to manually remember this value in the
    // first try because the local recovery happens once and its stats is reset when the recovery fails.
    SetOnce<Integer> localRecoveredOps = new SetOnce<>();
    for (String node : nodes) {
        MockTransportService transportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node);
        transportService.addSendBehavior((connection, requestId, action, request, options) -> {
            if (action.equals(PeerRecoverySourceService.Actions.START_RECOVERY)) {
                final RecoveryState recoveryState = internalCluster().getInstance(IndicesService.class, failingNode).getShardOrNull(new ShardId(resolveIndex(indexName), 0)).recoveryState();
                assertThat(recoveryState.getTranslog().recoveredOperations(), equalTo(recoveryState.getTranslog().totalLocal()));
                if (startRecoveryRequestFuture.isDone()) {
                    assertThat(recoveryState.getTranslog().totalLocal(), equalTo(0));
                    recoveryState.getTranslog().totalLocal(localRecoveredOps.get());
                    recoveryState.getTranslog().incrementRecoveredOperations(localRecoveredOps.get());
                } else {
                    localRecoveredOps.set(recoveryState.getTranslog().totalLocal());
                    startRecoveryRequestFuture.onResponse((StartRecoveryRequest) request);
                }
            }
            if (action.equals(PeerRecoveryTargetService.Actions.FILE_CHUNK)) {
                RetentionLeases retentionLeases = internalCluster().getInstance(IndicesService.class, node).indexServiceSafe(resolveIndex(indexName)).getShard(0).getRetentionLeases();
                throw new AssertionError("expect an operation-based recovery:" + "retention leases" + Strings.toString(retentionLeases) + "]");
            }
            connection.sendRequest(requestId, action, request, options);
        });
    }
    IndexShard shard = internalCluster().getInstance(IndicesService.class, failingNode).getShardOrNull(new ShardId(resolveIndex(indexName), 0));
    final long lastSyncedGlobalCheckpoint = shard.getLastSyncedGlobalCheckpoint();
    final long localCheckpointOfSafeCommit;
    try (Engine.IndexCommitRef safeCommitRef = shard.acquireSafeIndexCommit()) {
        localCheckpointOfSafeCommit = SequenceNumbers.loadSeqNoInfoFromLuceneCommit(safeCommitRef.getIndexCommit().getUserData().entrySet()).localCheckpoint;
    }
    final long maxSeqNo = shard.seqNoStats().getMaxSeqNo();
    shard.failShard("test", new IOException("simulated"));
    StartRecoveryRequest startRecoveryRequest = startRecoveryRequestFuture.actionGet();
    logger.info("--> start recovery request: starting seq_no {}, commit {}", startRecoveryRequest.startingSeqNo(), startRecoveryRequest.metadataSnapshot().getCommitUserData());
    SequenceNumbers.CommitInfo commitInfoAfterLocalRecovery = SequenceNumbers.loadSeqNoInfoFromLuceneCommit(startRecoveryRequest.metadataSnapshot().getCommitUserData().entrySet());
    assertThat(commitInfoAfterLocalRecovery.localCheckpoint, equalTo(lastSyncedGlobalCheckpoint));
    assertThat(commitInfoAfterLocalRecovery.maxSeqNo, equalTo(lastSyncedGlobalCheckpoint));
    assertThat(startRecoveryRequest.startingSeqNo(), equalTo(lastSyncedGlobalCheckpoint + 1));
    ensureGreen(indexName);
    assertThat((long) localRecoveredOps.get(), equalTo(lastSyncedGlobalCheckpoint - localCheckpointOfSafeCommit));
    for (var recoveryState : client().execute(RecoveryAction.INSTANCE, new RecoveryRequest()).get().shardRecoveryStates().get(indexName)) {
        if (startRecoveryRequest.targetNode().equals(recoveryState.getTargetNode())) {
            assertThat("expect an operation-based recovery", recoveryState.getIndex().fileDetails().values(), empty());
            assertThat("total recovered translog operations must include both local and remote recovery", recoveryState.getTranslog().recoveredOperations(), greaterThanOrEqualTo(Math.toIntExact(maxSeqNo - localCheckpointOfSafeCommit)));
        }
    }
    for (String node : nodes) {
        MockTransportService transportService = (MockTransportService) internalCluster().getInstance(TransportService.class, node);
        transportService.clearAllRules();
    }
}
Also used : MockTransportService(org.elasticsearch.test.transport.MockTransportService) ShardId(org.elasticsearch.index.shard.ShardId) RecoveryRequest(org.elasticsearch.action.admin.indices.recovery.RecoveryRequest) SequenceNumbers(org.elasticsearch.index.seqno.SequenceNumbers) Engine(org.elasticsearch.index.engine.Engine) SetOnce(org.apache.lucene.util.SetOnce) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) IOException(java.io.IOException) RetentionLeases(org.elasticsearch.index.seqno.RetentionLeases) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Test(org.junit.Test)

Example 73 with TransportService

use of org.elasticsearch.transport.TransportService in project crate by crate.

the class IndexRecoveryIT method testDisconnectsWhileRecovering.

@Test
public void testDisconnectsWhileRecovering() throws Exception {
    final String indexName = "test";
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), "100ms").put(RecoverySettings.INDICES_RECOVERY_INTERNAL_ACTION_TIMEOUT_SETTING.getKey(), "1s").put(NodeConnectionsService.CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.getKey(), "1s").build();
    // start a master node
    internalCluster().startNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    ClusterHealthResponse response = client().admin().cluster().prepareHealth().setWaitForNodes(">=3").get();
    assertThat(response.isTimedOut(), is(false));
    execute("CREATE TABLE doc." + indexName + " (id int) CLUSTERED INTO 1 SHARDS " + "WITH (" + " number_of_replicas=0," + " \"routing.allocation.include.color\" = 'blue'" + ")");
    int numDocs = scaledRandomIntBetween(25, 250);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { i };
    }
    execute("INSERT INTO doc." + indexName + " (id) VALUES (?)", args);
    ensureGreen();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final String blueNodeId = internalCluster().getInstance(ClusterService.class, blueNodeName).localNode().getId();
    assertFalse(stateResponse.getState().getRoutingNodes().node(blueNodeId).isEmpty());
    refresh();
    var searchResponse = execute("SELECT COUNT(*) FROM doc." + indexName);
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
    String[] recoveryActions = new String[] { PeerRecoverySourceService.Actions.START_RECOVERY, PeerRecoveryTargetService.Actions.FILES_INFO, PeerRecoveryTargetService.Actions.FILE_CHUNK, PeerRecoveryTargetService.Actions.CLEAN_FILES, // RecoveryTarget.Actions.TRANSLOG_OPS, <-- may not be sent if already flushed
    PeerRecoveryTargetService.Actions.PREPARE_TRANSLOG, PeerRecoveryTargetService.Actions.FINALIZE };
    final String recoveryActionToBlock = randomFrom(recoveryActions);
    final boolean dropRequests = randomBoolean();
    logger.info("--> will {} between blue & red on [{}]", dropRequests ? "drop requests" : "break connection", recoveryActionToBlock);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService redTransportService = internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService blueTransportService = internalCluster().getInstance(TransportService.class, blueNodeName);
    final CountDownLatch requestFailed = new CountDownLatch(1);
    if (randomBoolean()) {
        // Fail on the sending side
        blueMockTransportService.addSendBehavior(redTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, requestFailed));
        redMockTransportService.addSendBehavior(blueTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, requestFailed));
    } else {
        // Fail on the receiving side.
        blueMockTransportService.addRequestHandlingBehavior(recoveryActionToBlock, (handler, request, channel) -> {
            logger.info("--> preventing {} response by closing response channel", recoveryActionToBlock);
            requestFailed.countDown();
            redMockTransportService.disconnectFromNode(blueMockTransportService.getLocalNode());
            handler.messageReceived(request, channel);
        });
        redMockTransportService.addRequestHandlingBehavior(recoveryActionToBlock, (handler, request, channel) -> {
            logger.info("--> preventing {} response by closing response channel", recoveryActionToBlock);
            requestFailed.countDown();
            blueMockTransportService.disconnectFromNode(redMockTransportService.getLocalNode());
            handler.messageReceived(request, channel);
        });
    }
    logger.info("--> starting recovery from blue to red");
    execute("ALTER TABLE doc." + indexName + " SET (" + " number_of_replicas=1," + " \"routing.allocation.include.color\" = 'red,blue'" + ")");
    requestFailed.await();
    logger.info("--> clearing rules to allow recovery to proceed");
    blueMockTransportService.clearAllRules();
    redMockTransportService.clearAllRules();
    ensureGreen();
    // TODO: ES will use the shard routing preference here to prefer `_local` shards on that node
    var nodeRedExecutor = executor(redNodeName);
    searchResponse = nodeRedExecutor.exec("SELECT COUNT(*) FROM doc." + indexName);
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) CountDownLatch(java.util.concurrent.CountDownLatch) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) Test(org.junit.Test)

Example 74 with TransportService

use of org.elasticsearch.transport.TransportService in project crate by crate.

the class RetentionLeaseBackgroundSyncActionTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    threadPool = new TestThreadPool(getClass().getName());
    transport = new CapturingTransport();
    clusterService = createClusterService(threadPool);
    transportService = transport.createTransportService(clusterService.getSettings(), threadPool, boundAddress -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    shardStateAction = new ShardStateAction(clusterService, transportService, null, null);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) WriteStateException(org.elasticsearch.gateway.WriteStateException) ThreadPool(org.elasticsearch.threadpool.ThreadPool) LatchedActionListener(org.elasticsearch.action.LatchedActionListener) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOUtils(io.crate.common.io.IOUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TransportReplicationAction(org.elasticsearch.action.support.replication.TransportReplicationAction) Mockito.verify(org.mockito.Mockito.verify) CountDownLatch(java.util.concurrent.CountDownLatch) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ActionTestUtils(org.elasticsearch.action.support.ActionTestUtils) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool)

Example 75 with TransportService

use of org.elasticsearch.transport.TransportService in project crate by crate.

the class RetentionLeaseSyncActionTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    threadPool = new TestThreadPool(getClass().getName());
    transport = new CapturingTransport();
    clusterService = createClusterService(threadPool);
    transportService = transport.createTransportService(clusterService.getSettings(), threadPool, boundAddress -> clusterService.localNode(), null);
    transportService.start();
    transportService.acceptIncomingRequests();
    shardStateAction = new ShardStateAction(clusterService, transportService, null, null);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterServiceUtils.createClusterService(org.elasticsearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) Settings(org.elasticsearch.common.settings.Settings) WriteStateException(org.elasticsearch.gateway.WriteStateException) ThreadPool(org.elasticsearch.threadpool.ThreadPool) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) IOUtils(io.crate.common.io.IOUtils) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ActionTestUtils(org.elasticsearch.action.support.ActionTestUtils) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) ActionListener(org.elasticsearch.action.ActionListener) TransportWriteAction(org.elasticsearch.action.support.replication.TransportWriteAction) Mockito.mock(org.mockito.Mockito.mock) CapturingTransport(org.elasticsearch.test.transport.CapturingTransport) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool)

Aggregations

TransportService (org.elasticsearch.transport.TransportService)79 Settings (org.elasticsearch.common.settings.Settings)48 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)42 ThreadPool (org.elasticsearch.threadpool.ThreadPool)33 ClusterService (org.elasticsearch.cluster.service.ClusterService)30 ClusterState (org.elasticsearch.cluster.ClusterState)29 IOException (java.io.IOException)27 ESTestCase (org.elasticsearch.test.ESTestCase)27 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)24 Before (org.junit.Before)24 CapturingTransport (org.elasticsearch.test.transport.CapturingTransport)23 MockTransportService (org.elasticsearch.test.transport.MockTransportService)23 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 Collections (java.util.Collections)21 TimeUnit (java.util.concurrent.TimeUnit)21 HashSet (java.util.HashSet)20 ActionListener (org.elasticsearch.action.ActionListener)19 ArrayList (java.util.ArrayList)18