cluster_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. 'componentsState': 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. var updater = App.router.get('updateController');
  316. require('utils/component').loadStackServiceComponentModel(data);
  317. self.updateLoadStatus('stackComponents');
  318. updater.updateServices(function () {
  319. self.updateLoadStatus('services');
  320. self.loadUpdatedStatus(function () {
  321. updater.updateHost(function () {
  322. self.updateLoadStatus('hosts');
  323. });
  324. updater.updateServiceMetric(function () {
  325. updater.updateComponentsState(function () {
  326. self.updateLoadStatus('componentsState');
  327. });
  328. self.updateLoadStatus('serviceMetrics');
  329. });
  330. if (App.supports.hostOverrides) {
  331. updater.updateComponentConfig(function () {
  332. self.updateLoadStatus('componentConfigs');
  333. });
  334. } else {
  335. self.updateLoadStatus('componentConfigs');
  336. }
  337. }, true);
  338. });
  339. });
  340. },
  341. requestHosts: function (realUrl, callback) {
  342. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  343. var url = this.getUrl(testHostUrl, realUrl);
  344. App.HttpClient.get(url, App.hostsMapper, {
  345. complete: callback
  346. }, callback)
  347. },
  348. loadAmbariViews: function () {
  349. App.ajax.send({
  350. name: 'views.info',
  351. sender: this,
  352. success: 'loadAmbariViewsSuccess'
  353. });
  354. },
  355. loadAmbariViewsSuccess: function (data) {
  356. this.set('ambariViews', []);
  357. data.items.forEach(function (item) {
  358. App.ajax.send({
  359. name: 'views.instances',
  360. data: {
  361. viewName: item.ViewInfo.view_name
  362. },
  363. sender: this,
  364. success: 'loadViewInstancesSuccess'
  365. });
  366. }, this)
  367. },
  368. loadViewInstancesSuccess: function (data) {
  369. data.versions.forEach(function (version) {
  370. version.instances.forEach(function (instance) {
  371. var view = Em.Object.create({
  372. label: instance.ViewInstanceInfo.label || instance.ViewInstanceInfo.instance_name,
  373. viewName: instance.ViewInstanceInfo.view_name,
  374. instanceName: instance.ViewInstanceInfo.instance_name,
  375. version: version.ViewVersionInfo.version,
  376. href: "/views/" + instance.ViewInstanceInfo.view_name + "/" + version.ViewVersionInfo.version + "/" + instance.ViewInstanceInfo.instance_name
  377. });
  378. this.get('ambariViews').pushObject(view);
  379. }, this);
  380. }, this);
  381. },
  382. /**
  383. *
  384. * @param callback
  385. */
  386. loadStackServiceComponents: function (callback) {
  387. var callbackObj = {
  388. loadStackServiceComponentsSuccess: callback
  389. };
  390. App.ajax.send({
  391. name: 'wizard.service_components',
  392. data: {
  393. stackUrl: App.get('stack2VersionURL'),
  394. stackVersion: App.get('currentStackVersionNumber'),
  395. async: true
  396. },
  397. sender: callbackObj,
  398. success: 'loadStackServiceComponentsSuccess'
  399. });
  400. },
  401. loadAmbariProperties: function () {
  402. App.ajax.send({
  403. name: 'ambari.service',
  404. sender: this,
  405. success: 'loadAmbariPropertiesSuccess',
  406. error: 'loadAmbariPropertiesError'
  407. });
  408. return this.get('ambariProperties');
  409. },
  410. loadAmbariPropertiesSuccess: function (data) {
  411. console.log('loading ambari properties');
  412. this.set('ambariProperties', data.RootServiceComponents.properties);
  413. },
  414. loadAmbariPropertiesError: function () {
  415. console.warn('can\'t get ambari properties');
  416. },
  417. clusterName: function () {
  418. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  419. }.property('cluster'),
  420. updateClusterData: function () {
  421. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  422. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  423. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  424. complete: function () {
  425. }
  426. });
  427. }
  428. });