background_operations_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.BackgroundOperationsController = Em.Controller.extend({
  20. name: 'backgroundOperationsController',
  21. /**
  22. * Whether we need to refresh background operations or not
  23. */
  24. isWorking : false,
  25. allOperationsCount : 0,
  26. /**
  27. * For host component popup
  28. */
  29. services:[],
  30. serviceTimestamp: null,
  31. /**
  32. * Number of operation to load
  33. */
  34. operationsCount: 10,
  35. /**
  36. * Possible levels:
  37. * REQUESTS_LIST
  38. * HOSTS_LIST
  39. * TASKS_LIST
  40. * TASK_DETAILS
  41. */
  42. levelInfo: Em.Object.create({
  43. name: 'REQUESTS_LIST',
  44. requestId: null,
  45. taskId: null
  46. }),
  47. /**
  48. * Start polling, when <code>isWorking</code> become true
  49. */
  50. startPolling: function(){
  51. if(this.get('isWorking')){
  52. this.requestMostRecent();
  53. App.updater.run(this, 'requestMostRecent', 'isWorking', App.bgOperationsUpdateInterval);
  54. }
  55. }.observes('isWorking'),
  56. /**
  57. * Get requests data from server
  58. * @param callback
  59. */
  60. requestMostRecent: function (callback) {
  61. var queryParams = this.getQueryParams();
  62. App.ajax.send({
  63. 'name': queryParams.name,
  64. 'sender': this,
  65. 'success': queryParams.successCallback,
  66. 'callback': callback,
  67. 'data': queryParams.data
  68. });
  69. return !this.isInitLoading();
  70. },
  71. /**
  72. * indicate whether data for current level has already been loaded or not
  73. * @return {Boolean}
  74. */
  75. isInitLoading: function () {
  76. var levelInfo = this.get('levelInfo');
  77. var request = this.get('services').findProperty('id', levelInfo.get('requestId'));
  78. if (levelInfo.get('name') === 'HOSTS_LIST') {
  79. return !!(request && App.isEmptyObject(request.get('hostsMap')));
  80. }
  81. return false;
  82. },
  83. /**
  84. * construct params of ajax query regarding displayed level
  85. */
  86. getQueryParams: function () {
  87. var levelInfo = this.get('levelInfo');
  88. var count = this.get('operationsCount');
  89. var result = {
  90. name: 'background_operations.get_most_recent',
  91. successCallback: 'callBackForMostRecent',
  92. data: {
  93. 'operationsCount': count
  94. }
  95. };
  96. if (levelInfo.get('name') === 'TASK_DETAILS' && !App.get('testMode')) {
  97. result.name = 'background_operations.get_by_task';
  98. result.successCallback = 'callBackFilteredByTask';
  99. result.data = {
  100. 'taskId': levelInfo.get('taskId'),
  101. 'requestId': levelInfo.get('requestId')
  102. };
  103. } else if (levelInfo.get('name') === 'TASKS_LIST' || levelInfo.get('name') === 'HOSTS_LIST') {
  104. result.name = 'background_operations.get_by_request';
  105. result.successCallback = 'callBackFilteredByRequest';
  106. result.data = {
  107. 'requestId': levelInfo.get('requestId')
  108. };
  109. }
  110. return result;
  111. },
  112. /**
  113. * Push hosts and their tasks to request
  114. * @param data
  115. * @param ajaxQuery
  116. * @param params
  117. */
  118. callBackFilteredByRequest: function (data, ajaxQuery, params) {
  119. var requestId = data.Requests.id;
  120. var requestInputs = data.Requests.inputs;
  121. var request = this.get('services').findProperty('id', requestId);
  122. var hostsMap = {};
  123. var previousTaskStatusMap = request.get('previousTaskStatusMap');
  124. var currentTaskStatusMap = {};
  125. data.tasks.forEach(function (task) {
  126. var host = hostsMap[task.Tasks.host_name];
  127. task.Tasks.request_id = requestId;
  128. task.Tasks.request_inputs = requestInputs;
  129. if (host) {
  130. host.logTasks.push(task);
  131. host.isModified = (host.isModified) ? true : previousTaskStatusMap[task.Tasks.id] !== task.Tasks.status;
  132. } else {
  133. hostsMap[task.Tasks.host_name] = {
  134. name: task.Tasks.host_name,
  135. publicName: task.Tasks.host_name,
  136. logTasks: [task],
  137. isModified: previousTaskStatusMap[task.Tasks.id] !== task.Tasks.status
  138. };
  139. }
  140. currentTaskStatusMap[task.Tasks.id] = task.Tasks.status;
  141. }, this);
  142. /**
  143. * sync up request progress with up to date progress of hosts on Host's list,
  144. * to avoid discrepancies while waiting for response with latest progress of request
  145. * after switching to operation's list
  146. */
  147. if (request.get('isRunning')) {
  148. request.set('progress', App.HostPopup.getProgress(data.tasks));
  149. request.set('status', App.HostPopup.getStatus(data.tasks)[0]);
  150. request.set('isRunning', (request.get('progress') !== 100));
  151. }
  152. request.set('previousTaskStatusMap', currentTaskStatusMap);
  153. request.set('hostsMap', hostsMap);
  154. this.set('serviceTimestamp', App.dateTime());
  155. },
  156. /**
  157. * Update task, with uploading two additional properties: stdout and stderr
  158. * @param data
  159. * @param ajaxQuery
  160. * @param params
  161. */
  162. callBackFilteredByTask: function (data, ajaxQuery, params) {
  163. var request = this.get('services').findProperty('id', data.Tasks.request_id);
  164. var host = request.get('hostsMap')[data.Tasks.host_name];
  165. var task = host.logTasks.findProperty('Tasks.id', data.Tasks.id);
  166. task.Tasks.status = data.Tasks.status;
  167. task.Tasks.stdout = data.Tasks.stdout;
  168. task.Tasks.stderr = data.Tasks.stderr;
  169. // Put some command information to task object
  170. task.Tasks.command = data.Tasks.command;
  171. task.Tasks.custom_command_name = data.Tasks.custom_command_name;
  172. task.Tasks.structured_out = data.Tasks.structured_out;
  173. task.Tasks.output_log = data.Tasks.output_log;
  174. task.Tasks.error_log = data.Tasks.error_log;
  175. this.set('serviceTimestamp', App.dateTime());
  176. },
  177. /**
  178. * returns true if it's upgrade equest
  179. * use this flag to exclude upgrade requests from bgo
  180. * @param {object} request
  181. * @returns {boolean}
  182. */
  183. isUpgradeRequest: function(request) {
  184. var context = Em.get(request, 'Requests.request_context');
  185. return context ? context.toLowerCase().contains('upgrading') : false;
  186. },
  187. /**
  188. * Prepare, received from server, requests for host component popup
  189. * @param data
  190. */
  191. callBackForMostRecent: function (data) {
  192. var runningServices = 0;
  193. var currentRequestIds = [];
  194. var countIssued = this.get('operationsCount');
  195. var countGot = data.itemTotal;
  196. data.items.forEach(function (request) {
  197. if (this.isUpgradeRequest(request)) {
  198. return;
  199. }
  200. var rq = this.get("services").findProperty('id', request.Requests.id);
  201. var isRunning = this.isRequestRunning(request);
  202. var requestParams = this.parseRequestContext(request.Requests.request_context);
  203. this.assignScheduleId(request, requestParams);
  204. currentRequestIds.push(request.Requests.id);
  205. if (rq) {
  206. rq.set('progress', Math.floor(request.Requests.progress_percent));
  207. rq.set('status', request.Requests.request_status);
  208. rq.set('isRunning', isRunning);
  209. rq.set('startTime', request.Requests.start_time);
  210. rq.set('endTime', request.Requests.end_time);
  211. } else {
  212. rq = Em.Object.create({
  213. id: request.Requests.id,
  214. name: requestParams.requestContext,
  215. displayName: requestParams.requestContext,
  216. progress: Math.floor(request.Requests.progress_percent),
  217. status: request.Requests.request_status,
  218. isRunning: isRunning,
  219. hostsMap: {},
  220. tasks: [],
  221. startTime: request.Requests.start_time,
  222. endTime: request.Requests.end_time,
  223. dependentService: requestParams.dependentService,
  224. sourceRequestScheduleId: request.Requests.request_schedule && request.Requests.request_schedule.schedule_id,
  225. previousTaskStatusMap: {},
  226. contextCommand: requestParams.contextCommand
  227. });
  228. this.get("services").unshift(rq);
  229. //To sort DESC by request id
  230. this.set("services", this.get("services").sort( function(a,b) { return b.get('id') - a.get('id'); }));
  231. }
  232. runningServices += ~~isRunning;
  233. }, this);
  234. this.removeOldRequests(currentRequestIds);
  235. this.set("allOperationsCount", runningServices);
  236. this.set('isShowMoreAvailable', countGot >= countIssued);
  237. this.set('serviceTimestamp', App.dateTime());
  238. },
  239. isShowMoreAvailable: null,
  240. /**
  241. * remove old requests
  242. * as API returns 10, or 20 , or 30 ...etc latest request, the requests that absent in response should be removed
  243. * @param currentRequestIds
  244. */
  245. removeOldRequests: function (currentRequestIds) {
  246. this.get('services').forEach(function (service, index, services) {
  247. if (!currentRequestIds.contains(service.id)) {
  248. services.splice(index, 1);
  249. }
  250. });
  251. },
  252. /**
  253. * identify whether request is running by task counters
  254. * @param request
  255. * @return {Boolean}
  256. */
  257. isRequestRunning: function (request) {
  258. return (request.Requests.task_count -
  259. (request.Requests.aborted_task_count + request.Requests.completed_task_count + request.Requests.failed_task_count
  260. + request.Requests.timed_out_task_count - request.Requests.queued_task_count)) > 0;
  261. },
  262. /**
  263. * identify whether there is only one host in request
  264. * @param inputs
  265. * @return {Boolean}
  266. */
  267. isOneHost: function (inputs) {
  268. if (!inputs) {
  269. return false;
  270. }
  271. inputs = JSON.parse(inputs);
  272. if (inputs && inputs.included_hosts) {
  273. return inputs.included_hosts.split(',').length < 2;
  274. }
  275. return false
  276. },
  277. /**
  278. * assign schedule_id of request to null if it's Recommision operation
  279. * @param request
  280. * @param requestParams
  281. */
  282. assignScheduleId: function (request, requestParams) {
  283. var oneHost = this.isOneHost(request.Requests.inputs);
  284. if (request.Requests.request_schedule && oneHost && /Recommission/.test(requestParams.requestContext)) {
  285. request.Requests.request_schedule.schedule_id = null;
  286. }
  287. },
  288. /**
  289. * parse request context and if keyword "_PARSE_" is present then format it
  290. * @param requestContext
  291. * @return {Object}
  292. */
  293. parseRequestContext: function (requestContext) {
  294. var parsedRequestContext;
  295. var service;
  296. var contextCommand;
  297. if (requestContext) {
  298. if (requestContext.indexOf(App.BackgroundOperationsController.CommandContexts.PREFIX) !== -1) {
  299. var contextSplits = requestContext.split('.');
  300. contextCommand = contextSplits[1];
  301. service = contextSplits[2];
  302. switch(contextCommand){
  303. case "STOP":
  304. case "START":
  305. if (service === 'ALL_SERVICES') {
  306. parsedRequestContext = Em.I18n.t("requestInfo." + contextCommand.toLowerCase()).format(Em.I18n.t('common.allServices'));
  307. } else {
  308. parsedRequestContext = Em.I18n.t("requestInfo." + contextCommand.toLowerCase()).format(App.format.role(service));
  309. }
  310. break;
  311. case "ROLLING-RESTART":
  312. parsedRequestContext = Em.I18n.t("rollingrestart.rest.context").format(App.format.role(service), contextSplits[3], contextSplits[4]);
  313. break;
  314. }
  315. } else {
  316. parsedRequestContext = requestContext;
  317. }
  318. } else {
  319. parsedRequestContext = Em.I18n.t('requestInfo.unspecified');
  320. }
  321. return {
  322. requestContext: parsedRequestContext,
  323. dependentService: service,
  324. contextCommand: contextCommand
  325. }
  326. },
  327. popupView: null,
  328. /**
  329. * Onclick handler for background operations number located right to logo
  330. */
  331. showPopup: function(){
  332. // load the checkbox on footer first, then show popup.
  333. var self = this;
  334. App.router.get('applicationController').dataLoading().done(function (initValue) {
  335. App.updater.immediateRun('requestMostRecent');
  336. if(self.get('popupView') && App.HostPopup.get('isBackgroundOperations')){
  337. self.set ('popupView.isNotShowBgChecked', !initValue);
  338. self.set('popupView.isOpen', true);
  339. $(self.get('popupView.element')).appendTo('#wrapper');
  340. } else {
  341. self.set('popupView', App.HostPopup.initPopup("", self, true));
  342. self.set ('popupView.isNotShowBgChecked', !initValue);
  343. }
  344. });
  345. },
  346. /**
  347. * Called on logout
  348. */
  349. clear: function () {
  350. // set operations count to default value
  351. this.set('operationsCount', 10);
  352. }
  353. });
  354. /**
  355. * Each background operation has a context in which it operates.
  356. * Generally these contexts are fixed messages. However, we might
  357. * want to associate semantics to this context - like showing, disabling
  358. * buttons when certain operations are in progress.
  359. *
  360. * To make this possible we have command contexts where the context
  361. * is not a human readable string, but a pattern indicating the command
  362. * it is running. When UI shows these, they are translated into human
  363. * readable strings.
  364. *
  365. * General pattern of context names is "_PARSE_.{COMMAND}.{ID}[.{Additional-Data}...]"
  366. */
  367. App.BackgroundOperationsController.CommandContexts = {
  368. PREFIX : "_PARSE_",
  369. /**
  370. * Stops all services
  371. */
  372. STOP_ALL_SERVICES : "_PARSE_.STOP.ALL_SERVICES",
  373. /**
  374. * Starts all services
  375. */
  376. START_ALL_SERVICES : "_PARSE_.START.ALL_SERVICES",
  377. /**
  378. * Starts service indicated by serviceID.
  379. * @param {String} serviceID Parameter {0}. Example: HDFS
  380. */
  381. START_SERVICE : "_PARSE_.START.{0}",
  382. /**
  383. * Stops service indicated by serviceID.
  384. * @param {String} serviceID Parameter {0}. Example: HDFS
  385. */
  386. STOP_SERVICE : "_PARSE_.STOP.{0}",
  387. /**
  388. * Performs rolling restart of componentID in batches.
  389. * This context is the batchNumber batch out of totalBatchCount batches.
  390. * @param {String} componentID Parameter {0}. Example "DATANODE"
  391. * @param {Number} batchNumber Parameter {1}. Batch number of this batch. Example 3.
  392. * @param {Number} totalBatchCount Parameter {2}. Total number of batches. Example 10.
  393. */
  394. ROLLING_RESTART : "_PARSE_.ROLLING-RESTART.{0}.{1}.{2}"
  395. };