assignMasters.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  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,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. function AssignMasters() {
  22. var managerHostName;
  23. var allHosts;
  24. var registeredAssignHostsEventHandlers = false;
  25. function getNodeInfo(nodeName) {
  26. // globalYui.log("nodename: " + nodeName);
  27. if (nodeName == null) {
  28. return null;
  29. }
  30. for (var host in allHosts) {
  31. if (allHosts[host].hostName == nodeName) {
  32. // globalYui.log("Get node info: " + allHosts[host].hostName);
  33. return allHosts[host];
  34. }
  35. }
  36. return null;
  37. }
  38. function addCommasToInt(num) {
  39. return num.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
  40. }
  41. function getTotalMemForDisplay(totalMem) {
  42. return Math.round(totalMem / 102.4)/10 + "GB";
  43. }
  44. function renderHostsToMasterServices(hostsToMasterServices) {
  45. var markup = '';
  46. for (var host in hostsToMasterServices) {
  47. var hostInfo = getNodeInfo(host);
  48. markup += '<div class="hostToMasterServices"><h3>' + host + '<span class="hostInfo">' + getTotalMemForDisplay(hostInfo.totalMem) + ', ' + hostInfo.cpuCount + ' cores</span></h3><ul>';
  49. for (var j in hostsToMasterServices[host]) {
  50. markup += '<li>' + hostsToMasterServices[host][j] + '</li>';
  51. }
  52. markup += '</ul><div style="clear:both"></div></div>';
  53. }
  54. $('#hostsToMasterServices').html(markup);
  55. }
  56. function addMasterServiceToHost(masterName, hostName, hostsToMasterServices, masterServices) {
  57. // enforce constraints on what services can be co-hosted (unless those suggestions were made by the server initially)
  58. // we currently disallow:
  59. // 1. namenode and secondary namenode to be on the same host
  60. // 2. more than one zookeeper server to be on the same host
  61. if (hostsToMasterServices[hostName] != null) {
  62. for (var service in hostsToMasterServices[hostName]) {
  63. if (masterName == 'NAMENODE' && service == 'SNAMENODE' || masterName == 'SNAMENODE' && service == 'NAMENODE') {
  64. alert('NameNode and Secondary NameNode cannot be hosted on the same host.');
  65. return false;
  66. }
  67. if (masterName.indexOf('ZOOKEEPER') == 0 && service.indexOf('ZOOKEEPER') == 0) {
  68. alert('You cannot put more than one ZooKeeper Server on the same host.');
  69. return false;
  70. }
  71. }
  72. }
  73. if (hostsToMasterServices[hostName] == null) {
  74. hostsToMasterServices[hostName] = {};
  75. }
  76. hostsToMasterServices[hostName][masterName] = masterServices[masterName].displayName;
  77. return true;
  78. }
  79. function removeMasterServiceFromHost(masterName, hostName, hostsToMasterServices) {
  80. for (var i in hostsToMasterServices[hostName]) {
  81. if (i == masterName) {
  82. delete hostsToMasterServices[hostName][i];
  83. //alert(Object.keys(hostsToMasterServices[hostName]).length);
  84. if (Object.keys(hostsToMasterServices[hostName]).length == 0) {
  85. //alert('remove');
  86. delete hostsToMasterServices[hostName];
  87. }
  88. return;
  89. }
  90. }
  91. }
  92. function getHostInfoForDisplay(host) {
  93. return host.hostName + ' - ' + getTotalMemForDisplay(host.totalMem) + ', ' + host.cpuCount + ' cores';
  94. }
  95. function getMasterHostSelect(masterName, chosenHostName) {
  96. var chosenHost = getNodeInfo(chosenHostName);
  97. var markup = '<select name="' + masterName + '">';
  98. markup += '<option selected="selected" value="' + chosenHost.hostName + '">' + getHostInfoForDisplay(chosenHost) + '</option>';
  99. for (var i in allHosts) {
  100. var host = allHosts[i];
  101. if (host.hostName != chosenHost.hostName) {
  102. markup += '<option value="' + host.hostName + '">' + host.hostName + ' - ' + getTotalMemForDisplay(host.totalMem) + ', ' + host.cpuCount + ' cores</option>';
  103. }
  104. }
  105. markup += '</select><div style="clear:both"></div><input type="hidden" style="display:none" id="' + masterName + 'ChosenHost" value="' + chosenHost.hostName + '">';
  106. return markup;
  107. }
  108. this.render = function (clusterInfo) {
  109. hideLoadingImg();
  110. globalYui.log("Render assign hosts data " + globalYui.Lang.dump(clusterInfo));
  111. globalYui.one('#assignHostsCoreDivId').setStyle("display", "block");
  112. globalClusterName = clusterInfo.clusterName;
  113. if( !registeredAssignHostsEventHandlers ) {
  114. globalYui.one('#selectServiceMastersSubmitButtonId').on('click', function (e) {
  115. e.target.set('disabled', true);
  116. var assignHostsRequestData = {};
  117. for (var masterName in masterServices) {
  118. var hostName = $('select[name=' + masterName + ']').val();
  119. if (masterName.indexOf("ZOOKEEPER_SERVER") == 0) {
  120. if (assignHostsRequestData['ZOOKEEPER_SERVER'] == null) {
  121. assignHostsRequestData['ZOOKEEPER_SERVER'] = [];
  122. }
  123. assignHostsRequestData['ZOOKEEPER_SERVER'].push(hostName);
  124. } else {
  125. if (assignHostsRequestData[masterName] == null) {
  126. assignHostsRequestData[masterName] = [];
  127. }
  128. assignHostsRequestData[masterName].push(hostName);
  129. }
  130. // globalYui.log("Assignment for " + masterName + " is " + assignHostsRequestData[masterName]);
  131. };
  132. globalYui.io("../php/frontend/assignMasters.php?clusterName=" + clusterInfo.clusterName, {
  133. method: 'POST',
  134. data: globalYui.JSON.stringify(assignHostsRequestData),
  135. timeout : 10000,
  136. on: {
  137. start: function(x, o) {
  138. showLoadingImg();
  139. },
  140. complete: function(x, o) {
  141. e.target.set('disabled', false);
  142. hideLoadingImg();
  143. },
  144. success: function (x,o) {
  145. e.target.set('disabled', false);
  146. globalYui.log("RAW JSON DATA: " + o.responseText);
  147. // Process the JSON data returned from the server
  148. try {
  149. clusterConfigJson = globalYui.JSON.parse(o.responseText);
  150. }
  151. catch (e) {
  152. alert("JSON Parse failed!");
  153. return;
  154. }
  155. //globalYui.log("PARSED DATA: " + globalYui.Lang.dump(clusterConfigJson));
  156. if (clusterConfigJson.result != 0) {
  157. // Error!
  158. alert("Got error!" + clusterConfigJson.error);
  159. return;
  160. }
  161. clusterConfigJson = clusterConfigJson.response;
  162. /* Done with this stage, transition to the next. */
  163. transitionToNextStage( "#assignHostsCoreDivId", assignHostsRequestData,
  164. "#configureClusterCoreDivId", clusterConfigJson, renderConfigureCluster );
  165. },
  166. failure: function (x,o) {
  167. e.target.set('disabled', false);
  168. alert("Async call failed!");
  169. }
  170. }
  171. });
  172. });
  173. registeredAssignHostsEventHandlers = true;
  174. }
  175. allHosts = clusterInfo.allHosts;
  176. managerHostName = clusterInfo.managerHostName;
  177. var servicesInfo = globalYui.Array( clusterInfo.services );
  178. var masterServices = {};
  179. globalYui.Array.each(servicesInfo, function(serviceInfo) {
  180. if( serviceInfo.enabled == true ) {
  181. var zkIndex = 1;
  182. globalYui.Array.each(serviceInfo.masters, function(masterInfo) {
  183. for (var i in masterInfo.hostNames) {
  184. var masterHostInfo = {
  185. 'name' : masterInfo.name,
  186. 'displayName' : masterInfo.displayName,
  187. 'host' : masterInfo.hostNames[i]
  188. };
  189. // there could be multiple zookeepers
  190. if (masterInfo.name == 'ZOOKEEPER_SERVER') {
  191. masterHostInfo.name = 'ZOOKEEPER_SERVER_' + zkIndex;
  192. masterHostInfo.displayName = masterHostInfo.displayName + ' ' + zkIndex;
  193. zkIndex++;
  194. }
  195. masterServices[masterHostInfo.name] = masterHostInfo;
  196. }
  197. });
  198. }
  199. });
  200. var hostsToMasterServices = {};
  201. var markup = '';
  202. for (var i in masterServices) {
  203. markup += '<div class="masterServiceSelect"><label><b>'
  204. + masterServices[i].displayName
  205. + '</b></label>' + getMasterHostSelect(masterServices[i].name, masterServices[i].host)
  206. + '</div>';
  207. if (hostsToMasterServices[masterServices[i].host] == null) {
  208. hostsToMasterServices[masterServices[i].host] = {};
  209. }
  210. hostsToMasterServices[masterServices[i].host][masterServices[i].name] = masterServices[i].displayName;
  211. }
  212. // add manager server
  213. if (hostsToMasterServices[managerHostName] == null) {
  214. hostsToMasterServices[managerHostName] = {};
  215. }
  216. hostsToMasterServices[managerHostName].MANAGER_SERVER = App.Props.managerServiceName + ' Server';
  217. $('#masterServicesToHosts').html(markup);
  218. renderHostsToMasterServices(hostsToMasterServices);
  219. // prevValue is used to undo user selection in case we prevent the user from assigning a service
  220. var prevValue = '';
  221. $('select').click(function() {
  222. prevValue = $(this).val();
  223. }).change(function(event) {
  224. var masterName = $(this).attr('name');
  225. // masterServices[masterName] = $(this).val();
  226. var prevChosenHost = $('#' + masterName + 'ChosenHost').val();
  227. var newChosenHost = $(this).val();
  228. if (addMasterServiceToHost(masterName, newChosenHost, hostsToMasterServices, masterServices)) {
  229. removeMasterServiceFromHost(masterName, prevChosenHost, hostsToMasterServices);
  230. renderHostsToMasterServices(hostsToMasterServices);
  231. $('#' + masterName + 'ChosenHost').val(newChosenHost);
  232. } else {
  233. $(this).val(prevValue);
  234. }
  235. });
  236. }; // end render
  237. };