incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rez...@apache.org
Subject svn commit: r1362337 - in /incubator/ambari/trunk/hmc/php: db/HMCDBAccessor.php frontend/fetchGangliaClusters.php
Date Tue, 17 Jul 2012 03:07:53 GMT
Author: reznor
Date: Tue Jul 17 03:07:53 2012
New Revision: 1362337

URL: http://svn.apache.org/viewvc?rev=1362337&view=rev
Log:
AMBARI-640 Add entrypoint to fetch info pertaining to Ganglia Clusters.

Added:
    incubator/ambari/trunk/hmc/php/frontend/fetchGangliaClusters.php
Modified:
    incubator/ambari/trunk/hmc/php/db/HMCDBAccessor.php

Modified: incubator/ambari/trunk/hmc/php/db/HMCDBAccessor.php
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/hmc/php/db/HMCDBAccessor.php?rev=1362337&r1=1362336&r2=1362337&view=diff
==============================================================================
--- incubator/ambari/trunk/hmc/php/db/HMCDBAccessor.php (original)
+++ incubator/ambari/trunk/hmc/php/db/HMCDBAccessor.php Tue Jul 17 03:07:53 2012
@@ -1976,6 +1976,72 @@ class HMCDBAccessor {
   }
 
   /**
+  * Get host information for all hosts mapped to any component, grouped by component.
+  *
+  * @param string $clusterName
+  * @return mixed
+  *    array (
+  *       "result" => 0,
+  *       "error" => "",
+  *       "clusterName" => $clusterName,
+  *       "components" => array (
+  *          "$componentName" => array (
+  *              "componentName" => $componentName,
+  *              "hosts" => array (
+  *                  "$hostName" => array (
+  *                      "hostName" => $hostName
+  *                      "hostIp" => $hostIp
+  *                      "rackInfo" => $rackInfo
+  *                     )
+  *                  )
+  *              )
+  *          )
+  *     )
+  */
+  public function getAllHostsInfoByComponent($clusterName) {
+    LockAcquire();
+    $response = array ( "result" => 0, "error" => "");
+    $error = "";
+    $query = "SELECT HostRoles.component_name as component_name"
+        . ", HostRoles.host_name as host_name"
+        . ", Hosts.ip as host_ip"
+        . ", Hosts.rack_info as rack_info"
+        . " FROM HostRoles JOIN Hosts"
+        . " ON HostRoles.host_name = Hosts.host_name"
+        . " WHERE HostRoles.cluster_name = " . $this->dbHandle->quote($clusterName);
+    $this->logger->log_trace("Running query: $query");
+    $pdoStmt = $this->dbHandle->query($query);
+    if ($pdoStmt === FALSE) {
+      $error = $this->getLastDBErrorAsString();
+      $this->logger->log_error("Error when executing query"
+      . ", query=".$query
+      . ", error=".$error);
+      $response["result"] = 1;
+      $response["error"] = $error;
+      LockRelease(); return $response;
+    }
+    $result = $pdoStmt->fetchAll(PDO::FETCH_BOTH);
+    $response["clusterName"] = $clusterName;
+    $response["components"] = array();
+    foreach ($result as $entry) {
+      $componentName = $entry["component_name"];
+      if (!isset($response["components"][$componentName])) {
+        $response["components"][$componentName] = array();
+        $response["components"][$componentName]["componentName"] =
+            $componentName;
+        $response["components"][$componentName]["hosts"] = array();
+      }
+      $hostInfo = array();
+      $hostInfo["hostName"] = $entry["host_name"];
+      $hostInfo["hostIp"] = $entry["host_ip"];
+      $hostInfo["rackInfo"] = $entry["rack_info"];
+      $response["components"][$componentName]["hosts"]
+          [$hostInfo["hostName"]] = $hostInfo;
+    }
+    LockRelease(); return $response;
+  }
+
+  /**
    * Get all hosts mapped to all the  components
    * @param string $clusterName
    * @return mixed
@@ -2080,6 +2146,7 @@ class HMCDBAccessor {
     LockRelease(); return $response;
   }
 
+
   /**
    * Set the state for specified hosts mapped to given component/role
    * @param string $clusterName

Added: incubator/ambari/trunk/hmc/php/frontend/fetchGangliaClusters.php
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/hmc/php/frontend/fetchGangliaClusters.php?rev=1362337&view=auto
==============================================================================
--- incubator/ambari/trunk/hmc/php/frontend/fetchGangliaClusters.php (added)
+++ incubator/ambari/trunk/hmc/php/frontend/fetchGangliaClusters.php Tue Jul 17 03:07:53 2012
@@ -0,0 +1,104 @@
+<?php
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+
+include_once '../util/Logger.php';
+include_once '../conf/Config.inc';
+include_once "../util/lock.php";
+include_once '../db/HMCDBAccessor.php';
+
+$dbPath = $GLOBALS["DB_PATH"];
+
+header("Content-type: application/json");
+
+$logger = new HMCLogger("FetchGangliaClusterHosts");
+$dbAccessor = new HMCDBAccessor($dbPath);
+
+$clusterName = $_GET['clusterName'];
+
+$gangliaClusterNameToServiceComponentNamesMapping = 
+  array( 
+    'HDPHBaseMaster' => array('HBASE_MASTER'),
+    'HDPJobTracker' => array('JOBTRACKER'), 
+    'HDPNameNode' => array('NAMENODE','SNAMENODE'),
+    'HDPSlaves' => array('HBASE_REGIONSERVER','TASKTRACKER','DATANODE') 
+  );
+
+/* The meat of what we're here to return. */
+$gangliaClusters = array();
+
+/* The Main Event. */
+
+$allHostsInfoByComponent = $dbAccessor->getAllHostsInfoByComponent($clusterName);
+
+if( $allHostsInfoByComponent['result'] == 0 ) {
+
+  /* Use $gangliaClusterNameToServiceComponentNamesMapping as our guiding light. */
+  foreach( $gangliaClusterNameToServiceComponentNamesMapping as $gangliaClusterName =>
$serviceComponentNames ) {
+
+    /* Reinitialize this "working copy" for each $gangliaClusterName. */
+    $currentGangliaClusterHostsInfo = array();
+
+    /* Each Ganglia Cluster Name maps to a list of Service Component Names, so 
+     * gather information for the hosts in ALL the relevant Service Components.
+     */
+    foreach( $serviceComponentNames as $serviceComponentName ) {
+      if( isset($allHostsInfoByComponent['components'][$serviceComponentName]) ) {
+        foreach( $allHostsInfoByComponent['components'][$serviceComponentName]['hosts'] as
$hostName => $hostInfo ) {
+          $currentGangliaClusterHostsInfo[] = $hostInfo;
+        }
+      }
+    }
+
+    /* De-dupe all the host info entries before storing a final list for $gangliaClusterName.

+     *
+     * When we enforce a minimum version of PHP > 5.2.9, replace the encode+decode trick
below with:
+     * 
+     *    $gangliaClusters[$gangliaClusterName] = array_unique($currentGangliaClusterHostsInfo,
SORT_REGULAR);
+     */
+    $gangliaClusters[$gangliaClusterName] = 
+      array_map('json_decode', array_unique(array_map('json_encode', $currentGangliaClusterHostsInfo)));
+  }
+
+} else {
+  $gotError = "Error while getting all hosts info by component: " . $allHostsInfoByComponent['error']
;
+  $logger->log_error($gotError);
+  print (json_encode( array("result" => 1, "error" => $gotError)));
+  return;
+}
+
+$result = 0;
+$error = "";
+/* Create the output data... */
+$jsonOutput = array(
+  'result' => $result,
+  'error' => $error,
+  'response' => array(
+    'clusterName' => $clusterName,
+    'gangliaClusters' => $gangliaClusters)
+);
+
+/* ...and spit it out. */
+
+print (json_encode($jsonOutput));
+
+?>



Mime
View raw message