cluster_controller.js 13 KB

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