cluster_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. cluster: null,
  22. isLoaded: false,
  23. ambariProperties: null,
  24. ambariVersion: null,
  25. ambariViews: [],
  26. clusterDataLoadedPercent: 'width:0', // 0 to 1
  27. /**
  28. * Whether we need to update statuses automatically or not
  29. */
  30. isWorking: false,
  31. updateLoadStatus: function (item) {
  32. var loadList = this.get('dataLoadList');
  33. var loaded = true;
  34. var numLoaded = 0;
  35. var loadListLength = 0;
  36. loadList.set(item, true);
  37. for (var i in loadList) {
  38. if (loadList.hasOwnProperty(i)) {
  39. loadListLength++;
  40. if (!loadList[i] && loaded) {
  41. loaded = false;
  42. }
  43. }
  44. // calculate the number of true
  45. if (loadList.hasOwnProperty(i) && loadList[i]) {
  46. numLoaded++;
  47. }
  48. }
  49. this.set('isLoaded', loaded);
  50. this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
  51. },
  52. dataLoadList: Em.Object.create({
  53. 'hosts': false,
  54. 'serviceMetrics': false,
  55. 'stackComponents': false,
  56. 'services': false,
  57. 'cluster': false,
  58. 'clusterStatus': false,
  59. 'racks': false,
  60. 'users': false,
  61. 'componentConfigs': false
  62. }),
  63. /**
  64. * load cluster name
  65. */
  66. loadClusterName: function (reload) {
  67. if (this.get('clusterName') && !reload) {
  68. return false;
  69. }
  70. App.ajax.send({
  71. name: 'cluster.load_cluster_name',
  72. sender: this,
  73. success: 'loadClusterNameSuccessCallback',
  74. error: 'loadClusterNameErrorCallback'
  75. });
  76. if (!App.get('currentStackVersion')) {
  77. App.set('currentStackVersion', App.defaultStackVersion);
  78. }
  79. },
  80. loadClusterNameSuccessCallback: function (data) {
  81. this.set('cluster', data.items[0]);
  82. App.set('clusterName', data.items[0].Clusters.cluster_name);
  83. App.set('currentStackVersion', data.items[0].Clusters.version);
  84. },
  85. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  86. console.log('failed on loading cluster name');
  87. this.set('isLoaded', true);
  88. },
  89. /**
  90. * load current server clock in milli-seconds
  91. */
  92. loadClientServerClockDistance: function () {
  93. var dfd = $.Deferred();
  94. this.getServerClock().done(function () {
  95. dfd.resolve();
  96. });
  97. return dfd.promise();
  98. },
  99. getServerClock: function () {
  100. return App.ajax.send({
  101. name: 'ambari.service.load_server_clock',
  102. sender: this,
  103. success: 'getServerClockSuccessCallback',
  104. error: 'getServerClockErrorCallback'
  105. });
  106. },
  107. getServerClockSuccessCallback: function (data) {
  108. var clientClock = new Date().getTime();
  109. var serverClock = (data.RootServiceComponents.server_clock).toString();
  110. serverClock = serverClock.length < 13 ? serverClock + '000' : serverClock;
  111. App.set('clockDistance', serverClock - clientClock);
  112. App.set('currentServerTime', parseInt(serverClock));
  113. console.log('loading ambari server clock distance');
  114. },
  115. getServerClockErrorCallback: function () {
  116. console.log('Cannot load ambari server clock');
  117. },
  118. getUrl: function (testUrl, url) {
  119. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  120. },
  121. /**
  122. * Provides the URL to use for Ganglia server. This URL
  123. * is helpful in populating links in UI.
  124. *
  125. * If null is returned, it means GANGLIA service is not installed.
  126. */
  127. gangliaUrl: function () {
  128. if (App.testMode) {
  129. return 'http://gangliaserver/ganglia/?t=yes';
  130. } else {
  131. // We want live data here
  132. var svcs = App.Service.find();
  133. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  134. if (gangliaSvc) {
  135. var svcComponents = gangliaSvc.get('hostComponents');
  136. if (svcComponents) {
  137. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  138. if (gangliaSvcComponent) {
  139. var hostName = gangliaSvcComponent.get('host.hostName');
  140. if (hostName) {
  141. var host = App.Host.find(hostName);
  142. if (host) {
  143. hostName = host.get('publicHostName');
  144. }
  145. return this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/ganglia";
  146. }
  147. }
  148. }
  149. }
  150. return null;
  151. }
  152. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts', 'gangliaWebProtocol'),
  153. /**
  154. * Provides the URL to use for NAGIOS server. This URL
  155. * is helpful in getting alerts data from server and also
  156. * in populating links in UI.
  157. *
  158. * If null is returned, it means NAGIOS service is not installed.
  159. */
  160. nagiosUrl: function () {
  161. if (App.testMode) {
  162. return 'http://nagiosserver/nagios';
  163. } else {
  164. // We want live data here
  165. var nagiosSvc = App.Service.find("NAGIOS");
  166. if (nagiosSvc) {
  167. var svcComponents = nagiosSvc.get('hostComponents');
  168. if (svcComponents) {
  169. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  170. if (nagiosSvcComponent) {
  171. var hostName = nagiosSvcComponent.get('host.hostName');
  172. if (hostName) {
  173. var host = App.Host.find(hostName);
  174. if (host) {
  175. hostName = host.get('publicHostName');
  176. }
  177. return this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/nagios";
  178. }
  179. }
  180. }
  181. }
  182. return null;
  183. }
  184. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts', 'nagiosWebProtocol'),
  185. nagiosWebProtocol: function () {
  186. var properties = this.get('ambariProperties');
  187. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  188. return "https";
  189. } else {
  190. return "http";
  191. }
  192. }.property('ambariProperties'),
  193. gangliaWebProtocol: function () {
  194. var properties = this.get('ambariProperties');
  195. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  196. return "https";
  197. } else {
  198. return "http";
  199. }
  200. }.property('ambariProperties'),
  201. isNagiosInstalled: function () {
  202. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  203. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  204. isGangliaInstalled: function () {
  205. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  206. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  207. /**
  208. * Send request to server to load components updated statuses
  209. * @param callback Slave function, should be called to fire delayed update.
  210. * @param isInitialLoad
  211. * Look at <code>App.updater.run</code> for more information
  212. * @return {Boolean} Whether we have errors
  213. */
  214. loadUpdatedStatus: function (callback, isInitialLoad) {
  215. if (!this.get('clusterName')) {
  216. callback();
  217. return false;
  218. }
  219. App.set('currentServerTime', App.get('currentServerTime') + App.componentsUpdateInterval);
  220. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hc_host_status.json' : '/data/dashboard/services.json';
  221. var statusUrl = '/hosts?fields=Hosts/host_status,Hosts/maintenance_state,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,alerts/summary&minimal_response=true';
  222. if (isInitialLoad) {
  223. testUrl = '/data/hosts/HDP2/hosts_init.json';
  224. statusUrl = '/hosts?fields=Hosts/host_name,Hosts/maintenance_state,Hosts/public_host_name,Hosts/cpu_count,Hosts/ph_cpu_count,Hosts/total_mem,' +
  225. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,' +
  226. 'Hosts/disk_info,metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,' +
  227. 'metrics/memory/mem_total,metrics/memory/mem_free,alerts/summary&minimal_response=true';
  228. }
  229. //desired_state property is eliminated since calculateState function is commented out, it become useless
  230. statusUrl = this.getUrl(testUrl, statusUrl);
  231. App.HttpClient.get(statusUrl, App.statusMapper, {
  232. complete: callback
  233. });
  234. return true;
  235. },
  236. /**
  237. * Run <code>loadUpdatedStatus</code> with delay
  238. * @param delay
  239. */
  240. loadUpdatedStatusDelayed: function (delay) {
  241. setTimeout(function () {
  242. App.updater.immediateRun('loadUpdatedStatus');
  243. }, delay);
  244. },
  245. /**
  246. * Start polling, when <code>isWorking</code> become true
  247. */
  248. startPolling: function () {
  249. if (!this.get('isWorking')) {
  250. return false;
  251. }
  252. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  253. return true;
  254. }.observes('isWorking'),
  255. /**
  256. *
  257. * load all data and update load status
  258. */
  259. loadClusterData: function () {
  260. var self = this;
  261. this.loadAmbariProperties();
  262. this.loadAmbariViews();
  263. if (!this.get('clusterName')) {
  264. return;
  265. }
  266. if (this.get('isLoaded')) { // do not load data repeatedly
  267. App.router.get('mainController').startPolling();
  268. return;
  269. }
  270. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  271. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  272. var racksUrl = "/data/racks/racks.json";
  273. App.HttpClient.get(racksUrl, App.racksMapper, {
  274. complete: function (jqXHR, textStatus) {
  275. self.updateLoadStatus('racks');
  276. }
  277. }, function (jqXHR, textStatus) {
  278. self.updateLoadStatus('racks');
  279. });
  280. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  281. complete: function (jqXHR, textStatus) {
  282. self.updateLoadStatus('cluster');
  283. }
  284. }, function (jqXHR, textStatus) {
  285. self.updateLoadStatus('cluster');
  286. });
  287. if (App.testMode) {
  288. self.updateLoadStatus('clusterStatus');
  289. } else {
  290. App.clusterStatus.updateFromServer(true).complete(function () {
  291. self.updateLoadStatus('clusterStatus');
  292. });
  293. }
  294. App.HttpClient.get(usersUrl, App.usersMapper, {
  295. complete: function (jqXHR, textStatus) {
  296. self.updateLoadStatus('users');
  297. }
  298. }, function (jqXHR, textStatus) {
  299. self.updateLoadStatus('users');
  300. });
  301. /**
  302. * Order of loading:
  303. * 1. request for service components supported by stack
  304. * 2. load stack components to model
  305. * 3. request for services
  306. * 4. put services in cache
  307. * 5. request for hosts and host-components (single call)
  308. * 6. request for service metrics
  309. * 7. load host-components to model
  310. * 8. load hosts to model
  311. * 9. load services from cache with metrics to model
  312. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  313. */
  314. this.loadStackServiceComponents(function (data) {
  315. require('utils/component').loadStackServiceComponentModel(data);
  316. self.updateLoadStatus('stackComponents');
  317. App.router.get('updateController').updateServices(function () {
  318. self.updateLoadStatus('services');
  319. self.loadUpdatedStatus(function () {
  320. self.updateLoadStatus('hosts');
  321. if (App.supports.hostOverrides) {
  322. App.router.get('updateController').updateComponentConfig(function () {
  323. self.updateLoadStatus('componentConfigs');
  324. });
  325. } else {
  326. self.updateLoadStatus('componentConfigs');
  327. }
  328. }, true);
  329. App.router.get('updateController').updateServiceMetric(function () {});
  330. });
  331. });
  332. },
  333. /**
  334. * json from serviceMetricsMapper on initial load
  335. */
  336. serviceMetricsJson: null,
  337. /**
  338. * control that services was loaded to model strictly after hosts and host-components
  339. * regardless which request was completed first
  340. * @param json
  341. */
  342. deferServiceMetricsLoad: function (json) {
  343. if (json) {
  344. if (this.get('dataLoadList.hosts')) {
  345. App.serviceMetricsMapper.map(json, true);
  346. this.updateLoadStatus('serviceMetrics');
  347. } else {
  348. this.set('serviceMetricsJson', json);
  349. }
  350. } else if (this.get('serviceMetricsJson')) {
  351. json = this.get('serviceMetricsJson');
  352. this.set('serviceMetricsJson', null);
  353. App.serviceMetricsMapper.map(json, true);
  354. this.updateLoadStatus('serviceMetrics');
  355. }
  356. },
  357. requestHosts: function (realUrl, callback) {
  358. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  359. var url = this.getUrl(testHostUrl, realUrl);
  360. App.HttpClient.get(url, App.hostsMapper, {
  361. complete: callback
  362. }, callback)
  363. },
  364. loadAmbariViews: function () {
  365. App.ajax.send({
  366. name: 'views.info',
  367. sender: this,
  368. success: 'loadAmbariViewsSuccess'
  369. });
  370. },
  371. loadAmbariViewsSuccess: function (data) {
  372. this.set('ambariViews', []);
  373. data.items.forEach(function (item) {
  374. App.ajax.send({
  375. name: 'views.instances',
  376. data: {
  377. viewName: item.ViewInfo.view_name
  378. },
  379. sender: this,
  380. success: 'loadViewInstancesSuccess'
  381. });
  382. }, this)
  383. },
  384. loadViewInstancesSuccess: function (data) {
  385. data.instances.forEach(function (instance) {
  386. var view = Em.Object.create({
  387. label: data.ViewInfo.label,
  388. viewName: instance.ViewInstanceInfo.view_name,
  389. instanceName: instance.ViewInstanceInfo.instance_name,
  390. href: "/views/" + instance.ViewInstanceInfo.view_name + "/" + instance.ViewInstanceInfo.instance_name
  391. });
  392. this.get('ambariViews').push(view);
  393. }, this);
  394. },
  395. /**
  396. *
  397. * @param callback
  398. */
  399. loadStackServiceComponents: function (callback) {
  400. var callbackObj = {
  401. loadStackServiceComponentsSuccess: callback
  402. };
  403. App.ajax.send({
  404. name: 'wizard.service_components',
  405. data: {
  406. stackUrl: App.get('stack2VersionURL'),
  407. stackVersion: App.get('currentStackVersionNumber'),
  408. async: true
  409. },
  410. sender: callbackObj,
  411. success: 'loadStackServiceComponentsSuccess'
  412. });
  413. },
  414. loadAmbariProperties: function () {
  415. App.ajax.send({
  416. name: 'ambari.service',
  417. sender: this,
  418. success: 'loadAmbariPropertiesSuccess',
  419. error: 'loadAmbariPropertiesError'
  420. });
  421. return this.get('ambariProperties');
  422. },
  423. loadAmbariPropertiesSuccess: function (data) {
  424. console.log('loading ambari properties');
  425. this.set('ambariProperties', data.RootServiceComponents.properties);
  426. this.set('ambariVersion', data.RootServiceComponents.component_version);
  427. },
  428. loadAmbariPropertiesError: function () {
  429. console.warn('can\'t get ambari properties');
  430. },
  431. clusterName: function () {
  432. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  433. }.property('cluster'),
  434. updateClusterData: function () {
  435. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  436. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  437. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  438. complete: function () {
  439. }
  440. });
  441. }
  442. });