Search in sources :

Example 6 with TransportService

use of org.opensearch.transport.TransportService in project OpenSearch by opensearch-project.

the class ShardIndexingPressureSettingsIT method testShardIndexingPressureRequestSizeWindowSettingUpdate.

public void testShardIndexingPressureRequestSizeWindowSettingUpdate() throws Exception {
    final BulkRequest bulkRequest = new BulkRequest();
    int totalRequestSize = 0;
    for (int i = 0; i < 80; ++i) {
        IndexRequest request = new IndexRequest(INDEX_NAME).id(UUIDs.base64UUID()).source(Collections.singletonMap("key", randomAlphaOfLength(50)));
        totalRequestSize += request.ramBytesUsed();
        assertTrue(request.ramBytesUsed() > request.source().length());
        bulkRequest.add(request);
    }
    final long bulkShardRequestSize = totalRequestSize + (RamUsageEstimator.shallowSizeOfInstance(BulkItemRequest.class) * 80) + RamUsageEstimator.shallowSizeOfInstance(BulkShardRequest.class);
    Settings settings = Settings.builder().put(ShardIndexingPressureSettings.SHARD_INDEXING_PRESSURE_ENABLED.getKey(), true).put(ShardIndexingPressureSettings.SHARD_INDEXING_PRESSURE_ENFORCED.getKey(), true).put(IndexingPressure.MAX_INDEXING_BYTES.getKey(), (long) (bulkShardRequestSize * 1.2) + "B").put(ShardIndexingPressureSettings.REQUEST_SIZE_WINDOW.getKey(), 1).put(ShardIndexingPressureMemoryManager.THROUGHPUT_DEGRADATION_LIMITS.getKey(), 1).build();
    restartCluster(settings);
    assertAcked(prepareCreate(INDEX_NAME, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)));
    ensureGreen(INDEX_NAME);
    Tuple<String, String> primaryReplicaNodeNames = getPrimaryReplicaNodeNames(INDEX_NAME);
    String primaryName = primaryReplicaNodeNames.v1();
    String replicaName = primaryReplicaNodeNames.v2();
    String coordinatingOnlyNode = getCoordinatingOnlyNode();
    IndexService indexService = internalCluster().getInstance(IndicesService.class, primaryName).iterator().next();
    Index index = indexService.getIndexSettings().getIndex();
    ShardId shardId = new ShardId(index, 0);
    boolean randomBoolean = randomBoolean();
    // Send first request which gets successful
    ActionFuture<BulkResponse> successFuture;
    if (randomBoolean) {
        successFuture = client(coordinatingOnlyNode).bulk(bulkRequest);
    } else {
        successFuture = client(primaryName).bulk(bulkRequest);
    }
    successFuture.actionGet();
    final CountDownLatch replicationSendPointReached = new CountDownLatch(1);
    final CountDownLatch latchBlockingReplicationSend = new CountDownLatch(1);
    TransportService primaryService = internalCluster().getInstance(TransportService.class, primaryName);
    final MockTransportService primaryTransportService = (MockTransportService) primaryService;
    TransportService replicaService = internalCluster().getInstance(TransportService.class, replicaName);
    final MockTransportService replicaTransportService = (MockTransportService) replicaService;
    primaryTransportService.addSendBehavior((connection, requestId, action, request, options) -> {
        if (action.equals(TransportShardBulkAction.ACTION_NAME + "[r]")) {
            try {
                replicationSendPointReached.countDown();
                latchBlockingReplicationSend.await();
            } catch (InterruptedException e) {
                throw new IllegalStateException(e);
            }
        }
        connection.sendRequest(requestId, action, request, options);
    });
    final ThreadPool replicaThreadPool = replicaTransportService.getThreadPool();
    final Releasable replicaRelease = blockReplicas(replicaThreadPool);
    // Send one more requests which remains outstanding for delayed time
    if (randomBoolean) {
        successFuture = client(coordinatingOnlyNode).bulk(bulkRequest);
    } else {
        successFuture = client(primaryName).bulk(bulkRequest);
    }
    // Delay to breach the success time stamp threshold
    Thread.sleep(3000);
    latchBlockingReplicationSend.countDown();
    replicaRelease.close();
    successFuture.actionGet();
    // This request breaches the threshold and hence will be rejected
    if (randomBoolean) {
        expectThrows(OpenSearchRejectedExecutionException.class, () -> client(coordinatingOnlyNode).bulk(bulkRequest).actionGet());
    } else {
        expectThrows(OpenSearchRejectedExecutionException.class, () -> client(primaryName).bulk(bulkRequest).actionGet());
    }
    // Update the outstanding threshold setting to see no rejections
    ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
    updateSettingsRequest.persistentSettings(Settings.builder().put(ShardIndexingPressureSettings.REQUEST_SIZE_WINDOW.getKey(), 10));
    assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
    // Verify no rejection with similar request pattern
    if (randomBoolean) {
        successFuture = client(coordinatingOnlyNode).bulk(bulkRequest);
        successFuture.actionGet();
        successFuture = client(coordinatingOnlyNode).bulk(bulkRequest);
        Thread.sleep(10);
        successFuture.actionGet();
        client(coordinatingOnlyNode).bulk(bulkRequest).actionGet();
        ShardIndexingPressureTracker coordinatingShardTracker = internalCluster().getInstance(IndexingPressureService.class, coordinatingOnlyNode).getShardIndexingPressure().getShardIndexingPressureTracker(shardId);
        assertEquals(1, coordinatingShardTracker.getCoordinatingOperationTracker().getRejectionTracker().getTotalRejections());
        assertEquals(1, coordinatingShardTracker.getCoordinatingOperationTracker().getRejectionTracker().getThroughputDegradationLimitsBreachedRejections());
    } else {
        successFuture = client(primaryName).bulk(bulkRequest);
        successFuture.actionGet();
        successFuture = client(primaryName).bulk(bulkRequest);
        Thread.sleep(10);
        successFuture.actionGet();
        client(primaryName).bulk(bulkRequest).actionGet();
        ShardIndexingPressureTracker primaryShardTracker = internalCluster().getInstance(IndexingPressureService.class, primaryName).getShardIndexingPressure().getShardIndexingPressureTracker(shardId);
        assertEquals(1, primaryShardTracker.getCoordinatingOperationTracker().getRejectionTracker().getTotalRejections());
        assertEquals(1, primaryShardTracker.getCoordinatingOperationTracker().getRejectionTracker().getThroughputDegradationLimitsBreachedRejections());
    }
}
Also used : BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) MockTransportService(org.opensearch.test.transport.MockTransportService) ThreadPool(org.opensearch.threadpool.ThreadPool) BulkResponse(org.opensearch.action.bulk.BulkResponse) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) ShardId(org.opensearch.index.shard.ShardId) MockTransportService(org.opensearch.test.transport.MockTransportService) TransportService(org.opensearch.transport.TransportService) BulkRequest(org.opensearch.action.bulk.BulkRequest) Releasable(org.opensearch.common.lease.Releasable) Settings(org.opensearch.common.settings.Settings)

Example 7 with TransportService

use of org.opensearch.transport.TransportService in project OpenSearch by opensearch-project.

the class ReplicaShardAllocatorIT method testDoNotCancelRecoveryForBrokenNode.

/**
 * Make sure that we do not repeatedly cancel an ongoing recovery for a noop copy on a broken node.
 */
public void testDoNotCancelRecoveryForBrokenNode() throws Exception {
    internalCluster().startMasterOnlyNode();
    String nodeWithPrimary = internalCluster().startDataOnlyNode();
    String indexName = "test";
    assertAcked(client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexService.GLOBAL_CHECKPOINT_SYNC_INTERVAL_SETTING.getKey(), "100ms").put(IndexService.RETENTION_LEASE_SYNC_INTERVAL_SETTING.getKey(), "100ms")));
    indexRandom(randomBoolean(), randomBoolean(), randomBoolean(), IntStream.range(0, between(200, 500)).mapToObj(n -> client().prepareIndex(indexName).setSource("f", "v")).collect(Collectors.toList()));
    client().admin().indices().prepareFlush(indexName).get();
    String brokenNode = internalCluster().startDataOnlyNode();
    MockTransportService transportService = (MockTransportService) internalCluster().getInstance(TransportService.class, nodeWithPrimary);
    CountDownLatch newNodeStarted = new CountDownLatch(1);
    transportService.addSendBehavior((connection, requestId, action, request, options) -> {
        if (action.equals(PeerRecoveryTargetService.Actions.TRANSLOG_OPS)) {
            if (brokenNode.equals(connection.getNode().getName())) {
                try {
                    newNodeStarted.await();
                } catch (InterruptedException e) {
                    throw new AssertionError(e);
                }
                throw new CircuitBreakingException("not enough memory for indexing", 100, 50, CircuitBreaker.Durability.TRANSIENT);
            }
        }
        connection.sendRequest(requestId, action, request, options);
    });
    assertAcked(client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)));
    internalCluster().startDataOnlyNode();
    newNodeStarted.countDown();
    ensureGreen(indexName);
    transportService.clearAllRules();
}
Also used : MockTransportService(org.opensearch.test.transport.MockTransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) TransportService(org.opensearch.transport.TransportService) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with TransportService

use of org.opensearch.transport.TransportService in project OpenSearch by opensearch-project.

the class SingleNodeDiscoveryIT method testCannotJoinNodeWithSingleNodeDiscovery.

public void testCannotJoinNodeWithSingleNodeDiscovery() throws Exception {
    Logger clusterLogger = LogManager.getLogger(JoinHelper.class);
    try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(clusterLogger)) {
        mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test", JoinHelper.class.getCanonicalName(), Level.INFO, "failed to join") {

            @Override
            public boolean innerMatch(final LogEvent event) {
                return event.getThrown() != null && event.getThrown().getClass() == RemoteTransportException.class && event.getThrown().getCause() != null && event.getThrown().getCause().getClass() == IllegalStateException.class && event.getThrown().getCause().getMessage().contains("cannot join node with [discovery.type] set to [single-node]");
            }
        });
        final TransportService service = internalCluster().getInstance(TransportService.class);
        final int port = service.boundAddress().publishAddress().getPort();
        final NodeConfigurationSource configurationSource = new NodeConfigurationSource() {

            @Override
            public Settings nodeSettings(int nodeOrdinal) {
                return Settings.builder().put("discovery.type", "zen").put("transport.type", getTestTransportType()).put(DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), "0s").put("transport.port", port + "-" + (port + 5 - 1)).build();
            }

            @Override
            public Path nodeConfigPath(int nodeOrdinal) {
                return null;
            }
        };
        try (InternalTestCluster other = new InternalTestCluster(randomLong(), createTempDir(), false, false, 1, 1, internalCluster().getClusterName(), configurationSource, 0, "other", Arrays.asList(getTestTransportPlugin(), MockHttpTransport.TestPlugin.class), Function.identity())) {
            other.beforeTest(random());
            final ClusterState first = internalCluster().getInstance(ClusterService.class).state();
            assertThat(first.nodes().getSize(), equalTo(1));
            assertBusy(() -> mockAppender.assertAllExpectationsMatched());
        }
    }
}
Also used : RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterState(org.opensearch.cluster.ClusterState) MockLogAppender(org.opensearch.test.MockLogAppender) LogEvent(org.apache.logging.log4j.core.LogEvent) InternalTestCluster(org.opensearch.test.InternalTestCluster) NodeConfigurationSource(org.opensearch.test.NodeConfigurationSource) Logger(org.apache.logging.log4j.Logger) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService)

Example 9 with TransportService

use of org.opensearch.transport.TransportService in project OpenSearch by opensearch-project.

the class TasksIT method waitForCompletionTestCase.

/**
 * Test wait for completion.
 * @param storeResult should the task store its results
 * @param wait start waiting for a task. Accepts that id of the task to wait for and returns a future waiting for it.
 * @param validator validate the response and return the task ids that were found
 */
private <T> void waitForCompletionTestCase(boolean storeResult, Function<TaskId, ActionFuture<T>> wait, Consumer<T> validator) throws Exception {
    // Start blocking test task
    TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
    request.setShouldStoreResult(storeResult);
    ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
    ActionFuture<T> waitResponseFuture;
    TaskId taskId;
    try {
        taskId = waitForTestTaskStartOnAllNodes();
        // Wait for the task to start
        assertBusy(() -> client().admin().cluster().prepareGetTask(taskId).get());
        // Register listeners so we can be sure the waiting started
        CountDownLatch waitForWaitingToStart = new CountDownLatch(1);
        for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
            ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {

                @Override
                public void waitForTaskCompletion(Task task) {
                    waitForWaitingToStart.countDown();
                }

                @Override
                public void onTaskRegistered(Task task) {
                }

                @Override
                public void onTaskUnregistered(Task task) {
                }
            });
        }
        // Spin up a request to wait for the test task to finish
        waitResponseFuture = wait.apply(taskId);
        /* Wait for the wait to start. This should count down just *before* we wait for completion but after the list/get has got a
             * reference to the running task. Because we unblock immediately after this the task may no longer be running for us to wait
             * on which is fine. */
        waitForWaitingToStart.await();
    } finally {
        // Unblock the request so the wait for completion request can finish
        new TestTaskPlugin.UnblockTestTasksRequestBuilder(client(), TestTaskPlugin.UnblockTestTasksAction.INSTANCE).get();
    }
    // Now that the task is unblocked the list response will come back
    T waitResponse = waitResponseFuture.get();
    validator.accept(waitResponse);
    TestTaskPlugin.NodesResponse response = future.get();
    assertEquals(emptyList(), response.failures());
}
Also used : Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) MockTaskManagerListener(org.opensearch.test.tasks.MockTaskManagerListener) CountDownLatch(java.util.concurrent.CountDownLatch) MockTaskManager(org.opensearch.test.tasks.MockTaskManager) SearchTransportService(org.opensearch.action.search.SearchTransportService) TransportService(org.opensearch.transport.TransportService) MockTransportService(org.opensearch.test.transport.MockTransportService)

Example 10 with TransportService

use of org.opensearch.transport.TransportService in project OpenSearch by opensearch-project.

the class CancellableTasksIT method testCancelOrphanedTasks.

public void testCancelOrphanedTasks() throws Exception {
    final String nodeWithRootTask = internalCluster().startDataOnlyNode();
    Set<DiscoveryNode> nodes = StreamSupport.stream(clusterService().state().nodes().spliterator(), false).collect(Collectors.toSet());
    TestRequest rootRequest = generateTestRequest(nodes, 0, between(1, 3));
    client(nodeWithRootTask).execute(TransportTestAction.ACTION, rootRequest);
    allowPartialRequest(rootRequest);
    try {
        internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeWithRootTask));
        assertBusy(() -> {
            for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
                for (CancellableTask task : transportService.getTaskManager().getCancellableTasks().values()) {
                    if (task.getAction().equals(TransportTestAction.ACTION.name())) {
                        final TaskInfo taskInfo = task.taskInfo(transportService.getLocalNode().getId(), false);
                        assertTrue(taskInfo.toString(), task.isCancelled());
                        assertNotNull(taskInfo.toString(), task.getReasonCancelled());
                        assertThat(taskInfo.toString(), task.getReasonCancelled(), equalTo("channel was closed"));
                    }
                }
            }
        }, 30, TimeUnit.SECONDS);
    } finally {
        allowEntireRequest(rootRequest);
        ensureAllBansRemoved();
    }
}
Also used : TaskInfo(org.opensearch.tasks.TaskInfo) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) CancellableTask(org.opensearch.tasks.CancellableTask) TransportService(org.opensearch.transport.TransportService) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

TransportService (org.opensearch.transport.TransportService)167 Settings (org.opensearch.common.settings.Settings)90 ClusterService (org.opensearch.cluster.service.ClusterService)86 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)83 ActionListener (org.opensearch.action.ActionListener)69 ActionFilters (org.opensearch.action.support.ActionFilters)69 ThreadPool (org.opensearch.threadpool.ThreadPool)68 IOException (java.io.IOException)48 Version (org.opensearch.Version)47 ClusterState (org.opensearch.cluster.ClusterState)47 ArrayList (java.util.ArrayList)45 Collections (java.util.Collections)41 Before (org.junit.Before)41 List (java.util.List)40 TimeValue (org.opensearch.common.unit.TimeValue)40 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)39 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)38 Map (java.util.Map)37 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)35