cluster_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. 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. 'services': false,
  55. 'cluster':false,
  56. 'clusterStatus':false,
  57. 'racks':false,
  58. 'users':false,
  59. 'componentConfigs': false
  60. }),
  61. /**
  62. * load cluster name
  63. */
  64. loadClusterName:function (reload) {
  65. if (this.get('clusterName') && !reload) {
  66. return;
  67. }
  68. App.ajax.send({
  69. name: 'cluster.load_cluster_name',
  70. sender: this,
  71. success: 'loadClusterNameSuccessCallback',
  72. error: 'loadClusterNameErrorCallback'
  73. });
  74. if(!App.get('currentStackVersion')){
  75. App.set('currentStackVersion', App.defaultStackVersion);
  76. }
  77. },
  78. loadClusterNameSuccessCallback: function (data) {
  79. this.set('cluster', data.items[0]);
  80. App.set('clusterName', data.items[0].Clusters.cluster_name);
  81. App.set('currentStackVersion', data.items[0].Clusters.version);
  82. },
  83. loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
  84. console.log('failed on loading cluster name');
  85. this.set('isLoaded', true);
  86. },
  87. /**
  88. * load current server clock in milli-seconds
  89. */
  90. loadClientServerClockDistance: function () {
  91. var dfd = $.Deferred();
  92. this.getServerClock().done(function () {
  93. dfd.resolve();
  94. });
  95. return dfd.promise();
  96. },
  97. getServerClock: function(){
  98. return App.ajax.send({
  99. name: 'ambari.service.load_server_clock',
  100. sender: this,
  101. success: 'getServerClockSuccessCallback',
  102. error: 'getServerClockErrorCallback'
  103. });
  104. },
  105. getServerClockSuccessCallback: function (data) {
  106. var clientClock = new Date().getTime();
  107. var serverClock = (data.RootServiceComponents.server_clock).toString();
  108. serverClock = serverClock.length < 13? serverClock+ '000': serverClock;
  109. App.set('clockDistance', serverClock - clientClock);
  110. App.set('currentServerTime', parseInt(serverClock));
  111. console.log('loading ambari server clock distance');
  112. },
  113. getServerClockErrorCallback: function () {
  114. console.log('Cannot load ambari server clock');
  115. },
  116. getUrl:function (testUrl, url) {
  117. return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  118. },
  119. /**
  120. * Provides the URL to use for Ganglia server. This URL
  121. * is helpful in populating links in UI.
  122. *
  123. * If null is returned, it means GANGLIA service is not installed.
  124. */
  125. gangliaUrl: function () {
  126. if (App.testMode) {
  127. return 'http://gangliaserver/ganglia/?t=yes';
  128. } else {
  129. // We want live data here
  130. var svcs = App.Service.find();
  131. var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
  132. if (gangliaSvc) {
  133. var svcComponents = gangliaSvc.get('hostComponents');
  134. if (svcComponents) {
  135. var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
  136. if (gangliaSvcComponent) {
  137. var hostName = gangliaSvcComponent.get('host.hostName');
  138. if (hostName) {
  139. var host = App.Host.find(hostName);
  140. if (host) {
  141. hostName = host.get('publicHostName');
  142. }
  143. return this.get('gangliaWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/ganglia";
  144. }
  145. }
  146. }
  147. }
  148. return null;
  149. }
  150. }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts','gangliaWebProtocol'),
  151. /**
  152. * Provides the URL to use for NAGIOS server. This URL
  153. * is helpful in getting alerts data from server and also
  154. * in populating links in UI.
  155. *
  156. * If null is returned, it means NAGIOS service is not installed.
  157. */
  158. nagiosUrl:function () {
  159. if (App.testMode) {
  160. return 'http://nagiosserver/nagios';
  161. } else {
  162. // We want live data here
  163. var svcs = App.Service.find();
  164. var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
  165. if (nagiosSvc) {
  166. var svcComponents = nagiosSvc.get('hostComponents');
  167. if (svcComponents) {
  168. var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
  169. if (nagiosSvcComponent) {
  170. var hostName = nagiosSvcComponent.get('host.hostName');
  171. if (hostName) {
  172. var host = App.Host.find(hostName);
  173. if (host) {
  174. hostName = host.get('publicHostName');
  175. }
  176. return this.get('nagiosWebProtocol') + "://" + (App.singleNodeInstall ? App.singleNodeAlias + ":42080" : hostName) + "/nagios";
  177. }
  178. }
  179. }
  180. }
  181. return null;
  182. }
  183. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics', 'dataLoadList.hosts','nagiosWebProtocol'),
  184. nagiosWebProtocol: function () {
  185. var properties = this.get('ambariProperties');
  186. if (properties && properties.hasOwnProperty('nagios.https') && properties['nagios.https']) {
  187. return "https";
  188. } else {
  189. return "http";
  190. }
  191. }.property('ambariProperties'),
  192. gangliaWebProtocol: function () {
  193. var properties = this.get('ambariProperties');
  194. if (properties && properties.hasOwnProperty('ganglia.https') && properties['ganglia.https']) {
  195. return "https";
  196. } else {
  197. return "http";
  198. }
  199. }.property('ambariProperties'),
  200. isNagiosInstalled:function () {
  201. return !!App.Service.find().findProperty('serviceName', 'NAGIOS');
  202. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  203. isGangliaInstalled:function () {
  204. return !!App.Service.find().findProperty('serviceName', 'GANGLIA');
  205. }.property('App.router.updateController.isUpdated', 'dataLoadList.serviceMetrics'),
  206. /**
  207. * Send request to server to load components updated statuses
  208. * @param callback Slave function, should be called to fire delayed update.
  209. * @param isInitialLoad
  210. * Look at <code>App.updater.run</code> for more information
  211. * @return {Boolean} Whether we have errors
  212. */
  213. loadUpdatedStatus: function (callback, isInitialLoad) {
  214. if (!this.get('clusterName')) {
  215. callback();
  216. return false;
  217. }
  218. App.set('currentServerTime', App.get('currentServerTime') + App.componentsUpdateInterval);
  219. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hc_host_status.json' : '/data/dashboard/services.json';
  220. var statusUrl = '/hosts?fields=Hosts/host_status,Hosts/maintenance_state,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,alerts/summary&minimal_response=true';
  221. if (isInitialLoad) {
  222. testUrl = '/data/hosts/HDP2/hosts_init.json';
  223. statusUrl = '/hosts?fields=Hosts/host_name,Hosts/maintenance_state,Hosts/public_host_name,Hosts/cpu_count,Hosts/ph_cpu_count,Hosts/total_mem,' +
  224. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,' +
  225. 'Hosts/disk_info,metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,' +
  226. 'metrics/memory/mem_total,metrics/memory/mem_free,alerts/summary&minimal_response=true';
  227. }
  228. //desired_state property is eliminated since calculateState function is commented out, it become useless
  229. statusUrl = this.getUrl(testUrl, statusUrl);
  230. App.HttpClient.get(statusUrl, App.statusMapper, {
  231. complete: callback
  232. });
  233. return true;
  234. },
  235. /**
  236. * Run <code>loadUpdatedStatus</code> with delay
  237. * @param delay
  238. */
  239. loadUpdatedStatusDelayed: function(delay){
  240. setTimeout(function(){
  241. App.updater.immediateRun('loadUpdatedStatus');
  242. }, delay);
  243. },
  244. /**
  245. * Start polling, when <code>isWorking</code> become true
  246. */
  247. startPolling: function(){
  248. if(!this.get('isWorking')){
  249. return false;
  250. }
  251. App.updater.run(this, 'loadUpdatedStatus', 'isWorking', App.componentsUpdateInterval); //update will not run it immediately
  252. return true;
  253. }.observes('isWorking'),
  254. /**
  255. *
  256. * load all data and update load status
  257. */
  258. loadClusterData:function () {
  259. var self = this;
  260. this.loadAmbariProperties();
  261. if (!this.get('clusterName')) {
  262. return;
  263. }
  264. if(this.get('isLoaded')) { // do not load data repeatedly
  265. App.router.get('mainController').startPolling();
  266. return;
  267. }
  268. var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
  269. var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
  270. var racksUrl = "/data/racks/racks.json";
  271. App.HttpClient.get(racksUrl, App.racksMapper, {
  272. complete:function (jqXHR, textStatus) {
  273. self.updateLoadStatus('racks');
  274. }
  275. }, function (jqXHR, textStatus) {
  276. self.updateLoadStatus('racks');
  277. });
  278. App.HttpClient.get(clusterUrl, App.clusterMapper, {
  279. complete:function (jqXHR, textStatus) {
  280. self.updateLoadStatus('cluster');
  281. }
  282. }, function (jqXHR, textStatus) {
  283. self.updateLoadStatus('cluster');
  284. });
  285. if (App.testMode) {
  286. self.updateLoadStatus('clusterStatus');
  287. } else {
  288. App.clusterStatus.updateFromServer(true).complete(function() {
  289. self.updateLoadStatus('clusterStatus');
  290. });
  291. }
  292. App.HttpClient.get(usersUrl, App.usersMapper, {
  293. complete:function (jqXHR, textStatus) {
  294. self.updateLoadStatus('users');
  295. }
  296. }, function (jqXHR, textStatus) {
  297. self.updateLoadStatus('users');
  298. });
  299. /**
  300. * Order of loading:
  301. * 1. request for services
  302. * 2. put services in cache
  303. * 3. request for hosts and host-components (single call)
  304. * 4. request for service metrics
  305. * 5. load host-components to model
  306. * 6. load hosts to model
  307. * 7. load services from cache with metrics to model
  308. * 8. update stale_configs of host-components (depends on App.supports.hostOverrides)
  309. */
  310. App.router.get('updateController').updateServices(function () {
  311. self.updateLoadStatus('services');
  312. self.loadUpdatedStatus(function () {
  313. self.updateLoadStatus('hosts');
  314. if (App.supports.hostOverrides) {
  315. App.router.get('updateController').updateComponentConfig(function () {
  316. self.updateLoadStatus('componentConfigs');
  317. });
  318. } else {
  319. self.updateLoadStatus('componentConfigs');
  320. }
  321. }, true);
  322. App.router.get('updateController').updateServiceMetric(function () {}, true);
  323. });
  324. },
  325. /**
  326. * json from serviceMetricsMapper on initial load
  327. */
  328. serviceMetricsJson: null,
  329. /**
  330. * control that services was loaded to model strictly after hosts and host-components
  331. * regardless which request was completed first
  332. * @param json
  333. */
  334. deferServiceMetricsLoad: function (json) {
  335. if (json) {
  336. if (this.get('dataLoadList.hosts')) {
  337. App.serviceMetricsMapper.map(json, true);
  338. this.updateLoadStatus('serviceMetrics');
  339. } else {
  340. this.set('serviceMetricsJson', json);
  341. }
  342. } else if (this.get('serviceMetricsJson')) {
  343. json = this.get('serviceMetricsJson');
  344. this.set('serviceMetricsJson', null);
  345. App.serviceMetricsMapper.map(json, true);
  346. this.updateLoadStatus('serviceMetrics');
  347. }
  348. },
  349. requestHosts: function(realUrl, callback){
  350. var testHostUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json':'/data/hosts/hosts.json';
  351. var url = this.getUrl(testHostUrl, realUrl);
  352. App.HttpClient.get(url, App.hostsMapper, {
  353. complete: callback
  354. }, callback)
  355. },
  356. loadAmbariProperties: function() {
  357. App.ajax.send({
  358. name: 'ambari.service',
  359. sender: this,
  360. success: 'loadAmbariPropertiesSuccess',
  361. error: 'loadAmbariPropertiesError'
  362. });
  363. return this.get('ambariProperties');
  364. },
  365. loadAmbariPropertiesSuccess: function(data) {
  366. console.log('loading ambari properties');
  367. this.set('ambariProperties', data.RootServiceComponents.properties);
  368. this.set('ambariVersion', data.RootServiceComponents.component_version);
  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. });