cluster_controller.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. },
  148. success: 'setGangliaUrlSuccessCallback'
  149. });
  150. }
  151. }
  152. }.observes('App.router.updateController.isUpdated', 'dataLoadList.hosts', 'gangliaWebProtocol', 'isLoaded'),
  153. setGangliaUrlSuccessCallback: function (response) {
  154. var url = null;
  155. if (response.items.length > 0) {
  156. url = this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/ganglia";
  157. }
  158. this.set('gangliaUrl', url);
  159. this.set('isGangliaUrlLoaded', true);
  160. },
  161. setNagiosUrl: function () {
  162. if (App.testMode) {
  163. return 'http://nagiosserver/nagios';
  164. } else {
  165. // We want live data here
  166. if (this.get('isLoaded')) {
  167. this.set('isNagiosUrlLoaded', false);
  168. App.ajax.send({
  169. name: 'hosts.for_quick_links',
  170. sender: this,
  171. data: {
  172. clusterName: App.get('clusterName'),
  173. masterComponents: 'NAGIOS_SERVER'
  174. },
  175. success: 'setNagiosUrlSuccessCallback'
  176. });
  177. }
  178. }
  179. }.observes('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts', 'nagiosWebProtocol', 'isLoaded'),
  180. setNagiosUrlSuccessCallback: function (response) {
  181. var url = null;
  182. if (response.items.length > 0) {
  183. url = this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : response.items[0].Hosts.public_host_name) + "/nagios";
  184. }
  185. this.set('nagiosUrl', url);
  186. this.set('isNagiosUrlLoaded', true);
  187. },
  188. nagiosWebProtocol: function () {
  189. var properties = this.get('ambariProperties');
  190. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  191. return "https";
  192. } else {
  193. return "http";
  194. }
  195. }.property('ambariProperties'),
  196. gangliaWebProtocol: function () {
  197. var properties = this.get('ambariProperties');
  198. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  199. return "https";
  200. } else {
  201. return "http";
  202. }
  203. }.property('ambariProperties'),
  204. isNagiosInstalled: function () {
  205. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  206. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  207. isGangliaInstalled: function () {
  208. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  209. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  210. /**
  211. *
  212. * load all data and update load status
  213. */
  214. loadClusterData: function () {
  215. var self = this;
  216. this.loadAmbariProperties();
  217. this.loadAmbariViews();
  218. if (!this.get('clusterName')) {
  219. return;
  220. }
  221. if (this.get('isLoaded')) { // do not load data repeatedly
  222. App.router.get('mainController').startPolling();
  223. return;
  224. }
  225. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  226. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  227. var racksUrl = "/data/racks/racks.json";
  228. var hostsController = App.router.get('mainHostController');
  229. hostsController.set('isCountersUpdating', true);
  230. hostsController.updateStatusCounters();
  231. hostsController.set('isCountersUpdating', false);
  232. App.HttpClient.get(racksUrl, App.racksMapper, {
  233. complete: function (jqXHR, textStatus) {
  234. self.updateLoadStatus('racks');
  235. }
  236. }, function (jqXHR, textStatus) {
  237. self.updateLoadStatus('racks');
  238. });
  239. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  240. complete: function (jqXHR, textStatus) {
  241. self.updateLoadStatus('cluster');
  242. }
  243. }, function (jqXHR, textStatus) {
  244. self.updateLoadStatus('cluster');
  245. });
  246. if (App.testMode) {
  247. self.updateLoadStatus('clusterStatus');
  248. } else {
  249. App.clusterStatus.updateFromServer(true).complete(function () {
  250. self.updateLoadStatus('clusterStatus');
  251. });
  252. }
  253. App.HttpClient.get(usersUrl, App.usersMapper, {
  254. complete: function (jqXHR, textStatus) {
  255. self.updateLoadStatus('users');
  256. }
  257. }, function (jqXHR, textStatus) {
  258. self.updateLoadStatus('users');
  259. });
  260. /**
  261. * Order of loading:
  262. * 1. request for service components supported by stack
  263. * 2. load stack components to model
  264. * 3. request for services
  265. * 4. put services in cache
  266. * 5. request for hosts and host-components (single call)
  267. * 6. request for service metrics
  268. * 7. load host-components to model
  269. * 8. load hosts to model
  270. * 9. load services from cache with metrics to model
  271. * 10. update stale_configs of host-components (depends on App.supports.hostOverrides)
  272. */
  273. this.loadStackServiceComponents(function (data) {
  274. var updater = App.router.get('updateController');
  275. require('utils/component').loadStackServiceComponentModel(data);
  276. self.updateLoadStatus('stackComponents');
  277. updater.updateServices(function () {
  278. self.updateLoadStatus('services');
  279. updater.updateHost(function () {
  280. self.updateLoadStatus('hosts');
  281. });
  282. updater.updateServiceMetric(function () {
  283. updater.updateComponentsState(function () {
  284. self.updateLoadStatus('componentsState');
  285. });
  286. self.updateLoadStatus('serviceMetrics');
  287. });
  288. if (App.supports.hostOverrides) {
  289. updater.updateComponentConfig(function () {
  290. self.updateLoadStatus('componentConfigs');
  291. });
  292. } else {
  293. self.updateLoadStatus('componentConfigs');
  294. }
  295. });
  296. });
  297. },
  298. requestHosts: function (realUrl, callback) {
  299. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  300. var url = this.getUrl(testHostUrl, realUrl);
  301. App.HttpClient.get(url, App.hostsMapper, {
  302. complete: callback
  303. }, callback)
  304. },
  305. loadAmbariViews: function () {
  306. App.ajax.send({
  307. name: 'views.info',
  308. sender: this,
  309. success: 'loadAmbariViewsSuccess'
  310. });
  311. },
  312. loadAmbariViewsSuccess: function (data) {
  313. this.set('ambariViews', []);
  314. data.items.forEach(function (item) {
  315. App.ajax.send({
  316. name: 'views.instances',
  317. data: {
  318. viewName: item.ViewInfo.view_name
  319. },
  320. sender: this,
  321. success: 'loadViewInstancesSuccess'
  322. });
  323. }, this)
  324. },
  325. loadViewInstancesSuccess: function (data) {
  326. data.versions.forEach(function (version) {
  327. version.instances.forEach(function (instance) {
  328. var view = Em.Object.create({
  329. label: instance.ViewInstanceInfo.label || instance.ViewInstanceInfo.instance_name,
  330. viewName: instance.ViewInstanceInfo.view_name,
  331. instanceName: instance.ViewInstanceInfo.instance_name,
  332. version: version.ViewVersionInfo.version,
  333. href: "/views/" + instance.ViewInstanceInfo.view_name + "/" + version.ViewVersionInfo.version + "/" + instance.ViewInstanceInfo.instance_name
  334. });
  335. this.get('ambariViews').pushObject(view);
  336. }, this);
  337. }, this);
  338. },
  339. /**
  340. *
  341. * @param callback
  342. */
  343. loadStackServiceComponents: function (callback) {
  344. var callbackObj = {
  345. loadStackServiceComponentsSuccess: callback
  346. };
  347. App.ajax.send({
  348. name: 'wizard.service_components',
  349. data: {
  350. stackUrl: App.get('stack2VersionURL'),
  351. stackVersion: App.get('currentStackVersionNumber'),
  352. async: true
  353. },
  354. sender: callbackObj,
  355. success: 'loadStackServiceComponentsSuccess'
  356. });
  357. },
  358. loadAmbariProperties: function () {
  359. return App.ajax.send({
  360. name: 'ambari.service',
  361. sender: this,
  362. success: 'loadAmbariPropertiesSuccess',
  363. error: 'loadAmbariPropertiesError'
  364. });
  365. },
  366. loadAmbariPropertiesSuccess: function (data) {
  367. console.log('loading ambari properties');
  368. this.set('ambariProperties', data.RootServiceComponents.properties);
  369. },
  370. loadAmbariPropertiesError: function () {
  371. console.warn('can\'t get ambari properties');
  372. },
  373. clusterName: function () {
  374. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  375. }.property('cluster'),
  376. updateClusterData: function () {
  377. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json' : '/data/clusters/cluster.json';
  378. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  379. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  380. complete: function () {
  381. }
  382. });
  383. }
  384. });