update_controller.js 18 KB

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