0

I'm running a simple demo that is using the Java zookeeper client to connect zookeeper server. It takes a long time(at least 10 seconds) to connect successfully on windows, but it connects successfully on Linux very quickly. What is the reason and how to deal with it?

solft version:

OS: Windows 11 with two virtual machines, one is Ubuntu server, the other is Ubuntu desktop,

Java: OpenJDK 11,

zookeeper server: version3.7.0, deployed on Ubuntu virtual machine

demo code:

public static void main(String[] args) throws Exception {
    log.info("begin connect to zookeeper ================================>>");
    ZooKeeper zooKeeper = new ZooKeeper("192.168.1.16", 30000, watchedEvent -> {
    });
    log.info("connect zookeeper success =================================>>");
    zooKeeper.close();
    log.info("zookeeper is closed...");
}

result and logs:

  1. demo code run on Win11:
[04-04-22 11:57:32:013 CST] main  INFO provider.ProviderStartup: begin connect to zookeeper ================================>>
[04-04-22 11:57:41:087 CST] main  INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
[04-04-22 11:57:41:087 CST] main  INFO zookeeper.ZooKeeper: Client environment:java.version=11
[04-04-22 11:57:41:088 CST] main  INFO zookeeper.ZooKeeper: Client environment:java.home=D:\jdk-11
[04-04-22 11:57:41:088 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 10
[04-04-22 11:57:41:088 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[04-04-22 11:57:41:088 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.version=10.0
[04-04-22 11:57:41:096 CST] main  INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.1.16 sessionTimeout=30000 watcher=com.gonnaup.dubbo.provider.ProviderStartup$$Lambda$29/0x00000008000dc840@799d4f69
[04-04-22 11:57:41:102 CST] main  INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
[04-04-22 11:57:41:384 CST] main  INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes
[04-04-22 11:57:41:393 CST] main  INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false
[04-04-22 11:57:41:393 CST] main  INFO provider.ProviderStartup: connect zookeeper success =================================>>
[04-04-22 11:57:59:472 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.1.16/192.168.1.16:2181.
[04-04-22 11:57:59:473 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)
[04-04-22 11:57:59:476 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: Socket connection established, initiating session, client: /192.168.1.8:2474, server: 192.168.1.16/192.168.1.16:2181
[04-04-22 11:57:59:596 CST] main  INFO provider.ProviderStartup: zookeeper is closed...
[04-04-22 11:57:59:596 CST] main-EventThread  INFO zookeeper.ClientCnxn: EventThread shut down for session: 0x100001dd5b40058

I omitted some useless logs to reduce the number of words.It can be seen from the logging time that It takes a long time from the connection to the end(see the '======>>' flag).

  1. demo code run on ubuntu desktop virtual machine:
[04-04-22 11:57:03:739 CST] main  INFO simple.ZookeeperCli: begin connect to zookeeper ================================>>
[04-04-22 11:57:03:785 CST] main  INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
[04-04-22 11:57:03:786 CST] main  INFO zookeeper.ZooKeeper: Client environment:java.version=11.0.14.1
[04-04-22 11:57:03:788 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
[04-04-22 11:57:03:788 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
[04-04-22 11:57:03:788 CST] main  INFO zookeeper.ZooKeeper: Client environment:os.version=5.13.0-39-generic
[04-04-22 11:57:03:800 CST] main  INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.1.16 sessionTimeout=30000 watcher=cn.gonnaup.simple.ZookeeperCli$$Lambda$31/0x0000000840083040@47f37ef1
[04-04-22 11:57:03:808 CST] main  INFO common.X509Util: Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
[04-04-22 11:57:03:818 CST] main  INFO zookeeper.ClientCnxnSocket: jute.maxbuffer value is 1048575 Bytes
[04-04-22 11:57:03:838 CST] main  INFO zookeeper.ClientCnxn: zookeeper.request.timeout value is 0. feature enabled=false
[04-04-22 11:57:03:849 CST] main  INFO simple.ZookeeperCli: connect zookeeper success =================================>>
[04-04-22 11:57:03:941 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.1.16/192.168.1.16:2181.
[04-04-22 11:57:03:943 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: SASL config status: Will not attempt to authenticate using SASL (unknown error)
[04-04-22 11:57:03:964 CST] main-SendThread(192.168.1.16:2181)  INFO zookeeper.ClientCnxn: Socket connection established, initiating session, client: /10.0.2.15:56596, server: 192.168.1.16/192.168.1.16:2181
[04-04-22 11:57:04:109 CST] main  INFO zookeeper.ZooKeeper: Session: 0x100001dd5b40057 closed
[04-04-22 11:57:04:109 CST] main  INFO simple.ZookeeperCli: zookeeper is closed...
[04-04-22 11:57:04:109 CST] main-EventThread  INFO zookeeper.ClientCnxn: EventThread shut down for session: 0x100001dd5b40057

That can see connecting zookeeper and closing the connection is completion immediately.

LW001
  • 2,452
  • 6
  • 27
  • 36
gonnaup
  • 1
  • 1
  • Sorry, I left my crystal ball in the other pants. Can you [edit] your question and add more details on how your Linux and Windows setups differ, how the network is between them and zookeper, how exactly you test, and so on? See [ask]. – Robert Apr 03 '22 at 16:35
  • sorry, it's my first time to ask question. I add my environment, code and logs yet. Thanks for your answer and please ignore my poor English. – gonnaup Apr 04 '22 at 04:37

2 Answers2

0

I can't say this is your problem exactly, but do you have some type of antivirus software running on the Windows box that is scanning jar files on access? I've run into that problem on a server in the past, where an antivirus update caused all Zookeeper connections to go from less than a second to connect to approximately 60 seconds while the on-access scanner was checking all jar files that were being touched/loaded, including both Zookeeper and JVM libraries. If you have antivirus, you can try temporarily disabling it while running your connection tests to at least rule that possibility out.

FreemanB
  • 51
  • 1
0

I finally discovered the problem through debug the source code of the Zookeeper. Before JVM instantiate the Zookeeper object, it execute the static block in Zookeeper.class which load your enviroment. There has a method InetAddress.getLocalHost().getCanonicalHostName() which make the code running slowly.I found a solution in getcanonicalhostname-is-very-slow. This is the Npcap Loopback Adapter cause the slowly execution.

gonnaup
  • 1
  • 1