You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by si...@apache.org on 2018/10/28 12:56:51 UTC

[bookkeeper] branch master updated: Don't cache Bookie hostname DNS resolution forever

This is an automated email from the ASF dual-hosted git repository.

sijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d8bad4  Don't cache Bookie hostname DNS resolution forever
3d8bad4 is described below

commit 3d8bad44a40ce5cb173df5dfd7cb06d81bed26e1
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Sun Oct 28 05:56:47 2018 -0700

    Don't cache Bookie hostname DNS resolution forever
    
    ### Motivation
    
    `BookieSocketAddress` is resolving the bookie DNS name in its constructor and then using the already resolved `InetSocketAddress` instance.
    
    If the IP of a bookie changes, the BK client will continue to use the old IP address.
    
    ### Changes
    
    Construct a new `InetSocketAddress` each time `getSocketAddress()` gets called (eg: each time we attempt to make a new connection) so that we're making sure to get the right IP.
    
    I cannot think of a good way to add unit test for this at this point, suggestions are welcome.
    
    I think this should be included in a patch release as well 4.7.3 or 4.8.1
    
    Reviewers: Andrey Yegorov <None>, Enrico Olivelli <eo...@gmail.com>, Sijie Guo <si...@apache.org>, Ivan Kelly <iv...@apache.org>
    
    This closes #1762 from merlimat/fix-dns
---
 .../java/org/apache/bookkeeper/net/BookieSocketAddress.java   | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/net/BookieSocketAddress.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/net/BookieSocketAddress.java
index 4e8f324..6d562e3 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/net/BookieSocketAddress.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/net/BookieSocketAddress.java
@@ -39,13 +39,10 @@ public class BookieSocketAddress {
     private final String hostname;
     private final int port;
 
-    private final InetSocketAddress socketAddress;
-
     // Constructor that takes in both a port.
     public BookieSocketAddress(String hostname, int port) {
         this.hostname = hostname;
         this.port = port;
-        socketAddress = new InetSocketAddress(hostname, port);
     }
 
     // Constructor from a String "serialized" version of this class.
@@ -60,7 +57,6 @@ public class BookieSocketAddress {
         } catch (NumberFormatException nfe) {
             throw new UnknownHostException(addr);
         }
-        socketAddress = new InetSocketAddress(hostname, port);
     }
 
     // Public getters
@@ -74,7 +70,12 @@ public class BookieSocketAddress {
 
     // Method to return an InetSocketAddress for the regular port.
     public InetSocketAddress getSocketAddress() {
-        return socketAddress;
+        // Return each time a new instance of the InetSocketAddress because the hostname
+        // gets resolved in its constructor and then cached forever.
+        // If we keep using the same InetSocketAddress instance, if bookies are advertising
+        // hostnames and the IP change, the BK client will keep forever to try to connect
+        // to the old IP.
+        return new InetSocketAddress(hostname, port);
     }
 
     /**