update_controller.js 16 KB

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