cluster_controller.js 14 KB

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