Search in sources :

Example 26 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project sonarqube by SonarSource.

the class SearchServerTest method start_stop_server.

@Test
public void start_stop_server() throws Exception {
    Props props = new Props(new Properties());
    // the following properties have always default values (see ProcessProperties)
    InetAddress host = InetAddress.getLoopbackAddress();
    props.set(ProcessProperties.SEARCH_HOST, host.getHostAddress());
    props.set(ProcessProperties.SEARCH_PORT, String.valueOf(port));
    props.set(ProcessProperties.CLUSTER_NAME, A_CLUSTER_NAME);
    props.set(EsSettings.CLUSTER_SEARCH_NODE_NAME, A_NODE_NAME);
    props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
    props.set(ProcessEntryPoint.PROPERTY_SHARED_PATH, temp.newFolder().getAbsolutePath());
    underTest = new SearchServer(props);
    underTest.start();
    assertThat(underTest.getStatus()).isEqualTo(Monitored.Status.OPERATIONAL);
    Settings settings = Settings.builder().put("cluster.name", A_CLUSTER_NAME).build();
    client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(host, port));
    assertThat(client.admin().cluster().prepareClusterStats().get().getStatus()).isEqualTo(ClusterHealthStatus.GREEN);
    underTest.stop();
    underTest.awaitStop();
    underTest = null;
    try {
        client.admin().cluster().prepareClusterStats().get();
        fail();
    } catch (NoNodeAvailableException exception) {
    // ok
    }
}
Also used : Props(org.sonar.process.Props) Properties(java.util.Properties) ProcessProperties(org.sonar.process.ProcessProperties) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) InetAddress(java.net.InetAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 27 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress 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 28 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project jstorm by alibaba.

the class EsConfig method getTransportAddresses.

List<TransportAddress> getTransportAddresses() throws UnknownHostException {
    List<TransportAddress> transportAddresses = Lists.newArrayList();
    for (String node : nodes) {
        String[] hostAndPort = node.split(DELIMITER);
        Preconditions.checkArgument(hostAndPort.length == 2, "Incorrect node format");
        String host = hostAndPort[0];
        int port = Integer.parseInt(hostAndPort[1]);
        InetSocketTransportAddress inetSocketTransportAddress = new InetSocketTransportAddress(InetAddress.getByName(host), port);
        transportAddresses.add(inetSocketTransportAddress);
    }
    return transportAddresses;
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 29 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project titan by thinkaurelius.

the class ElasticSearchIndex method legacyConfiguration.

/**
     * Configure ElasticSearchIndex's ES client according to 0.4.x - 0.5.0 semantics.
     * This checks local-mode first.  If local-mode is true, then it creates a Node that
     * uses JVM local transport and can't talk over the network.  If local-mode is
     * false, then it creates a TransportClient that can talk over the network and
     * uses {@link com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration#INDEX_HOSTS}
     * as the server addresses.  Note that this configuration method
     * does not allow creating a Node that talks over the network.
     * <p>
     * This is activated by <b>not</b> setting an explicit value for {@link #INTERFACE} in the
     * Titan configuration.
     *
     * @see #interfaceConfiguration(com.thinkaurelius.titan.diskstorage.configuration.Configuration)
     * @param config a config passed to ElasticSearchIndex's constructor
     * @return a node and client object open and ready for use
     */
private ElasticSearchSetup.Connection legacyConfiguration(Configuration config) {
    Node node;
    Client client;
    if (config.get(LOCAL_MODE)) {
        log.debug("Configuring ES for JVM local transport");
        boolean clientOnly = config.get(CLIENT_ONLY);
        boolean local = config.get(LOCAL_MODE);
        NodeBuilder builder = NodeBuilder.nodeBuilder();
        Preconditions.checkArgument(config.has(INDEX_CONF_FILE) || config.has(INDEX_DIRECTORY), "Must either configure configuration file or base directory");
        if (config.has(INDEX_CONF_FILE)) {
            String configFile = config.get(INDEX_CONF_FILE);
            ImmutableSettings.Builder sb = ImmutableSettings.settingsBuilder();
            log.debug("Configuring ES from YML file [{}]", configFile);
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(configFile);
                sb.loadFromStream(configFile, fis);
                builder.settings(sb.build());
            } catch (FileNotFoundException e) {
                throw new TitanException(e);
            } finally {
                IOUtils.closeQuietly(fis);
            }
        } else {
            String dataDirectory = config.get(INDEX_DIRECTORY);
            log.debug("Configuring ES with data directory [{}]", dataDirectory);
            File f = new File(dataDirectory);
            if (!f.exists())
                f.mkdirs();
            ImmutableSettings.Builder b = ImmutableSettings.settingsBuilder();
            for (String sub : DATA_SUBDIRS) {
                String subdir = dataDirectory + File.separator + sub;
                f = new File(subdir);
                if (!f.exists())
                    f.mkdirs();
                b.put("path." + sub, subdir);
            }
            b.put("script.disable_dynamic", false);
            b.put("indices.ttl.interval", "5s");
            builder.settings(b.build());
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            builder.clusterName(clustername);
        }
        node = builder.client(clientOnly).data(!clientOnly).local(local).node();
        client = node.client();
    } else {
        log.debug("Configuring ES for network transport");
        ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();
        if (config.has(CLUSTER_NAME)) {
            String clustername = config.get(CLUSTER_NAME);
            Preconditions.checkArgument(StringUtils.isNotBlank(clustername), "Invalid cluster name: %s", clustername);
            settings.put("cluster.name", clustername);
        } else {
            settings.put("client.transport.ignore_cluster_name", true);
        }
        log.debug("Transport sniffing enabled: {}", config.get(CLIENT_SNIFF));
        settings.put("client.transport.sniff", config.get(CLIENT_SNIFF));
        settings.put("script.disable_dynamic", false);
        TransportClient tc = new TransportClient(settings.build());
        int defaultPort = config.has(INDEX_PORT) ? config.get(INDEX_PORT) : HOST_PORT_DEFAULT;
        for (String host : config.get(INDEX_HOSTS)) {
            String[] hostparts = host.split(":");
            String hostname = hostparts[0];
            int hostport = defaultPort;
            if (hostparts.length == 2)
                hostport = Integer.parseInt(hostparts[1]);
            log.info("Configured remote host: {} : {}", hostname, hostport);
            tc.addTransportAddress(new InetSocketTransportAddress(hostname, hostport));
        }
        client = tc;
        node = null;
    }
    return new ElasticSearchSetup.Connection(node, client);
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Node(org.elasticsearch.node.Node) FileNotFoundException(java.io.FileNotFoundException) NodeBuilder(org.elasticsearch.node.NodeBuilder) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) FileInputStream(java.io.FileInputStream) TitanException(com.thinkaurelius.titan.core.TitanException) TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) File(java.io.File)

Example 30 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project metacat by Netflix.

the class ElasticSearchConfig method elasticSearchClient.

/**
     * The ElasticSearch client.
     *
     * @param config System config
     * @return Configured client or error
     */
@Bean
public Client elasticSearchClient(final Config config) {
    final String clusterName = config.getElasticSearchClusterName();
    if (StringUtils.isBlank(clusterName)) {
        throw new IllegalStateException("No cluster name set. Unable to continue");
    }
    final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).put("transport.tcp.connect_timeout", "60s").build();
    final Client client = new TransportClient(settings);
    // Add the transport address if exists
    final String clusterNodesStr = config.getElasticSearchClusterNodes();
    if (StringUtils.isNotBlank(clusterNodesStr)) {
        final int port = config.getElasticSearchClusterPort();
        final Iterable<String> clusterNodes = Splitter.on(',').split(clusterNodesStr);
        clusterNodes.forEach(clusterNode -> ((TransportClient) client).addTransportAddress(new InetSocketTransportAddress(clusterNode, port)));
    }
    return client;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) Client(org.elasticsearch.client.Client) TransportClient(org.elasticsearch.client.transport.TransportClient) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ImmutableSettings(org.elasticsearch.common.settings.ImmutableSettings) Settings(org.elasticsearch.common.settings.Settings) Bean(org.springframework.context.annotation.Bean)

Aggregations

InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)35 TransportClient (org.elasticsearch.client.transport.TransportClient)15 Settings (org.elasticsearch.common.settings.Settings)14 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 TransportAddress (org.elasticsearch.common.transport.TransportAddress)7 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)7 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 IOException (java.io.IOException)4 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)3 BoundTransportAddress (org.elasticsearch.common.transport.BoundTransportAddress)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2