From cb817d70818976edb76b9fd9bbd226312f50c8ca Mon Sep 17 00:00:00 2001 From: James Taylor Date: Wed, 18 Nov 2020 10:20:58 -0800 Subject: [PATCH] Go through NodeManager to get current host name and address for prestosql --- .../prestosql/ClusterManagerNodeGetter.java | 18 +++++++++++++++ .../rubix/prestosql/PrestoClusterManager.java | 22 +++++++++++++++++-- .../prestosql/StandaloneNodeManager.java | 12 +++------- .../prestosql/SyncPrestoClusterManager.java | 21 ++++++++++++++++-- .../rubix/prestosql/TestClusterManager.java | 3 ++- .../prestosql/TestSyncClusterManager.java | 4 +++- 6 files changed, 65 insertions(+), 15 deletions(-) diff --git a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/ClusterManagerNodeGetter.java b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/ClusterManagerNodeGetter.java index a1a6f4a4..281673ab 100644 --- a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/ClusterManagerNodeGetter.java +++ b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/ClusterManagerNodeGetter.java @@ -14,11 +14,18 @@ import io.prestosql.spi.Node; import io.prestosql.spi.NodeManager; +import java.net.UnknownHostException; import java.util.Set; import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; +/* + * Class that encapsulates logic using NodeManager to satisfy API + * of ClusterManager. This logic was put in a separate class so + * that both PrestoClusterManager and SyncPrestoClusterManager + * can share it. + */ public class ClusterManagerNodeGetter { public static Set getNodesInternal(NodeManager nodeManager) { @@ -27,4 +34,15 @@ public static Set getNodesInternal(NodeManager nodeManager) .map(Node::getHost) .collect(Collectors.toSet()); } + + public static String getCurrentNodeHostname(NodeManager nodeManager) { + requireNonNull(nodeManager, "nodeManager is null"); + return nodeManager.getCurrentNode().getHost(); + } + + public static String getCurrentNodeHostAddress(NodeManager nodeManager) + throws UnknownHostException { + requireNonNull(nodeManager, "nodeManager is null"); + return nodeManager.getCurrentNode().getHostAndPort().toInetAddress().getHostAddress(); + } } diff --git a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/PrestoClusterManager.java b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/PrestoClusterManager.java index a71b3374..8ac89b67 100644 --- a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/PrestoClusterManager.java +++ b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/PrestoClusterManager.java @@ -12,6 +12,8 @@ */ package com.qubole.rubix.prestosql; +import static java.util.Objects.requireNonNull; + import com.qubole.rubix.spi.AsyncClusterManager; import com.qubole.rubix.spi.ClusterType; import io.prestosql.spi.Node; @@ -49,11 +51,27 @@ public void initialize(Configuration conf) throws UnknownHostException } @Override - public Set getNodesInternal() - { + public Set getNodesInternal() { return ClusterManagerNodeGetter.getNodesInternal(nodeManager); } + @Override + protected String getCurrentNodeHostname() { + return ClusterManagerNodeGetter.getCurrentNodeHostname(nodeManager); + } + + @Override + protected String getCurrentNodeHostAddress() { + try { + return ClusterManagerNodeGetter.getCurrentNodeHostAddress(nodeManager); + } + catch (UnknownHostException e) { + log.warn("Could not get HostAddress from NodeManager", e); + // fallback + } + return super.getCurrentNodeHostAddress(); + } + @Override public ClusterType getClusterType() { diff --git a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/StandaloneNodeManager.java b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/StandaloneNodeManager.java index eb57f7a5..781d54b8 100644 --- a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/StandaloneNodeManager.java +++ b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/StandaloneNodeManager.java @@ -52,17 +52,11 @@ public class StandaloneNodeManager private final Node currentNode; private final int serverPort; - public StandaloneNodeManager(Configuration conf) { + public StandaloneNodeManager(Configuration conf) + throws UnknownHostException { this.serverPort = conf.getInt(SERVER_PORT_CONF_KEY, DEFAULT_SERVER_PORT); this.serverAddress = ClusterUtil.getMasterHostname(conf); - Node currentNode = null; - try { - currentNode = new StandaloneNode(URI.create("http://" + InetAddress.getLocalHost().getHostAddress())); - } - catch (UnknownHostException e) { - LOG.warn("Unable to set current node", e); - } - this.currentNode = currentNode; + this.currentNode = new StandaloneNode(URI.create("http://" + InetAddress.getLocalHost().getHostAddress())); } @Override diff --git a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/SyncPrestoClusterManager.java b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/SyncPrestoClusterManager.java index 65e759b6..8332f750 100644 --- a/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/SyncPrestoClusterManager.java +++ b/rubix-prestosql/src/main/java/com/qubole/rubix/prestosql/SyncPrestoClusterManager.java @@ -16,6 +16,7 @@ import com.qubole.rubix.spi.SyncClusterManager; import io.prestosql.spi.Node; import io.prestosql.spi.NodeManager; +import java.net.UnknownHostException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -38,11 +39,27 @@ protected boolean hasStateChanged() { } @Override - public Set getNodesInternal() - { + public Set getNodesInternal() { return ClusterManagerNodeGetter.getNodesInternal(PrestoClusterManager.NODE_MANAGER); } + @Override + protected String getCurrentNodeHostname() { + return ClusterManagerNodeGetter.getCurrentNodeHostname(PrestoClusterManager.NODE_MANAGER); + } + + @Override + protected String getCurrentNodeHostAddress() { + try { + return ClusterManagerNodeGetter.getCurrentNodeHostAddress(PrestoClusterManager.NODE_MANAGER); + } + catch (UnknownHostException e) { + log.warn("Could not get HostAddress from NodeManager", e); + // fallback + } + return super.getCurrentNodeHostAddress(); + } + @Override public ClusterType getClusterType() { diff --git a/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestClusterManager.java b/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestClusterManager.java index c7d2101d..986018b5 100644 --- a/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestClusterManager.java +++ b/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestClusterManager.java @@ -122,7 +122,8 @@ private HttpServer createServer(String endpoint1, HttpHandler handler1, String e return server; } - abstract protected ClusterManager newPrestoClusterManager(Configuration conf); + abstract protected ClusterManager newPrestoClusterManager(Configuration conf) + throws UnknownHostException; private ClusterManager getPrestoClusterManager() throws UnknownHostException diff --git a/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestSyncClusterManager.java b/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestSyncClusterManager.java index 4634ed9a..b34a043f 100644 --- a/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestSyncClusterManager.java +++ b/rubix-prestosql/src/test/java/com/qubole/rubix/prestosql/TestSyncClusterManager.java @@ -13,11 +13,13 @@ package com.qubole.rubix.prestosql; import com.qubole.rubix.spi.ClusterManager; +import java.net.UnknownHostException; import org.apache.hadoop.conf.Configuration; public class TestSyncClusterManager extends TestClusterManager { @Override - protected ClusterManager newPrestoClusterManager(Configuration conf) { + protected ClusterManager newPrestoClusterManager(Configuration conf) + throws UnknownHostException { PrestoClusterManager.setNodeManager(new StandaloneNodeManager(conf)); return new SyncPrestoClusterManager(); }