Search in sources :

Example 36 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project kylo by Teradata.

the class ElasticSearchService method buildTransportClient.

private void buildTransportClient() {
    if (this.client == null) {
        try {
            Settings settings = Settings.settingsBuilder().put("cluster.name", clientConfig.getClusterName()).build();
            client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(clientConfig.getHost()), clientConfig.getTransportPort()));
        } catch (UnknownHostException e) {
            throw new RuntimeException("Error encountered during search.");
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 37 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project incubator-gobblin by apache.

the class ElasticsearchRestWriter method buildRestClient.

// TODO: Support pass through of configuration (e.g. timeouts etc) of rest client from above
private static RestClient buildRestClient(List<InetSocketTransportAddress> hosts, int threadCount, boolean sslEnabled, String keyStoreType, String keyStoreFilePassword, String identityFilepath, String trustStoreType, String trustStoreFilePassword, String cacertsFilepath) throws Exception {
    HttpHost[] httpHosts = new HttpHost[hosts.size()];
    String scheme = sslEnabled ? "https" : "http";
    for (int h = 0; h < httpHosts.length; h++) {
        InetSocketTransportAddress host = hosts.get(h);
        httpHosts[h] = new HttpHost(host.getAddress(), host.getPort(), scheme);
    }
    RestClientBuilder builder = RestClient.builder(httpHosts);
    if (sslEnabled) {
        log.info("ssl configuration: trustStoreType = {}, cacertsFilePath = {}", trustStoreType, cacertsFilepath);
        KeyStore truststore = KeyStore.getInstance(trustStoreType);
        FileInputStream trustInputStream = new FileInputStream(cacertsFilepath);
        try {
            truststore.load(trustInputStream, trustStoreFilePassword.toCharArray());
        } finally {
            trustInputStream.close();
        }
        SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
        log.info("ssl key configuration: keyStoreType = {}, keyFilePath = {}", keyStoreType, identityFilepath);
        KeyStore keystore = KeyStore.getInstance(keyStoreType);
        FileInputStream keyInputStream = new FileInputStream(identityFilepath);
        try {
            keystore.load(keyInputStream, keyStoreFilePassword.toCharArray());
        } finally {
            keyInputStream.close();
        }
        sslBuilder.loadKeyMaterial(keystore, keyStoreFilePassword.toCharArray());
        final SSLContext sslContext = sslBuilder.build();
        builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build()));
    } else {
        builder = builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build()));
    }
    // Configure timeouts
    builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectionRequestTimeout(// Important, otherwise the client has spurious timeouts
    0));
    return builder.build();
}
Also used : RestClient(org.elasticsearch.client.RestClient) SSLContext(javax.net.ssl.SSLContext) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) ConfigUtils(org.apache.gobblin.util.ConfigUtils) Batch(org.apache.gobblin.writer.Batch) Future(java.util.concurrent.Future) SSLContexts(org.apache.http.ssl.SSLContexts) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) Nullable(javax.annotation.Nullable) WriteResponse(org.apache.gobblin.writer.WriteResponse) WriteCallback(org.apache.gobblin.writer.WriteCallback) Pair(org.apache.commons.math3.util.Pair) Config(com.typesafe.config.Config) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) IOException(java.io.IOException) KeyStore(java.security.KeyStore) PasswordManager(org.apache.gobblin.password.PasswordManager) FileInputStream(java.io.FileInputStream) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Paths(java.nio.file.Paths) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BatchAsyncDataWriter(org.apache.gobblin.writer.BatchAsyncDataWriter) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) SSLContext(javax.net.ssl.SSLContext) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) KeyStore(java.security.KeyStore) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) FileInputStream(java.io.FileInputStream)

Example 38 with InetSocketTransportAddress

use of org.elasticsearch.common.transport.InetSocketTransportAddress in project flink by apache.

the class ElasticsearchUtils method convertInetSocketAddresses.

/**
	 * Utility method to convert a {@link List} of {@link InetSocketAddress} to Elasticsearch {@link TransportAddress}.
	 *
	 * @param inetSocketAddresses The list of {@link InetSocketAddress} to convert.
	 */
public static List<TransportAddress> convertInetSocketAddresses(List<InetSocketAddress> inetSocketAddresses) {
    if (inetSocketAddresses == null) {
        return null;
    } else {
        List<TransportAddress> converted;
        converted = new ArrayList<>(inetSocketAddresses.size());
        for (InetSocketAddress address : inetSocketAddresses) {
            converted.add(new InetSocketTransportAddress(address));
        }
        return converted;
    }
}
Also used : TransportAddress(org.elasticsearch.common.transport.TransportAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 39 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 40 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)

Aggregations

InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)69 TransportClient (org.elasticsearch.client.transport.TransportClient)37 Settings (org.elasticsearch.common.settings.Settings)35 PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)25 UnknownHostException (java.net.UnknownHostException)17 InetSocketAddress (java.net.InetSocketAddress)11 TransportAddress (org.elasticsearch.common.transport.TransportAddress)10 HashMap (java.util.HashMap)8 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)7 Builder (org.elasticsearch.common.settings.Settings.Builder)7 ArrayList (java.util.ArrayList)6 Client (org.elasticsearch.client.Client)6 Bean (org.springframework.context.annotation.Bean)5 InetAddress (java.net.InetAddress)4 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)4 Node (org.elasticsearch.node.Node)4 File (java.io.File)3 IOException (java.io.IOException)3 Properties (java.util.Properties)3