Search in sources :

Example 16 with TransportService

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

the class UnicastZenPing method resolveHostsLists.

/**
     * Resolves a list of hosts to a list of discovery nodes. Each host is resolved into a transport address (or a collection of addresses
     * if the number of ports is greater than one) and the transport addresses are used to created discovery nodes. 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 limitPortCounts  the number of ports to resolve (should be 1 for non-local transport)
     * @param transportService the transport service
     * @param nodeId_prefix    a prefix to use for node ids
     * @param resolveTimeout   the timeout before returning from hostname lookups
     * @return a list of discovery nodes with resolved transport addresses
     */
public static List<DiscoveryNode> resolveHostsLists(final ExecutorService executorService, final Logger logger, final List<String> hosts, final int limitPortCounts, final TransportService transportService, final String nodeId_prefix, final TimeValue resolveTimeout) throws InterruptedException {
    Objects.requireNonNull(executorService);
    Objects.requireNonNull(logger);
    Objects.requireNonNull(hosts);
    Objects.requireNonNull(transportService);
    Objects.requireNonNull(nodeId_prefix);
    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, limitPortCounts)).collect(Collectors.toList());
    final List<Future<TransportAddress[]>> futures = executorService.invokeAll(callables, resolveTimeout.nanos(), TimeUnit.NANOSECONDS);
    final List<DiscoveryNode> discoveryNodes = 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) {
        final String hostname = it.next();
        if (!future.isCancelled()) {
            assert future.isDone();
            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) {
                        discoveryNodes.add(new DiscoveryNode(nodeId_prefix + hostname + "_" + addressId + "#", address, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
                    }
                }
            } catch (final ExecutionException e) {
                assert e.getCause() != null;
                final String message = "failed to resolve host [" + hostname + "]";
                logger.warn(message, e.getCause());
            }
        } else {
            logger.warn("timed out after [{}] resolving host [{}]", resolveTimeout, hostname);
        }
    }
    return discoveryNodes;
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) TransportRequest(org.elasticsearch.transport.TransportRequest) Releasables(org.elasticsearch.common.lease.Releasables) Property(org.elasticsearch.common.settings.Setting.Property) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ConcurrentCollections.newConcurrentMap(org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) Future(java.util.concurrent.Future) Settings(org.elasticsearch.common.settings.Settings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) PingResponse.readPingResponse(org.elasticsearch.discovery.zen.ZenPing.PingResponse.readPingResponse) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterName(org.elasticsearch.cluster.ClusterName) CollectionUtils(org.elasticsearch.common.util.CollectionUtils) ThreadFactory(java.util.concurrent.ThreadFactory) Releasable(org.elasticsearch.common.lease.Releasable) Setting(org.elasticsearch.common.settings.Setting) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) KeyedLock(org.elasticsearch.common.util.concurrent.KeyedLock) TransportRequestHandler(org.elasticsearch.transport.TransportRequestHandler) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) TransportRequestOptions(org.elasticsearch.transport.TransportRequestOptions) Queue(java.util.Queue) TransportException(org.elasticsearch.transport.TransportException) TransportChannel(org.elasticsearch.transport.TransportChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) EsThreadPoolExecutor(org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TimeValue(org.elasticsearch.common.unit.TimeValue) TransportResponse(org.elasticsearch.transport.TransportResponse) TransportService(org.elasticsearch.transport.TransportService) ExecutorService(java.util.concurrent.ExecutorService) ConnectionProfile(org.elasticsearch.transport.ConnectionProfile) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) Connection(org.elasticsearch.transport.Transport.Connection) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NodeNotConnectedException(org.elasticsearch.transport.NodeNotConnectedException) Collections(java.util.Collections) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet)

Example 17 with TransportService

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

the class TransportBulkActionIngestTests method setupAction.

@Before
public void setupAction() {
    // initialize captors, which must be members to use @Capture because of generics
    MockitoAnnotations.initMocks(this);
    // setup services that will be called by action
    transportService = mock(TransportService.class);
    clusterService = mock(ClusterService.class);
    localIngest = true;
    // setup nodes for local and remote
    DiscoveryNode localNode = mock(DiscoveryNode.class);
    when(localNode.isIngestNode()).thenAnswer(stub -> localIngest);
    when(clusterService.localNode()).thenReturn(localNode);
    remoteNode1 = mock(DiscoveryNode.class);
    remoteNode2 = mock(DiscoveryNode.class);
    nodes = mock(DiscoveryNodes.class);
    ImmutableOpenMap<String, DiscoveryNode> ingestNodes = ImmutableOpenMap.<String, DiscoveryNode>builder(2).fPut("node1", remoteNode1).fPut("node2", remoteNode2).build();
    when(nodes.getIngestNodes()).thenReturn(ingestNodes);
    ClusterState state = mock(ClusterState.class);
    when(state.getNodes()).thenReturn(nodes);
    when(clusterService.state()).thenReturn(state);
    doAnswer(invocation -> {
        ClusterChangedEvent event = mock(ClusterChangedEvent.class);
        when(event.state()).thenReturn(state);
        ((ClusterStateApplier) invocation.getArguments()[0]).applyClusterState(event);
        return null;
    }).when(clusterService).addStateApplier(any(ClusterStateApplier.class));
    // setup the mocked ingest service for capturing calls
    ingestService = mock(IngestService.class);
    executionService = mock(PipelineExecutionService.class);
    when(ingestService.getPipelineExecutionService()).thenReturn(executionService);
    action = new TestTransportBulkAction();
    singleItemBulkWriteAction = new TestSingleItemBulkWriteAction(action);
    // call on construction of action
    reset(transportService);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IngestService(org.elasticsearch.ingest.IngestService) PipelineExecutionService(org.elasticsearch.ingest.PipelineExecutionService) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterService(org.elasticsearch.cluster.service.ClusterService) TransportService(org.elasticsearch.transport.TransportService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) Before(org.junit.Before)

Example 18 with TransportService

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

the class TransportBulkActionIngestTests method testSingleItemBulkActionIngestLocal.

public void testSingleItemBulkActionIngestLocal() throws Exception {
    Exception exception = new Exception("fake exception");
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    indexRequest.setPipeline("testpipeline");
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    AtomicBoolean failureCalled = new AtomicBoolean(false);
    singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> {
        responseCalled.set(true);
    }, e -> {
        assertThat(e, sameInstance(exception));
        failureCalled.set(true);
    }));
    // check failure works, and passes through to the listener
    // haven't executed yet
    assertFalse(action.isExecuted);
    assertFalse(responseCalled.get());
    assertFalse(failureCalled.get());
    verify(executionService).executeBulkRequest(bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture());
    completionHandler.getValue().accept(exception);
    assertTrue(failureCalled.get());
    // now check success
    // this is done by the real pipeline execution service when processing
    indexRequest.setPipeline(null);
    completionHandler.getValue().accept(null);
    assertTrue(action.isExecuted);
    // listener would only be called by real index action, not our mocked one
    assertFalse(responseCalled.get());
    verifyZeroInteractions(transportService);
}
Also used : IngestService(org.elasticsearch.ingest.IngestService) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) PipelineExecutionService(org.elasticsearch.ingest.PipelineExecutionService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Captor(org.mockito.Captor) Supplier(java.util.function.Supplier) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MockitoAnnotations(org.mockito.MockitoAnnotations) IndexRequest(org.elasticsearch.action.index.IndexRequest) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.eq(org.mockito.Matchers.eq) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BiConsumer(java.util.function.BiConsumer) IndexResponse(org.elasticsearch.action.index.IndexResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) Before(org.junit.Before) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ActionFilters(org.elasticsearch.action.support.ActionFilters) Iterator(java.util.Iterator) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) Mockito.when(org.mockito.Mockito.when) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) IndexAction(org.elasticsearch.action.index.IndexAction) Mockito.never(org.mockito.Mockito.never) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.reset(org.mockito.Mockito.reset) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 19 with TransportService

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

the class TransportClientRetryIT method testRetry.

public void testRetry() throws IOException, ExecutionException, InterruptedException {
    Iterable<TransportService> instances = internalCluster().getInstances(TransportService.class);
    TransportAddress[] addresses = new TransportAddress[internalCluster().size()];
    int i = 0;
    for (TransportService instance : instances) {
        addresses[i++] = instance.boundAddress().publishAddress();
    }
    Settings.Builder builder = Settings.builder().put("client.transport.nodes_sampler_interval", "1s").put("node.name", "transport_client_retry_test").put(ClusterName.CLUSTER_NAME_SETTING.getKey(), internalCluster().getClusterName()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir());
    try (TransportClient client = new MockTransportClient(builder.build())) {
        client.addTransportAddresses(addresses);
        assertEquals(client.connectedNodes().size(), internalCluster().size());
        int size = cluster().size();
        //kill all nodes one by one, leaving a single master/data node at the end of the loop
        for (int j = 1; j < size; j++) {
            internalCluster().stopRandomNode(input -> true);
            ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest().local(true);
            ClusterState clusterState;
            //use both variants of execute method: with and without listener
            if (randomBoolean()) {
                clusterState = client.admin().cluster().state(clusterStateRequest).get().getState();
            } else {
                PlainListenableActionFuture<ClusterStateResponse> future = new PlainListenableActionFuture<>(client.threadPool());
                client.admin().cluster().state(clusterStateRequest, future);
                clusterState = future.get().getState();
            }
            assertThat(clusterState.nodes().getSize(), greaterThanOrEqualTo(size - j));
            assertThat(client.connectedNodes().size(), greaterThanOrEqualTo(size - j));
        }
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) MockTransportClient(org.elasticsearch.transport.MockTransportClient) TransportService(org.elasticsearch.transport.TransportService) PlainListenableActionFuture(org.elasticsearch.action.support.PlainListenableActionFuture) Settings(org.elasticsearch.common.settings.Settings)

Example 20 with TransportService

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

the class TransportKillJobsNodeActionTest method testKillIsCalledOnJobContextService.

@Test
public void testKillIsCalledOnJobContextService() throws Exception {
    TransportService transportService = mock(TransportService.class);
    JobContextService jobContextService = mock(JobContextService.class, Answers.RETURNS_MOCKS.get());
    TransportKillJobsNodeAction transportKillJobsNodeAction = new TransportKillJobsNodeAction(Settings.EMPTY, jobContextService, new NoopClusterService(), transportService);
    final CountDownLatch latch = new CountDownLatch(1);
    List<UUID> toKill = ImmutableList.of(UUID.randomUUID(), UUID.randomUUID());
    transportKillJobsNodeAction.nodeOperation(new KillJobsRequest(toKill), new ActionListener<KillResponse>() {

        @Override
        public void onResponse(KillResponse killAllResponse) {
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable throwable) {
            latch.countDown();
        }
    });
    latch.await(1, TimeUnit.SECONDS);
    verify(jobContextService, times(1)).killJobs(toKill);
}
Also used : TransportService(org.elasticsearch.transport.TransportService) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) NoopClusterService(org.elasticsearch.test.cluster.NoopClusterService) JobContextService(io.crate.jobs.JobContextService) Test(org.junit.Test)

Aggregations

TransportService (org.elasticsearch.transport.TransportService)42 Settings (org.elasticsearch.common.settings.Settings)25 ThreadPool (org.elasticsearch.threadpool.ThreadPool)21 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)19 ClusterState (org.elasticsearch.cluster.ClusterState)18 ClusterService (org.elasticsearch.cluster.service.ClusterService)17 Collections (java.util.Collections)16 IOException (java.io.IOException)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 TimeUnit (java.util.concurrent.TimeUnit)15 Before (org.junit.Before)15 ActionFilters (org.elasticsearch.action.support.ActionFilters)14 ESTestCase (org.elasticsearch.test.ESTestCase)14 MockTransportService (org.elasticsearch.test.transport.MockTransportService)14 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)14 HashSet (java.util.HashSet)13 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)13 IndexNameExpressionResolver (org.elasticsearch.cluster.metadata.IndexNameExpressionResolver)12 TransportAddress (org.elasticsearch.common.transport.TransportAddress)12 After (org.junit.After)12