cluster_controller.js 13 KB

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