AMBARI-701. Ambari does not handle a pre-setup user-supplied Hive Metastore. (Contributed by hitesh)
svn merge -c r1382097 --ignore-ancestry ../../trunk/


git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/branches/branch-0.9@1382111 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 1498d48..a8b0c18 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,12 @@
 should be listed by their full name. Please keep the file to a max of 80
 characters wide.
 
+Release 0.9.1 - unreleased
+
+  BUGS
+
+    AMBARI-701. Ambari does not handle a pre-setup user-supplied Hive Metastore. (hitesh)
+
 Release 0.9.0 - unreleased
 
   AMBARI-690. Monitoring Dashboard - update the RPM version and documentation (yusaku)
diff --git a/hmc/php/db/HMCDBAccessor.php b/hmc/php/db/HMCDBAccessor.php
index cac93a8..d81803d 100644
--- a/hmc/php/db/HMCDBAccessor.php
+++ b/hmc/php/db/HMCDBAccessor.php
@@ -3429,5 +3429,54 @@
     LockRelease(); return $response;
   }
 
+ /**
+   * Remove host-role mappings for given hostnames from the specified component
+   * @param string $clusterName
+   * @param string $componentName
+   * @param array $hosts Array of hostnames
+   * @return mixed
+   *    array (
+   *       "result" => 0,
+   *       "error" => ""
+   *     )
+   */
+  public function removeAllHostsFromComponent($clusterName, $componentName) {
+    LockAcquire();
+    $response = array ( "result" => 0, "error" => "");
+
+    $error = "";
+    $ret = $this->dbHandle->beginTransaction();
+    if (!$ret) {
+      $error = $this->getLastDBErrorAsString();
+      $response["result"] = 1;
+      $response["error"] = "Failed to start DB transaction, error=".$error;
+      LockRelease(); return $response;
+    }
+    $query = "DELETE FROM HostRoles WHERE "
+        . " cluster_name = " . $this->dbHandle->quote($clusterName)
+        . " AND component_name = " . $this->dbHandle->quote($componentName);
+    $this->logger->log_trace("Running query: $query");
+    $ret = $this->dbHandle->exec($query);
+    if (FALSE === $ret) {
+      $error = $this->getLastDBErrorAsString();
+      $ret = $this->dbHandle->rollBack();
+      $this->logger->log_error("Error when executing query"
+          . ", query=".$query
+          . ", error=".$error);
+      $response["result"] = 1;
+      $response["error"] = $error;
+      LockRelease(); return $response;
+    }
+    $ret = $this->dbHandle->commit();
+    if (!$ret) {
+      $error = $this->getLastDBErrorAsString();
+      $this->logger->log_error("Failed to commit DB transaction, error=".$error);
+      $response["result"] = 1;
+      $response["error"] = "Failed to commit DB transaction, error=". $error;
+      LockRelease(); return $response;
+    }
+    LockRelease(); return $response;
+  }
+
 }
 ?>
diff --git a/hmc/php/frontend/configUtils.php b/hmc/php/frontend/configUtils.php
index b0633ab..23ad7f2 100644
--- a/hmc/php/frontend/configUtils.php
+++ b/hmc/php/frontend/configUtils.php
@@ -453,18 +453,28 @@
 }
 
 ////////////Helper function definitions
-function handleHiveMysql($clusterName, &$finalProperties,
-    $dbAccessor, $logHandle) {
+function handleHiveMysql($clusterName, $dbAccessor, $logHandle) {
   $services = $dbAccessor->getAllServicesInfo($clusterName);
+  $configs = $dbAccessor->getServiceConfig($clusterName);
   $hostForMysql = $dbAccessor->getHostsForComponent($clusterName, "HIVE_MYSQL");
   if ( ($services["services"]["HIVE"]["isEnabled"] == 1) &&
-      (empty($finalProperties["hive_mysql_host"])) && (empty($hostForMysql["hosts"])) ) {
+       ( isset($configs["properties"])
+         && ( !isset($configs["properties"]["hive_mysql_host"])
+              || empty($configs["properties"]["hive_mysql_host"])) ) &&
+       (empty($hostForMysql["hosts"])) ) {
     $logHandle->log_debug("Hive is enabled but mysql server is not set, set it up on hive server itself");
     $hostComponents = $dbAccessor->getHostsForComponent($clusterName, "HIVE_SERVER");
     $hiveServerHosts = array_keys($hostComponents["hosts"]);
-    $finalProperties["hive_mysql_host"] = "localhost";
+    $newConfig = array ( "hive_mysql_host" => "localhost" );  
+    $dbAccessor->updateServiceConfigs($clusterName, $newConfig);
     $dbAccessor->addHostsToComponent($clusterName, "HIVE_MYSQL",
           $hiveServerHosts, "ASSIGNED", "");
+  } else {
+    if (isset($configs["properties"])
+        && isset($configs["properties"]["hive_mysql_host"])
+        && $configs["properties"]["hive_mysql_host"] != "localhost") {
+      $dbAccessor->removeAllHostsFromComponent($clusterName, "HIVE_MYSQL");
+    }
   }
 }
 
@@ -522,10 +532,6 @@
  */
 function validateConfigsFromUser($dbAccessor, $logger, $clusterName, $finalProperties) {
 
-  //Additional services that need to be enabled based on configuration.
-  //Hack to handle mysql for hive
-  handleHiveMysql($clusterName, $finalProperties, $dbAccessor, $logger);
-
   // Validate/verify configs
   $cfgResult = validateConfigs($finalProperties);
   $suggestProperties = new SuggestProperties();
@@ -559,6 +565,7 @@
  * @return array|mixed
  */
 function validateAndPersistConfigsFromUser($dbAccessor, $logger, $clusterName, $finalProperties) {
+
   // sanitize and persist the user entered configs *******
 
   $result = validateConfigsFromUser($dbAccessor, $logger, $clusterName, $finalProperties);
@@ -571,6 +578,11 @@
     $logger->log_error("Got error while persisting configs: ".$dbResponse["error"]);
     return $dbResponse;
   }
+
+  //Additional services that need to be enabled based on configuration.
+  //Hack to handle mysql for hive
+  handleHiveMysql($clusterName, $dbAccessor, $logger);
+
   // finished persisting the configs *******
   return array("result" => 0, "error" => 0);
 }