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

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


The following commit(s) were added to refs/heads/master by this push:
     new 37728a43 CURATOR-664: Use getHostString() to build connection string 
in EnsembleTracker even for wildcard addresses (#452)
37728a43 is described below

commit 37728a431e64dad8b989d4cc4ae5a4171e15f667
Author: Denes Daniel <[email protected]>
AuthorDate: Fri Mar 31 13:50:53 2023 +0200

    CURATOR-664: Use getHostString() to build connection string in 
EnsembleTracker even for wildcard addresses (#452)
---
 .../org/apache/curator/utils/Compatibility.java    |  4 +--
 .../curator/framework/imps/EnsembleTracker.java    | 26 +++++++++++--------
 .../framework/imps/TestReconfiguration.java        | 30 +++++++++++++++++++---
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git 
a/curator-client/src/main/java/org/apache/curator/utils/Compatibility.java 
b/curator-client/src/main/java/org/apache/curator/utils/Compatibility.java
index 4fb1979e..5e5f2987 100644
--- a/curator-client/src/main/java/org/apache/curator/utils/Compatibility.java
+++ b/curator-client/src/main/java/org/apache/curator/utils/Compatibility.java
@@ -88,7 +88,7 @@ public class Compatibility
         return (addrField != null);
     }
 
-    public static String getHostAddress(QuorumPeer.QuorumServer server)
+    public static String getHostString(QuorumPeer.QuorumServer server)
     {
         InetSocketAddress address = null;
         if ( getReachableOrOneMethod != null )
@@ -113,7 +113,7 @@ public class Compatibility
                 log.error("Could not call addrField.get({})", server, e);
             }
         }
-        return (address != null && address.getAddress() != null) ? 
address.getAddress().getHostAddress() : "unknown";
+        return address != null ? address.getHostString() : "unknown";
     }
 
     public static boolean hasPersistentWatchers()
diff --git 
a/curator-framework/src/main/java/org/apache/curator/framework/imps/EnsembleTracker.java
 
b/curator-framework/src/main/java/org/apache/curator/framework/imps/EnsembleTracker.java
index aef536f9..82ba6e2f 100644
--- 
a/curator-framework/src/main/java/org/apache/curator/framework/imps/EnsembleTracker.java
+++ 
b/curator-framework/src/main/java/org/apache/curator/framework/imps/EnsembleTracker.java
@@ -42,6 +42,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import java.io.ByteArrayInputStream;
 import java.io.Closeable;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.util.Properties;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
@@ -179,21 +181,25 @@ public class EnsembleTracker implements Closeable, 
CuratorWatcher
             {
                 sb.append(",");
             }
-            String hostAddress;
-            if ( server.clientAddr.getAddress().isAnyLocalAddress() )
-            {
-                hostAddress = Compatibility.getHostAddress(server);
-            }
-            else
-            {
-                hostAddress = server.clientAddr.getHostString();
-            }
-            
sb.append(hostAddress).append(":").append(server.clientAddr.getPort());
+            
sb.append(getHostString(server)).append(":").append(server.clientAddr.getPort());
         }
 
         return sb.toString();
     }
 
+    private static String getHostString(QuorumPeer.QuorumServer server) {
+        InetSocketAddress clientAddr = server.clientAddr;
+        InetAddress clientIpAddr = clientAddr.getAddress();
+        if ( clientIpAddr != null && clientIpAddr.isAnyLocalAddress() )
+        {
+            return Compatibility.getHostString(server);
+        }
+        else
+        {
+            return clientAddr.getHostString();
+        }
+    }
+
     private void processConfigData(byte[] data) throws Exception
     {
         Properties properties = new Properties();
diff --git 
a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java
 
b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java
index c4b10616..13c162e3 100644
--- 
a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java
+++ 
b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestReconfiguration.java
@@ -477,11 +477,35 @@ public class TestReconfiguration extends CuratorTestBase
     }
 
     @Test
-    public void testHostname() throws Exception
+    public void testConfigToConnectionStringPreservesHostnameNormal() throws 
Exception
     {
-        String config = 
"server.1=localhost:2888:3888:participant;localhost:2181";
+        String config = 
"server.1=server.addr:2888:3888:participant;client.addr:2181";
         String configString = 
EnsembleTracker.configToConnectionString(toQuorumVerifier(config.getBytes()));
-        assertEquals("localhost:2181", configString);
+        assertEquals("client.addr:2181", configString);
+    }
+
+    @Test
+    public void testConfigToConnectionStringPreservesHostnameNoClientAddr() 
throws Exception
+    {
+        String config = "server.1=server.addr:2888:3888:participant;2181";
+        String configString = 
EnsembleTracker.configToConnectionString(toQuorumVerifier(config.getBytes()));
+        assertEquals("server.addr:2181", configString);
+    }
+
+    @Test
+    public void testConfigToConnectionStringPreservesHostnameIPv4Wildcard() 
throws Exception
+    {
+        String config = 
"server.1=server.addr:2888:3888:participant;0.0.0.0:2181";
+        String configString = 
EnsembleTracker.configToConnectionString(toQuorumVerifier(config.getBytes()));
+        assertEquals("server.addr:2181", configString);
+    }
+
+    @Test
+    public void testConfigToConnectionStringPreservesHostnameIPv6Wildcard() 
throws Exception
+    {
+        String config = "server.1=server.addr:2888:3888:participant;[::]:2181";
+        String configString = 
EnsembleTracker.configToConnectionString(toQuorumVerifier(config.getBytes()));
+        assertEquals("server.addr:2181", configString);
     }
 
     @Test

Reply via email to