cluster_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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;
  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 svcs = App.Service.find();
  166. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  167. if (nagiosSvc) {
  168. var svcComponents = nagiosSvc.get('hostComponents');
  169. if (svcComponents) {
  170. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  171. if (nagiosSvcComponent) {
  172. var hostName = nagiosSvcComponent.get('host.hostName');
  173. if (hostName) {
  174. var host = App.Host.find(hostName);
  175. if (host) {
  176. hostName = host.get('publicHostName');
  177. }
  178. return this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/nagios";
  179. }
  180. }
  181. }
  182. }
  183. return null;
  184. }
  185. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts', 'nagiosWebProtocol'),
  186. nagiosWebProtocol: function () {
  187. var properties = this.get('ambariProperties');
  188. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  189. return "https";
  190. } else {
  191. return "http";
  192. }
  193. }.property('ambariProperties'),
  194. gangliaWebProtocol: function () {
  195. var properties = this.get('ambariProperties');
  196. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  197. return "https";
  198. } else {
  199. return "http";
  200. }
  201. }.property('ambariProperties'),
  202. isNagiosInstalled: function () {
  203. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  204. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  205. isGangliaInstalled: function () {
  206. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  207. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  208. /**
  209. * Send request to server to load components updated statuses
  210. * @param callback Slave function, should be called to fire delayed update.
  211. * @param isInitialLoad
  212. * Look at <code>App.updater.run</code> for more information
  213. * @return {Boolean} Whether we have errors
  214. */
  215. loadUpdatedStatus: function (callback, isInitialLoad) {
  216. if (!this.get('clusterName')) {
  217. callback();
  218. return false;
  219. }
  220. App.set('currentServerTime', App.get('currentServerTime') + App.componentsUpdateInterval);
  221. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hc_host_status.json' : '/data/dashboard/services.json';
  222. var statusUrl = '/hosts?fields=Hosts/host_status,Hosts/maintenance_state,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,alerts/summary&minimal_response=true';
  223. if (isInitialLoad) {
  224. testUrl = '/data/hosts/HDP2/hosts_init.json';
  225. statusUrl = '/hosts?fields=Hosts/host_name,Hosts/maintenance_state,Hosts/public_host_name,Hosts/cpu_count,Hosts/ph_cpu_count,Hosts/total_mem,' +
  226. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,' +
  227. 'Hosts/disk_info,metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,' +
  228. 'metrics/memory/mem_total,metrics/memory/mem_free,alerts/summary&minimal_response=true';
  229. }
  230. //desired_state property is eliminated since calculateState function is commented out, it become useless
  231. statusUrl = this.getUrl(testUrl, statusUrl);
  232. App.HttpClient.get(statusUrl, App.statusMapper, {
  233. complete: callback
  234. });
  235. return true;
  236. },
  237. /**
  238. * Run <code>loadUpdatedStatus</code> with delay
  239. * @param delay
  240. */
  241. loadUpdatedStatusDelayed: function (delay) {
  242. setTimeout(function () {
  243. App.updater.immediateRun('loadUpdatedStatus');
  244. }, delay);
  245. },
  246. /**
  247. * Start polling, when <code>isWorking</code> become true
  248. */
  249. startPolling: function () {
  250. if (!this.get('isWorking')) {
  251. return false;
  252. }
  253. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  254. return true;
  255. }.observes('isWorking'),
  256. /**
  257. *
  258. * load all data and update load status
  259. */
  260. loadClusterData: function () {
  261. var self = this;
  262. this.loadAmbariProperties();
  263. this.loadAmbariViews();
  264. if (!this.get('clusterName')) {
  265. return;
  266. }
  267. if (this.get('isLoaded')) { // do not load data repeatedly
  268. App.router.get('mainController').startPolling();
  269. return;
  270. }
  271. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  272. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  273. var racksUrl = "/data/racks/racks.json";
  274. App.HttpClient.get(racksUrl, App.racksMapper, {
  275. complete: function (jqXHR, textStatus) {
  276. self.updateLoadStatus('racks');
  277. }
  278. }, function (jqXHR, textStatus) {
  279. self.updateLoadStatus('racks');
  280. });
  281. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  282. complete: function (jqXHR, textStatus) {
  283. self.updateLoadStatus('cluster');
  284. }
  285. }, function (jqXHR, textStatus) {
  286. self.updateLoadStatus('cluster');
  287. });
  288. if (App.testMode) {
  289. self.updateLoadStatus('clusterStatus');
  290. } else {
  291. App.clusterStatus.updateFromServer(true).complete(function () {
  292. self.updateLoadStatus('clusterStatus');
  293. });
  294. }
  295. App.HttpClient.get(usersUrl, App.usersMapper, {
  296. complete: function (jqXHR, textStatus) {
  297. self.updateLoadStatus('users');
  298. }
  299. }, function (jqXHR, textStatus) {
  300. self.updateLoadStatus('users');
  301. });
  302. /**
  303. * Order of loading:
  304. * 1. request for service components supported by stack
  305. * 2. load stack components to model
  306. * 3. request for services
  307. * 4. put services in cache
  308. * 5. request for hosts and host-components (single call)
  309. * 6. request for service metrics
  310. * 7. load host-components to model
  311. * 8. load hosts to model
  312. * 9. load services from cache with metrics to model
  313. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  314. */
  315. this.loadStackServiceComponents(function (data) {
  316. require('utils/component').loadStackServiceComponentModel(data);
  317. self.updateLoadStatus('stackComponents');
  318. App.router.get('updateController').updateServices(function () {
  319. self.updateLoadStatus('services');
  320. self.loadUpdatedStatus(function () {
  321. self.updateLoadStatus('hosts');
  322. if (App.supports.hostOverrides) {
  323. App.router.get('updateController').updateComponentConfig(function () {
  324. self.updateLoadStatus('componentConfigs');
  325. });
  326. } else {
  327. self.updateLoadStatus('componentConfigs');
  328. }
  329. }, true);
  330. App.router.get('updateController').updateServiceMetric(function () {
  331. }, true);
  332. });
  333. });
  334. },
  335. /**
  336. * json from serviceMetricsMapper on initial load
  337. */
  338. serviceMetricsJson: null,
  339. /**
  340. * control that services was loaded to model strictly after hosts and host-components
  341. * regardless which request was completed first
  342. * @param json
  343. */
  344. deferServiceMetricsLoad: function (json) {
  345. if (json) {
  346. if (this.get('dataLoadList.hosts')) {
  347. App.serviceMetricsMapper.map(json, true);
  348. this.updateLoadStatus('serviceMetrics');
  349. } else {
  350. this.set('serviceMetricsJson', json);
  351. }
  352. } else if (this.get('serviceMetricsJson')) {
  353. json = this.get('serviceMetricsJson');
  354. this.set('serviceMetricsJson', null);
  355. App.serviceMetricsMapper.map(json, true);
  356. this.updateLoadStatus('serviceMetrics');
  357. }
  358. },
  359. requestHosts: function (realUrl, callback) {
  360. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  361. var url = this.getUrl(testHostUrl, realUrl);
  362. App.HttpClient.get(url, App.hostsMapper, {
  363. complete: callback
  364. }, callback)
  365. },
  366. loadAmbariViews: function() {
  367. App.ajax.send({
  368. name: 'views.info',
  369. sender: this,
  370. success: 'loadAmbariViewsSuccess'
  371. });
  372. },
  373. loadAmbariViewsSuccess: function(data) {
  374. this.set('ambariViews',[]);
  375. data.items.forEach(function(item){
  376. App.ajax.send({
  377. name: 'views.instances',
  378. data: {
  379. viewName: item.ViewInfo.view_name
  380. },
  381. sender: this,
  382. success: 'loadViewInstancesSuccess'
  383. });
  384. }, this)
  385. },
  386. loadViewInstancesSuccess: function(data) {
  387. data.instances.forEach(function(instance){
  388. var view = Em.Object.create({
  389. label: data.ViewInfo.label,
  390. viewName: instance.ViewInstanceInfo.view_name,
  391. instanceName: instance.ViewInstanceInfo.instance_name,
  392. href: "/views/" + instance.ViewInstanceInfo.view_name + "/" + instance.ViewInstanceInfo.instance_name
  393. });
  394. this.get('ambariViews').push(view);
  395. }, this);
  396. },
  397. /**
  398. *
  399. * @param callback
  400. */
  401. loadStackServiceComponents: function(callback) {
  402. var callbackObj = {
  403. loadStackServiceComponentsSuccess: callback
  404. };
  405. App.ajax.send({
  406. name: 'wizard.service_components',
  407. data: {
  408. stackUrl: App.get('stack2VersionURL'),
  409. stackVersion: App.get('currentStackVersionNumber'),
  410. async: true
  411. },
  412. sender: callbackObj,
  413. success: 'loadStackServiceComponentsSuccess'
  414. });
  415. },
  416. loadAmbariProperties: function () {
  417. App.ajax.send({
  418. name: 'ambari.service',
  419. sender: this,
  420. success: 'loadAmbariPropertiesSuccess',
  421. error: 'loadAmbariPropertiesError'
  422. });
  423. return this.get('ambariProperties');
  424. },
  425. loadAmbariPropertiesSuccess: function (data) {
  426. console.log('loading ambari properties');
  427. this.set('ambariProperties', data.RootServiceComponents.properties);
  428. this.set('ambariVersion', data.RootServiceComponents.component_version);
  429. },
  430. loadAmbariPropertiesError: function () {
  431. console.warn('can\'t get ambari properties');
  432. },
  433. clusterName: function () {
  434. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  435. }.property('cluster'),
  436. updateClusterData: function () {
  437. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  438. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  439. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  440. complete: function () {
  441. }
  442. });
  443. }
  444. });