batch_scheduled_requests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. /**
  19. * Default success callback for ajax-requests in this module
  20. * @type {Function}
  21. */
  22. var defaultSuccessCallback = function(data, ajaxOptions, params) {
  23. App.router.get('applicationController').dataLoading().done(function(initValue) {
  24. params.query && params.query.set('status', 'SUCCESS');
  25. if (initValue) {
  26. App.router.get('backgroundOperationsController').showPopup();
  27. }
  28. });
  29. };
  30. /**
  31. * Default error callback for ajax-requests in this module
  32. * @param {Object} xhr
  33. * @param {String} textStatus
  34. * @param {String} error
  35. * @param {Object} opt
  36. * @type {Function}
  37. */
  38. var defaultErrorCallback = function(xhr, textStatus, error, opt, params) {
  39. params.query && params.query.set('status', 'FAIL');
  40. App.ajax.defaultErrorHandler(xhr, opt.url, 'POST', xhr.status);
  41. };
  42. /**
  43. * Contains helpful utilities for handling batch and scheduled requests.
  44. */
  45. module.exports = {
  46. /**
  47. * Some services have components which have a need for rolling restarts. This
  48. * method returns the name of the host-component which supports rolling
  49. * restarts for a service.
  50. * @param {String} serviceName
  51. */
  52. getRollingRestartComponentName: function(serviceName) {
  53. var rollingRestartComponents = {
  54. HDFS: 'DATANODE',
  55. YARN: 'NODEMANAGER',
  56. MAPREDUCE: 'TASKTRACKER',
  57. HBASE: 'HBASE_REGIONSERVER',
  58. STORM: 'SUPERVISOR'
  59. };
  60. return rollingRestartComponents[serviceName] ? rollingRestartComponents[serviceName] : null;
  61. },
  62. /**
  63. * Facade-function for restarting host components of specific service
  64. * @param {String} serviceName for which service hostComponents should be restarted
  65. * @param {bool} staleConfigsOnly restart only hostComponents with <code>staleConfig</code> true
  66. */
  67. restartAllServiceHostComponents: function(serviceName, staleConfigsOnly, query) {
  68. var service = App.Service.find(serviceName);
  69. var context = staleConfigsOnly ? Em.I18n.t('rollingrestart.context.allWithStaleConfigsForSelectedService').format(serviceName) : Em.I18n.t('rollingrestart.context.allForSelectedService').format(serviceName);
  70. if (service) {
  71. var hostComponents = service.get('hostComponents').filterProperty('host.passiveState','OFF');
  72. // HCatalog components are technically owned by Hive.
  73. if (serviceName == 'HIVE') {
  74. var hcatService = App.Service.find('HCATALOG');
  75. if (hcatService != null && hcatService.get('isLoaded')) {
  76. var hcatHcs = hcatService.get('hostComponents').filterProperty('host.passiveState', 'OFF');
  77. if (hcatHcs != null) {
  78. hostComponents.pushObjects(hcatHcs);
  79. }
  80. }
  81. }
  82. if (staleConfigsOnly) {
  83. hostComponents = hostComponents.filterProperty('staleConfigs', true);
  84. }
  85. this.restartHostComponents(hostComponents, context, "SERVICE", query);
  86. }
  87. },
  88. /**
  89. * Restart list of host components
  90. * @param {Ember.Enumerable} hostComponentsList list of host components should be restarted
  91. * @param {String} context message to show in BG popup
  92. * @param {String} level - operation level, can be ("CLUSTER", "SERVICE", "HOST", "HOSTCOMPONENT")
  93. * @param {String} query
  94. */
  95. restartHostComponents: function(hostComponentsList, context, level, query) {
  96. context = context || Em.I18n.t('rollingrestart.context.default');
  97. /**
  98. * Format: {
  99. * 'DATANODE': ['host1', 'host2'],
  100. * 'NAMENODE': ['host1', 'host3']
  101. * ...
  102. * }
  103. */
  104. var componentToHostsMap = {};
  105. var hosts = [];
  106. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  107. hostComponentsList.forEach(function(hc) {
  108. var componentName = hc.get('componentName');
  109. if (!componentToHostsMap[componentName]) {
  110. componentToHostsMap[componentName] = [];
  111. }
  112. componentToHostsMap[componentName].push(hc.get('host.hostName'));
  113. hosts.push(hc.get('host.hostName'));
  114. });
  115. var resource_filters = [];
  116. for (var componentName in componentToHostsMap) {
  117. if (componentToHostsMap.hasOwnProperty(componentName)) {
  118. resource_filters.push({
  119. service_name: componentServiceMap[componentName],
  120. component_name: componentName,
  121. hosts: componentToHostsMap[componentName].join(",")
  122. });
  123. }
  124. }
  125. if(hostComponentsList.length > 0) {
  126. var operation_level = this.getOperationLevelobject(level, hosts.uniq().join(","),
  127. hostComponentsList[0].get("service.serviceName"), hostComponentsList[0].get("componentName"));
  128. }
  129. if (resource_filters.length) {
  130. App.ajax.send({
  131. name: 'restart.hostComponents',
  132. sender: {
  133. successCallback: defaultSuccessCallback,
  134. errorCallback: defaultErrorCallback
  135. },
  136. data: {
  137. context: context,
  138. resource_filters: resource_filters,
  139. query: query,
  140. operation_level: operation_level
  141. },
  142. success: 'successCallback',
  143. error: 'errorCallback'
  144. });
  145. }
  146. },
  147. /**
  148. * @param {String} level - operation level name, can be ("CLUSTER", "SERVICE", "HOST", "HOSTCOMPONENT")
  149. * @param {String} hostName get host name or hostNames as String("host1,host2")
  150. * @param {String} serviceName
  151. * @param {String} componentName
  152. * @returns {Object} {{level: *, cluster_name: *}} - operation level object
  153. * @method getOperationLevelobject - create operation level object to be included into ajax query
  154. */
  155. getOperationLevelobject: function(level, hostName, serviceName, componentName) {
  156. var operationLevel = {
  157. "level": level,
  158. "cluster_name": App.get("clusterName")
  159. };
  160. if (level === "HOST") {
  161. operationLevel["host_name"] = hostName;
  162. } else if (level === "SERVICE") {
  163. operationLevel["service_name"] = serviceName;
  164. } else {
  165. operationLevel["host_name"] = hostName;
  166. operationLevel["service_name"] = serviceName;
  167. operationLevel["hostcomponent_name"] = componentName;
  168. }
  169. return operationLevel;
  170. },
  171. /**
  172. * Makes a REST call to the server requesting the rolling restart of the
  173. * provided host components.
  174. * @param {Array} restartHostComponents list of host components should be restarted
  175. * @param {Number} batchSize size of each batch
  176. * @param {Number} intervalTimeSeconds delay between two batches
  177. * @param {Number} tolerateSize task failure tolerance
  178. * @param {callback} successCallback
  179. * @param {callback} errorCallback
  180. */
  181. _doPostBatchRollingRestartRequest: function(restartHostComponents, batchSize, intervalTimeSeconds, tolerateSize, successCallback, errorCallback) {
  182. successCallback = successCallback || defaultSuccessCallback;
  183. errorCallback = errorCallback || defaultErrorCallback;
  184. if (!restartHostComponents.length) {
  185. console.log('No batch rolling restart if no restartHostComponents provided!');
  186. return;
  187. }
  188. App.ajax.send({
  189. name: 'rolling_restart.post',
  190. sender: {
  191. successCallback: successCallback,
  192. errorCallback: errorCallback
  193. },
  194. data: {
  195. intervalTimeSeconds: intervalTimeSeconds,
  196. tolerateSize: tolerateSize,
  197. batches: this.getBatchesForRollingRestartRequest(restartHostComponents, batchSize)
  198. },
  199. success: 'successCallback',
  200. error: 'errorCallback'
  201. });
  202. },
  203. /**
  204. * Create list of batches for rolling restart request
  205. * @param {Array} restartHostComponents list host components should be restarted
  206. * @param {Number} batchSize size of each batch
  207. * @returns {Array} list of batches
  208. */
  209. getBatchesForRollingRestartRequest: function(restartHostComponents, batchSize) {
  210. var hostIndex = 0,
  211. batches = [],
  212. batchCount = Math.ceil(restartHostComponents.length / batchSize),
  213. sampleHostComponent = restartHostComponents.objectAt(0),
  214. componentName = sampleHostComponent.get('componentName'),
  215. serviceName = sampleHostComponent.get('service.serviceName');
  216. for ( var count = 0; count < batchCount; count++) {
  217. var hostNames = [];
  218. for ( var hc = 0; hc < batchSize && hostIndex < restartHostComponents.length; hc++) {
  219. hostNames.push(restartHostComponents.objectAt(hostIndex++).get('host.hostName'));
  220. }
  221. if (hostNames.length > 0) {
  222. batches.push({
  223. "order_id" : count + 1,
  224. "type" : "POST",
  225. "uri" : App.apiPrefix + "/clusters/" + App.get('clusterName') + "/requests",
  226. "RequestBodyInfo" : {
  227. "RequestInfo" : {
  228. "context" : "_PARSE_.ROLLING-RESTART." + componentName + "." + (count + 1) + "." + batchCount,
  229. "command" : "RESTART"
  230. },
  231. "Requests/resource_filters": [{
  232. "service_name" : serviceName,
  233. "component_name" : componentName,
  234. "hosts" : hostNames.join(",")
  235. }]
  236. }
  237. });
  238. }
  239. }
  240. return batches;
  241. },
  242. /**
  243. * Launches dialog to handle rolling restarts of host components.
  244. *
  245. * Rolling restart is supported only for components listed in <code>getRollingRestartComponentName</code>
  246. * @see getRollingRestartComponentName
  247. * @param {String} hostComponentName
  248. * Type of host-component to restart across cluster
  249. * (ex: DATANODE)
  250. * @param {bool} staleConfigsOnly
  251. * Pre-select host-components which have stale
  252. * configurations
  253. */
  254. launchHostComponentRollingRestart: function(hostComponentName, serviceName, isMaintenanceModeOn, staleConfigsOnly, skipMaintenance) {
  255. if (App.get('components.rollinRestartAllowed').contains(hostComponentName)) {
  256. this.showRollingRestartPopup(hostComponentName, serviceName, isMaintenanceModeOn, staleConfigsOnly, null, skipMaintenance);
  257. }
  258. else {
  259. this.showWarningRollingRestartPopup(hostComponentName);
  260. }
  261. },
  262. /**
  263. * Show popup with rolling restart dialog
  264. * @param {String} hostComponentName name of the host components that should be restarted
  265. * @param {bool} staleConfigsOnly restart only components with <code>staleConfigs</code> = true
  266. * @param {App.hostComponent[]} hostComponents list of hostComponents that should be restarted (optional).
  267. * Using this parameter will reset hostComponentName
  268. */
  269. showRollingRestartPopup: function(hostComponentName, serviceName, isMaintenanceModeOn, staleConfigsOnly, hostComponents, skipMaintenance) {
  270. hostComponents = hostComponents || [];
  271. var componentDisplayName = App.format.role(hostComponentName);
  272. if (!componentDisplayName) {
  273. componentDisplayName = hostComponentName;
  274. }
  275. var title = Em.I18n.t('rollingrestart.dialog.title').format(componentDisplayName);
  276. var viewExtend = {
  277. staleConfigsOnly : staleConfigsOnly,
  278. hostComponentName : hostComponentName,
  279. skipMaintenance: skipMaintenance,
  280. serviceName: serviceName,
  281. isServiceInMM: isMaintenanceModeOn,
  282. didInsertElement : function() {
  283. this.set('parentView.innerView', this);
  284. this.initialize();
  285. }
  286. };
  287. if (hostComponents.length) {
  288. viewExtend.allHostComponents = hostComponents;
  289. }
  290. var self = this;
  291. App.ModalPopup.show({
  292. header : title,
  293. hostComponentName : hostComponentName,
  294. serviceName: serviceName,
  295. isServiceInMM: isMaintenanceModeOn,
  296. staleConfigsOnly : staleConfigsOnly,
  297. skipMaintenance: skipMaintenance,
  298. innerView : null,
  299. bodyClass : App.RollingRestartView.extend(viewExtend),
  300. classNames : [ 'rolling-restart-popup' ],
  301. primary : Em.I18n.t('rollingrestart.dialog.primary'),
  302. onPrimary : function() {
  303. var dialog = this;
  304. var restartComponents = this.get('innerView.restartHostComponents');
  305. var batchSize = this.get('innerView.batchSize');
  306. var waitTime = this.get('innerView.interBatchWaitTimeSeconds');
  307. var tolerateSize = this.get('innerView.tolerateSize');
  308. self._doPostBatchRollingRestartRequest(restartComponents, batchSize, waitTime, tolerateSize, function(data, ajaxOptions, params) {
  309. dialog.hide();
  310. defaultSuccessCallback(data, ajaxOptions, params);
  311. });
  312. },
  313. updateButtons : function() {
  314. var errors = this.get('innerView.errors');
  315. this.set('disablePrimary', (errors != null && errors.length > 0))
  316. }.observes('innerView.errors')
  317. });
  318. },
  319. /**
  320. * Show warning popup about not supported host components
  321. * @param {String} hostComponentName
  322. */
  323. showWarningRollingRestartPopup: function(hostComponentName) {
  324. var componentDisplayName = App.format.role(hostComponentName);
  325. if (!componentDisplayName) {
  326. componentDisplayName = hostComponentName;
  327. }
  328. var title = Em.I18n.t('rollingrestart.dialog.title').format(componentDisplayName);
  329. var msg = Em.I18n.t('rollingrestart.notsupported.hostComponent').format(componentDisplayName);
  330. console.log(msg);
  331. App.ModalPopup.show({
  332. header : title,
  333. secondary : false,
  334. msg : msg,
  335. bodyClass : Em.View.extend({
  336. template : Em.Handlebars.compile('<div class="alert alert-warning">{{msg}}</div>')
  337. })
  338. });
  339. },
  340. /**
  341. * Warn user that alerts will be updated in few minutes
  342. * @param {String} hostComponentName
  343. */
  344. infoPassiveState: function(passiveState) {
  345. var enabled = passiveState == 'OFF' ? 'enabled' : 'suppressed';
  346. App.ModalPopup.show({
  347. header: Em.I18n.t('common.information'),
  348. secondary: null,
  349. bodyClass: Ember.View.extend({
  350. template: Ember.Handlebars.compile('<p>{{view.message}}</p>'),
  351. message: function() {
  352. return Em.I18n.t('hostPopup.warning.alertsTimeOut').format(passiveState.toLowerCase(), enabled);
  353. }.property()
  354. })
  355. });
  356. },
  357. /**
  358. * Retrieves the latest information about a specific request schedule
  359. * identified by 'requestScheduleId'
  360. *
  361. * @param {Number} requestScheduleId ID of the request schedule to get
  362. * @param {Function} successCallback Called with request_schedule data from server. An
  363. * empty object returned for invalid ID.
  364. * @param {Function} errorCallback Optional error callback. Default behavior is to
  365. * popup default error dialog.
  366. */
  367. getRequestSchedule: function(requestScheduleId, successCallback, errorCallback) {
  368. if (requestScheduleId != null && !isNaN(requestScheduleId) && requestScheduleId > -1) {
  369. errorCallback = errorCallback ? errorCallback : defaultErrorCallback;
  370. App.ajax.send({
  371. name : 'request_schedule.get',
  372. sender : {
  373. successCallbackFunction : function(data) {
  374. successCallback(data);
  375. },
  376. errorCallbackFunction : function(xhr, textStatus, error, opt) {
  377. errorCallback(xhr, textStatus, error, opt);
  378. }
  379. },
  380. data : {
  381. request_schedule_id : requestScheduleId
  382. },
  383. success : 'successCallbackFunction',
  384. error : 'errorCallbackFunction'
  385. });
  386. } else {
  387. successCallback({});
  388. }
  389. },
  390. /**
  391. * Attempts to abort a specific request schedule identified by 'requestScheduleId'
  392. *
  393. * @param {Number} requestScheduleId ID of the request schedule to get
  394. * @param {Function} successCallback Called when request schedule successfully aborted
  395. * @param {Function} errorCallback Optional error callback. Default behavior is to
  396. * popup default error dialog.
  397. */
  398. doAbortRequestSchedule: function(requestScheduleId, successCallback, errorCallback) {
  399. if (requestScheduleId != null && !isNaN(requestScheduleId) && requestScheduleId > -1) {
  400. errorCallback = errorCallback || defaultErrorCallback;
  401. App.ajax.send({
  402. name : 'request_schedule.delete',
  403. sender : {
  404. successCallbackFunction : function(data) {
  405. successCallback(data);
  406. },
  407. errorCallbackFunction : function(xhr, textStatus, error, opt) {
  408. errorCallback(xhr, textStatus, error, opt);
  409. }
  410. },
  411. data : {
  412. request_schedule_id : requestScheduleId
  413. },
  414. success : 'successCallbackFunction',
  415. error : 'errorCallbackFunction'
  416. });
  417. } else {
  418. successCallback({});
  419. }
  420. }
  421. };