update_controller.js 17 KB

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