suggestProperties.php 15 KB

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