Search code examples
javaelasticsearch-5elasticsearch-java-api

Address must be resolved but wasn't - InetSocketAddress#getAddress() returned null


When I start my java application that interact with ElasticSearch 5.6.10 through elasticsearch java api and my elasticsearch cluster have more than 1 node, I receive the following exception:

**

**Caused by: java.lang.IllegalArgumentException: Address must be resolved but wasn't - InetSocketAddress#getAddress() returned null
              at org.elasticsearch.common.transport.InetSocketTransportAddress.<init>(InetSocketTransportAddress.java:48)
              at com.intel.mar.data.elasticsearch.client.TransportClientFactory.createTransportClient(TransportClientFactory.java:30)
              at com.intel.mar.service.config.ServiceConfig.serviceDataManager(ServiceConfig.java:81)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134.CGLIB$serviceDataManager$2(<generated>)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134$$FastClassBySpringCGLIB$$b506e141.invoke(<generated>)
              at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
              at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
              at com.intel.mar.service.config.ServiceConfig$$EnhancerBySpringCGLIB$$77f6a134.serviceDataManager(<generated>)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
              ... 40 more**

**

elasticsearch.yml

network.bind_host: 0.0.0.0
network.publish_host: "{{getenv "IP"}}"
path.data: /shared/el
cluster.name:  "{{  getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/es_cluster_name")}}"
cluster.routing.allocation.awareness.attributes: aws_zone_id
node.name: "{{getenv "ID"}}"
node.master: true
node.data: true
discovery.zen.ping.unicast.hosts: [{{getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/elastic_search_seeds")}}]
discovery.zen.minimum_master_nodes: "{{getv (print "/solutions/" (getenv "COMPONENT_NAMESPACE") "/services/mar-server/es_minimum_master_nodes")}}"
http.cors.allow-origin: "/.*/"
http.cors.enabled: true

# AWS discovery

http.enabled: true
http.port: 9200

action.auto_create_index: true

transport.tcp.port: 9300
indices.fielddata.cache.size: 25%
script.inline: true
thread_pool.bulk.queue_size: 900

I am creating the elasticsearch java client in the following way:

    public Client createTransportClient() {
        Settings settings = Settings.builder()
                .put("cluster.name", config.db().clusterName())
                .build();

    return new PreBuiltTransportClient(settings)
                    .addTransportAddress(
                            new InetSocketTransportAddress(
                                    new InetSocketAddress(
                                            config.db().getElasticSearchHost(),
                                            config.db().getElasticSearchNodePort()
                                    )
                            )
                    );
}

Solution

  • The problem was that config.db().getElasticSearchHost() was returning a list of master cluster nodes separate by ",". The solution was the following:

    Settings settings = Settings.builder()
                    .put("cluster.name", config.db().clusterName())
                    .build();
    
        PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);
        String[] hosts = config.db().getElasticSearchHost().split(SPLIT_SEPARATOR);
    
        for(int i=0; i<hosts.length;i++){
            preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hosts[i])
                    ,config.db().getElasticSearchNodePort()));
        }
    
        return preBuiltTransportClient;