cluster_controller.js 14 KB

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