assignMasters.js 9.3 KB

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