cluster_controller.js 16 KB

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