Search in sources :

Example 31 with TransportService

use of org.opensearch.transport.TransportService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testCreateAndGetAndDelete.

public void testCreateAndGetAndDelete() throws IOException, InterruptedException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    TransportService transportService = getInstanceFromNode(TransportService.class);
    AsynchronousSearchResponse asResponse = submitAndGetPersistedAsynchronousSearchResponse();
    AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUIDs.base64UUID(), randomInt(100));
    AsynchronousSearchId newAsynchronousSearchId = new AsynchronousSearchId(transportService.getLocalNode().getId(), 1, asContextId);
    String id = AsynchronousSearchIdConverter.buildAsyncId(newAsynchronousSearchId);
    User user1 = TestClientUtils.randomUser();
    User user2 = TestClientUtils.randomUser();
    for (User user : Arrays.asList(user1, null)) {
        AsynchronousSearchResponse newAsynchronousSearchResponse = new AsynchronousSearchResponse(id, AsynchronousSearchState.STORE_RESIDENT, asResponse.getStartTimeMillis(), asResponse.getExpirationTimeMillis(), asResponse.getSearchResponse(), asResponse.getError());
        createDoc(persistenceService, newAsynchronousSearchResponse, user);
        if (user != null) {
            CountDownLatch getLatch1 = new CountDownLatch(1);
            ActionListener<AsynchronousSearchPersistenceModel> getListener = ActionListener.wrap(r -> fail("Expected exception. Got " + r), e -> assertTrue(e instanceof OpenSearchSecurityException));
            persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user2, new LatchedActionListener<>(getListener, getLatch1));
            getLatch1.await();
        }
        CountDownLatch getLatch2 = new CountDownLatch(1);
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user, new LatchedActionListener<>(ActionListener.wrap(r -> assertEquals(new AsynchronousSearchPersistenceModel(asResponse.getStartTimeMillis(), asResponse.getExpirationTimeMillis(), asResponse.getSearchResponse(), null, user), r), e -> {
            logger.error("Expected get result got ", e);
            fail(e.getMessage());
        }), getLatch2));
        getLatch2.await();
        if (user != null) {
            CountDownLatch deleteLatch1 = new CountDownLatch(1);
            User diffUser = TestClientUtils.randomUser();
            ActionListener<Boolean> deleteListener = ActionListener.wrap(r -> fail("Expected exception on delete. Got acknowledgment" + r), e -> assertTrue(e instanceof OpenSearchSecurityException));
            persistenceService.deleteResponse(newAsynchronousSearchResponse.getId(), diffUser, new LatchedActionListener<>(deleteListener, deleteLatch1));
            deleteLatch1.await();
        }
        CountDownLatch deleteLatch2 = new CountDownLatch(1);
        ActionListener<Boolean> deleteListener = ActionListener.wrap(Assert::assertTrue, e -> {
            logger.debug(() -> new ParameterizedMessage("Delete failed unexpectedly "), e);
            fail("delete failed.expected success");
        });
        persistenceService.deleteResponse(newAsynchronousSearchResponse.getId(), user, new LatchedActionListener<>(deleteListener, deleteLatch2));
        deleteLatch2.await();
        // assert failure
        CountDownLatch getLatch3 = new CountDownLatch(2);
        ActionListener<AsynchronousSearchPersistenceModel> getListener = ActionListener.wrap((r) -> fail("Expected RNF, Got " + r), exception -> assertTrue(exception instanceof ResourceNotFoundException));
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), null, new LatchedActionListener<>(getListener, getLatch3));
        persistenceService.getResponse(newAsynchronousSearchResponse.getId(), user2, new LatchedActionListener<>(getListener, getLatch3));
        getLatch3.await();
    }
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) User(org.opensearch.commons.authuser.User) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) CountDownLatch(java.util.concurrent.CountDownLatch) Assert(org.junit.Assert) TransportService(org.opensearch.transport.TransportService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel)

Example 32 with TransportService

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

the class NodeJoinTests method setupMasterServiceAndCoordinator.

private void setupMasterServiceAndCoordinator(long term, ClusterState initialState, MasterService masterService, ThreadPool threadPool, Random random, NodeHealthService nodeHealthService) {
    if (this.masterService != null || coordinator != null) {
        throw new IllegalStateException("method setupMasterServiceAndCoordinator can only be called once");
    }
    this.masterService = masterService;
    CapturingTransport capturingTransport = new CapturingTransport() {

        @Override
        protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode destination) {
            if (action.equals(HANDSHAKE_ACTION_NAME)) {
                handleResponse(requestId, new TransportService.HandshakeResponse(destination, initialState.getClusterName(), destination.getVersion()));
            } else if (action.equals(JoinHelper.VALIDATE_JOIN_ACTION_NAME)) {
                handleResponse(requestId, new TransportResponse.Empty());
            } else {
                super.onSendRequest(requestId, action, request, destination);
            }
        }
    };
    final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    TransportService transportService = capturingTransport.createTransportService(Settings.EMPTY, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> initialState.nodes().getLocalNode(), clusterSettings, Collections.emptySet());
    coordinator = new Coordinator("test_node", Settings.EMPTY, clusterSettings, transportService, writableRegistry(), OpenSearchAllocationTestCase.createAllocationService(Settings.EMPTY), masterService, () -> new InMemoryPersistedState(term, initialState), r -> emptyList(), new NoOpClusterApplier(), Collections.emptyList(), random, (s, p, r) -> {
    }, ElectionStrategy.DEFAULT_INSTANCE, nodeHealthService);
    transportService.start();
    transportService.acceptIncomingRequests();
    transport = capturingTransport;
    coordinator.start();
    coordinator.startInitialJoin();
}
Also used : Metadata(org.opensearch.cluster.metadata.Metadata) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HANDSHAKE_ACTION_NAME(org.opensearch.transport.TransportService.HANDSHAKE_ACTION_NAME) Version(org.opensearch.Version) Random(java.util.Random) FutureUtils(org.opensearch.common.util.concurrent.FutureUtils) TestTransportChannel(org.opensearch.transport.TestTransportChannel) Collections.singletonList(java.util.Collections.singletonList) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Collections.singleton(java.util.Collections.singleton) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) After(org.junit.After) ActionListener(org.opensearch.action.ActionListener) AfterClass(org.junit.AfterClass) CyclicBarrier(java.util.concurrent.CyclicBarrier) MasterService(org.opensearch.cluster.service.MasterService) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) NodeHealthService(org.opensearch.monitor.NodeHealthService) Set(java.util.Set) HEALTHY(org.opensearch.monitor.StatusInfo.Status.HEALTHY) Settings(org.opensearch.common.settings.Settings) TransportResponse(org.opensearch.transport.TransportResponse) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) FakeThreadPoolMasterService(org.opensearch.cluster.service.FakeThreadPoolMasterService) List(java.util.List) Stream(java.util.stream.Stream) Randomness(org.opensearch.common.Randomness) Matchers.equalTo(org.hamcrest.Matchers.equalTo) StatusInfo(org.opensearch.monitor.StatusInfo) Optional(java.util.Optional) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) Matchers.containsString(org.hamcrest.Matchers.containsString) IntStream(java.util.stream.IntStream) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BeforeClass(org.junit.BeforeClass) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Node(org.opensearch.node.Node) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) MasterServiceTests(org.opensearch.cluster.service.MasterServiceTests) ClusterState(org.opensearch.cluster.ClusterState) RequestHandlerRegistry(org.opensearch.transport.RequestHandlerRegistry) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) TransportRequest(org.opensearch.transport.TransportRequest) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) BaseFuture(org.opensearch.common.util.concurrent.BaseFuture) TimeUnit(java.util.concurrent.TimeUnit) ClusterName(org.opensearch.cluster.ClusterName) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TransportRequest(org.opensearch.transport.TransportRequest) ClusterSettings(org.opensearch.common.settings.ClusterSettings) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportService(org.opensearch.transport.TransportService)

Example 33 with TransportService

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

the class JoinTaskExecutorTests method testUpdatesNodeWithOpenSearchVersionForExistingAndNewNodes.

public void testUpdatesNodeWithOpenSearchVersionForExistingAndNewNodes() throws Exception {
    // During the upgrade from Elasticsearch, OpenSearch node send their version as 7.10.2 to Elasticsearch master
    // in order to successfully join the cluster. But as soon as OpenSearch node becomes the master, cluster state
    // should show the OpenSearch nodes version as 1.x. As the cluster state was carry forwarded from ES master,
    // version in DiscoveryNode is stale 7.10.2.
    final AllocationService allocationService = mock(AllocationService.class);
    when(allocationService.adaptAutoExpandReplicas(any())).then(invocationOnMock -> invocationOnMock.getArguments()[0]);
    when(allocationService.disassociateDeadNodes(any(), anyBoolean(), any())).then(invocationOnMock -> invocationOnMock.getArguments()[0]);
    final RerouteService rerouteService = (reason, priority, listener) -> listener.onResponse(null);
    Map<String, Version> channelVersions = new HashMap<>();
    // OpenSearch node running BWC version
    String node_1 = UUIDs.base64UUID();
    // OpenSearch node running BWC version
    String node_2 = UUIDs.base64UUID();
    // OpenSearch node running BWC version, sending new join request and no active channel
    String node_3 = UUIDs.base64UUID();
    // ES node 7.10.2
    String node_4 = UUIDs.base64UUID();
    // ES node 7.10.2 in cluster but missing channel version
    String node_5 = UUIDs.base64UUID();
    // ES node 7.9.0
    String node_6 = UUIDs.base64UUID();
    // ES node 7.9.0 in cluster but missing channel version
    String node_7 = UUIDs.base64UUID();
    channelVersions.put(node_1, Version.CURRENT);
    channelVersions.put(node_2, Version.CURRENT);
    channelVersions.put(node_4, LegacyESVersion.V_7_10_2);
    channelVersions.put(node_6, LegacyESVersion.V_7_10_0);
    final TransportService transportService = mock(TransportService.class);
    when(transportService.getChannelVersion(any())).thenReturn(channelVersions);
    DiscoveryNodes.Builder nodes = new DiscoveryNodes.Builder().localNodeId(node_1);
    nodes.add(new DiscoveryNode(node_1, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_2, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_3, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_4, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_5, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_2));
    nodes.add(new DiscoveryNode(node_6, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_1));
    nodes.add(new DiscoveryNode(node_7, buildNewFakeTransportAddress(), LegacyESVersion.V_7_10_0));
    final ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).nodes(nodes).build();
    final JoinTaskExecutor joinTaskExecutor = new JoinTaskExecutor(Settings.EMPTY, allocationService, logger, rerouteService, transportService);
    final DiscoveryNode existing_node_3 = clusterState.nodes().get(node_3);
    final DiscoveryNode node_3_new_join = new DiscoveryNode(existing_node_3.getName(), existing_node_3.getId(), existing_node_3.getEphemeralId(), existing_node_3.getHostName(), existing_node_3.getHostAddress(), existing_node_3.getAddress(), existing_node_3.getAttributes(), existing_node_3.getRoles(), Version.CURRENT);
    final ClusterStateTaskExecutor.ClusterTasksResult<JoinTaskExecutor.Task> result = joinTaskExecutor.execute(clusterState, List.of(new JoinTaskExecutor.Task(node_3_new_join, "test"), JoinTaskExecutor.newBecomeMasterTask(), JoinTaskExecutor.newFinishElectionTask()));
    final ClusterStateTaskExecutor.TaskResult taskResult = result.executionResults.values().iterator().next();
    assertTrue(taskResult.isSuccess());
    DiscoveryNodes resultNodes = result.resultingState.getNodes();
    assertEquals(Version.CURRENT, resultNodes.get(node_1).getVersion());
    assertEquals(Version.CURRENT, resultNodes.get(node_2).getVersion());
    // 7.10.2 in old state but sent new join and processed
    assertEquals(Version.CURRENT, resultNodes.get(node_3).getVersion());
    assertEquals(LegacyESVersion.V_7_10_2, resultNodes.get(node_4).getVersion());
    // 7.10.2 node without active channel will be removed and should rejoin
    assertFalse(resultNodes.nodeExists(node_5));
    assertEquals(LegacyESVersion.V_7_10_0, resultNodes.get(node_6).getVersion());
    // 7.9.0 node without active channel but shouldn't get removed
    assertEquals(LegacyESVersion.V_7_10_0, resultNodes.get(node_7).getVersion());
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) VersionUtils.maxCompatibleVersion(org.opensearch.test.VersionUtils.maxCompatibleVersion) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) HashMap(java.util.HashMap) VersionUtils.randomVersionBetween(org.opensearch.test.VersionUtils.randomVersionBetween) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) Map(java.util.Map) RerouteService(org.opensearch.cluster.routing.RerouteService) Matchers.hasSize(org.hamcrest.Matchers.hasSize) UUIDs(org.opensearch.common.UUIDs) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) VersionUtils.randomVersion(org.opensearch.test.VersionUtils.randomVersion) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) TransportService(org.opensearch.transport.TransportService) List(org.opensearch.common.collect.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) ClusterName(org.opensearch.cluster.ClusterName) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) VersionUtils.maxCompatibleVersion(org.opensearch.test.VersionUtils.maxCompatibleVersion) VersionUtils.randomCompatibleVersion(org.opensearch.test.VersionUtils.randomCompatibleVersion) Version(org.opensearch.Version) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils.randomVersion(org.opensearch.test.VersionUtils.randomVersion) TransportService(org.opensearch.transport.TransportService) ClusterStateTaskExecutor(org.opensearch.cluster.ClusterStateTaskExecutor) RerouteService(org.opensearch.cluster.routing.RerouteService) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 34 with TransportService

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

the class DedicatedClusterSnapshotRestoreIT method testAbortWaitsOnDataNode.

public void testAbortWaitsOnDataNode() throws Exception {
    internalCluster().startClusterManagerOnlyNode();
    final String dataNodeName = internalCluster().startDataOnlyNode();
    final String indexName = "test-index";
    createIndex(indexName);
    index(indexName, "_doc", "some_id", "foo", "bar");
    final String otherDataNode = internalCluster().startDataOnlyNode();
    final String repoName = "test-repo";
    createRepository(repoName, "mock");
    blockAllDataNodes(repoName);
    final String snapshotName = "test-snap";
    final ActionFuture<CreateSnapshotResponse> snapshotResponse = startFullSnapshot(repoName, snapshotName);
    waitForBlock(dataNodeName, repoName, TimeValue.timeValueSeconds(30L));
    final AtomicBoolean blocked = new AtomicBoolean(true);
    final TransportService transportService = internalCluster().getInstance(TransportService.class, otherDataNode);
    transportService.addMessageListener(new TransportMessageListener() {

        @Override
        public void onRequestSent(DiscoveryNode node, long requestId, String action, TransportRequest request, TransportRequestOptions finalOptions) {
            if (blocked.get() && action.equals(SnapshotsService.UPDATE_SNAPSHOT_STATUS_ACTION_NAME)) {
                throw new AssertionError("Node had no assigned shard snapshots so it shouldn't send out shard state updates");
            }
        }
    });
    logger.info("--> abort snapshot");
    final ActionFuture<AcknowledgedResponse> deleteResponse = startDeleteSnapshot(repoName, snapshotName);
    awaitClusterState(otherDataNode, state -> state.custom(SnapshotsInProgress.TYPE, SnapshotsInProgress.EMPTY).entries().stream().anyMatch(entry -> entry.state() == SnapshotsInProgress.State.ABORTED));
    assertFalse("delete should not be able to finish until data node is unblocked", deleteResponse.isDone());
    blocked.set(false);
    unblockAllDataNodes(repoName);
    assertAcked(deleteResponse.get());
    assertThat(snapshotResponse.get().getSnapshotInfo().state(), is(SnapshotState.FAILED));
}
Also used : RepositoryMissingException(org.opensearch.repositories.RepositoryMissingException) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) CheckedFunction(org.opensearch.common.CheckedFunction) Matchers.not(org.hamcrest.Matchers.not) ClusterScope(org.opensearch.test.OpenSearchIntegTestCase.ClusterScope) Version(org.opensearch.Version) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) Strings(org.opensearch.common.Strings) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) RecoveryState(org.opensearch.indices.recovery.RecoveryState) SnapshotStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers.allOf(org.hamcrest.Matchers.allOf) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Settings(org.opensearch.common.settings.Settings) TestCustomMetadata(org.opensearch.test.TestCustomMetadata) Scope(org.opensearch.test.OpenSearchIntegTestCase.Scope) TransportService(org.opensearch.transport.TransportService) RetentionLeaseActions(org.opensearch.index.seqno.RetentionLeaseActions) UncheckedIOException(java.io.UncheckedIOException) FileVisitResult(java.nio.file.FileVisitResult) CountDownLatch(java.util.concurrent.CountDownLatch) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Priority(org.opensearch.common.Priority) Node(org.opensearch.node.Node) ParseField(org.opensearch.common.ParseField) Writeable(org.opensearch.common.io.stream.Writeable) MockTransportService(org.opensearch.test.transport.MockTransportService) ArrayList(java.util.ArrayList) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) ClusterState(org.opensearch.cluster.ClusterState) BusyMasterServiceDisruption(org.opensearch.test.disruption.BusyMasterServiceDisruption) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.hasSize(org.hamcrest.Matchers.hasSize) AbstractRestChannel(org.opensearch.rest.AbstractRestChannel) Environment(org.opensearch.env.Environment) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Files(java.nio.file.Files) RETAIN_ALL(org.opensearch.index.seqno.RetentionLeaseActions.RETAIN_ALL) IOException(java.io.IOException) TransportMessageListener(org.opensearch.transport.TransportMessageListener) Plugin(org.opensearch.plugins.Plugin) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) RetentionLeases(org.opensearch.index.seqno.RetentionLeases) ClusterService(org.opensearch.cluster.service.ClusterService) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) BlobStoreRepository(org.opensearch.repositories.blobstore.BlobStoreRepository) NodeRoles.nonMasterNode(org.opensearch.test.NodeRoles.nonMasterNode) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) XContentParser(org.opensearch.common.xcontent.XContentParser) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Locale(java.util.Locale) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Collection(java.util.Collection) RestStatus(org.opensearch.rest.RestStatus) ServiceDisruptionScheme(org.opensearch.test.disruption.ServiceDisruptionScheme) MockRepository(org.opensearch.snapshots.mockstore.MockRepository) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SnapshotStats(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStats) RestGetRepositoriesAction(org.opensearch.rest.action.admin.cluster.RestGetRepositoriesAction) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTestCluster(org.opensearch.test.InternalTestCluster) AtomicReference(java.util.concurrent.atomic.AtomicReference) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) PeerRecoveryTargetService(org.opensearch.indices.recovery.PeerRecoveryTargetService) StreamInput(org.opensearch.common.io.stream.StreamInput) SettingsFilter(org.opensearch.common.settings.SettingsFilter) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Setting(org.opensearch.common.settings.Setting) TransportRequest(org.opensearch.transport.TransportRequest) RestRequest(org.opensearch.rest.RestRequest) IntHashSet(com.carrotsearch.hppc.IntHashSet) IntSet(com.carrotsearch.hppc.IntSet) RestClusterStateAction(org.opensearch.rest.action.admin.cluster.RestClusterStateAction) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) OpenSearchAssertions.assertFutureThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertFutureThrows) RestResponse(org.opensearch.rest.RestResponse) ActionFuture(org.opensearch.action.ActionFuture) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Sets(org.opensearch.common.util.set.Sets) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) NamedDiff(org.opensearch.cluster.NamedDiff) Collections(java.util.Collections) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TransportRequest(org.opensearch.transport.TransportRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) TransportService(org.opensearch.transport.TransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportMessageListener(org.opensearch.transport.TransportMessageListener)

Example 35 with TransportService

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

the class SeedHostsResolver method resolveHostsLists.

/**
 * Resolves a list of hosts to a list of transport addresses. Each host is resolved into a transport address (or a collection of
 * addresses if the number of ports is greater than one). Host lookups are done in parallel using specified executor service up
 * to the specified resolve timeout.
 *
 * @param executorService  the executor service used to parallelize hostname lookups
 * @param logger           logger used for logging messages regarding hostname lookups
 * @param hosts            the hosts to resolve
 * @param transportService the transport service
 * @param resolveTimeout   the timeout before returning from hostname lookups
 * @return a list of resolved transport addresses
 */
public static List<TransportAddress> resolveHostsLists(final CancellableThreads cancellableThreads, final ExecutorService executorService, final Logger logger, final List<String> hosts, final TransportService transportService, final TimeValue resolveTimeout) {
    Objects.requireNonNull(executorService);
    Objects.requireNonNull(logger);
    Objects.requireNonNull(hosts);
    Objects.requireNonNull(transportService);
    Objects.requireNonNull(resolveTimeout);
    if (resolveTimeout.nanos() < 0) {
        throw new IllegalArgumentException("resolve timeout must be non-negative but was [" + resolveTimeout + "]");
    }
    // create tasks to submit to the executor service; we will wait up to resolveTimeout for these tasks to complete
    final List<Callable<TransportAddress[]>> callables = hosts.stream().map(hn -> (Callable<TransportAddress[]>) () -> transportService.addressesFromString(hn)).collect(Collectors.toList());
    final SetOnce<List<Future<TransportAddress[]>>> futures = new SetOnce<>();
    try {
        cancellableThreads.execute(() -> futures.set(executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS)));
    } catch (CancellableThreads.ExecutionCancelledException e) {
        return Collections.emptyList();
    }
    final List<TransportAddress> transportAddresses = new ArrayList<>();
    final Set<TransportAddress> localAddresses = new HashSet<>();
    localAddresses.add(transportService.boundAddress().publishAddress());
    localAddresses.addAll(Arrays.asList(transportService.boundAddress().boundAddresses()));
    // ExecutorService#invokeAll guarantees that the futures are returned in the iteration order of the tasks so we can associate the
    // hostname with the corresponding task by iterating together
    final Iterator<String> it = hosts.iterator();
    for (final Future<TransportAddress[]> future : futures.get()) {
        assert future.isDone();
        final String hostname = it.next();
        if (!future.isCancelled()) {
            try {
                final TransportAddress[] addresses = future.get();
                logger.trace("resolved host [{}] to {}", hostname, addresses);
                for (int addressId = 0; addressId < addresses.length; addressId++) {
                    final TransportAddress address = addresses[addressId];
                    // no point in pinging ourselves
                    if (localAddresses.contains(address) == false) {
                        transportAddresses.add(address);
                    }
                }
            } catch (final ExecutionException e) {
                assert e.getCause() != null;
                final String message = "failed to resolve host [" + hostname + "]";
                logger.warn(message, e.getCause());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            // ignore
            }
        } else {
            logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
        }
    }
    return Collections.unmodifiableList(transportAddresses);
}
Also used : CancellableThreads(org.opensearch.common.util.CancellableThreads) Arrays(java.util.Arrays) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callable(java.util.concurrent.Callable) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) ConfiguredHostsResolver(org.opensearch.discovery.PeerFinder.ConfiguredHostsResolver) Setting(org.opensearch.common.settings.Setting) TimeValue(org.opensearch.common.unit.TimeValue) SetOnce(org.apache.lucene.util.SetOnce) Iterator(java.util.Iterator) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AbstractLifecycleComponent(org.opensearch.common.component.AbstractLifecycleComponent) List(java.util.List) Logger(org.apache.logging.log4j.Logger) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) CancellableThreads(org.opensearch.common.util.CancellableThreads) SetOnce(org.apache.lucene.util.SetOnce) TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Aggregations

TransportService (org.opensearch.transport.TransportService)177 Settings (org.opensearch.common.settings.Settings)99 ClusterService (org.opensearch.cluster.service.ClusterService)90 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)88 ActionListener (org.opensearch.action.ActionListener)75 ThreadPool (org.opensearch.threadpool.ThreadPool)73 ActionFilters (org.opensearch.action.support.ActionFilters)72 Version (org.opensearch.Version)51 IOException (java.io.IOException)49 Collections (java.util.Collections)49 ClusterState (org.opensearch.cluster.ClusterState)48 ArrayList (java.util.ArrayList)47 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)46 List (java.util.List)42 Before (org.junit.Before)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 TimeValue (org.opensearch.common.unit.TimeValue)40 Map (java.util.Map)38 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)38 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)38