update_controller.js 16 KB

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