update_controller.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. getUrl: function (testUrl, url) {
  32. return (App.get('testMode')) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
  33. },
  34. /**
  35. * construct URL from real URL and query parameters
  36. * @param testUrl
  37. * @param realUrl
  38. * @param queryParams
  39. * @return {String}
  40. */
  41. getComplexUrl: function (testUrl, realUrl, queryParams) {
  42. var prefix = App.apiPrefix + '/clusters/' + App.get('clusterName');
  43. var params = '';
  44. if (App.get('testMode')) {
  45. return testUrl;
  46. } else {
  47. if (queryParams) {
  48. params = this.computeParameters(queryParams);
  49. }
  50. return prefix + realUrl.replace('<parameters>', params);
  51. }
  52. },
  53. /**
  54. * compute parameters according to their type
  55. * @param queryParams
  56. * @return {String}
  57. */
  58. computeParameters: function (queryParams) {
  59. var params = '';
  60. queryParams.forEach(function (param) {
  61. switch (param.type) {
  62. case 'EQUAL':
  63. params += param.key + '=' + param.value;
  64. break;
  65. case 'LESS':
  66. params += param.key + '<' + param.value;
  67. break;
  68. case 'MORE':
  69. params += param.key + '>' + param.value;
  70. break;
  71. case 'MATCH':
  72. params += param.key + '.matches(' + param.value + ')';
  73. break;
  74. case 'MULTIPLE':
  75. params += param.key + '.in(' + param.value.join(',') + ')';
  76. break;
  77. case 'SORT':
  78. params += 'sortBy=' + param.key + '.' + param.value;
  79. break;
  80. case 'CUSTOM':
  81. param.value.forEach(function(item, index){
  82. param.key = param.key.replace('{' + index + '}', item);
  83. }, this);
  84. params += param.key;
  85. break;
  86. }
  87. params += '&';
  88. });
  89. return params;
  90. },
  91. /**
  92. * depict query parameters of table
  93. */
  94. queryParams: Em.Object.create({
  95. 'Hosts': []
  96. }),
  97. /**
  98. * map describes relations between updater function and table
  99. */
  100. tableUpdaterMap: {
  101. 'Hosts': 'updateHost'
  102. },
  103. /**
  104. * Start polling, when <code>isWorking</code> become true
  105. */
  106. updateAll: function () {
  107. if (this.get('isWorking')) {
  108. App.updater.run(this, 'updateServices', 'isWorking');
  109. App.updater.run(this, 'updateHost', 'isWorking');
  110. App.updater.run(this, 'updateServiceMetricConditionally', 'isWorking', App.componentsUpdateInterval);
  111. App.updater.run(this, 'updateComponentsState', 'isWorking', App.componentsUpdateInterval);
  112. App.updater.run(this, 'graphsUpdate', 'isWorking');
  113. if (App.supports.hostOverrides) {
  114. App.updater.run(this, 'updateComponentConfig', 'isWorking');
  115. }
  116. }
  117. }.observes('isWorking'),
  118. /**
  119. * Update service metrics depending on which page is open
  120. * Make a call only on follow pages:
  121. * /main/dashboard
  122. * /main/services/*
  123. * @param callback
  124. */
  125. updateServiceMetricConditionally: function (callback) {
  126. if (/\/main\/(dashboard|services).*/.test(this.get('location'))) {
  127. this.updateServiceMetric(callback);
  128. } else {
  129. callback();
  130. }
  131. },
  132. updateHost: function (callback, error) {
  133. var testUrl = App.get('isHadoop2Stack') ? '/data/hosts/HDP2/hosts.json' : '/data/hosts/hosts.json';
  134. 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,' +
  135. 'Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components/HostRoles/state,host_components/HostRoles/maintenance_state,' +
  136. 'host_components/HostRoles/stale_configs,host_components/HostRoles/service_name,metrics/disk,metrics/load/load_one,metrics/cpu/cpu_system,metrics/cpu/cpu_user,' +
  137. 'metrics/memory/mem_total,metrics/memory/mem_free,alerts/summary&minimal_response=true';
  138. if (App.router.get('currentState.name') == 'index' && App.router.get('currentState.parentState.name') == 'hosts') {
  139. App.updater.updateInterval('updateHost', App.get('contentUpdateInterval'));
  140. } else if(App.router.get('currentState.name') == 'summary' && App.router.get('currentState.parentState.name') == 'hostDetails') {
  141. realUrl = realUrl.replace('<parameters>', 'Hosts/host_name=' + App.router.get('location.lastSetURL').match(/\/hosts\/(.*)\/summary/)[1] + '&');
  142. App.updater.updateInterval('updateHost', App.get('componentsUpdateInterval'));
  143. } else {
  144. callback();
  145. // On pages except for hosts/hostDetails, making sure hostsMapper loaded only once on page load, no need to update, but at least once
  146. if (this.get('queryParams.Hosts') && this.get('queryParams.Hosts').length > 0) {
  147. return;
  148. }
  149. }
  150. this.get('queryParams').set('Hosts', App.router.get('mainHostController').getQueryParameters());
  151. var hostsUrl = this.getComplexUrl(testUrl, realUrl, this.get('queryParams.Hosts'));
  152. App.HttpClient.get(hostsUrl, App.hostsMapper, {
  153. complete: callback,
  154. error: error
  155. });
  156. },
  157. graphs: [],
  158. graphsUpdate: function (callback) {
  159. var existedGraphs = [];
  160. this.get('graphs').forEach(function (_graph) {
  161. var view = Em.View.views[_graph.id];
  162. if (view) {
  163. existedGraphs.push(_graph);
  164. //console.log('updated graph', _graph.name);
  165. view.loadData();
  166. //if graph opened as modal popup update it to
  167. if ($(".modal-graph-line .modal-body #" + _graph.popupId + "-container-popup").length) {
  168. view.loadData();
  169. }
  170. }
  171. });
  172. callback();
  173. this.set('graphs', existedGraphs);
  174. },
  175. /**
  176. * Updates the services information.
  177. *
  178. * @param callback
  179. */
  180. updateServiceMetric: function (callback) {
  181. var self = this;
  182. self.set('isUpdated', false);
  183. var conditionalFields = this.getConditionalFields(),
  184. conditionalFieldsString = conditionalFields.length > 0 ? ',' + conditionalFields.join(',') : '',
  185. testUrl = App.get('isHadoop2Stack') ? '/data/dashboard/HDP2/master_components.json' : '/data/dashboard/services.json',
  186. isFlumeInstalled = App.cache['services'].mapProperty('ServiceInfo.service_name').contains('FLUME'),
  187. isATSInstalled = App.cache['services'].mapProperty('ServiceInfo.service_name').contains('YARN') && App.get('isHadoop21Stack'),
  188. flumeHandlerParam = isFlumeInstalled ? 'ServiceComponentInfo/component_name=FLUME_HANDLER|' : '',
  189. atsHandlerParam = isATSInstalled ? 'ServiceComponentInfo/component_name=APP_TIMELINE_SERVER|' : '',
  190. haComponents = App.get('isHaEnabled') ? 'ServiceComponentInfo/component_name=JOURNALNODE|' : '',
  191. realUrl = '/components/?' + flumeHandlerParam + atsHandlerParam + haComponents +
  192. 'ServiceComponentInfo/category=MASTER&fields=' +
  193. 'ServiceComponentInfo/Version,' +
  194. 'ServiceComponentInfo/StartTime,' +
  195. 'ServiceComponentInfo/HeapMemoryUsed,' +
  196. 'ServiceComponentInfo/HeapMemoryMax,' +
  197. 'ServiceComponentInfo/service_name,' +
  198. 'host_components/HostRoles/host_name,' +
  199. 'host_components/HostRoles/state,' +
  200. 'host_components/HostRoles/maintenance_state,' +
  201. 'host_components/HostRoles/stale_configs,' +
  202. 'host_components/metrics/jvm/memHeapUsedM,' +
  203. 'host_components/metrics/jvm/HeapMemoryMax,' +
  204. 'host_components/metrics/jvm/HeapMemoryUsed,' +
  205. 'host_components/metrics/jvm/memHeapCommittedM,' +
  206. 'host_components/metrics/mapred/jobtracker/trackers_decommissioned,' +
  207. 'host_components/metrics/cpu/cpu_wio,' +
  208. 'host_components/metrics/rpc/RpcQueueTime_avg_time,' +
  209. 'host_components/metrics/dfs/FSNamesystem/*,' +
  210. 'host_components/metrics/dfs/namenode/Version,' +
  211. 'host_components/metrics/dfs/namenode/DecomNodes,' +
  212. 'host_components/metrics/dfs/namenode/TotalFiles,' +
  213. 'host_components/metrics/dfs/namenode/UpgradeFinalized,' +
  214. 'host_components/metrics/dfs/namenode/Safemode,' +
  215. 'host_components/metrics/runtime/StartTime' +
  216. conditionalFieldsString +
  217. '&minimal_response=true';
  218. var servicesUrl = this.getUrl(testUrl, realUrl);
  219. callback = callback || function () {
  220. self.set('isUpdated', true);
  221. };
  222. App.HttpClient.get(servicesUrl, App.serviceMetricsMapper, {
  223. complete: function () {
  224. callback();
  225. }
  226. });
  227. },
  228. /**
  229. * construct conditional parameters of query, depending on which services are installed
  230. * @return {Array}
  231. */
  232. getConditionalFields: function () {
  233. var conditionalFields = [];
  234. var serviceSpecificParams = {
  235. 'FLUME': "host_components/metrics/flume/flume," +
  236. "host_components/processes/HostComponentProcess",
  237. 'YARN': "host_components/metrics/yarn/Queue," +
  238. "ServiceComponentInfo/rm_metrics/cluster/activeNMcount," +
  239. "ServiceComponentInfo/rm_metrics/cluster/unhealthyNMcount," +
  240. "ServiceComponentInfo/rm_metrics/cluster/rebootedNMcount," +
  241. "ServiceComponentInfo/rm_metrics/cluster/decommissionedNMcount",
  242. 'HBASE': "host_components/metrics/hbase/master/IsActiveMaster," +
  243. "ServiceComponentInfo/MasterStartTime," +
  244. "ServiceComponentInfo/MasterActiveTime," +
  245. "ServiceComponentInfo/AverageLoad," +
  246. "ServiceComponentInfo/Revision," +
  247. "ServiceComponentInfo/RegionsInTransition",
  248. 'MAPREDUCE': "ServiceComponentInfo/AliveNodes," +
  249. "ServiceComponentInfo/GrayListedNodes," +
  250. "ServiceComponentInfo/BlackListedNodes," +
  251. "ServiceComponentInfo/jobtracker/*,",
  252. 'STORM': "metrics/api/cluster/summary,"
  253. };
  254. var services = App.cache['services'];
  255. services.forEach(function (service) {
  256. var urlParams = serviceSpecificParams[service.ServiceInfo.service_name];
  257. if (urlParams) {
  258. conditionalFields.push(urlParams);
  259. }
  260. });
  261. return conditionalFields;
  262. },
  263. updateServices: function (callback) {
  264. var testUrl = '/data/services/HDP2/services.json';
  265. var componentConfigUrl = this.getUrl(testUrl, '/services?fields=alerts/summary,ServiceInfo/state,ServiceInfo/maintenance_state&minimal_response=true');
  266. App.HttpClient.get(componentConfigUrl, App.serviceMapper, {
  267. complete: callback
  268. });
  269. },
  270. updateComponentConfig: function (callback) {
  271. var testUrl = '/data/services/host_component_stale_configs.json';
  272. 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');
  273. App.HttpClient.get(componentConfigUrl, App.componentConfigMapper, {
  274. complete: callback
  275. });
  276. },
  277. updateComponentsState: function (callback) {
  278. var testUrl = '/data/services/HDP2/components_state.json';
  279. var realUrl = '/components/?ServiceComponentInfo/category.in(SLAVE,CLIENT)&fields=ServiceComponentInfo/service_name,' +
  280. 'ServiceComponentInfo/installed_count,ServiceComponentInfo/started_count,ServiceComponentInfo/total_count&minimal_response=true';
  281. var url = this.getUrl(testUrl, realUrl);
  282. App.HttpClient.get(url, App.componentsStateMapper, {
  283. complete: callback
  284. });
  285. }
  286. });