Allow hostnames instead of IPs for CC/NC startup (Issue 747)

Change-Id: I5337dbe1892d17962f9f20096af8638c3ca02b25
Reviewed-on: http://fulliautomatix.ics.uci.edu:8443/152
Reviewed-by: Chris Hillery <ceej@lambda.nu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/common/AsterixHyracksIntegrationUtil.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/common/AsterixHyracksIntegrationUtil.java
index 11c0a5c..05dd499 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/common/AsterixHyracksIntegrationUtil.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/common/AsterixHyracksIntegrationUtil.java
@@ -63,7 +63,7 @@
         ncConfig1.ccPort = DEFAULT_HYRACKS_CC_CLUSTER_PORT;
         ncConfig1.clusterNetIPAddress = "127.0.0.1";
         ncConfig1.dataIPAddress = "127.0.0.1";
-        ncConfig1.datasetIPAddress = "127.0.0.1";
+        ncConfig1.resultIPAddress = "127.0.0.1";
         ncConfig1.nodeId = NC_IDS[0];
         ncConfig1.resultTTL = 30000;
         ncConfig1.resultSweepThreshold = 1000;
@@ -78,7 +78,7 @@
         ncConfig2.ccPort = DEFAULT_HYRACKS_CC_CLUSTER_PORT;
         ncConfig2.clusterNetIPAddress = "127.0.0.1";
         ncConfig2.dataIPAddress = "127.0.0.1";
-        ncConfig2.datasetIPAddress = "127.0.0.1";
+        ncConfig2.resultIPAddress = "127.0.0.1";
         ncConfig2.nodeId = NC_IDS[1];
         ncConfig2.resultTTL = 30000;
         ncConfig2.resultSweepThreshold = 1000;
diff --git a/asterix-events/src/main/java/edu/uci/ics/asterix/event/management/EventUtil.java b/asterix-events/src/main/java/edu/uci/ics/asterix/event/management/EventUtil.java
index 49b7abf..2f2b9d8 100644
--- a/asterix-events/src/main/java/edu/uci/ics/asterix/event/management/EventUtil.java
+++ b/asterix-events/src/main/java/edu/uci/ics/asterix/event/management/EventUtil.java
@@ -47,22 +47,12 @@
     private static final String SCRIPT = "SCRIPT";
     private static final String ARGS = "ARGS";
     private static final String EXECUTE_SCRIPT = "events/execute.sh";
-    private static final String LOCALHOST = "localhost";
-    private static final String LOCALHOST_IP = "127.0.0.1";
 
     public static Cluster getCluster(String clusterConfigurationPath) throws JAXBException {
         File file = new File(clusterConfigurationPath);
         JAXBContext ctx = JAXBContext.newInstance(Cluster.class);
         Unmarshaller unmarshaller = ctx.createUnmarshaller();
         Cluster cluster = (Cluster) unmarshaller.unmarshal(file);
-        if (cluster.getMasterNode().getClusterIp().equals(LOCALHOST)) {
-            cluster.getMasterNode().setClusterIp(LOCALHOST_IP);
-        }
-        for (Node node : cluster.getNode()) {
-            if (node.getClusterIp().equals(LOCALHOST)) {
-                node.setClusterIp(LOCALHOST_IP);
-            }
-        }
         return cluster;
     }
 
diff --git a/asterix-events/src/main/java/edu/uci/ics/asterix/event/service/AsterixEventServiceUtil.java b/asterix-events/src/main/java/edu/uci/ics/asterix/event/service/AsterixEventServiceUtil.java
index a238ed6..2dc6dad 100644
--- a/asterix-events/src/main/java/edu/uci/ics/asterix/event/service/AsterixEventServiceUtil.java
+++ b/asterix-events/src/main/java/edu/uci/ics/asterix/event/service/AsterixEventServiceUtil.java
@@ -25,6 +25,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
+import java.net.InetAddress;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
@@ -507,8 +508,9 @@
             if (existing.getState().equals(State.INACTIVE)) {
                 continue;
             }
-            conflictFound = existing.getCluster().getMasterNode().getClusterIp().equals(masterIp);
-            if (conflictFound) {
+            InetAddress extantAddress = InetAddress.getByName(existing.getCluster().getMasterNode().getClusterIp());
+            InetAddress masterAddress = InetAddress.getByName(masterIp);
+            if (extantAddress.equals(masterAddress)) {
                 conflictingInstance = existing;
                 break;
             }
diff --git a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/indexing/dataflow/IndexingScheduler.java b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/indexing/dataflow/IndexingScheduler.java
index b207eb9..ee5872d 100644
--- a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/indexing/dataflow/IndexingScheduler.java
+++ b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/indexing/dataflow/IndexingScheduler.java
@@ -322,7 +322,7 @@
              * build the IP address to NC map
              */
             for (Map.Entry<String, NodeControllerInfo> entry : ncNameToNcInfos.entrySet()) {
-                String ipAddr = InetAddress.getByAddress(entry.getValue().getNetworkAddress().getIpAddress())
+                String ipAddr = InetAddress.getByAddress(entry.getValue().getNetworkAddress().lookupIpAddress())
                         .getHostAddress();
                 List<String> matchedNCs = ipToNcMapping.get(ipAddr);
                 if (matchedNCs == null) {
diff --git a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/util/DNSResolver.java b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/util/DNSResolver.java
index 113a112..125b40a 100644
--- a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/util/DNSResolver.java
+++ b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/util/DNSResolver.java
@@ -14,6 +14,8 @@
  */
 package edu.uci.ics.asterix.external.util;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Random;
 import java.util.Set;
 
@@ -30,13 +32,15 @@
     @Override
     public String resolveNode(String value) throws AsterixException {
         try {
-            String ipAddress = AsterixRuntimeUtil.getIPAddress(value);
+            InetAddress ipAddress = InetAddress.getByName(value);
             Set<String> nodeControllers = AsterixRuntimeUtil.getNodeControllersOnIP(ipAddress);
             if (nodeControllers == null || nodeControllers.isEmpty()) {
                 throw new AsterixException(" No node controllers found at the address: " + value);
             }
             String chosenNCId = nodeControllers.toArray(new String[] {})[random.nextInt(nodeControllers.size())];
             return chosenNCId;
+        }catch (UnknownHostException e){
+            throw new AsterixException("Unable to resolve hostname '"+ value + "' to an IP address");
         } catch (AsterixException ae) {
             throw ae;
         } catch (Exception e) {
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
index 5977fa8..f1ba85a 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
@@ -29,8 +29,8 @@
  */
 public class AsterixRuntimeUtil {
 
-    public static Set<String> getNodeControllersOnIP(String ipAddress) throws Exception {
-        Map<String, Set<String>> nodeControllerInfo = getNodeControllerMap();
+    public static Set<String> getNodeControllersOnIP(InetAddress ipAddress) throws Exception {
+        Map<InetAddress, Set<String>> nodeControllerInfo = getNodeControllerMap();
         Set<String> nodeControllersAtLocation = nodeControllerInfo.get(ipAddress);
         return nodeControllersAtLocation;
     }
@@ -44,17 +44,9 @@
         return nodeControllers;
     }
 
-    public static Map<String, Set<String>> getNodeControllerMap() throws Exception {
-        Map<String, Set<String>> map = new HashMap<String, Set<String>>();
+    public static Map<InetAddress, Set<String>> getNodeControllerMap() throws Exception {
+        Map<InetAddress, Set<String>> map = new HashMap<InetAddress, Set<String>>();
         AsterixAppContextInfo.getInstance().getCCApplicationContext().getCCContext().getIPAddressNodeMap(map);
         return map;
     }
-
-    public static String getIPAddress(String hostname) throws UnknownHostException {
-        String address = InetAddress.getByName(hostname).getHostAddress();
-        if (address.equals("127.0.1.1")) {
-            address = "127.0.0.1";
-        }
-        return address;
-    }
 }
diff --git a/asterix-tools/src/main/java/edu/uci/ics/asterix/tools/external/data/GenericSocketFeedAdapterFactory.java b/asterix-tools/src/main/java/edu/uci/ics/asterix/tools/external/data/GenericSocketFeedAdapterFactory.java
index ee3ea19..71ff53a 100644
--- a/asterix-tools/src/main/java/edu/uci/ics/asterix/tools/external/data/GenericSocketFeedAdapterFactory.java
+++ b/asterix-tools/src/main/java/edu/uci/ics/asterix/tools/external/data/GenericSocketFeedAdapterFactory.java
@@ -14,6 +14,7 @@
  */
 package edu.uci.ics.asterix.tools.external.data;
 
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -110,7 +111,7 @@
         if (socketsValue == null) {
             throw new IllegalArgumentException("\'sockets\' parameter not specified as part of adapter configuration");
         }
-        Map<String, Set<String>> ncMap = AsterixRuntimeUtil.getNodeControllerMap();
+        Map<InetAddress, Set<String>> ncMap = AsterixRuntimeUtil.getNodeControllerMap();
         List<String> ncs = AsterixRuntimeUtil.getAllNodeControllers();
         String[] socketsArray = socketsValue.split(",");
         Random random = new Random();
@@ -121,7 +122,7 @@
             Pair<String, Integer> p = null;
             switch (mode) {
                 case IP:
-                    Set<String> ncsOnIp = ncMap.get(host);
+                    Set<String> ncsOnIp = ncMap.get(InetAddress.getByName(host));
                     if (ncsOnIp == null || ncsOnIp.isEmpty()) {
                         throw new IllegalArgumentException("Invalid host " + host
                                 + " as it is not part of the AsterixDB cluster. Valid choices are "