zookeeper-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anmolnar <...@git.apache.org>
Subject [GitHub] zookeeper pull request #534: ZOOKEEPER-2184 Zookeeper Client should re-resol...
Date Mon, 04 Jun 2018 10:36:19 GMT
Github user anmolnar commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/534#discussion_r192696652
  
    --- Diff: src/java/main/org/apache/zookeeper/client/StaticHostProvider.java ---
    @@ -96,36 +115,46 @@ public StaticHostProvider(Collection<InetSocketAddress> serverAddresses)
{
          */
         public StaticHostProvider(Collection<InetSocketAddress> serverAddresses,
             long randomnessSeed) {
    -        sourceOfRandomness = new Random(randomnessSeed);
    +        init(serverAddresses, randomnessSeed, new Resolver() {
    +            @Override
    +            public InetAddress[] getAllByName(String name) throws UnknownHostException
{
    +                return InetAddress.getAllByName(name);
    +            }
    +        });
    +    }
     
    -        this.serverAddresses = resolveAndShuffle(serverAddresses);
    -        if (this.serverAddresses.isEmpty()) {
    +    private void init(Collection<InetSocketAddress> serverAddresses, long randomnessSeed,
Resolver resolver) {
    +        this.sourceOfRandomness = new Random(randomnessSeed);
    +        this.resolver = resolver;
    +        if (serverAddresses.isEmpty()) {
                 throw new IllegalArgumentException(
                         "A HostProvider may not be empty!");
    -        }       
    +        }
    +        this.serverAddresses = shuffle(serverAddresses);
             currentIndex = -1;
    -        lastIndex = -1;              
    +        lastIndex = -1;
         }
     
    -    private List<InetSocketAddress> resolveAndShuffle(Collection<InetSocketAddress>
serverAddresses) {
    -        List<InetSocketAddress> tmpList = new ArrayList<InetSocketAddress>(serverAddresses.size());
      
    -        for (InetSocketAddress address : serverAddresses) {
    -            try {
    -                InetAddress ia = address.getAddress();
    -                String addr = (ia != null) ? ia.getHostAddress() : address.getHostString();
    -                InetAddress resolvedAddresses[] = InetAddress.getAllByName(addr);
    -                for (InetAddress resolvedAddress : resolvedAddresses) {
    -                    InetAddress taddr = InetAddress.getByAddress(address.getHostString(),
resolvedAddress.getAddress());
    -                    tmpList.add(new InetSocketAddress(taddr, address.getPort()));
    -                }
    -            } catch (UnknownHostException ex) {
    -                LOG.warn("No IP address found for server: {}", address, ex);
    +    private InetSocketAddress resolve(InetSocketAddress address) {
    +        try {
    +            String curHostString = address.getHostString();
    +            List<InetAddress> resolvedAddresses = new ArrayList<>(Arrays.asList(this.resolver.getAllByName(curHostString)));
    +            if (resolvedAddresses.isEmpty()) {
    +                return address;
                 }
    +            Collections.shuffle(resolvedAddresses);
    +            return new InetSocketAddress(resolvedAddresses.get(0), address.getPort());
    +        } catch (UnknownHostException e) {
    --- End diff --
    
    That's correct. The caller will end up getting UnknownHostException when trying to open
the socket to the unresolvable address:
    ```
    2018-06-04 12:31:26,022 [myid:huhuuhujkdshgfjksgd.com:2181] - WARN  [main-SendThread(huhuuhujkdshgfjksgd.com:2181):ClientCnxn$SendThread@1237]
- Session 0x0 for server huhuuhujkdshgfjksgd.com:2181, unexpected error, closing socket connection
and attempting reconnect
    java.nio.channels.UnresolvedAddressException
    	at sun.nio.ch.Net.checkAddress(Net.java:101)
    	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622)
    	at org.apache.zookeeper.ClientCnxnSocketNIO.registerAndConnect(ClientCnxnSocketNIO.java:275)
    	at org.apache.zookeeper.ClientCnxnSocketNIO.connect(ClientCnxnSocketNIO.java:285)
    	at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1091)
    	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1133)
    ```
    
    Logging makes sense, I added an error log entry to make it clear.


---

Mime
View raw message