Long time to create TransportClient object

I am using below Java code to create Transportclient object.

               logger.info("Starting creation of Transport Client object");
		try {
			Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName)		        .put("client.transport.sniff", sniff).build();
			
			client = TransportClient.builder().settings(settings).build();
			  
			logger.info("Transport client object created successfully");

                   }catch(){}

Time between two log statements is 12 to 13 seconds. Is there any reason why it takes so long to create this object? Do I have something wrong in my configuration ?

I think your code is correct.
What happens if you remove "sniff" option?

Are you connecting to a local node?

Without sniff option, it is same i.e. 12 seconds. I am connecting on Elasticsearch instance on my machine only. Debug logs are given below.

2016-07-28_12:42:32.653 [http-nio-8080-exec-4] INFO  c.g.d.MyClass - Starting creation of Transport Client object without sniff option
2016-07-28_12:42:34.177 [http-nio-8080-exec-4] INFO  org.elasticsearch.plugins - [Vermin] modules [], plugins [], sites []
2016-07-28_12:42:34.356 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [generic], type [cached], keep_alive [30s]
2016-07-28_12:42:34.381 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [index], type [fixed], size [8], queue_size [200]
2016-07-28_12:42:34.384 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [fetch_shard_store], type [scaling], min [1], size [16], keep_alive [5m]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [get], type [fixed], size [8], queue_size [1k]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [snapshot], type [scaling], min [1], size [4], keep_alive [5m]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [force_merge], type [fixed], size [1], queue_size [null]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [suggest], type [fixed], size [8], queue_size [1k]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [bulk], type [fixed], size [8], queue_size [50]
2016-07-28_12:42:34.385 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [warmer], type [scaling], min [1], size [4], keep_alive [5m]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [flush], type [scaling], min [1], size [4], keep_alive [5m]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [search], type [fixed], size [13], queue_size [1k]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [fetch_shard_started], type [scaling], min [1], size [16], keep_alive [5m]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [listener], type [fixed], size [4], queue_size [null]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [percolate], type [fixed], size [8], queue_size [1k]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [management], type [scaling], min [1], size [5], keep_alive [5m]
2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [refresh], type [scaling], min [1], size [4], keep_alive [5m]
2016-07-28_12:42:43.382 [http-nio-8080-exec-4] DEBUG org.elasticsearch.common.network - configuration:

lo
        inet 127.0.0.1 netmask:255.255.255.255 scope:host
        inet6 ::1 prefixlen:128 scope:host
        UP LOOPBACK mtu:65536 index:1

enp1s0
        inet 192.168.1.121 netmask:255.255.255.0 broadcast:192.168.1.255 scope:site
        inet6 fe80::428d:5cff:feee:e19c prefixlen:64 scope:link
        hardware 40:8D:5C:EE:E1:9C
        UP MULTICAST mtu:1500 index:2

2016-07-28_12:42:43.437 [http-nio-8080-exec-4] DEBUG org.elasticsearch.common.netty - using gathering [true]
2016-07-28_12:42:43.524 [http-nio-8080-exec-4] DEBUG org.elasticsearch.client.transport - [Vermin] node_sampler_interval[5s]
2016-07-28_12:42:44.793 [http-nio-8080-exec-4] DEBUG o.e.n.c.socket.nio.SelectorUtil - Using select timeout of 500
2016-07-28_12:42:44.793 [http-nio-8080-exec-4] DEBUG o.e.n.c.socket.nio.SelectorUtil - Epoll-bug workaround enabled = false
2016-07-28_12:42:44.917 [http-nio-8080-exec-4] INFO  c.g.d.MyClass - Transport client object created successfully

Apparently it takes 10s to get Network Information:

2016-07-28_12:42:34.386 [http-nio-8080-exec-4] DEBUG org.elasticsearch.threadpool - [Vermin] creating thread_pool [refresh], type [scaling], min [1], size [4], keep_alive [5m]
2016-07-28_12:42:43.382 [http-nio-8080-exec-4] DEBUG org.elasticsearch.common.network - configuration:

lo
        inet 127.0.0.1 netmask:255.255.255.255 scope:host
        inet6 ::1 prefixlen:128 scope:host
        UP LOOPBACK mtu:65536 index:1

enp1s0
        inet 192.168.1.121 netmask:255.255.255.0 broadcast:192.168.1.255 scope:site
        inet6 fe80::428d:5cff:feee:e19c prefixlen:64 scope:link
        hardware 40:8D:5C:EE:E1:9C
        UP MULTICAST mtu:1500 index:2

What is your elasticsearch version BTW?

To my wonder, when I call my API from my JUnit test case to search documents with sniff = true and sniff = false, it takes less than 1 second to create transport object. But when I call same API from my application deployed on same machine, it takes 12 seconds. :frowning: :frowning: What could be wrong in my application ?

I have ES 2.3.3

Interesting.

When you run that with JUnit, are you connecting to the same external cluster or are you running a local cluster on your machine ?

Its same cluster in both cases
:frowning:

Is your application and your JUnit tests running from the same machine as well?

yes. Everything is on same machine. ES cluster, Junit and my application.

One thing I noticed. Sometimes that object creation takes 4 seconds. I have set network.host property in elasticsearch.yml file to my hostname. ( I have observed that when I am not connected at all to LAN then my elasticsearch node does not start as it is not able to resolve my hostname.) So is that time varying because of network speed? I have a very bad LAN setup. Many times it goes down.

Do you try using IP address or host name?
If hostname, may be it's a DNS resolution thing?

ok... I will try with IP address and revert in some time.

With IP addresses it is taking around 3 seconds. Does that mean my DNS query is taking very long to resolve ? By the way, thank you very much for spending time on my issue. I really appreciate your help.