cluster_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.ClusterController = Em.Controller.extend({
  20. name: 'clusterController',
  21. isLoaded: false,
  22. ambariProperties: null,
  23. clusterDataLoadedPercent: 'width:0', // 0 to 1
  24. isGangliaUrlLoaded: false,
  25. isNagiosUrlLoaded: false,
  26. /**
  27. * Provides the URL to use for Ganglia server. This URL
  28. * is helpful in populating links in UI.
  29. *
  30. * If null is returned, it means GANGLIA service is not installed.
  31. */
  32. gangliaUrl: null,
  33. /**
  34. * Provides the URL to use for NAGIOS server. This URL
  35. * is helpful in getting alerts data from server and also
  36. * in populating links in UI.
  37. *
  38. * If null is returned, it means NAGIOS service is not installed.
  39. */
  40. nagiosUrl: null,
  41. clusterName: function () {
  42. return App.get('clusterName');
  43. }.property('App.clusterName'),
  44. updateLoadStatus: function (item) {
  45. var loadList = this.get('dataLoadList');
  46. var loaded = true;
  47. var numLoaded = 0;
  48. var loadListLength = 0;
  49. loadList.set(item, true);
  50. for (var i in loadList) {
  51. if (loadList.hasOwnProperty(i)) {
  52. loadListLength++;
  53. if (!loadList[i] && loaded) {
  54. loaded = false;
  55. }
  56. }
  57. // calculate the number of true
  58. if (loadList.hasOwnProperty(i) && loadList[i]) {
  59. numLoaded++;
  60. }
  61. }
  62. this.set('isLoaded', loaded);
  63. this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
  64. },
  65. doOnClusterLoad: function (item) {
  66. if (this.get('isLoaded')) {
  67. App.router.get('mainAdminSecurityController').getUpdatedSecurityStatus();
  68. }
  69. }.observes('isLoaded'),
  70. dataLoadList: Em.Object.create({
  71. 'hosts': false,
  72. 'serviceMetrics': false,
  73. 'stackComponents': false,
  74. 'services': false,
  75. 'cluster': false,
  76. 'clusterStatus': false,
  77. 'racks': false,
  78. 'componentConfigs': false,
  79. 'componentsState': false,
  80. 'rootService': false,
  81. 'alertDefinitions': false
  82. }),
  83. /**
  84. * load cluster name
  85. */
  86. loadClusterName: function (reload) {
  87. var dfd = $.Deferred();
  88. if (App.get('clusterName') && !reload) {
  89. dfd.resolve();
  90. } else {
  91. App.ajax.send({
  92. name: 'cluster.load_cluster_name',
  93. sender: this,
  94. success: 'loadClusterNameSuccessCallback',
  95. error: 'loadClusterNameErrorCallback'
  96. }).complete(function () {
  97. if (!App.get('currentStackVersion')) {
  98. App.set('currentStackVersion', App.defaultStackVersion);
  99. }
  100. dfd.resolve();
  101. });
  102. }
  103. return dfd.promise()
  104. },
  105. loadClusterNameSuccessCallback: function (data) {
  106. if (data.items && data.items.length > 0) {
  107. App.set('clusterName', data.items[0].Clusters.cluster_name);
  108. App.set('currentStackVersion', data.items[0].Clusters.version);
  109. }
  110. },
  111. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  112. console.log('failed on loading cluster name');
  113. this.set('isLoaded', true);
  114. },
  115. /**
  116. * load current server clock in milli-seconds
  117. */
  118. loadClientServerClockDistance: function () {
  119. var dfd = $.Deferred();
  120. this.getServerClock().done(function () {
  121. dfd.resolve();
  122. });
  123. return dfd.promise();
  124. },
  125. getServerClock: function () {
  126. return App.ajax.send({
  127. name: 'ambari.service.load_server_clock',
  128. sender: this,
  129. success: 'getServerClockSuccessCallback',
  130. error: 'getServerClockErrorCallback'
  131. });
  132. },
  133. getServerClockSuccessCallback: function (data) {
  134. var clientClock = new Date().getTime();
  135. var serverClock = (data.RootServiceComponents.server_clock).toString();
  136. serverClock = serverClock.length < 13 ? serverClock + '000' : serverClock;
  137. App.set('clockDistance', serverClock - clientClock);
  138. App.set('currentServerTime', parseInt(serverClock));
  139. console.log('loading ambari server clock distance');
  140. },
  141. getServerClockErrorCallback: function () {
  142. console.log('Cannot load ambari server clock');
  143. },
  144. getUrl: function (testUrl, url) {
  145. return (App.get('testMode')) ? testUrl : App.get('apiPrefix') + '/clusters/' + App.get('clusterName') + url;
  146. },
  147. setGangliaUrl: function () {
  148. if (App.get('testMode')) {
  149. this.set('gangliaUrl', 'http://gangliaserver/ganglia/?t=yes');
  150. this.set('isGangliaUrlLoaded', true);
  151. } else {
  152. // We want live data here
  153. var gangliaServer = App.HostComponent.find().findProperty('componentName', 'GANGLIA_SERVER');
  154. if (this.get('isLoaded') && gangliaServer) {
  155. this.set('isGangliaUrlLoaded', false);
  156. App.ajax.send({
  157. name: 'hosts.for_quick_links',
  158. sender: this,
  159. data: {
  160. clusterName: App.get('clusterName'),
  161. masterHosts: gangliaServer.get('hostName'),
  162. urlParams: ''
  163. },
  164. success: 'setGangliaUrlSuccessCallback'
  165. });
  166. }
  167. }
  168. }.observes('App.router.updateController.isUpdated', 'dataLoadList.hosts', 'gangliaWebProtocol', 'isLoaded'),
  169. setGangliaUrlSuccessCallback: function (response) {
  170. var url = null;
  171. if (response.items.length > 0) {
  172. url = this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/ganglia";
  173. }
  174. this.set('gangliaUrl', url);
  175. this.set('isGangliaUrlLoaded', true);
  176. },
  177. setNagiosUrl: function () {
  178. if (App.get('testMode')) {
  179. this.set('nagiosUrl', 'http://nagiosserver/nagios');
  180. this.set('isNagiosUrlLoaded', true);
  181. } else {
  182. // We want live data here
  183. var nagiosServer = App.HostComponent.find().findProperty('componentName', 'NAGIOS_SERVER');
  184. if (this.get('isLoaded') && nagiosServer) {
  185. this.set('isNagiosUrlLoaded', false);
  186. App.ajax.send({
  187. name: 'hosts.for_quick_links',
  188. sender: this,
  189. data: {
  190. clusterName: App.get('clusterName'),
  191. masterHosts: nagiosServer.get('hostName'),
  192. urlParams: ''
  193. },
  194. success: 'setNagiosUrlSuccessCallback'
  195. });
  196. }
  197. }
  198. }.observes('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts', 'nagiosWebProtocol', 'isLoaded'),
  199. setNagiosUrlSuccessCallback: function (response) {
  200. var url = null;
  201. if (response.items.length > 0) {
  202. url = this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/nagios";
  203. }
  204. this.set('nagiosUrl', url);
  205. this.set('isNagiosUrlLoaded', true);
  206. },
  207. nagiosWebProtocol: function () {
  208. var properties = this.get('ambariProperties');
  209. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  210. return "https";
  211. } else {
  212. return "http";
  213. }
  214. }.property('ambariProperties'),
  215. gangliaWebProtocol: function () {
  216. var properties = this.get('ambariProperties');
  217. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  218. return "https";
  219. } else {
  220. return "http";
  221. }
  222. }.property('ambariProperties'),
  223. isNagiosInstalled: function () {
  224. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  225. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  226. isGangliaInstalled: function () {
  227. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  228. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  229. /**
  230. * load all data and update load status
  231. */
  232. loadClusterData: function () {
  233. var self = this;
  234. this.getAllHostNames();
  235. this.loadAmbariProperties();
  236. if (!App.get('clusterName')) {
  237. return;
  238. }
  239. if (this.get('isLoaded')) { // do not load data repeatedly
  240. App.router.get('mainController').startPolling();
  241. return;
  242. }
  243. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  244. var racksUrl = "/data/racks/racks.json";
  245. var hostsController = App.router.get('mainHostController');
  246. hostsController.set('isCountersUpdating', true);
  247. hostsController.updateStatusCounters();
  248. hostsController.set('isCountersUpdating', false);
  249. App.HttpClient.get(racksUrl, App.racksMapper, {
  250. complete: function (jqXHR, textStatus) {
  251. self.updateLoadStatus('racks');
  252. }
  253. }, function (jqXHR, textStatus) {
  254. self.updateLoadStatus('racks');
  255. });
  256. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  257. complete: function (jqXHR, textStatus) {
  258. self.updateLoadStatus('cluster');
  259. }
  260. }, function (jqXHR, textStatus) {
  261. self.updateLoadStatus('cluster');
  262. });
  263. if (App.get('testMode')) {
  264. self.updateLoadStatus('clusterStatus');
  265. } else {
  266. App.clusterStatus.updateFromServer().complete(function () {
  267. self.updateLoadStatus('clusterStatus');
  268. });
  269. }
  270. /**
  271. * Order of loading:
  272. * 1. request for service components supported by stack
  273. * 2. load stack components to model
  274. * 3. request for services
  275. * 4. put services in cache
  276. * 5. request for hosts and host-components (single call)
  277. * 6. request for service metrics
  278. * 7. load host-components to model
  279. * 8. load hosts to model
  280. * 9. load services from cache with metrics to model
  281. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  282. * 11. load root service (Ambari)
  283. * 12. load alert definitions to model
  284. */
  285. self.loadStackServiceComponents(function (data) {
  286. data.items.forEach(function (service) {
  287. service.StackServices.is_selected = true;
  288. service.StackServices.is_installed = false;
  289. }, self);
  290. App.stackServiceMapper.mapStackServices(data);
  291. App.config.setPreDefinedServiceConfigs();
  292. var updater = App.router.get('updateController');
  293. self.updateLoadStatus('stackComponents');
  294. updater.updateServices(function () {
  295. self.updateLoadStatus('services');
  296. updater.updateHost(function () {
  297. self.updateLoadStatus('hosts');
  298. });
  299. updater.updateServiceMetric(function () {
  300. updater.updateComponentConfig(function () {
  301. self.updateLoadStatus('componentConfigs');
  302. });
  303. updater.updateComponentsState(function () {
  304. self.updateLoadStatus('componentsState');
  305. });
  306. self.updateLoadStatus('serviceMetrics');
  307. updater.updateAlertDefinitions(function () {
  308. if (App.get('supports.alerts')) {
  309. updater.updateAlertDefinitionSummary(function() {
  310. self.updateLoadStatus('alertDefinitions');
  311. });
  312. }
  313. else {
  314. self.updateLoadStatus('alertDefinitions');
  315. }
  316. });
  317. updater.updateAlertGroups(function () {
  318. self.updateLoadStatus('alertGroups');
  319. });
  320. });
  321. });
  322. self.loadRootService().done(function (data) {
  323. App.rootServiceMapper.map(data);
  324. self.updateLoadStatus('rootService');
  325. });
  326. });
  327. },
  328. loadRootService: function () {
  329. return App.ajax.send({
  330. name: 'service.ambari',
  331. sender: this
  332. });
  333. },
  334. requestHosts: function (realUrl, callback) {
  335. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  336. var url = this.getUrl(testHostUrl, realUrl);
  337. App.HttpClient.get(url, App.hostsMapper, {
  338. complete: callback
  339. }, callback)
  340. },
  341. /**
  342. *
  343. * @param callback
  344. */
  345. loadStackServiceComponents: function (callback) {
  346. var callbackObj = {
  347. loadStackServiceComponentsSuccess: callback
  348. };
  349. App.ajax.send({
  350. name: 'wizard.service_components',
  351. data: {
  352. stackUrl: App.get('stackVersionURL'),
  353. stackVersion: App.get('currentStackVersionNumber')
  354. },
  355. sender: callbackObj,
  356. success: 'loadStackServiceComponentsSuccess'
  357. });
  358. },
  359. loadAmbariProperties: function () {
  360. return App.ajax.send({
  361. name: 'ambari.service',
  362. sender: this,
  363. success: 'loadAmbariPropertiesSuccess',
  364. error: 'loadAmbariPropertiesError'
  365. });
  366. },
  367. loadAmbariPropertiesSuccess: function (data) {
  368. console.log('loading ambari properties');
  369. this.set('ambariProperties', data.RootServiceComponents.properties);
  370. },
  371. loadAmbariPropertiesError: function () {
  372. console.warn('can\'t get ambari properties');
  373. },
  374. updateClusterData: function () {
  375. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  376. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  377. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  378. complete: function () {
  379. }
  380. });
  381. },
  382. /**
  383. *
  384. * @returns {*|Transport|$.ajax|boolean|ServerResponse}
  385. */
  386. getAllHostNames: function () {
  387. return App.ajax.send({
  388. name: 'hosts.all',
  389. sender: this,
  390. success: 'getHostNamesSuccess',
  391. error: 'getHostNamesError'
  392. });
  393. },
  394. getHostNamesSuccess: function (data) {
  395. App.set("allHostNames", data.items.mapProperty("Hosts.host_name"));
  396. },
  397. getHostNamesError: function () {
  398. console.error('failed to load hostNames');
  399. }
  400. });