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

stoty pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix-omid.git


The following commit(s) were added to refs/heads/master by this push:
     new a9c14a37 OMID-249 Improve default network address logic
a9c14a37 is described below

commit a9c14a37427a03dbb04c3292082f8d2c9b25e350
Author: Istvan Toth <st...@apache.org>
AuthorDate: Fri Jan 19 14:33:59 2024 +0100

    OMID-249 Improve default network address logic
---
 .../org/apache/omid/tso/NetworkInterfaceUtils.java | 40 +++++++++++++++++++++-
 .../java/org/apache/omid/tso/TSOServerConfig.java  |  2 +-
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git 
a/tso-server/src/main/java/org/apache/omid/tso/NetworkInterfaceUtils.java 
b/tso-server/src/main/java/org/apache/omid/tso/NetworkInterfaceUtils.java
index ad39bf31..ab932389 100644
--- a/tso-server/src/main/java/org/apache/omid/tso/NetworkInterfaceUtils.java
+++ b/tso-server/src/main/java/org/apache/omid/tso/NetworkInterfaceUtils.java
@@ -17,12 +17,16 @@
  */
 package org.apache.omid.tso;
 
+import org.apache.omid.NetworkUtils;
 import org.apache.phoenix.thirdparty.com.google.common.net.HostAndPort;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.inject.Module;
+
 import java.net.InetAddress;
 import java.net.NetworkInterface;
+import java.net.Socket;
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.Enumeration;
@@ -106,9 +110,24 @@ final public class NetworkInterfaceUtils {
     }
 
     public static String getTSOHostAndPort(TSOServerConfig config) throws 
SocketException, UnknownHostException {
+        if (config.getNetworkIfaceName() == null) {
+            try {
+                return getTSOHostAndPortRelativeToZK(config);
+            } catch (Exception e) {
+                LOG.info("Could not determine local address relative to ZK 
server", e);
+                // Fall back to interface guessing
+            }
+        };
+        return getTSOHostAndPortFromInterface(config);
+    }
+
+    public static String getTSOHostAndPortFromInterface(TSOServerConfig 
config) throws SocketException, UnknownHostException {
 
         // Build TSO host:port string and validate it
-        final String tsoNetIfaceName = config.getNetworkIfaceName();
+        String tsoNetIfaceName = config.getNetworkIfaceName();
+        if (tsoNetIfaceName == null) {
+            tsoNetIfaceName = NetworkUtils.getDefaultNetworkInterface();
+        }
         InetAddress addr = getIPAddressFromNetworkInterface(tsoNetIfaceName);
         final int tsoPort = config.getPort();
 
@@ -120,7 +139,26 @@ final public class NetworkInterfaceUtils {
             throw e;
         }
         return tsoHostAndPortAsString;
+    }
 
+    public static String getTSOHostAndPortRelativeToZK(TSOServerConfig config) 
throws Exception {
+        Module leaseModule = config.getLeaseModule();
+        String zkQuorum;
+        if (leaseModule instanceof HALeaseManagementModule) {
+            LOG.info("HA is configured. Trying to determine local address 
facing ZK server");
+            zkQuorum = ((HALeaseManagementModule) leaseModule).getZkCluster();
+            // Zookeeper doesn't expose its socket, so we have to try and 
parse the quorum.
+            String firstHost = zkQuorum.split(",")[0];
+            LOG.info("ZK quorum is {}, first server is {}", zkQuorum, 
firstHost);
+            HostAndPort hostAndPort = HostAndPort.fromString(firstHost);
+            Socket socket = new Socket(hostAndPort.getHost(), 
hostAndPort.getPort());
+            InetAddress addr = socket.getLocalAddress();
+            socket.close();
+            LOG.info("Local address facing ZK server is {}", addr);
+            return HostAndPort.fromParts(addr.getHostAddress(), 
config.getPort()).toString();
+        } else {
+            throw new Exception("HA is not configured");
+        }
     }
 
 }
diff --git a/tso-server/src/main/java/org/apache/omid/tso/TSOServerConfig.java 
b/tso-server/src/main/java/org/apache/omid/tso/TSOServerConfig.java
index 53d8c011..b1e7a066 100644
--- a/tso-server/src/main/java/org/apache/omid/tso/TSOServerConfig.java
+++ b/tso-server/src/main/java/org/apache/omid/tso/TSOServerConfig.java
@@ -87,7 +87,7 @@ public class TSOServerConfig extends SecureHBaseConfig {
 
     private String waitStrategy;
 
-    private String networkIfaceName = 
NetworkUtils.getDefaultNetworkInterface();
+    private String networkIfaceName;
 
     private String timestampType;
 

Reply via email to