suggestProperties.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. class SuggestProperties {
  20. private $logger;
  21. /**
  22. * Needs a default constructor else warnings will appear.
  23. */
  24. function __construct() {
  25. $this->logger = new HMCLogger("SuggestProperties");
  26. }
  27. /**
  28. * Allocate Heap Size for a component given what all processes are running
  29. * on the host.
  30. */
  31. function allocateHeapSizeForDaemon($componentName, $hostRoles, $hostInfoMap,
  32. $allHostsToComponents, $is32bit) {
  33. // TODO fix
  34. // code should handle 32-bit checks - cannot assign over 4 GB for a role
  35. // which uses 32-bit procs
  36. // if os is 32-bit we have even more restrictions
  37. // for now assuming 64-bit os
  38. $this->logger->log_info("Calculating Heap Size For ".$componentName);
  39. $host = $this->getHostForComponent($hostRoles, $componentName);
  40. $this->logger->log_info("Model HostName for ".$componentName." ".$host);
  41. $hostMem = $hostInfoMap[$host]["totalMem"]*0.9;
  42. $numProcs = sizeof($allHostsToComponents["hosts"][$host]["components"]);
  43. $normalizedMem = (int) (ceil ($hostMem/$numProcs));
  44. if ($is32bit) {
  45. $normalizedMem = min($normalizedMem, (pow(2,32)/(1024*1024))-1);
  46. }
  47. $normalizedMem = ((int)($normalizedMem/8))*8;
  48. $this->logger->log_info("Component=" . $componentName . " Host="
  49. . $host." Mem=".$hostMem." numComponents=".$numProcs.
  50. " perComponentMem=".$normalizedMem);
  51. if ($normalizedMem < 256) {
  52. $this->logger->log_info("Normalizing memory to 256 as min required");
  53. $normalizedMem = 256;
  54. }
  55. return $normalizedMem;
  56. }
  57. function getMaxHeapSizeForDaemon($componentName, $hostRoles, $hostInfoMap,
  58. $allHostsToComponents, $is32bit) {
  59. $this->logger->log_info("Calculating Max Heap Size For ".$componentName);
  60. $host = $this->getHostForComponent($hostRoles, $componentName);
  61. $this->logger->log_info("Model HostName for ".$componentName." ".$host);
  62. $hostMem = $hostInfoMap[$host]["totalMem"];
  63. $normalizedMem = $hostMem;
  64. if ($is32bit) {
  65. $normalizedMem = min($normalizedMem, (pow(2,32)/(1024*1024))-1);
  66. }
  67. $this->logger->log_info("Component=" . $componentName . " Host="
  68. . $host." HostMem=".$hostMem
  69. ." MaxNormalizedMem=".$normalizedMem);
  70. return $normalizedMem;
  71. }
  72. /**
  73. * get the host info in a list, convert it to a map so that easy
  74. * to lookup
  75. */
  76. function createHostToInfoMap($hostInfo) {
  77. $hosts = $hostInfo["hosts"];
  78. $result = array();
  79. foreach($hosts as $host) {
  80. $result[$host["hostName"]] = $host;
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Return a single host that maps to a master service.
  86. */
  87. function getHostForComponent($hostRoles, $role) {
  88. $listHosts = $hostRoles["components"][$role]["hosts"];
  89. foreach ($listHosts as $hostName=>$hostInfo) {
  90. $retHost = $hostName;
  91. break;
  92. }
  93. return $retHost;
  94. }
  95. /** Return only the enabled services.
  96. */
  97. function filterEnabledServices($services) {
  98. $enabledServices = array();
  99. foreach($services as $serviceName=>$serviceInfo) {
  100. if ($serviceInfo["isEnabled"] == 1) {
  101. $enabledServices[$serviceName] = $serviceInfo;
  102. }
  103. }
  104. return $enabledServices;
  105. }
  106. /**
  107. * Function to suggest Properties to the user.
  108. * It will read the db to get the sevices that are configured
  109. * return back the configs with suggestions based on the services that are
  110. * configured.
  111. * NOTE: It will only return recommended configs - does not return other
  112. * props or defaults from DB
  113. * @param clustername the name of the cluster we are deploying/managing
  114. * @param db database from where to read, usually pass in new HMCDBAccessor("mydb.data");
  115. * @param updateDB bool whether to update db with suggested settings
  116. * @return mixed
  117. * array (
  118. * "result" => 0,
  119. * "error" => "",
  120. * "configs" => array(
  121. * "key" => "val",
  122. * ...
  123. * )
  124. * );
  125. */
  126. public function suggestProperties($clusterName, $db,
  127. $updateDB) {
  128. $result = array();
  129. $result["result"] = 0;
  130. $result["error"] = "";
  131. $servicesDBInfo = $db->getAllServicesInfo($clusterName);
  132. if ($servicesDBInfo["result"] != 0) {
  133. $result["result"] = $servicesDBInfo["result"];
  134. $result["error"] = $servicesDBInfo["error"];
  135. return $result;
  136. }
  137. $services_tmp = $servicesDBInfo["services"];
  138. $services = $this->filterEnabledServices($services_tmp);
  139. $this->logger->log_debug("Services Enabled \n".print_r($services, true));
  140. $hostRoles = $db->getAllHostsByComponent($clusterName);
  141. if ($hostRoles["result"] != 0) {
  142. $result["result"] = $hostRoles["result"];
  143. $result["error"] = $hostRoles["error"];
  144. return $result;
  145. }
  146. $order = array("sortColumn" => "cpuCount",
  147. "sortOrder" => "ASC");
  148. $allHosts = $db->getAllHostsInfo($clusterName,
  149. array("=" => array ( "discoveryStatus" => "SUCCESS")), $order);
  150. if ($allHosts["result"] != 0) {
  151. $result["result"] = $allHosts["result"];
  152. $result["error"] = $allHosts["error"];
  153. return $result;
  154. }
  155. // convert host list to a map so thats easy to lookup
  156. $hostInfoMap = $this->createHostToInfoMap($allHosts);
  157. $allHostsToComponents = $db->getAllHostsToComponentMap($clusterName);
  158. if ($allHostsToComponents["result"] != 0) {
  159. $result["result"] = $allHostsToComponents["result"];
  160. $result["error"] = $allHostsToComponents["error"];
  161. return $result;
  162. }
  163. // filter host roles for client-only components
  164. $ignoredComponents = array();
  165. $allComponents = $db->getAllServiceComponentsList();
  166. if ($allComponents["result"] == 0) {
  167. if (isset($allComponents["services"])
  168. && is_array($allComponents["services"])) {
  169. foreach ($allComponents["services"] as $svcName => $svcInfo) {
  170. if (isset($svcInfo["components"])
  171. && is_array($svcInfo["components"])) {
  172. foreach ($svcInfo["components"] as $compName => $compInfo) {
  173. if (isset($compInfo["isClient"]) && $compInfo["isClient"]) {
  174. $ignoredComponents[$compName] = TRUE;
  175. } else if ($compName == "GANGLIA_MONITOR") {
  176. $ignoredComponents[$compName] = TRUE;
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. foreach ($allHostsToComponents["hosts"] as $hostName => $compList) {
  184. $newComps = array();
  185. foreach ($compList["components"] as $compName) {
  186. if (!isset($ignoredComponents[$compName])) {
  187. array_push($newComps, $compName);
  188. }
  189. }
  190. $allHostsToComponents["hosts"][$hostName]["components"] = $newComps;
  191. }
  192. $result["configs"] = array();
  193. // set the num map/reduce tasks
  194. // assuming that there is atleast one host
  195. if (count($allHosts["hosts"]) == 1) {
  196. // for single node install use 2 maps and 2 reduce slots
  197. $this->logger->log_info("Single node install: Using Num Maps 2, Num Reduces 2");
  198. $result["configs"]["mapred_map_tasks_max"] = 2;
  199. $result["configs"]["mapred_red_tasks_max"] = 2;
  200. } else {
  201. $minCpuHost = $allHosts["hosts"][0];
  202. $this->logger->log_info("Host Info with Min Cpu \n".print_r($minCpuHost, true));
  203. $minCpus = $minCpuHost["cpuCount"];
  204. $numMap = (int) (ceil ($minCpus/3 * 2 * 2)); // 2/3'rd of cpucount and multiply it by 2.
  205. if ($numMap <= 0) {
  206. $numMap = 1;
  207. }
  208. $numRed = ($minCpus * 2) - $numMap;
  209. if ($numRed <= 0) {
  210. $numRed = 1;
  211. }
  212. $this->logger->log_info("Num Maps ".$numMap ." Num Reduces ".$numRed);
  213. $result["configs"]["mapred_map_tasks_max"] = $numMap;
  214. $result["configs"]["mapred_red_tasks_max"] = $numRed;
  215. }
  216. /* suggest memory for all the needed master daemons */
  217. /* assume MR and HDFS are always selected */
  218. $nnHeap = $this->allocateHeapSizeForDaemon("NAMENODE", $hostRoles,
  219. $hostInfoMap, $allHostsToComponents, FALSE);
  220. $result["configs"]["namenode_heapsize"] = $nnHeap;
  221. /* suggest the jt heap size */
  222. $jtHeap = $this->allocateHeapSizeForDaemon("JOBTRACKER", $hostRoles,
  223. $hostInfoMap, $allHostsToComponents, FALSE);
  224. $result["configs"]["jtnode_heapsize"] = $jtHeap;
  225. /* check if HBase is installed and then pick */
  226. if (array_key_exists("HBASE", $services)) {
  227. $hbaseHeap = $this->allocateHeapSizeForDaemon("HBASE_MASTER", $hostRoles,
  228. $hostInfoMap, $allHostsToComponents, FALSE);
  229. $result["configs"]["hbase_master_heapsize"] = $hbaseHeap;
  230. }
  231. $heapSize = $this->allocateHeapSizeForDaemon("DATANODE", $hostRoles,
  232. $hostInfoMap, $allHostsToComponents, TRUE);
  233. $result["configs"]["dtnode_heapsize"] = $heapSize;
  234. $result["configs"]["hadoop_heapsize"] = $heapSize;
  235. // TODO fix - this should be based on heap size divided by max task
  236. // limit on the host
  237. $heapSize = $this->allocateHeapSizeForDaemon("TASKTRACKER", $hostRoles,
  238. $hostInfoMap, $allHostsToComponents, TRUE);
  239. $result["configs"]["mapred_child_java_opts_sz"] = $heapSize;
  240. if (array_key_exists("HBASE", $services)) {
  241. $heapSize = $this->allocateHeapSizeForDaemon("HBASE_REGIONSERVER", $hostRoles,
  242. $hostInfoMap, $allHostsToComponents, FALSE);
  243. $result["configs"]["hbase_regionserver_heapsize"] = $heapSize;
  244. }
  245. /** TODO change this to be from the UI later **/
  246. $hostname = strtolower(exec('hostname -f'));
  247. $result["configs"]["jdk_location"] = "http://".$hostname."/downloads";
  248. if ($updateDB) {
  249. $this->logger->log_info("Updating suggested configs into DB");
  250. $ret = $db->updateServiceConfigs($clusterName, $result["configs"]);
  251. if ($ret["result"] != 0) {
  252. $this->logger->log_error("Error updating suggested configs into DB"
  253. . ", result=" . $ret["result"]
  254. . ", error=" . $ret["error"]);
  255. $result["result"] = $ret["result"];
  256. $result["error"] = $ret["error"];
  257. }
  258. }
  259. $this->logger->log_info("Calculated Config Parameters \n".print_r($result, true));
  260. return $result;
  261. }
  262. /**
  263. * Verify properties set in DB
  264. * @param string $clusterName
  265. * @param object $db - HMCDBAccessor
  266. * @param mixed $configs
  267. * array ( "prop_key1" => "prop_val1", ... )
  268. * @return mixed
  269. * array (
  270. * "result" => 0,
  271. * "error" => "",
  272. * "cfgErrors" => array (
  273. * "propKey" => array (
  274. * "value" => "current val in DB",
  275. * "recommendedValue" => $recoVal,
  276. * "error" => "reason why this is an error"
  277. * ),
  278. * ...
  279. * ),
  280. * "cfgWarnings" => array (
  281. * "propKey" => array (
  282. * "value" => "current val in DB",
  283. * "recommendedValue" => $recoVal,
  284. * "error" => "reason why this is an warning"
  285. * ),
  286. * ...
  287. * )
  288. * )
  289. */
  290. public function verifyProperties($clusterName, $db, $configs) {
  291. $result = array();
  292. $result["result"] = 0;
  293. $result["error"] = "";
  294. $servicesDBInfo = $db->getAllServicesInfo($clusterName);
  295. if ($servicesDBInfo["result"] != 0) {
  296. $result["result"] = $servicesDBInfo["result"];
  297. $result["error"] = $servicesDBInfo["error"];
  298. return $result;
  299. }
  300. $services_tmp = $servicesDBInfo["services"];
  301. $services = $this->filterEnabledServices($services_tmp);
  302. $this->logger->log_debug("Services Enabled \n".print_r($services, true));
  303. $hostRoles = $db->getAllHostsByComponent($clusterName);
  304. if ($hostRoles["result"] != 0) {
  305. $result["result"] = $hostRoles["result"];
  306. $result["error"] = $hostRoles["error"];
  307. return $result;
  308. }
  309. $order = array("sortColumn" => "cpuCount",
  310. "sortOrder" => "ASC");
  311. $allHosts = $db->getAllHostsInfo($clusterName,
  312. array("=" => array ( "discoveryStatus" => "SUCCESS")), $order);
  313. if ($allHosts["result"] != 0) {
  314. $result["result"] = $allHosts["result"];
  315. $result["error"] = $allHosts["error"];
  316. return $result;
  317. }
  318. // convert host list to a map so thats easy to lookup
  319. $hostInfoMap = $this->createHostToInfoMap($allHosts);
  320. $allHostsToComponents = $db->getAllHostsToComponentMap($clusterName);
  321. if ($allHostsToComponents["result"] != 0) {
  322. $result["result"] = $allHostsToComponents["result"];
  323. $result["error"] = $allHostsToComponents["error"];
  324. return $result;
  325. }
  326. $recommendedInfo = $this->suggestProperties($clusterName, $db, FALSE);
  327. if ($recommendedInfo["result"] != 0) {
  328. $result["result"] = $recommendedInfo["result"];
  329. $result["error"] = $recommendedInfo["error"];
  330. return $result;
  331. }
  332. $recommendedConfigs = $recommendedInfo["configs"];
  333. // errors => array ( key => array ( value, recommended_value, reason ))
  334. $cfgErrors = array();
  335. $cfgWarnings = array();
  336. // verify map and reduce tasks max settings
  337. if (isset($configs["mapred_map_tasks_max"])
  338. && isset($configs["mapred_red_tasks_max"])) {
  339. if ($configs["mapred_map_tasks_max"] == 0
  340. || $configs["mapred_red_tasks_max"] == 0) {
  341. $reason = "Value cannot be 0";
  342. if ($configs["mapred_map_tasks_max"] == 0) {
  343. $cfgErrors["mapred_map_tasks_max"] = array ( "value" => 0,
  344. "recommendedValue" => $recommendedConfigs["mapred_map_tasks_max"],
  345. "error" => $reason);
  346. }
  347. if ($configs["mapred_red_tasks_max"] == 0) {
  348. $cfgErrors["mapred_red_tasks_max"] = array ( "value" => 0,
  349. "recommendedValue" => $recommendedConfigs["mapred_red_tasks_max"],
  350. "error" => $reason);
  351. }
  352. }
  353. if ($configs["mapred_map_tasks_max"] >
  354. $recommendedConfigs["mapred_map_tasks_max"]) {
  355. $cfgWarnings["mapred_map_tasks_max"] = array (
  356. "value" => $configs["mapred_map_tasks_max"],
  357. "recommendedValue" => $recommendedConfigs["mapred_map_tasks_max"],
  358. "error" => "Value greater than recommended");
  359. }
  360. if ($configs["mapred_red_tasks_max"] >
  361. $recommendedConfigs["mapred_red_tasks_max"]) {
  362. $cfgWarnings["mapred_red_tasks_max"] = array (
  363. "value" => $configs["mapred_red_tasks_max"],
  364. "recommendedValue" => $recommendedConfigs["mapred_red_tasks_max"],
  365. "error" => "Value greater than recommended");
  366. }
  367. }
  368. $memProps = array (
  369. "namenode_heapsize" => array ( "role" => "NAMENODE", "32bit" => FALSE),
  370. "jtnode_heapsize" => array ( "role" => "JOBTRACKER", "32bit" => FALSE),
  371. "dtnode_heapsize" => array ( "role" => "DATANODE", "32bit" => TRUE),
  372. "hadoop_heapsize" => array ( "role" => "DATANODE", "32bit" => TRUE),
  373. "mapred_child_java_opts_sz" => array ( "role" => "TASKTRACKER", "32bit" => TRUE)
  374. );
  375. if (array_key_exists("HBASE", $services)) {
  376. $memProps["hbase_master_heapsize"] =
  377. array ( "role" => "HBASE_MASTER", "32bit" => FALSE);
  378. $memProps["hbase_regionserver_heapsize"] =
  379. array ( "role" => "HBASE_REGIONSERVER", "32bit" => FALSE);
  380. }
  381. foreach ($memProps as $prop => $propInfo) {
  382. if (!isset($configs[$prop])) {
  383. continue;
  384. }
  385. if ($configs[$prop] < 256) {
  386. $reason = "Value less than min 256M";
  387. $cfgErrors[$prop] = array (
  388. "value" => $configs[$prop],
  389. "recommendedValue" => $recommendedConfigs[$prop],
  390. "error" => $reason
  391. );
  392. continue;
  393. }
  394. if ($configs[$prop] >
  395. $recommendedConfigs[$prop]) {
  396. $maxHeap = $this->getMaxHeapSizeForDaemon($propInfo["role"], $hostRoles,
  397. $hostInfoMap, $allHostsToComponents, $propInfo["32bit"]);
  398. if ($configs[$prop] > $maxHeap) {
  399. $reason = "Value greater than mem limit allowed";
  400. $cfgErrors[$prop] = array (
  401. "value" => $configs[$prop],
  402. "recommendedValue" => $recommendedConfigs[$prop],
  403. "error" => $reason
  404. );
  405. } else {
  406. $reason = "Value greater than recommended mem limit";
  407. $cfgWarnings[$prop] = array (
  408. "value" => $configs[$prop],
  409. "recommendedValue" => $recommendedConfigs[$prop],
  410. "error" => $reason
  411. );
  412. }
  413. }
  414. }
  415. $result = count($cfgErrors);
  416. $error = "";
  417. if ($result != 0 ) {
  418. $error = "Invalid Configs";
  419. }
  420. return array ("result" => $result, "error" => $error,
  421. "cfgErrors" => $cfgErrors,
  422. "cfgWarnings" => $cfgWarnings);
  423. }
  424. }
  425. ?>