cluster_controller.js 16 KB

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