assignMasters.js 10 KB

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