Explorar el Código

AMBARI-4791. API call to restart all components on one or more hosts should result in one request. (onechiporenko via yusaku)

Yusaku Sako hace 11 años
padre
commit
a8be41bf72

+ 12 - 16
ambari-web/app/utils/ajax.js

@@ -1186,7 +1186,7 @@ var urls = {
   'wizard.step9.installer.get_host_status': {
     'real': '/clusters/{cluster}/hosts?fields=Hosts/host_state,host_components/HostRoles/state',
     'mock': '/data/wizard/deploy/5_hosts/get_host_status.json',
-    'format': function (data, opt) {
+    'format': function () {
       return {
         async: false
       };
@@ -1404,22 +1404,18 @@ var urls = {
     'real': '/clusters/{clusterName}/request_schedules/{request_schedule_id}',
     'mock': ''
   },
-  'restart.service.hostComponents' : {
-    'real' : '/clusters/{clusterName}/requests',
-    'mock' : '',
-    'format' : function(data) {
-      var componentDisplayName = App.format.role(data.componentName);
-      var serviceDisplayName = App.Service.DisplayNames[data.serviceName];
+  'restart.hostComponents': {
+    'real':'/clusters/{clusterName}/requests',
+    'mock':'',
+    'format': function(data) {
       return {
         type : 'POST',
         data : JSON.stringify({
-          "RequestInfo" : {
-            "context" : Em.I18n.t('restart.service.rest.context').format(componentDisplayName),
-            "command" : "RESTART",
-            "service_name" : data.serviceName,
-            "component_name" : data.componentName,
-            "hosts" : data.hosts
-          }
+          "RequestInfo": {
+            "command": "RESTART",
+            "context": data.context
+          },
+          "Requests/resource_filters": data.resource_filters
         })
       }
     }
@@ -1511,7 +1507,7 @@ var urls = {
     'mock': '/data/mirroring/succeeded.json',
     'apiPrefix': '',
     'type': 'DELETE',
-    'format': function (data) {
+    'format': function () {
       return {
         dataType: 'xml'
       }
@@ -1536,7 +1532,7 @@ var urls = {
     'mock': '/data/mirroring/succeeded.json',
     'apiPrefix': '',
     'type': 'POST',
-    'format': function (data) {
+    'format': function () {
       return {
         dataType: 'xml'
       }

+ 24 - 14
ambari-web/app/utils/batch_scheduled_requests.js

@@ -80,7 +80,7 @@ module.exports = {
 
   /**
    * Restart list of host components
-   * @param {Array} hostComponentsList list of host components should be restarted
+   * @param {*} hostComponentsList list of host components should be restarted
    */
   restartHostComponents: function(hostComponentsList) {
     /**
@@ -100,22 +100,32 @@ module.exports = {
       }
       componentToHostsMap[componentName].push(hc.get('host.hostName'));
     });
+    var resource_filters = [];
     for (var componentName in componentToHostsMap) {
-      App.ajax.send({
-        name: 'restart.service.hostComponents',
-        sender: {
-          successCallback: defaultSuccessCallback,
-          errorCallback: defaultErrorCallback
-        },
-        data: {
-          serviceName:  componentServiceMap[componentName],
-          componentName: componentName,
+      if (componentToHostsMap.hasOwnProperty(componentName)) {
+        resource_filters.push({
+          service_name:  componentServiceMap[componentName],
+          component_name: componentName,
           hosts: componentToHostsMap[componentName].join(",")
-        },
-        success: 'successCallback',
-        error: 'errorCallback'
-      });
+        });
+      }
+    }
+    if (!resource_filters.length) {
+      return;
     }
+    App.ajax.send({
+      name: 'restart.hostComponents',
+      sender: {
+        successCallback: defaultSuccessCallback,
+        errorCallback: defaultErrorCallback
+      },
+      data: {
+        context: 'RESTART ' + Em.keys(componentToHostsMap).map(function(componentName) {return App.format.components[componentName];}).join(', '),
+        resource_filters: resource_filters
+      },
+      success: 'successCallback',
+      error: 'errorCallback'
+    });
   },
 
   /**

+ 78 - 0
ambari-web/test/utils/batch_scheduled_requests_test.js

@@ -118,4 +118,82 @@ describe('batch_scheduled_requests', function() {
 
   });
 
+  describe('#restartHostComponents', function() {
+
+    beforeEach(function() {
+      sinon.spy($, 'ajax');
+      App.testMode = true;
+    });
+
+    afterEach(function() {
+      $.ajax.restore();
+      App.testMode = false;
+    });
+
+    var tests = Em.A([
+      {
+        hostComponentList: Em.A([
+          Em.Object.create({
+            componentName: 'n1',
+            host: Em.Object.create({
+              hostName: 'h1'
+            })
+          }),
+          Em.Object.create({
+            componentName: 'n1',
+            host: Em.Object.create({
+              hostName: 'h2'
+            })
+          })
+        ]),
+        e: {
+          ajaxCalledOnce: true,
+          resource_filters: [{"component_name":"n1","hosts":"h1,h2"}]
+        },
+        m: '1 component on 2 hosts'
+      },
+      {
+        hostComponentList: Em.A([
+          Em.Object.create({
+            componentName: 'n1',
+            host: Em.Object.create({
+              hostName: 'h1'
+            })
+          }),
+          Em.Object.create({
+            componentName: 'n1',
+            host: Em.Object.create({
+              hostName: 'h2'
+            })
+          }),
+          Em.Object.create({
+            componentName: 'n2',
+            host: Em.Object.create({
+              hostName: 'h2'
+            })
+          })
+        ]),
+        e: {
+          ajaxCalledOnce: true,
+          resource_filters: [{"component_name":"n1","hosts":"h1,h2"},{"component_name":"n2","hosts":"h2"}]
+        },
+        m: '1 component on 2 hosts, 1 on 1 host'
+      }
+    ]);
+
+    tests.forEach(function(test) {
+      it(test.m, function() {
+        batchUtils.restartHostComponents(test.hostComponentList);
+        expect($.ajax.calledOnce).to.equal(test.e.ajaxCalledOnce);
+        expect( JSON.parse($.ajax.args[0][0].data)['Requests/resource_filters']).to.eql(test.e.resource_filters);
+      });
+    });
+
+    it('Empty data', function() {
+      batchUtils.restartHostComponents([]);
+      expect($.ajax.called).to.equal(false);
+    });
+
+  });
+
 });