suggestProperties.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 allocateHeapSizeWithMax($componentName, $hostRoles, $hostInfoMap,
  58. $allHostsToComponents, $is32bit, $max) {
  59. $heapSizeT = $this->allocateHeapSizeForDaemon($componentName, $hostRoles, $hostInfoMap,
  60. $allHostsToComponents, $is32bit);
  61. if ($heapSizeT > $max) {
  62. $heapSizeT = $max;
  63. }
  64. $this->logger->log_info("Calculating Maxed Heap Size For ".$componentName ." $heapSizeT with max $max" );
  65. return $heapSizeT;
  66. }
  67. function getMaxHeapSizeForDaemon($componentName, $hostRoles, $hostInfoMap,
  68. $allHostsToComponents, $is32bit) {
  69. $this->logger->log_info("Calculating Max Heap Size For ".$componentName);
  70. $host = $this->getHostForComponent($hostRoles, $componentName);
  71. $this->logger->log_info("Model HostName for ".$componentName." ".$host);
  72. $hostMem = $hostInfoMap[$host]["totalMem"];
  73. $normalizedMem = $hostMem;
  74. if ($is32bit) {
  75. $normalizedMem = min($normalizedMem, (pow(2,32)/(1024*1024))-1);
  76. }
  77. $this->logger->log_info("Component=" . $componentName . " Host="
  78. . $host." HostMem=".$hostMem
  79. ." MaxNormalizedMem=".$normalizedMem);
  80. return $normalizedMem;
  81. }
  82. /**
  83. * get the host info in a list, convert it to a map so that easy
  84. * to lookup
  85. */
  86. function createHostToInfoMap($hostInfo) {
  87. $hosts = $hostInfo["hosts"];
  88. $result = array();
  89. foreach($hosts as $host) {
  90. $result[$host["hostName"]] = $host;
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Return a single host that maps to a master service.
  96. */
  97. function getHostForComponent($hostRoles, $role) {
  98. $listHosts = $hostRoles["components"][$role]["hosts"];
  99. foreach ($listHosts as $hostName=>$hostInfo) {
  100. $retHost = $hostName;
  101. break;
  102. }
  103. return $retHost;
  104. }
  105. /** Return only the enabled services.
  106. */
  107. function filterEnabledServices($services) {
  108. $enabledServices = array();
  109. foreach($services as $serviceName=>$serviceInfo) {
  110. if ($serviceInfo["isEnabled"] == 1) {
  111. $enabledServices[$serviceName] = $serviceInfo;
  112. }
  113. }
  114. return $enabledServices;
  115. }
  116. /**
  117. * Function to suggest Properties to the user.
  118. * It will read the db to get the sevices that are configured
  119. * return back the configs with suggestions based on the services that are
  120. * configured.
  121. * NOTE: It will only return recommended configs - does not return other
  122. * props or defaults from DB
  123. * @param clustername the name of the cluster we are deploying/managing
  124. * @param db database from where to read, usually pass in new HMCDBAccessor("mydb.data");
  125. * @param updateDB bool whether to update db with suggested settings
  126. * @return mixed
  127. * array (
  128. * "result" => 0,
  129. * "error" => "",
  130. * "configs" => array(
  131. * "key" => "val",
  132. * ...
  133. * )
  134. * );
  135. */
  136. public function suggestProperties($clusterName, $db,
  137. $updateDB) {
  138. $result = array();
  139. $result["result"] = 0;
  140. $result["error"] = "";
  141. $servicesDBInfo = $db->getAllServicesInfo($clusterName);
  142. if ($servicesDBInfo["result"] != 0) {
  143. $result["result"] = $servicesDBInfo["result"];
  144. $result["error"] = $servicesDBInfo["error"];
  145. return $result;
  146. }
  147. $services_tmp = $servicesDBInfo["services"];
  148. $services = $this->filterEnabledServices($services_tmp);
  149. $this->logger->log_debug("Services Enabled \n".print_r($services, true));
  150. $hostRoles = $db->getAllHostsByComponent($clusterName);
  151. if ($hostRoles["result"] != 0) {
  152. $result["result"] = $hostRoles["result"];
  153. $result["error"] = $hostRoles["error"];
  154. return $result;
  155. }
  156. $order = array("sortColumn" => "cpuCount",
  157. "sortOrder" => "ASC");
  158. $allHosts = $db->getAllHostsInfo($clusterName,
  159. array("=" => array ( "discoveryStatus" => "SUCCESS")), $order);
  160. if ($allHosts["result"] != 0) {
  161. $result["result"] = $allHosts["result"];
  162. $result["error"] = $allHosts["error"];
  163. return $result;
  164. }
  165. // convert host list to a map so thats easy to lookup
  166. $hostInfoMap = $this->createHostToInfoMap($allHosts);
  167. $allHostsToComponents = $db->getAllHostsToComponentMap($clusterName);
  168. if ($allHostsToComponents["result"] != 0) {
  169. $result["result"] = $allHostsToComponents["result"];
  170. $result["error"] = $allHostsToComponents["error"];
  171. return $result;
  172. }
  173. // filter host roles for client-only components
  174. $ignoredComponents = array();
  175. $allComponents = $db->getAllServiceComponentsList();
  176. if ($allComponents["result"] == 0) {
  177. if (isset($allComponents["services"])
  178. && is_array($allComponents["services"])) {
  179. foreach ($allComponents["services"] as $svcName => $svcInfo) {
  180. if (isset($svcInfo["components"])
  181. && is_array($svcInfo["components"])) {
  182. foreach ($svcInfo["components"] as $compName => $compInfo) {
  183. if (isset($compInfo["isClient"]) && $compInfo["isClient"]) {
  184. $ignoredComponents[$compName] = TRUE;
  185. } else if ($compName == "GANGLIA_MONITOR") {
  186. $ignoredComponents[$compName] = TRUE;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. foreach ($allHostsToComponents["hosts"] as $hostName => $compList) {
  194. $newComps = array();
  195. foreach ($compList["components"] as $compName) {
  196. if (!isset($ignoredComponents[$compName])) {
  197. array_push($newComps, $compName);
  198. }
  199. }
  200. $allHostsToComponents["hosts"][$hostName]["components"] = $newComps;
  201. }
  202. $result["configs"] = array();
  203. // set the num map/reduce tasks
  204. // assuming that there is atleast one host
  205. if (count($allHosts["hosts"]) == 1) {
  206. // for single node install use 2 maps and 2 reduce slots
  207. $this->logger->log_info("Single node install: Using Num Maps 2, Num Reduces 2");
  208. $result["configs"]["mapred_map_tasks_max"] = 2;
  209. $result["configs"]["mapred_red_tasks_max"] = 2;
  210. } else {
  211. $minCpuHost = $allHosts["hosts"][0];
  212. $this->logger->log_info("Host Info with Min Cpu \n".print_r($minCpuHost, true));
  213. $minCpus = $minCpuHost["cpuCount"];
  214. $numMap = (int) (ceil ($minCpus/3 * 2 * 2)); // 2/3'rd of cpucount and multiply it by 2.
  215. if ($numMap <= 0) {
  216. $numMap = 1;
  217. }
  218. $numRed = ($minCpus * 2) - $numMap;
  219. if ($numRed <= 0) {
  220. $numRed = 1;
  221. }
  222. $this->logger->log_info("Num Maps ".$numMap ." Num Reduces ".$numRed);
  223. $result["configs"]["mapred_map_tasks_max"] = $numMap;
  224. $result["configs"]["mapred_red_tasks_max"] = $numRed;
  225. }
  226. /* suggest memory for all the needed master daemons */
  227. /* assume MR and HDFS are always selected */
  228. $nnHeap = $this->allocateHeapSizeForDaemon("NAMENODE", $hostRoles,
  229. $hostInfoMap, $allHostsToComponents, FALSE);
  230. $result["configs"]["namenode_heapsize"] = $nnHeap;
  231. /* suggest the jt heap size */
  232. $jtHeap = $this->allocateHeapSizeForDaemon("JOBTRACKER", $hostRoles,
  233. $hostInfoMap, $allHostsToComponents, FALSE);
  234. $result["configs"]["jtnode_heapsize"] = $jtHeap;
  235. /* check if HBase is installed and then pick */
  236. if (array_key_exists("HBASE", $services)) {
  237. $hbaseHeap = $this->allocateHeapSizeForDaemon("HBASE_MASTER", $hostRoles,
  238. $hostInfoMap, $allHostsToComponents, FALSE);
  239. $result["configs"]["hbase_master_heapsize"] = $hbaseHeap;
  240. }
  241. $heapSize = $this->allocateHeapSizeWithMax("DATANODE", $hostRoles,
  242. $hostInfoMap, $allHostsToComponents, TRUE, 2048);
  243. // cap the datanode heap size and hadoop heap size
  244. $result["configs"]["dtnode_heapsize"] = $heapSize;
  245. $result["configs"]["hadoop_heapsize"] = $heapSize;
  246. // TODO fix - this should be based on heap size divided by max task
  247. // limit on the host
  248. $heapSize = $this->allocateHeapSizeForDaemon("TASKTRACKER", $hostRoles,
  249. $hostInfoMap, $allHostsToComponents, TRUE);
  250. $heapSizeWithMax = $this->allocateHeapSizeWithMax("TASKTRACKER", $hostRoles,
  251. $hostInfoMap, $allHostsToComponents, TRUE, 3072);
  252. $this->logger->log_info("Maxed Heap Size for MR Child opts ".$heapSizeWithMax);
  253. $result["configs"]["mapred_child_java_opts_sz"] = $heapSizeWithMax;
  254. if (array_key_exists("HBASE", $services)) {
  255. $heapSize = $this->allocateHeapSizeForDaemon("HBASE_REGIONSERVER", $hostRoles,
  256. $hostInfoMap, $allHostsToComponents, FALSE);
  257. $result["configs"]["hbase_regionserver_heapsize"] = $heapSize;
  258. }
  259. /** TODO change this to be from the UI later **/
  260. $hostname = strtolower(exec('hostname -f'));
  261. $result["configs"]["jdk_location"] = "http://".$hostname."/downloads";
  262. if ($updateDB) {
  263. $this->logger->log_info("Updating suggested configs into DB");
  264. $ret = $db->updateServiceConfigs($clusterName, $result["configs"]);
  265. if ($ret["result"] != 0) {
  266. $this->logger->log_error("Error updating suggested configs into DB"
  267. . ", result=" . $ret["result"]
  268. . ", error=" . $ret["error"]);
  269. $result["result"] = $ret["result"];
  270. $result["error"] = $ret["error"];
  271. }
  272. }
  273. $this->logger->log_info("Calculated Config Parameters \n".print_r($result, true));
  274. return $result;
  275. }
  276. /**
  277. * Verify properties set in DB
  278. * @param string $clusterName
  279. * @param object $db - HMCDBAccessor
  280. * @param mixed $configs
  281. * array ( "prop_key1" => "prop_val1", ... )
  282. * @return mixed
  283. * array (
  284. * "result" => 0,
  285. * "error" => "",
  286. * "cfgErrors" => array (
  287. * "propKey" => array (
  288. * "value" => "current val in DB",
  289. * "recommendedValue" => $recoVal,
  290. * "error" => "reason why this is an error"
  291. * ),
  292. * ...
  293. * ),
  294. * "cfgWarnings" => array (
  295. * "propKey" => array (
  296. * "value" => "current val in DB",
  297. * "recommendedValue" => $recoVal,
  298. * "error" => "reason why this is an warning"
  299. * ),
  300. * ...
  301. * )
  302. * )
  303. */
  304. public function verifyProperties($clusterName, $db, $configs) {
  305. $result = array();
  306. $result["result"] = 0;
  307. $result["error"] = "";
  308. $servicesDBInfo = $db->getAllServicesInfo($clusterName);
  309. if ($servicesDBInfo["result"] != 0) {
  310. $result["result"] = $servicesDBInfo["result"];
  311. $result["error"] = $servicesDBInfo["error"];
  312. return $result;
  313. }
  314. $services_tmp = $servicesDBInfo["services"];
  315. $services = $this->filterEnabledServices($services_tmp);
  316. $this->logger->log_debug("Services Enabled \n".print_r($services, true));
  317. $hostRoles = $db->getAllHostsByComponent($clusterName);
  318. if ($hostRoles["result"] != 0) {
  319. $result["result"] = $hostRoles["result"];
  320. $result["error"] = $hostRoles["error"];
  321. return $result;
  322. }
  323. $order = array("sortColumn" => "cpuCount",
  324. "sortOrder" => "ASC");
  325. $allHosts = $db->getAllHostsInfo($clusterName,
  326. array("=" => array ( "discoveryStatus" => "SUCCESS")), $order);
  327. if ($allHosts["result"] != 0) {
  328. $result["result"] = $allHosts["result"];
  329. $result["error"] = $allHosts["error"];
  330. return $result;
  331. }
  332. // convert host list to a map so thats easy to lookup
  333. $hostInfoMap = $this->createHostToInfoMap($allHosts);
  334. $allHostsToComponents = $db->getAllHostsToComponentMap($clusterName);
  335. if ($allHostsToComponents["result"] != 0) {
  336. $result["result"] = $allHostsToComponents["result"];
  337. $result["error"] = $allHostsToComponents["error"];
  338. return $result;
  339. }
  340. $recommendedInfo = $this->suggestProperties($clusterName, $db, FALSE);
  341. if ($recommendedInfo["result"] != 0) {
  342. $result["result"] = $recommendedInfo["result"];
  343. $result["error"] = $recommendedInfo["error"];
  344. return $result;
  345. }
  346. $recommendedConfigs = $recommendedInfo["configs"];
  347. // errors => array ( key => array ( value, recommended_value, reason ))
  348. $cfgErrors = array();
  349. $cfgWarnings = array();
  350. // verify map and reduce tasks max settings
  351. if (isset($configs["mapred_map_tasks_max"])
  352. && isset($configs["mapred_red_tasks_max"])) {
  353. if ($configs["mapred_map_tasks_max"] == 0
  354. || $configs["mapred_red_tasks_max"] == 0) {
  355. $reason = "Value cannot be 0";
  356. if ($configs["mapred_map_tasks_max"] == 0) {
  357. $cfgErrors["mapred_map_tasks_max"] = array ( "value" => 0,
  358. "recommendedValue" => $recommendedConfigs["mapred_map_tasks_max"],
  359. "error" => $reason);
  360. }
  361. if ($configs["mapred_red_tasks_max"] == 0) {
  362. $cfgErrors["mapred_red_tasks_max"] = array ( "value" => 0,
  363. "recommendedValue" => $recommendedConfigs["mapred_red_tasks_max"],
  364. "error" => $reason);
  365. }
  366. }
  367. if ($configs["mapred_map_tasks_max"] >
  368. $recommendedConfigs["mapred_map_tasks_max"]) {
  369. $cfgWarnings["mapred_map_tasks_max"] = array (
  370. "value" => $configs["mapred_map_tasks_max"],
  371. "recommendedValue" => $recommendedConfigs["mapred_map_tasks_max"],
  372. "error" => "Value greater than recommended");
  373. }
  374. if ($configs["mapred_red_tasks_max"] >
  375. $recommendedConfigs["mapred_red_tasks_max"]) {
  376. $cfgWarnings["mapred_red_tasks_max"] = array (
  377. "value" => $configs["mapred_red_tasks_max"],
  378. "recommendedValue" => $recommendedConfigs["mapred_red_tasks_max"],
  379. "error" => "Value greater than recommended");
  380. }
  381. }
  382. $memProps = array (
  383. "namenode_heapsize" => array ( "role" => "NAMENODE", "32bit" => FALSE),
  384. "jtnode_heapsize" => array ( "role" => "JOBTRACKER", "32bit" => FALSE),
  385. "dtnode_heapsize" => array ( "role" => "DATANODE", "32bit" => TRUE),
  386. "hadoop_heapsize" => array ( "role" => "DATANODE", "32bit" => TRUE),
  387. "mapred_child_java_opts_sz" => array ( "role" => "TASKTRACKER", "32bit" => TRUE)
  388. );
  389. if (array_key_exists("HBASE", $services)) {
  390. $memProps["hbase_master_heapsize"] =
  391. array ( "role" => "HBASE_MASTER", "32bit" => FALSE);
  392. $memProps["hbase_regionserver_heapsize"] =
  393. array ( "role" => "HBASE_REGIONSERVER", "32bit" => FALSE);
  394. }
  395. foreach ($memProps as $prop => $propInfo) {
  396. if (!isset($configs[$prop])) {
  397. continue;
  398. }
  399. if ($configs[$prop] < 256) {
  400. $reason = "Value less than min 256M";
  401. $cfgErrors[$prop] = array (
  402. "value" => $configs[$prop],
  403. "recommendedValue" => $recommendedConfigs[$prop],
  404. "error" => $reason
  405. );
  406. continue;
  407. }
  408. if ($configs[$prop] >
  409. $recommendedConfigs[$prop]) {
  410. $maxHeap = $this->getMaxHeapSizeForDaemon($propInfo["role"], $hostRoles,
  411. $hostInfoMap, $allHostsToComponents, $propInfo["32bit"]);
  412. if ($configs[$prop] > $maxHeap) {
  413. $reason = "Value greater than mem limit allowed";
  414. $cfgErrors[$prop] = array (
  415. "value" => $configs[$prop],
  416. "recommendedValue" => $recommendedConfigs[$prop],
  417. "error" => $reason
  418. );
  419. } else {
  420. $reason = "Value greater than recommended mem limit";
  421. $cfgWarnings[$prop] = array (
  422. "value" => $configs[$prop],
  423. "recommendedValue" => $recommendedConfigs[$prop],
  424. "error" => $reason
  425. );
  426. }
  427. }
  428. }
  429. $result = count($cfgErrors);
  430. $error = "";
  431. if ($result != 0 ) {
  432. $error = "Invalid Configs";
  433. }
  434. return array ("result" => $result, "error" => $error,
  435. "cfgErrors" => $cfgErrors,
  436. "cfgWarnings" => $cfgWarnings);
  437. }
  438. }
  439. ?>