Repository: ambari
Updated Branches:
  refs/heads/trunk 968ff91d9 -> 53c5b621a


AMBARI-10879: If hostname used by agents is upper-case or mixed-case then 
upgrading to Ambari-2.0 may create issues (jluniya)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/53c5b621
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/53c5b621
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/53c5b621

Branch: refs/heads/trunk
Commit: 53c5b621a80fcd1ff6450948875071938f063419
Parents: 968ff91
Author: Jayush Luniya <jlun...@hortonworks.com>
Authored: Fri May 1 01:22:46 2015 -0700
Committer: Jayush Luniya <jlun...@hortonworks.com>
Committed: Fri May 1 01:22:46 2015 -0700

----------------------------------------------------------------------
 .../src/main/python/ambari_agent/hostname.py    | 18 ++--
 .../server/upgrade/UpgradeCatalog210.java       | 86 +++++++++++++++++---
 2 files changed, 80 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/53c5b621/ambari-agent/src/main/python/ambari_agent/hostname.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/hostname.py 
b/ambari-agent/src/main/python/ambari_agent/hostname.py
index e33b739..9c03d98 100644
--- a/ambari-agent/src/main/python/ambari_agent/hostname.py
+++ b/ambari-agent/src/main/python/ambari_agent/hostname.py
@@ -45,11 +45,12 @@ def hostname(config):
       if (0 == osStat.returncode and 0 != len(out.strip())):
         cached_hostname = out.strip()
       else:
-        cached_hostname = socket.getfqdn().lower()
+        cached_hostname = socket.getfqdn()
     except:
-      cached_hostname = socket.getfqdn().lower()
+      cached_hostname = socket.getfqdn()
   except:
-    cached_hostname = socket.getfqdn().lower()
+    cached_hostname = socket.getfqdn()
+  cached_hostname = cached_hostname.lower()
   return cached_hostname
 
 
@@ -66,7 +67,7 @@ def public_hostname(config):
       output = subprocess.Popen(scriptname, stdout=subprocess.PIPE, 
stderr=subprocess.PIPE, shell=True)
       out, err = output.communicate()
       if (0 == output.returncode and 0 != len(out.strip())):
-        cached_public_hostname = out.strip()
+        cached_public_hostname = out.strip().lower()
         logger.info("Read public hostname '" + cached_public_hostname + "' 
using agent:public_hostname_script")
         return cached_public_hostname
   except:
@@ -76,14 +77,7 @@ def public_hostname(config):
                 + " :out " + out + " :err " + err)
     logger.info("Defaulting to fqdn.")
 
-  # future - do an agent entry for this too
-  try:
-    handle = 
urllib2.urlopen('http://169.254.169.254/latest/meta-data/public-hostname', '', 
2)
-    str = handle.read()
-    handle.close()
-    cached_public_hostname = str
-  except Exception, e:
-    cached_public_hostname = socket.getfqdn().lower()
+  cached_public_hostname = socket.getfqdn().lower()
   return cached_public_hostname
 
 def server_hostname(config):

http://git-wip-us.apache.org/repos/asf/ambari/blob/53c5b621/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
index c7866db..d376cd6 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog210.java
@@ -74,8 +74,11 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
   private static final String VIEW_PARAMETER_TABLE = "viewparameter";
   private static final String STACK_TABLE = "stack";
   private static final String REPO_VERSION_TABLE = "repo_version";
+  private static final String ALERT_HISTORY_TABLE = "alert_history";
 
   private static final String HOST_ID_COL = "host_id";
+  private static final String HOST_NAME_COL = "host_name";
+  private static final String PUBLIC_HOST_NAME_COL = "public_host_name";
 
   // constants for stack table changes
   private static final String STACK_ID_COLUMN_NAME = "stack_id";
@@ -371,16 +374,16 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
         " ADD CONSTRAINT serviceconfighosts_pkey PRIMARY KEY 
(service_config_id, host_id)");
 
     // Finish by deleting the unnecessary host_name columns.
-    dbAccessor.dropColumn(CONFIG_GROUP_HOST_MAPPING_TABLE, "host_name");
-    dbAccessor.dropColumn(CLUSTER_HOST_MAPPING_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_CONFIG_MAPPING_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_COMPONENT_STATE_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_COMPONENT_DESIRED_STATE_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_ROLE_COMMAND_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_STATE_TABLE, "host_name");
-    dbAccessor.dropColumn(HOST_VERSION_TABLE, "host_name");
-    dbAccessor.dropColumn(KERBEROS_PRINCIPAL_HOST_TABLE, "host_name");
-    dbAccessor.dropColumn(REQUEST_OPERATION_LEVEL_TABLE, "host_name");
+    dbAccessor.dropColumn(CONFIG_GROUP_HOST_MAPPING_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(CLUSTER_HOST_MAPPING_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_CONFIG_MAPPING_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_COMPONENT_STATE_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_COMPONENT_DESIRED_STATE_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_ROLE_COMMAND_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_STATE_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(HOST_VERSION_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(KERBEROS_PRINCIPAL_HOST_TABLE, HOST_NAME_COL);
+    dbAccessor.dropColumn(REQUEST_OPERATION_LEVEL_TABLE, HOST_NAME_COL);
 
     // Notice that the column name doesn't have an underscore here.
     dbAccessor.dropColumn(SERVICE_CONFIG_HOSTS_TABLE, "hostname");
@@ -388,6 +391,20 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
     // view columns for cluster association
     dbAccessor.addColumn(VIEW_INSTANCE_TABLE, new 
DBColumnInfo("cluster_handle", String.class, 255, null, true));
     dbAccessor.addColumn(VIEW_PARAMETER_TABLE, new 
DBColumnInfo("cluster_config", String.class, 255, null, true));
+
+    // Update host names to be case insensitive
+    String UPDATE_TEMPLATE = "UPDATE {0} SET {1} = lower({1})";
+    // First remove duplicate hosts
+    removeDuplicateHosts();
+    // Lowercase host name in hosts
+    String updateHostName = MessageFormat.format(UPDATE_TEMPLATE, HOSTS_TABLE, 
HOST_NAME_COL);
+    dbAccessor.executeQuery(updateHostName);
+    // Lowercase public host name in hosts
+    String updatePublicHostName = MessageFormat.format(UPDATE_TEMPLATE, 
HOSTS_TABLE, PUBLIC_HOST_NAME_COL);
+    dbAccessor.executeQuery(updatePublicHostName);
+    // Lowercase host name in alert_history
+    String updateAlertHostName = MessageFormat.format(UPDATE_TEMPLATE, 
ALERT_HISTORY_TABLE, HOST_NAME_COL);
+    dbAccessor.executeQuery(updateAlertHostName);
   }
 
   private void executeWidgetDDLUpdates() throws AmbariException, SQLException {
@@ -633,7 +650,7 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
 
           if (StringUtils.isNotBlank(hostName)) {
             dbAccessor.executeQuery("UPDATE " + HOSTS_TABLE + " SET host_id = 
" + hostId +
-                " WHERE host_name = '" + hostName + "'");
+                " WHERE " + HOST_NAME_COL + " = '" + hostName + "'");
           }
         }
       } catch (Exception e) {
@@ -647,7 +664,7 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
     String randomHostName = null;
     ResultSet resultSet = null;
     try {
-      resultSet = dbAccessor.executeSelect("SELECT host_name FROM hosts ORDER 
BY host_name ASC");
+      resultSet = dbAccessor.executeSelect("SELECT " + HOST_NAME_COL + " FROM 
" + HOSTS_TABLE + " ORDER BY " + HOST_NAME_COL + " ASC");
       if (resultSet != null && resultSet.next()) {
         randomHostName = resultSet.getString(1);
       }
@@ -662,6 +679,51 @@ public class UpgradeCatalog210 extends 
AbstractUpgradeCatalog {
   }
 
   /**
+   * Remove duplicate hosts before making host name case-insensitive
+   * @throws SQLException
+   */
+  private void removeDuplicateHosts() throws SQLException {
+    // Select hosts not in the cluster
+    String hostsNotInClusterQuery = MessageFormat.format(
+        "SELECT * FROM {0} WHERE {1} NOT IN (SELECT {1} FROM {2})",
+        HOSTS_TABLE, HOST_ID_COL, CLUSTER_HOST_MAPPING_TABLE);
+    ResultSet hostsNotInCluster = null;
+    try {
+      hostsNotInCluster = dbAccessor.executeSelect(hostsNotInClusterQuery);
+      if(hostsNotInCluster != null) {
+        while (hostsNotInCluster.next()) {
+          long hostToDeleteId = hostsNotInCluster.getLong(HOST_ID_COL);
+          String hostToDeleteName = hostsNotInCluster.getString(HOST_NAME_COL);
+          String duplicateHostsQuery = "SELECT count(*) FROM hosts WHERE 
lower(host_name) = '" + hostToDeleteName + "' AND host_id != " + hostToDeleteId;
+          long count = 0;
+          ResultSet duplicateHosts = null;
+          try {
+            duplicateHosts = dbAccessor.executeSelect(duplicateHostsQuery);
+            if (duplicateHosts != null && duplicateHosts.next()) {
+              count = duplicateHosts.getLong(1);
+            }
+          } finally {
+            if (null != duplicateHosts) {
+              duplicateHosts.close();
+            }
+          }
+          if (count > 0) {
+            // Delete hosts and host_state table entries for this duplicate 
host entry
+            dbAccessor.executeQuery(
+                MessageFormat.format("DELETE from {0} WHERE {1} = {2}", 
HOST_STATE_TABLE, HOST_ID_COL, hostToDeleteId));
+            dbAccessor.executeQuery(
+                MessageFormat.format("DELETE from {0} WHERE {1} = {2}", 
HOSTS_TABLE, HOST_ID_COL, hostToDeleteId));
+          }
+        }
+      }
+    } finally {
+      if (null != hostsNotInCluster) {
+        hostsNotInCluster.close();
+      }
+    }
+  }
+
+  /**
    * Get the constraint name created by Derby if one was not specified for the 
table.
    * @param type Constraint-type, either, "p" (Primary), "c" (Check), "f" 
(Foreign), "u" (Unique)
    * @param tableName Table Name

Reply via email to