Search in sources :

Example 21 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project zipkin by openzipkin.

the class ElasticsearchDependenciesTest method writeDependencyLinks.

protected void writeDependencyLinks(List<DependencyLink> links, long timestampMillis) {
    long midnight = Util.midnightUTC(timestampMillis);
    TransportClient client = ((NativeClient) storage().client()).client;
    BulkRequestBuilder request = client.prepareBulk();
    for (DependencyLink link : links) {
        request.add(client.prepareIndex(storage().indexNameFormatter.indexNameForTimestamp(midnight), ElasticsearchConstants.DEPENDENCY_LINK).setId(// Unique constraint
        link.parent + "|" + link.child).setSource("parent", link.parent, "child", link.child, "callCount", link.callCount));
    }
    request.execute().actionGet();
    client.admin().indices().flush(new FlushRequest()).actionGet();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) FlushRequest(org.elasticsearch.action.admin.indices.flush.FlushRequest) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) DependencyLink(zipkin.DependencyLink)

Example 22 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project sonarqube by SonarSource.

the class EsClientProviderTest method connection_to_local_es_when_cluster_mode_is_disabled.

@Test
public void connection_to_local_es_when_cluster_mode_is_disabled() throws Exception {
    settings.setProperty(ProcessProperties.CLUSTER_ENABLED, false);
    settings.setProperty(ProcessProperties.SEARCH_HOST, localhost);
    settings.setProperty(ProcessProperties.SEARCH_PORT, 8080);
    EsClient client = underTest.provide(settings);
    TransportClient transportClient = (TransportClient) client.nativeClient();
    assertThat(transportClient.transportAddresses()).hasSize(1);
    TransportAddress address = transportClient.transportAddresses().get(0);
    assertThat(address.getAddress()).isEqualTo(localhost);
    assertThat(address.getPort()).isEqualTo(8080);
    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to local Elasticsearch: [" + localhost + ":8080]"), ""));
    // keep in cache
    assertThat(underTest.provide(settings)).isSameAs(client);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TransportClient(org.elasticsearch.client.transport.TransportClient) Test(org.junit.Test) String.format(java.lang.String.format) InetAddress(java.net.InetAddress) ProcessProperties(org.sonar.process.ProcessProperties) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.sonar.api.config.Settings) Condition(org.assertj.core.api.Condition) MapSettings(org.sonar.api.config.MapSettings) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) TransportClient(org.elasticsearch.client.transport.TransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Test(org.junit.Test)

Example 23 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project sonarqube by SonarSource.

the class EsClientProviderTest method connection_to_remote_es_nodes_when_cluster_mode_is_enabled_and_local_es_is_disabled.

@Test
public void connection_to_remote_es_nodes_when_cluster_mode_is_enabled_and_local_es_is_disabled() throws Exception {
    settings.setProperty(ProcessProperties.CLUSTER_ENABLED, true);
    settings.setProperty(ProcessProperties.CLUSTER_SEARCH_DISABLED, true);
    settings.setProperty(ProcessProperties.CLUSTER_SEARCH_HOSTS, format("%s:8080,%s:8081", localhost, localhost));
    EsClient client = underTest.provide(settings);
    TransportClient transportClient = (TransportClient) client.nativeClient();
    assertThat(transportClient.transportAddresses()).hasSize(2);
    TransportAddress address = transportClient.transportAddresses().get(0);
    assertThat(address.getAddress()).isEqualTo(localhost);
    assertThat(address.getPort()).isEqualTo(8080);
    address = transportClient.transportAddresses().get(1);
    assertThat(address.getAddress()).isEqualTo(localhost);
    assertThat(address.getPort()).isEqualTo(8081);
    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [" + localhost + ":8080, " + localhost + ":8081]"), ""));
    // keep in cache
    assertThat(underTest.provide(settings)).isSameAs(client);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TransportClient(org.elasticsearch.client.transport.TransportClient) Test(org.junit.Test) String.format(java.lang.String.format) InetAddress(java.net.InetAddress) ProcessProperties(org.sonar.process.ProcessProperties) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.sonar.api.config.Settings) Condition(org.assertj.core.api.Condition) MapSettings(org.sonar.api.config.MapSettings) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) TransportClient(org.elasticsearch.client.transport.TransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Test(org.junit.Test)

Example 24 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project MSEC by Tencent.

the class ESHelper method ClusterStatus.

public void ClusterStatus(ArrayList<String> ips, String cluster_name, QueryESClusterDetailResponse response) {
    Logger logger = Logger.getLogger(ESHelper.class);
    TransportClient client = null;
    HashSet<String> total_ips = new HashSet<>(ips);
    try {
        Settings settings = Settings.builder().put("cluster.name", cluster_name).put("client.transport.sniff", true).build();
        for (String ip : ips) {
            if (client == null) {
                client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
            } else
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
        }
        ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth().setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
        if (healthResponse != null && !healthResponse.isTimedOut()) {
            response.setActive_shards(healthResponse.getActiveShards());
            response.setTotal_shards(healthResponse.getActiveShards() + healthResponse.getUnassignedShards());
            response.setHealth_status(healthResponse.getStatus().toString().toLowerCase());
            response.setServer_port(default_port);
            logger.info(healthResponse);
            if (healthResponse.getNumberOfNodes() > 0) {
                NodesStatsResponse nodeStats = client.admin().cluster().prepareNodesStats().all().get();
                if (nodeStats != null) {
                    for (NodeStats stats : nodeStats.getNodes()) {
                        QueryESClusterDetailResponse.RTInfo info = new QueryESClusterDetailResponse().new RTInfo();
                        info.setOK(true);
                        info.setAvail_disk_size(stats.getFs().getTotal().getAvailable().getBytes());
                        info.setDoc_count(stats.getIndices().getDocs().getCount());
                        info.setDoc_disk_size(stats.getIndices().getStore().getSizeInBytes());
                        response.getInfo_map().put(stats.getNode().getAddress().getHost(), info);
                        total_ips.add(stats.getNode().getAddress().getHost());
                    }
                }
            }
        }
        //update Zen settings
        client.admin().cluster().prepareUpdateSettings().setPersistentSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).setTransientSettings(Settings.builder().put("discovery.zen.minimum_master_nodes", total_ips.size() / 2 + 1)).get();
        //update template settings
        PutIndexTemplateResponse tpl_resp = client.admin().indices().preparePutTemplate("template_msec").setTemplate("msec_*").setSettings(Settings.builder().put("number_of_replicas", 1).put("refresh_interval", "30s")).addMapping("logs", MAPPING).execute().get();
        logger.info("Create mapping: " + tpl_resp.isAcknowledged());
    } catch (UnknownHostException e) {
        logger.error(e);
    } catch (Exception e) {
        logger.error(e);
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) UnknownHostException(java.net.UnknownHostException) Logger(org.apache.log4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) MasterNotDiscoveredException(org.elasticsearch.discovery.MasterNotDiscoveredException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) Settings(org.elasticsearch.common.settings.Settings) QueryESClusterDetailResponse(beans.response.QueryESClusterDetailResponse)

Example 25 with TransportClient

use of org.elasticsearch.client.transport.TransportClient in project elasticsearch-jetty by sonian.

the class AbstractJettyHttpServerTests method closeAllNodes.

public void closeAllNodes() {
    for (TransportClient client : transportClients) {
        client.close();
    }
    transportClients.clear();
    for (Client client : clients.values()) {
        client.close();
    }
    clients.clear();
    for (Node node : nodes.values()) {
        node.close();
    }
    nodes.clear();
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) InternalNode(org.elasticsearch.node.internal.InternalNode) Node(org.elasticsearch.node.Node) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient)

Aggregations

TransportClient (org.elasticsearch.client.transport.TransportClient)31 Settings (org.elasticsearch.common.settings.Settings)17 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)15 TransportAddress (org.elasticsearch.common.transport.TransportAddress)8 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 Client (org.elasticsearch.client.Client)5 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Test (org.junit.Test)3 Settings (org.sonar.api.config.Settings)3 File (java.io.File)2 String.format (java.lang.String.format)2 InetAddress (java.net.InetAddress)2 Logger (org.apache.log4j.Logger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Condition (org.assertj.core.api.Condition)2 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 Node (org.elasticsearch.node.Node)2