update_controller.js 15 KB

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