assignMasters.js 8.4 KB

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