update_controller.js 18 KB

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