update_controller.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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.UpdateController = Em.Controller.extend({
  20. name: 'updateController',
  21. isUpdated: false,
  22. cluster: null,
  23. isWorking: false,
  24. timeIntervalId: null,
  25. clusterName: function () {
  26. return App.router.get('clusterController.clusterName');
  27. }.property('App.router.clusterController.clusterName'),
  28. location: function () {
  29. return App.router.get('location.lastSetURL');
  30. }.property('App.router.location.lastSetURL'),
  31. /**
  32. * keys which should be preloaded in order to filter hosts by host-components
  33. */
  34. hostsPreLoadKeys: ['host_components/HostRoles/component_name', 'host_components/HostRoles/stale_configs', 'host_components/HostRoles/maintenance_state'],
  35. paginationKeys: ['page_size', 'from'],
  36. getUrl: function (testUrl, url) {
  37. return (App.get('testMode')) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  38. },
  39. /**
  40. * construct URL from real URL and query parameters
  41. * @param testUrl
  42. * @param realUrl
  43. * @param queryParams
  44. * @return {String}
  45. */
  46. getComplexUrl: function (testUrl, realUrl, queryParams) {
  47. var prefix = App.get('apiPrefix') + '/clusters/' + App.get('clusterName'),
  48. params = '';
  49. if (App.get('testMode')) {
  50. return testUrl;
  51. } else {
  52. if (queryParams) {
  53. params = this.computeParameters(queryParams);
  54. }
  55. return prefix + realUrl.replace('<parameters>', params);
  56. }
  57. },
  58. /**
  59. * compute parameters according to their type
  60. * @param queryParams
  61. * @return {String}
  62. */
  63. computeParameters: function (queryParams) {
  64. var params = '';
  65. queryParams.forEach(function (param) {
  66. switch (param.type) {
  67. case 'EQUAL':
  68. params += param.key + '=' + param.value;
  69. break;
  70. case 'LESS':
  71. params += param.key + '<' + param.value;
  72. break;
  73. case 'MORE':
  74. params += param.key + '>' + param.value;
  75. break;
  76. case 'MATCH':
  77. params += param.key + '.matches(' + param.value + ')';
  78. break;
  79. case 'MULTIPLE':
  80. params += param.key + '.in(' + param.value.join(',') + ')';
  81. break;
  82. case 'SORT':
  83. params += 'sortBy=' + param.key + '.' + param.value;
  84. break;
  85. case 'CUSTOM':
  86. param.value.forEach(function(item, index){
  87. param.key = param.key.replace('{' + index + '}', item);
  88. }, this);
  89. params += param.key;
  90. break;
  91. }
  92. params += '&';
  93. });
  94. return params;
  95. },
  96. /**
  97. * depict query parameters of table
  98. */
  99. queryParams: Em.Object.create({
  100. 'Hosts': []
  101. }),
  102. /**
  103. * map describes relations between updater function and table
  104. */
  105. tableUpdaterMap: {
  106. 'Hosts': 'updateHost'
  107. },
  108. /**
  109. * Start polling, when <code>isWorking</code> become true
  110. */
  111. updateAll: function () {
  112. if (this.get('isWorking')) {
  113. App.updater.run(this, 'updateServices', 'isWorking');
  114. App.updater.run(this, 'updateHost', 'isWorking');
  115. App.updater.run(this, 'updateServiceMetricConditionally', 'isWorking', App.componentsUpdateInterval);
  116. App.updater.run(this, 'updateComponentsState', 'isWorking', App.componentsUpdateInterval);
  117. App.updater.run(this, 'graphsUpdate', 'isWorking');
  118. App.updater.run(this, 'updateComponentConfig', 'isWorking');
  119. }
  120. }.observes('isWorking'),
  121. /**
  122. * Update service metrics depending on which page is open
  123. * Make a call only on follow pages:
  124. * /main/dashboard
  125. * /main/services/*
  126. * @param callback
  127. */
  128. updateServiceMetricConditionally: function (callback) {
  129. if (/\/main\/(dashboard|services).*/.test(this.get('location'))) {
  130. this.updateServiceMetric(callback);
  131. } else {
  132. callback();
  133. }
  134. },
  135. updateHost: function (callback, error) {
  136. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json',
  137. self = this,
  138. hostDetailsFilter = '';
  139. var realUrl = '/hosts?<parameters>fields=Hosts/host_name,Hosts/maintenance_state,Hosts/public_host_name,Hosts/cpu_count,Hosts/ph_cpu_count,' +
  140. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/ip,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,' +
  141. 'host_components/HostRoles/stale_configs,host_components/HostRoles/service_name,host_components/HostRoles/desired_admin_state,' +
  142. 'metrics/disk,metrics/load/load_one,Hosts/total_mem,legacy_alerts/summary<hostAuxiliaryInfo>&minimal_response=true';
  143. var hostAuxiliaryInfo = ',Hosts/os_arch,Hosts/os_type,metrics/cpu/cpu_system,metrics/cpu/cpu_user,metrics/memory/mem_total,metrics/memory/mem_free';
  144. if (App.router.get('currentState.name') == 'index' && App.router.get('currentState.parentState.name') == 'hosts') {
  145. App.updater.updateInterval('updateHost', App.get('contentUpdateInterval'));
  146. }
  147. else {
  148. if (App.router.get('currentState.name') == 'summary' && App.router.get('currentState.parentState.name') == 'hostDetails') {
  149. hostDetailsFilter = App.router.get('location.lastSetURL').match(/\/hosts\/(.*)\/summary/)[1];
  150. App.updater.updateInterval('updateHost', App.get('componentsUpdateInterval'));
  151. }
  152. else {
  153. callback();
  154. // On pages except for hosts/hostDetails, making sure hostsMapper loaded only once on page load, no need to update, but at least once
  155. if (App.router.get('clusterController.isLoaded')) {
  156. return;
  157. }
  158. }
  159. }
  160. var mainHostController = App.router.get('mainHostController'),
  161. sortProperties = mainHostController.getSortProperties();
  162. if (hostDetailsFilter) {
  163. //if host details page opened then request info only of one displayed host
  164. this.get('queryParams').set('Hosts', [
  165. {
  166. key: 'Hosts/host_name',
  167. value: [hostDetailsFilter],
  168. type: 'MULTIPLE'
  169. }
  170. ]);
  171. } else {
  172. hostAuxiliaryInfo = '';
  173. this.get('queryParams').set('Hosts', mainHostController.getQueryParameters(true));
  174. }
  175. realUrl = realUrl.replace('<hostAuxiliaryInfo>', hostAuxiliaryInfo);
  176. var clientCallback = function (skipCall, queryParams) {
  177. if (skipCall) {
  178. //no hosts match filter by component
  179. App.hostsMapper.map({
  180. items: [],
  181. itemTotal: '0'
  182. });
  183. callback();
  184. }
  185. else {
  186. var params = self.computeParameters(queryParams),
  187. paginationProps = self.computeParameters(queryParams.filter(function (param) {
  188. return (this.get('paginationKeys').contains(param.key));
  189. }, self)),
  190. sortProps = self.computeParameters(sortProperties);
  191. if ((params.length + paginationProps.length + sortProps.length) > 0) {
  192. realUrl = App.get('apiPrefix') + '/clusters/' + App.get('clusterName') +
  193. realUrl.replace('<parameters>', '') +
  194. (paginationProps.length > 0 ? '&' + paginationProps.substring(0, paginationProps.length - 1) : '') +
  195. (sortProps.length > 0 ? '&' + sortProps.substring(0, sortProps.length - 1) : '');
  196. if (App.get('testMode')) {
  197. realUrl = testUrl;
  198. }
  199. App.HttpClient.get(realUrl, App.hostsMapper, {
  200. complete: callback,
  201. doGetAsPost: true,
  202. params: params.substring(0, params.length - 1),
  203. error: error
  204. });
  205. }
  206. else {
  207. var hostsUrl = self.getComplexUrl(testUrl, realUrl, queryParams);
  208. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  209. complete: callback,
  210. doGetAsPost: false,
  211. error: error
  212. });
  213. }
  214. }
  215. };
  216. if (!this.preLoadHosts(clientCallback)) {
  217. clientCallback(false, self.get('queryParams.Hosts'));
  218. }
  219. },
  220. /**
  221. * identify if any filter by host-component is active
  222. * if so run @getHostByHostComponents
  223. *
  224. * @param callback
  225. * @return {Boolean}
  226. */
  227. preLoadHosts: function (callback) {
  228. var preLoadKeys = this.get('hostsPreLoadKeys');
  229. if (this.get('queryParams.Hosts').length > 0 && this.get('queryParams.Hosts').filter(function (param) {
  230. return (preLoadKeys.contains(param.key));
  231. }, this).length > 0) {
  232. this.getHostByHostComponents(callback);
  233. return true;
  234. }
  235. return false;
  236. },
  237. /**
  238. * get hosts' names which match filter by host-component
  239. * @param callback
  240. */
  241. getHostByHostComponents: function (callback) {
  242. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  243. var realUrl = '/hosts?<parameters>minimal_response=true';
  244. App.ajax.send({
  245. name: 'hosts.host_components.pre_load',
  246. sender: this,
  247. data: {
  248. url: this.getComplexUrl(testUrl, realUrl, this.get('queryParams.Hosts')),
  249. callback: callback
  250. },
  251. success: 'getHostByHostComponentsSuccessCallback',
  252. error: 'getHostByHostComponentsErrorCallback'
  253. })
  254. },
  255. getHostByHostComponentsSuccessCallback: function (data, opt, params) {
  256. var preLoadKeys = this.get('hostsPreLoadKeys');
  257. var queryParams = this.get('queryParams.Hosts');
  258. var hostNames = data.items.mapProperty('Hosts.host_name');
  259. var skipCall = hostNames.length === 0;
  260. /**
  261. * exclude pagination parameters as they were applied in previous call
  262. * to obtain hostnames of filtered hosts
  263. */
  264. preLoadKeys = preLoadKeys.concat(this.get('paginationKeys'));
  265. var itemTotal = parseInt(data.itemTotal);
  266. if (!isNaN(itemTotal)) {
  267. App.router.set('mainHostController.filteredCount', itemTotal);
  268. }
  269. if (skipCall) {
  270. params.callback(skipCall);
  271. } else {
  272. queryParams = queryParams.filter(function (param) {
  273. return !(preLoadKeys.contains(param.key));
  274. });
  275. queryParams.push({
  276. key: 'Hosts/host_name',
  277. value: hostNames,
  278. type: 'MULTIPLE'
  279. });
  280. params.callback(skipCall, queryParams);
  281. }
  282. },
  283. getHostByHostComponentsErrorCallback: function () {
  284. console.warn('ERROR: filtering hosts by host-component failed');
  285. },
  286. graphs: [],
  287. graphsUpdate: function (callback) {
  288. var existedGraphs = [];
  289. this.get('graphs').forEach(function (_graph) {
  290. var view = Em.View.views[_graph.id];
  291. if (view) {
  292. existedGraphs.push(_graph);
  293. //console.log('updated graph', _graph.name);
  294. view.loadData();
  295. //if graph opened as modal popup update it to
  296. if ($(".modal-graph-line .modal-body #" + _graph.popupId + "-container-popup").length) {
  297. view.loadData();
  298. }
  299. }
  300. });
  301. callback();
  302. this.set('graphs', existedGraphs);
  303. },
  304. /**
  305. * Updates the services information.
  306. *
  307. * @param callback
  308. */
  309. updateServiceMetric: function (callback) {
  310. var self = this;
  311. self.set('isUpdated', false);
  312. var isATSPresent = App.StackServiceComponent.find().findProperty('componentName','APP_TIMELINE_SERVER');
  313. var conditionalFields = this.getConditionalFields(),
  314. conditionalFieldsString = conditionalFields.length > 0 ? ',' + conditionalFields.join(',') : '',
  315. testUrl = App.get('isHadoop2Stack') ? '/data/dashboard/HDP2/master_components.json' : '/data/dashboard/services.json',
  316. isFlumeInstalled = App.cache['services'].mapProperty('ServiceInfo.service_name').contains('FLUME'),
  317. isATSInstalled = App.cache['services'].mapProperty('ServiceInfo.service_name').contains('YARN') && isATSPresent,
  318. flumeHandlerParam = isFlumeInstalled ? 'ServiceComponentInfo/component_name=FLUME_HANDLER|' : '',
  319. atsHandlerParam = isATSInstalled ? 'ServiceComponentInfo/component_name=APP_TIMELINE_SERVER|' : '',
  320. haComponents = App.get('isHaEnabled') ? 'ServiceComponentInfo/component_name=JOURNALNODE|ServiceComponentInfo/component_name=ZKFC|' : '',
  321. realUrl = '/components/?' + flumeHandlerParam + atsHandlerParam + haComponents +
  322. 'ServiceComponentInfo/category=MASTER&fields=' +
  323. 'ServiceComponentInfo/Version,' +
  324. 'ServiceComponentInfo/StartTime,' +
  325. 'ServiceComponentInfo/HeapMemoryUsed,' +
  326. 'ServiceComponentInfo/HeapMemoryMax,' +
  327. 'ServiceComponentInfo/service_name,' +
  328. 'host_components/HostRoles/host_name,' +
  329. 'host_components/HostRoles/state,' +
  330. 'host_components/HostRoles/maintenance_state,' +
  331. 'host_components/HostRoles/stale_configs,' +
  332. 'host_components/HostRoles/ha_state,' +
  333. 'host_components/HostRoles/desired_admin_state,' +
  334. 'host_components/metrics/jvm/memHeapUsedM,' +
  335. 'host_components/metrics/jvm/HeapMemoryMax,' +
  336. 'host_components/metrics/jvm/HeapMemoryUsed,' +
  337. 'host_components/metrics/jvm/memHeapCommittedM,' +
  338. 'host_components/metrics/mapred/jobtracker/trackers_decommissioned,' +
  339. 'host_components/metrics/cpu/cpu_wio,' +
  340. 'host_components/metrics/rpc/RpcQueueTime_avg_time,' +
  341. 'host_components/metrics/dfs/FSNamesystem/*,' +
  342. 'host_components/metrics/dfs/namenode/Version,' +
  343. 'host_components/metrics/dfs/namenode/DecomNodes,' +
  344. 'host_components/metrics/dfs/namenode/TotalFiles,' +
  345. 'host_components/metrics/dfs/namenode/UpgradeFinalized,' +
  346. 'host_components/metrics/dfs/namenode/Safemode,' +
  347. 'host_components/metrics/runtime/StartTime' +
  348. conditionalFieldsString +
  349. '&minimal_response=true';
  350. var servicesUrl = this.getUrl(testUrl, realUrl);
  351. callback = callback || function () {
  352. self.set('isUpdated', true);
  353. };
  354. App.HttpClient.get(servicesUrl, App.serviceMetricsMapper, {
  355. complete: function () {
  356. callback();
  357. }
  358. });
  359. },
  360. /**
  361. * construct conditional parameters of query, depending on which services are installed
  362. * @return {Array}
  363. */
  364. getConditionalFields: function () {
  365. var conditionalFields = [];
  366. var serviceSpecificParams = {
  367. 'FLUME': "host_components/metrics/flume/flume," +
  368. "host_components/processes/HostComponentProcess",
  369. 'YARN': "host_components/metrics/yarn/Queue," +
  370. "ServiceComponentInfo/rm_metrics/cluster/activeNMcount," +
  371. "ServiceComponentInfo/rm_metrics/cluster/unhealthyNMcount," +
  372. "ServiceComponentInfo/rm_metrics/cluster/rebootedNMcount," +
  373. "ServiceComponentInfo/rm_metrics/cluster/decommissionedNMcount",
  374. 'HBASE': "host_components/metrics/hbase/master/IsActiveMaster," +
  375. "ServiceComponentInfo/MasterStartTime," +
  376. "ServiceComponentInfo/MasterActiveTime," +
  377. "ServiceComponentInfo/AverageLoad," +
  378. "ServiceComponentInfo/Revision," +
  379. "ServiceComponentInfo/RegionsInTransition",
  380. 'MAPREDUCE': "ServiceComponentInfo/AliveNodes," +
  381. "ServiceComponentInfo/GrayListedNodes," +
  382. "ServiceComponentInfo/BlackListedNodes," +
  383. "ServiceComponentInfo/jobtracker/*,",
  384. 'STORM': /^2.1/.test(App.get('currentStackVersionNumber')) ? 'metrics/api/cluster/summary' : 'metrics/api/v1/cluster/summary,metrics/api/v1/topology/summary'
  385. };
  386. var services = App.cache['services'];
  387. services.forEach(function (service) {
  388. var urlParams = serviceSpecificParams[service.ServiceInfo.service_name];
  389. if (urlParams) {
  390. conditionalFields.push(urlParams);
  391. }
  392. });
  393. return conditionalFields;
  394. },
  395. updateServices: function (callback) {
  396. var testUrl = '/data/services/HDP2/services.json';
  397. var componentConfigUrl = this.getUrl(testUrl, '/services?fields=legacy_alerts/summary,ServiceInfo/state,ServiceInfo/maintenance_state&minimal_response=true');
  398. App.HttpClient.get(componentConfigUrl, App.serviceMapper, {
  399. complete: callback
  400. });
  401. },
  402. updateComponentConfig: function (callback) {
  403. var testUrl = '/data/services/host_component_stale_configs.json';
  404. var componentConfigUrl = this.getUrl(testUrl, '/components?ServiceComponentInfo/category.in(SLAVE,CLIENT)&host_components/HostRoles/stale_configs=true&fields=host_components/HostRoles/service_name,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,host_components/HostRoles/host_name,host_components/HostRoles/stale_configs,host_components/HostRoles/desired_admin_state&minimal_response=true');
  405. App.HttpClient.get(componentConfigUrl, App.componentConfigMapper, {
  406. complete: callback
  407. });
  408. },
  409. updateComponentsState: function (callback) {
  410. var testUrl = '/data/services/HDP2/components_state.json';
  411. var realUrl = '/components/?ServiceComponentInfo/category.in(SLAVE,CLIENT)&fields=ServiceComponentInfo/service_name,' +
  412. 'ServiceComponentInfo/category,ServiceComponentInfo/installed_count,ServiceComponentInfo/started_count,ServiceComponentInfo/total_count&minimal_response=true';
  413. var url = this.getUrl(testUrl, realUrl);
  414. App.HttpClient.get(url, App.componentsStateMapper, {
  415. complete: callback
  416. });
  417. }
  418. });