From 19dd652a887efed7039cbe2bf67536dc3ee6148e Mon Sep 17 00:00:00 2001 From: jrcrawfo Date: Thu, 25 Jan 2018 22:28:06 -0800 Subject: [PATCH] Fix for localhost metrics manager --- .../common/utils/misc/PhysicalPlanHelper.java | 6 +++++- .../heron/network/MetricsManagerClient.java | 9 +++++++-- .../metricscachemgr/MetricsCacheManager.java | 15 ++++++++++++++- .../com/twitter/heron/spi/utils/NetworkUtils.java | 6 +++++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/heron/common/src/java/com/twitter/heron/common/utils/misc/PhysicalPlanHelper.java b/heron/common/src/java/com/twitter/heron/common/utils/misc/PhysicalPlanHelper.java index 3e2b7d524d6..f4c59044318 100644 --- a/heron/common/src/java/com/twitter/heron/common/utils/misc/PhysicalPlanHelper.java +++ b/heron/common/src/java/com/twitter/heron/common/utils/misc/PhysicalPlanHelper.java @@ -111,7 +111,11 @@ public PhysicalPlanHelper(PhysicalPlans.PhysicalPlan pplan, String instanceId) { } try { - this.hostname = InetAddress.getLocalHost().getHostName(); + if (System.getenv("HOST") != null) { + this.hostname = System.getenv("HOST"); + } else { + this.hostname = InetAddress.getLocalHost().getHostName(); + } } catch (UnknownHostException e) { throw new RuntimeException("GetHostName failed"); } diff --git a/heron/instance/src/java/com/twitter/heron/network/MetricsManagerClient.java b/heron/instance/src/java/com/twitter/heron/network/MetricsManagerClient.java index fe0d9f7e495..9606504713e 100644 --- a/heron/instance/src/java/com/twitter/heron/network/MetricsManagerClient.java +++ b/heron/instance/src/java/com/twitter/heron/network/MetricsManagerClient.java @@ -72,9 +72,14 @@ public MetricsManagerClient(NIOLooper s, String metricsHost, int metricsPort, addMetricsManagerClientTasksOnWakeUp(); try { - this.hostname = InetAddress.getLocalHost().getHostName(); + if (System.getenv("HOST") != null) { + this.hostname = System.getenv("HOST"); + } else { + this.hostname = InetAddress.getLocalHost().getHostName(); + } } catch (UnknownHostException e) { - throw new RuntimeException("GetHostName failed"); + LOG.log(Level.SEVERE, "Unable to get local host name", e); + this.hostname = "localhost"; } } diff --git a/heron/metricscachemgr/src/java/com/twitter/heron/metricscachemgr/MetricsCacheManager.java b/heron/metricscachemgr/src/java/com/twitter/heron/metricscachemgr/MetricsCacheManager.java index 53f671f2923..167121d744c 100644 --- a/heron/metricscachemgr/src/java/com/twitter/heron/metricscachemgr/MetricsCacheManager.java +++ b/heron/metricscachemgr/src/java/com/twitter/heron/metricscachemgr/MetricsCacheManager.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -354,12 +355,24 @@ public static void main(String[] args) throws Exception { .build()); LOG.info("Cli Config: " + config.toString()); + String hostname; + try { + if (System.getenv("HOST") != null) { + hostname = System.getenv("HOST"); + } else { + hostname = InetAddress.getLocalHost().getHostName(); + } + } catch( UnknownHostException e) { + LOG.log(Level.SEVERE, "Unable to get local hostname", e); + hostname = "localhost"; + } + // build metricsCache location TopologyMaster.MetricsCacheLocation metricsCacheLocation = TopologyMaster.MetricsCacheLocation.newBuilder() .setTopologyName(topologyName) .setTopologyId(topologyId) - .setHost(InetAddress.getLocalHost().getHostName()) + .setHost(hostname) .setControllerPort(-1) // not used for metricscache .setMasterPort(masterPort) .setStatsPort(statsPort) diff --git a/heron/spi/src/java/com/twitter/heron/spi/utils/NetworkUtils.java b/heron/spi/src/java/com/twitter/heron/spi/utils/NetworkUtils.java index f564f7f8b26..0353bb1a0a7 100644 --- a/heron/spi/src/java/com/twitter/heron/spi/utils/NetworkUtils.java +++ b/heron/spi/src/java/com/twitter/heron/spi/utils/NetworkUtils.java @@ -399,7 +399,11 @@ public static boolean checkHttpResponseCode(HttpURLConnection connection, int ex public static String getHostName() { String hostName; try { - hostName = InetAddress.getLocalHost().getHostName(); + if (System.getenv("HOST") != null) { + hostName = System.getenv("HOST"); + } else { + hostName = InetAddress.getLocalHost().getHostName(); + } } catch (UnknownHostException e) { LOG.log(Level.SEVERE, "Unable to get local host name", e); hostName = "localhost";