cluster_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. 'services': false,
  56. 'cluster':false,
  57. 'clusterStatus':false,
  58. 'racks':false,
  59. 'users':false,
  60. 'componentConfigs': false
  61. }),
  62. /**
  63. * load cluster name
  64. */
  65. loadClusterName:function (reload) {
  66. if (this.get('clusterName') && !reload) {
  67. return;
  68. }
  69. App.ajax.send({
  70. name: 'cluster.load_cluster_name',
  71. sender: this,
  72. success: 'loadClusterNameSuccessCallback',
  73. error: 'loadClusterNameErrorCallback'
  74. });
  75. if(!App.get('currentStackVersion')){
  76. App.set('currentStackVersion', App.defaultStackVersion);
  77. }
  78. },
  79. loadClusterNameSuccessCallback: function (data) {
  80. this.set('cluster', data.items[0]);
  81. App.set('clusterName', data.items[0].Clusters.cluster_name);
  82. App.set('currentStackVersion', data.items[0].Clusters.version);
  83. },
  84. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  85. console.log('failed on loading cluster name');
  86. this.set('isLoaded', true);
  87. },
  88. /**
  89. * load current server clock in milli-seconds
  90. */
  91. loadClientServerClockDistance: function () {
  92. var dfd = $.Deferred();
  93. this.getServerClock().done(function () {
  94. dfd.resolve();
  95. });
  96. return dfd.promise();
  97. },
  98. getServerClock: function(){
  99. return App.ajax.send({
  100. name: 'ambari.service.load_server_clock',
  101. sender: this,
  102. success: 'getServerClockSuccessCallback',
  103. error: 'getServerClockErrorCallback'
  104. });
  105. },
  106. getServerClockSuccessCallback: function (data) {
  107. var clientClock = new Date().getTime();
  108. var serverClock = (data.RootServiceComponents.server_clock).toString();
  109. serverClock = serverClock.length < 13? serverClock+ '000': serverClock;
  110. App.set('clockDistance', serverClock - clientClock);
  111. App.set('currentServerTime', parseInt(serverClock));
  112. console.log('loading ambari server clock distance');
  113. },
  114. getServerClockErrorCallback: function () {
  115. console.log('Cannot load ambari server clock');
  116. },
  117. getUrl:function (testUrl, url) {
  118. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  119. },
  120. /**
  121. * Provides the URL to use for Ganglia server. This URL
  122. * is helpful in populating links in UI.
  123. *
  124. * If null is returned, it means GANGLIA service is not installed.
  125. */
  126. gangliaUrl: function () {
  127. if (App.testMode) {
  128. return 'http://gangliaserver/ganglia/?t=yes';
  129. } else {
  130. // We want live data here
  131. var svcs = App.Service.find();
  132. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  133. if (gangliaSvc) {
  134. var svcComponents = gangliaSvc.get('hostComponents');
  135. if (svcComponents) {
  136. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  137. if (gangliaSvcComponent) {
  138. var hostName = gangliaSvcComponent.get('host.hostName');
  139. if (hostName) {
  140. var host = App.Host.find(hostName);
  141. if (host) {
  142. hostName = host.get('publicHostName');
  143. }
  144. return this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/ganglia";
  145. }
  146. }
  147. }
  148. }
  149. return null;
  150. }
  151. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts','gangliaWebProtocol'),
  152. /**
  153. * Provides the URL to use for NAGIOS server. This URL
  154. * is helpful in getting alerts data from server and also
  155. * in populating links in UI.
  156. *
  157. * If null is returned, it means NAGIOS service is not installed.
  158. */
  159. nagiosUrl:function () {
  160. if (App.testMode) {
  161. return 'http://nagiosserver/nagios';
  162. } else {
  163. // We want live data here
  164. var svcs = App.Service.find();
  165. var nagiosSvc = svcs.findProperty("serviceName", "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 services
  304. * 2. put services in cache
  305. * 3. request for hosts and host-components (single call)
  306. * 4. request for service metrics
  307. * 5. load host-components to model
  308. * 6. load hosts to model
  309. * 7. load services from cache with metrics to model
  310. * 8. update stale_configs of host-components (depends on App.supports.hostOverrides)
  311. */
  312. App.router.get('updateController').updateServices(function () {
  313. self.updateLoadStatus('services');
  314. self.loadUpdatedStatus(function () {
  315. self.updateLoadStatus('hosts');
  316. if (App.supports.hostOverrides) {
  317. App.router.get('updateController').updateComponentConfig(function () {
  318. self.updateLoadStatus('componentConfigs');
  319. });
  320. } else {
  321. self.updateLoadStatus('componentConfigs');
  322. }
  323. }, true);
  324. App.router.get('updateController').updateServiceMetric(function () {}, true);
  325. });
  326. },
  327. /**
  328. * json from serviceMetricsMapper on initial load
  329. */
  330. serviceMetricsJson: null,
  331. /**
  332. * control that services was loaded to model strictly after hosts and host-components
  333. * regardless which request was completed first
  334. * @param json
  335. */
  336. deferServiceMetricsLoad: function (json) {
  337. if (json) {
  338. if (this.get('dataLoadList.hosts')) {
  339. App.serviceMetricsMapper.map(json, true);
  340. this.updateLoadStatus('serviceMetrics');
  341. } else {
  342. this.set('serviceMetricsJson', json);
  343. }
  344. } else if (this.get('serviceMetricsJson')) {
  345. json = this.get('serviceMetricsJson');
  346. this.set('serviceMetricsJson', null);
  347. App.serviceMetricsMapper.map(json, true);
  348. this.updateLoadStatus('serviceMetrics');
  349. }
  350. },
  351. requestHosts: function(realUrl, callback){
  352. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json':'/data/hosts/hosts.json';
  353. var url = this.getUrl(testHostUrl, realUrl);
  354. App.HttpClient.get(url, App.hostsMapper, {
  355. complete: callback
  356. }, callback)
  357. },
  358. loadAmbariViews: function() {
  359. App.ajax.send({
  360. name: 'views.info',
  361. sender: this,
  362. success: 'loadAmbariViewsSuccess'
  363. });
  364. },
  365. loadAmbariViewsSuccess: function(data) {
  366. this.set('ambariViews',[]);
  367. data.items.forEach(function(item){
  368. App.ajax.send({
  369. name: 'views.instances',
  370. data: {
  371. viewName: item.ViewInfo.view_name
  372. },
  373. sender: this,
  374. success: 'loadViewInstancesSuccess'
  375. });
  376. }, this)
  377. },
  378. loadViewInstancesSuccess: function(data) {
  379. data.instances.forEach(function(instance){
  380. var view = Em.Object.create({
  381. label: data.ViewInfo.label,
  382. viewName: instance.ViewInstanceInfo.view_name,
  383. instanceName: instance.ViewInstanceInfo.instance_name,
  384. href: "/views/" + instance.ViewInstanceInfo.view_name + "/" + instance.ViewInstanceInfo.instance_name
  385. });
  386. this.get('ambariViews').push(view);
  387. }, this);
  388. },
  389. loadAmbariProperties: function() {
  390. App.ajax.send({
  391. name: 'ambari.service',
  392. sender: this,
  393. success: 'loadAmbariPropertiesSuccess',
  394. error: 'loadAmbariPropertiesError'
  395. });
  396. return this.get('ambariProperties');
  397. },
  398. loadAmbariPropertiesSuccess: function(data) {
  399. console.log('loading ambari properties');
  400. this.set('ambariProperties', data.RootServiceComponents.properties);
  401. this.set('ambariVersion', data.RootServiceComponents.component_version);
  402. },
  403. loadAmbariPropertiesError: function() {
  404. console.warn('can\'t get ambari properties');
  405. },
  406. clusterName:function () {
  407. return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
  408. }.property('cluster'),
  409. updateClusterData: function () {
  410. var testUrl = App.get('isHadoop2Stack') ? '/data/clusters/HDP2/cluster.json':'/data/clusters/cluster.json';
  411. var clusterUrl = this.getUrl(testUrl, '?fields=Clusters');
  412. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  413. complete:function(){}
  414. });
  415. }
  416. });