Search in sources :

Example 1 with MockTransportClient

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

the class BulkProcessorIT method testBulkProcessorConcurrentRequestsNoNodeAvailableException.

//https://github.com/elastic/elasticsearch/issues/5038
public void testBulkProcessorConcurrentRequestsNoNodeAvailableException() throws Exception {
    //we create a transport client with no nodes to make sure it throws NoNodeAvailableException
    Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    Client transportClient = new MockTransportClient(settings);
    int bulkActions = randomIntBetween(10, 100);
    int numDocs = randomIntBetween(bulkActions, bulkActions + 100);
    int concurrentRequests = randomIntBetween(0, 10);
    int expectedBulkActions = numDocs / bulkActions;
    final CountDownLatch latch = new CountDownLatch(expectedBulkActions);
    int totalExpectedBulkActions = numDocs % bulkActions == 0 ? expectedBulkActions : expectedBulkActions + 1;
    final CountDownLatch closeLatch = new CountDownLatch(totalExpectedBulkActions);
    BulkProcessorTestListener listener = new BulkProcessorTestListener(latch, closeLatch);
    try (BulkProcessor processor = BulkProcessor.builder(transportClient, listener).setConcurrentRequests(concurrentRequests).setBulkActions(bulkActions).setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) {
        indexDocs(transportClient, processor, numDocs);
        latch.await();
        assertThat(listener.beforeCounts.get(), equalTo(expectedBulkActions));
        assertThat(listener.afterCounts.get(), equalTo(expectedBulkActions));
        assertThat(listener.bulkFailures.size(), equalTo(expectedBulkActions));
        assertThat(listener.bulkItems.size(), equalTo(0));
    }
    closeLatch.await();
    assertThat(listener.bulkFailures.size(), equalTo(totalExpectedBulkActions));
    assertThat(listener.bulkItems.size(), equalTo(0));
    transportClient.close();
}
Also used : ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) MockTransportClient(org.elasticsearch.transport.MockTransportClient) Client(org.elasticsearch.client.Client) MockTransportClient(org.elasticsearch.transport.MockTransportClient) CountDownLatch(java.util.concurrent.CountDownLatch) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with MockTransportClient

use of org.elasticsearch.transport.MockTransportClient 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 3 with MockTransportClient

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

the class TransportClientHeadersTests method testWithSniffing.

public void testWithSniffing() throws Exception {
    try (TransportClient client = new MockTransportClient(Settings.builder().put("client.transport.sniff", true).put("cluster.name", "cluster1").put("node.name", "transport_client_" + this.getTestName() + "_1").put("client.transport.nodes_sampler_interval", "1s").put(HEADER_SETTINGS).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(), InternalTransportServiceInterceptor.TestPlugin.class)) {
        InternalTransportServiceInterceptor.TestPlugin plugin = client.injector.getInstance(PluginsService.class).filterPlugins(InternalTransportServiceInterceptor.TestPlugin.class).stream().findFirst().get();
        plugin.instance.threadPool = client.threadPool();
        plugin.instance.address = transportService.boundAddress().publishAddress();
        client.addTransportAddress(transportService.boundAddress().publishAddress());
        if (!plugin.instance.clusterStateLatch.await(5, TimeUnit.SECONDS)) {
            fail("takes way too long to get the cluster state");
        }
        assertEquals(1, client.connectedNodes().size());
        assertEquals(client.connectedNodes().get(0).getAddress(), transportService.boundAddress().publishAddress());
    }
}
Also used : MockTransportClient(org.elasticsearch.transport.MockTransportClient) MockTransportClient(org.elasticsearch.transport.MockTransportClient)

Example 4 with MockTransportClient

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

the class TransportClientIT method testThatTransportClientSettingCannotBeChanged.

public void testThatTransportClientSettingCannotBeChanged() {
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
    try (TransportClient client = new MockTransportClient(baseSettings)) {
        Settings settings = client.injector.getInstance(Settings.class);
        assertThat(Client.CLIENT_TYPE_SETTING_S.get(settings), is("transport"));
    }
}
Also used : MockTransportClient(org.elasticsearch.transport.MockTransportClient) MockTransportClient(org.elasticsearch.transport.MockTransportClient) Settings(org.elasticsearch.common.settings.Settings)

Example 5 with MockTransportClient

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

the class Netty4TransportMultiPortIntegrationIT method testThatTransportClientCanConnect.

public void testThatTransportClientCanConnect() throws Exception {
    Settings settings = Settings.builder().put("cluster.name", internalCluster().getClusterName()).put(NetworkModule.TRANSPORT_TYPE_KEY, Netty4Plugin.NETTY_TRANSPORT_NAME).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    try (TransportClient transportClient = new MockTransportClient(settings, Netty4Plugin.class)) {
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), randomPort));
        ClusterHealthResponse response = transportClient.admin().cluster().prepareHealth().get();
        assertThat(response.getStatus(), is(ClusterHealthStatus.GREEN));
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) MockTransportClient(org.elasticsearch.transport.MockTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) TransportAddress(org.elasticsearch.common.transport.TransportAddress) MockTransportClient(org.elasticsearch.transport.MockTransportClient) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

MockTransportClient (org.elasticsearch.transport.MockTransportClient)10 Settings (org.elasticsearch.common.settings.Settings)6 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3 TransportService (org.elasticsearch.transport.TransportService)3 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Collectors (java.util.stream.Collectors)1 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 PlainListenableActionFuture (org.elasticsearch.action.support.PlainListenableActionFuture)1 Client (org.elasticsearch.client.Client)1 TransportClient (org.elasticsearch.client.transport.TransportClient)1 CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL (org.elasticsearch.client.transport.TransportClient.CLIENT_TRANSPORT_NODES_SAMPLER_INTERVAL)1 ClusterState (org.elasticsearch.cluster.ClusterState)1