batch_scheduled_requests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. if (initValue) {
  25. params.query && params.query.set('status', 'SUCCESS');
  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');
  72. if (staleConfigsOnly) {
  73. hostComponents = hostComponents.filterProperty('staleConfigs', true);
  74. }
  75. this.restartHostComponents(hostComponents, context, query);
  76. }
  77. },
  78. /**
  79. * Restart list of host components
  80. * @param {Ember.Enumerable} hostComponentsList list of host components should be restarted
  81. * @param {String} context message to show in BG popup
  82. */
  83. restartHostComponents: function(hostComponentsList, context, query) {
  84. context = context || Em.I18n.t('rollingrestart.context.default');
  85. /**
  86. * Format: {
  87. * 'DATANODE': ['host1', 'host2'],
  88. * 'NAMENODE': ['host1', 'host3']
  89. * ...
  90. * }
  91. */
  92. var componentToHostsMap = {};
  93. var componentServiceMap = App.QuickDataMapper.componentServiceMap();
  94. hostComponentsList.forEach(function(hc) {
  95. var componentName = hc.get('componentName');
  96. if (!componentToHostsMap[componentName]) {
  97. componentToHostsMap[componentName] = [];
  98. }
  99. componentToHostsMap[componentName].push(hc.get('host.hostName'));
  100. });
  101. var resource_filters = [];
  102. for (var componentName in componentToHostsMap) {
  103. if (componentToHostsMap.hasOwnProperty(componentName)) {
  104. resource_filters.push({
  105. service_name: componentServiceMap[componentName],
  106. component_name: componentName,
  107. hosts: componentToHostsMap[componentName].join(",")
  108. });
  109. }
  110. }
  111. if (resource_filters.length) {
  112. App.ajax.send({
  113. name: 'restart.hostComponents',
  114. sender: {
  115. successCallback: defaultSuccessCallback,
  116. errorCallback: defaultErrorCallback
  117. },
  118. data: {
  119. context: context,
  120. resource_filters: resource_filters,
  121. query: query
  122. },
  123. success: 'successCallback',
  124. error: 'errorCallback'
  125. });
  126. }
  127. },
  128. /**
  129. * Makes a REST call to the server requesting the rolling restart of the
  130. * provided host components.
  131. * @param {Array} restartHostComponents list of host components should be restarted
  132. * @param {Number} batchSize size of each batch
  133. * @param {Number} intervalTimeSeconds delay between two batches
  134. * @param {Number} tolerateSize task failure tolerance
  135. * @param {callback} successCallback
  136. * @param {callback} errorCallback
  137. */
  138. _doPostBatchRollingRestartRequest: function(restartHostComponents, batchSize, intervalTimeSeconds, tolerateSize, successCallback, errorCallback) {
  139. successCallback = successCallback || defaultSuccessCallback;
  140. errorCallback = errorCallback || defaultErrorCallback;
  141. if (!restartHostComponents.length) {
  142. console.log('No batch rolling restart if no restartHostComponents provided!');
  143. return;
  144. }
  145. App.ajax.send({
  146. name: 'rolling_restart.post',
  147. sender: {
  148. successCallback: successCallback,
  149. errorCallback: errorCallback
  150. },
  151. data: {
  152. intervalTimeSeconds: intervalTimeSeconds,
  153. tolerateSize: tolerateSize,
  154. batches: this.getBatchesForRollingRestartRequest(restartHostComponents, batchSize)
  155. },
  156. success: 'successCallback',
  157. error: 'errorCallback'
  158. });
  159. },
  160. /**
  161. * Create list of batches for rolling restart request
  162. * @param {Array} restartHostComponents list host components should be restarted
  163. * @param {Number} batchSize size of each batch
  164. * @returns {Array} list of batches
  165. */
  166. getBatchesForRollingRestartRequest: function(restartHostComponents, batchSize) {
  167. var hostIndex = 0,
  168. batches = [],
  169. batchCount = Math.ceil(restartHostComponents.length / batchSize),
  170. sampleHostComponent = restartHostComponents.objectAt(0),
  171. componentName = sampleHostComponent.get('componentName'),
  172. serviceName = sampleHostComponent.get('service.serviceName');
  173. for ( var count = 0; count < batchCount; count++) {
  174. var hostNames = [];
  175. for ( var hc = 0; hc < batchSize && hostIndex < restartHostComponents.length; hc++) {
  176. hostNames.push(restartHostComponents.objectAt(hostIndex++).get('host.hostName'));
  177. }
  178. if (hostNames.length > 0) {
  179. batches.push({
  180. "order_id" : count + 1,
  181. "type" : "POST",
  182. "uri" : App.apiPrefix + "/clusters/" + App.get('clusterName') + "/requests",
  183. "RequestBodyInfo" : {
  184. "RequestInfo" : {
  185. "context" : "_PARSE_.ROLLING-RESTART." + componentName + "." + (count + 1) + "." + batchCount,
  186. "command" : "RESTART"
  187. },
  188. "Requests/resource_filters": [{
  189. "service_name" : serviceName,
  190. "component_name" : componentName,
  191. "hosts" : hostNames.join(",")
  192. }]
  193. }
  194. });
  195. }
  196. }
  197. return batches;
  198. },
  199. /**
  200. * Launches dialog to handle rolling restarts of host components.
  201. *
  202. * Rolling restart is supported only for components listed in <code>getRollingRestartComponentName</code>
  203. * @see getRollingRestartComponentName
  204. * @param {String} hostComponentName
  205. * Type of host-component to restart across cluster
  206. * (ex: DATANODE)
  207. * @param {bool} staleConfigsOnly
  208. * Pre-select host-components which have stale
  209. * configurations
  210. */
  211. launchHostComponentRollingRestart: function(hostComponentName, staleConfigsOnly, skipMaintenance) {
  212. if (App.get('components.rollinRestartAllowed').contains(hostComponentName)) {
  213. this.showRollingRestartPopup(hostComponentName, staleConfigsOnly, null, skipMaintenance);
  214. }
  215. else {
  216. this.showWarningRollingRestartPopup(hostComponentName);
  217. }
  218. },
  219. /**
  220. * Show popup with rolling restart dialog
  221. * @param {String} hostComponentName name of the host components that should be restarted
  222. * @param {bool} staleConfigsOnly restart only components with <code>staleConfigs</code> = true
  223. * @param {App.hostComponent[]} hostComponents list of hostComponents that should be restarted (optional).
  224. * Using this parameter will reset hostComponentName
  225. */
  226. showRollingRestartPopup: function(hostComponentName, staleConfigsOnly, hostComponents, skipMaintenance) {
  227. hostComponents = hostComponents || [];
  228. var componentDisplayName = App.format.role(hostComponentName);
  229. if (!componentDisplayName) {
  230. componentDisplayName = hostComponentName;
  231. }
  232. var title = Em.I18n.t('rollingrestart.dialog.title').format(componentDisplayName);
  233. var viewExtend = {
  234. staleConfigsOnly : staleConfigsOnly,
  235. hostComponentName : hostComponentName,
  236. skipMaintenance: skipMaintenance,
  237. didInsertElement : function() {
  238. this.set('parentView.innerView', this);
  239. this.initialize();
  240. }
  241. };
  242. if (hostComponents.length) {
  243. viewExtend.allHostComponents = hostComponents;
  244. }
  245. var self = this;
  246. App.ModalPopup.show({
  247. header : title,
  248. hostComponentName : hostComponentName,
  249. staleConfigsOnly : staleConfigsOnly,
  250. skipMaintenance: skipMaintenance,
  251. innerView : null,
  252. bodyClass : App.RollingRestartView.extend(viewExtend),
  253. classNames : [ 'rolling-restart-popup' ],
  254. primary : Em.I18n.t('rollingrestart.dialog.primary'),
  255. onPrimary : function() {
  256. var dialog = this;
  257. if (!dialog.get('enablePrimary')) {
  258. return;
  259. }
  260. var restartComponents = this.get('innerView.restartHostComponents');
  261. var batchSize = this.get('innerView.batchSize');
  262. var waitTime = this.get('innerView.interBatchWaitTimeSeconds');
  263. var tolerateSize = this.get('innerView.tolerateSize');
  264. self._doPostBatchRollingRestartRequest(restartComponents, batchSize, waitTime, tolerateSize, function() {
  265. dialog.hide();
  266. defaultSuccessCallback();
  267. });
  268. },
  269. updateButtons : function() {
  270. var errors = this.get('innerView.errors');
  271. this.set('enablePrimary', !(errors != null && errors.length > 0))
  272. }.observes('innerView.errors')
  273. });
  274. },
  275. /**
  276. * Show warning popup about not supported host components
  277. * @param {String} hostComponentName
  278. */
  279. showWarningRollingRestartPopup: function(hostComponentName) {
  280. var componentDisplayName = App.format.role(hostComponentName);
  281. if (!componentDisplayName) {
  282. componentDisplayName = hostComponentName;
  283. }
  284. var title = Em.I18n.t('rollingrestart.dialog.title').format(componentDisplayName);
  285. var msg = Em.I18n.t('rollingrestart.notsupported.hostComponent').format(componentDisplayName);
  286. console.log(msg);
  287. App.ModalPopup.show({
  288. header : title,
  289. secondary : false,
  290. msg : msg,
  291. bodyClass : Em.View.extend({
  292. template : Em.Handlebars.compile('<div class="alert alert-warning">{{msg}}</div>')
  293. })
  294. });
  295. },
  296. /**
  297. * Warn user that alerts will be updated in few minutes
  298. * @param {String} hostComponentName
  299. */
  300. infoPassiveState: function(passiveState) {
  301. var enabled = passiveState == 'OFF' ? 'enabled' : 'suppressed';
  302. App.ModalPopup.show({
  303. header: Em.I18n.t('common.information'),
  304. bodyClass: Ember.View.extend({
  305. template: Ember.Handlebars.compile('<p>{{view.message}}</p>'),
  306. message: function() {
  307. return Em.I18n.t('hostPopup.warning.alertsTimeOut').format(passiveState.toLowerCase(), enabled);
  308. }.property()
  309. })
  310. });
  311. },
  312. /**
  313. * Retrieves the latest information about a specific request schedule
  314. * identified by 'requestScheduleId'
  315. *
  316. * @param {Number} requestScheduleId ID of the request schedule to get
  317. * @param {Function} successCallback Called with request_schedule data from server. An
  318. * empty object returned for invalid ID.
  319. * @param {Function} errorCallback Optional error callback. Default behavior is to
  320. * popup default error dialog.
  321. */
  322. getRequestSchedule: function(requestScheduleId, successCallback, errorCallback) {
  323. if (requestScheduleId != null && !isNaN(requestScheduleId) && requestScheduleId > -1) {
  324. errorCallback = errorCallback ? errorCallback : defaultErrorCallback;
  325. App.ajax.send({
  326. name : 'request_schedule.get',
  327. sender : {
  328. successCallbackFunction : function(data) {
  329. successCallback(data);
  330. },
  331. errorCallbackFunction : function(xhr, textStatus, error, opt) {
  332. errorCallback(xhr, textStatus, error, opt);
  333. }
  334. },
  335. data : {
  336. request_schedule_id : requestScheduleId
  337. },
  338. success : 'successCallbackFunction',
  339. error : 'errorCallbackFunction'
  340. });
  341. } else {
  342. successCallback({});
  343. }
  344. },
  345. /**
  346. * Attempts to abort a specific request schedule identified by 'requestScheduleId'
  347. *
  348. * @param {Number} requestScheduleId ID of the request schedule to get
  349. * @param {Function} successCallback Called when request schedule successfully aborted
  350. * @param {Function} errorCallback Optional error callback. Default behavior is to
  351. * popup default error dialog.
  352. */
  353. doAbortRequestSchedule: function(requestScheduleId, successCallback, errorCallback) {
  354. if (requestScheduleId != null && !isNaN(requestScheduleId) && requestScheduleId > -1) {
  355. errorCallback = errorCallback || defaultErrorCallback;
  356. App.ajax.send({
  357. name : 'request_schedule.delete',
  358. sender : {
  359. successCallbackFunction : function(data) {
  360. successCallback(data);
  361. },
  362. errorCallbackFunction : function(xhr, textStatus, error, opt) {
  363. errorCallback(xhr, textStatus, error, opt);
  364. }
  365. },
  366. data : {
  367. request_schedule_id : requestScheduleId
  368. },
  369. success : 'successCallbackFunction',
  370. error : 'errorCallbackFunction'
  371. });
  372. } else {
  373. successCallback({});
  374. }
  375. }
  376. };