Просмотр исходного кода

AMBARI-4224. When issuing Start/Stop of host components then predicate stale_config=true does not work

Sumit Mohanty 11 лет назад
Родитель
Сommit
5774dd81c6

+ 14 - 5
ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariManagementControllerImpl.java

@@ -601,7 +601,7 @@ public class AmbariManagementControllerImpl implements
 
     if (request.getHostname() != null) {
       try {
-        if (! clusters.getClustersForHost(request.getHostname()).contains(cluster)) {
+        if (!clusters.getClustersForHost(request.getHostname()).contains(cluster)) {
           // case where host exists but not associated with given cluster
           throw new ParentObjectNotFoundException("Parent Host resource doesn't exist",
               new HostNotFoundException(request.getClusterName(), request.getHostname()));
@@ -629,7 +629,7 @@ public class AmbariManagementControllerImpl implements
         if (serviceName == null
             || serviceName.isEmpty()) {
           throw new ServiceComponentHostNotFoundException(
-              cluster.getClusterName(), null, request.getComponentName(),request.getHostname());
+              cluster.getClusterName(), null, request.getComponentName(), request.getHostname());
         }
         request.setServiceName(serviceName);
       }
@@ -647,6 +647,12 @@ public class AmbariManagementControllerImpl implements
 
     boolean checkDesiredState = false;
     State desiredStateToCheck = null;
+    boolean filterBasedConfigStaleness = false;
+    boolean staleConfig = true;
+    if (request.getStaleConfig() != null) {
+      filterBasedConfigStaleness = true;
+      staleConfig = "true".equals(request.getStaleConfig().toLowerCase());
+    }
     if (request.getDesiredState() != null
         && !request.getDesiredState().isEmpty()) {
       desiredStateToCheck = State.valueOf(request.getDesiredState());
@@ -665,7 +671,7 @@ public class AmbariManagementControllerImpl implements
       } else {
         components.addAll(s.getServiceComponents().values());
       }
-      for(ServiceComponent sc : components) {
+      for (ServiceComponent sc : components) {
         if (request.getComponentName() != null) {
           if (!sc.getName().equals(request.getComponentName())) {
             continue;
@@ -688,13 +694,12 @@ public class AmbariManagementControllerImpl implements
           } catch (ServiceComponentHostNotFoundException e) {
             if (request.getServiceName() != null && request.getComponentName() != null) {
               throw new ServiceComponentHostNotFoundException(cluster.getClusterName(),
-                  request.getServiceName(), request.getComponentName(),request.getHostname());
+                  request.getServiceName(), request.getComponentName(), request.getHostname());
             } else {
               // ignore this since host_component was not specified
               // this is an artifact of how we get host_components and can happen
               // in case where we get all host_components for a host
             }
-
           }
         } else {
           for (ServiceComponentHost sch :
@@ -703,7 +708,11 @@ public class AmbariManagementControllerImpl implements
                 && (desiredStateToCheck != sch.getDesiredState())) {
               continue;
             }
+
             ServiceComponentHostResponse r = sch.convertToResponse();
+            if (filterBasedConfigStaleness && r.isStaleConfig() != staleConfig) {
+              continue;
+            }
             response.add(r);
           }
         }

+ 16 - 0
ambari-server/src/main/java/org/apache/ambari/server/controller/ServiceComponentHostRequest.java

@@ -33,6 +33,8 @@ public class ServiceComponentHostRequest {
   private String desiredState; // CREATE/UPDATE
 
   private String desiredStackId; // UPDATE
+
+  private String staleConfig; // GET - predicate
   
   public ServiceComponentHostRequest(String clusterName,
                                      String serviceName,
@@ -131,6 +133,20 @@ public class ServiceComponentHostRequest {
     this.clusterName = clusterName;
   }
 
+  /**
+   * @param staleConfig whether the config is stale
+   */
+  public void setStaleConfig(String staleConfig) {
+    this.staleConfig = staleConfig;
+  }
+
+  /**
+   * @return Stale config indicator
+   */
+  public String getStaleConfig() {
+    return this.staleConfig;
+  }
+
   public String toString() {
     StringBuilder sb = new StringBuilder();
     sb.append("{"

+ 4 - 0
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HostComponentResourceProvider.java

@@ -292,6 +292,10 @@ public class HostComponentResourceProvider extends AbstractControllerResourcePro
         (String) properties.get(HOST_COMPONENT_STATE_PROPERTY_ID));
     serviceComponentHostRequest.setDesiredStackId(
         (String) properties.get(HOST_COMPONENT_STACK_ID_PROPERTY_ID));
+    if (properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID) != null) {
+      serviceComponentHostRequest.setStaleConfig(
+          properties.get(HOST_COMPONENT_STALE_CONFIGS_PROPERTY_ID).toString());
+    }
 
     return serviceComponentHostRequest;
   }

+ 87 - 0
ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java

@@ -2100,9 +2100,96 @@ public class AmbariManagementControllerTest {
         resp.getStackVersion());
     Assert.assertNotNull(resp.getActualConfigs());
     Assert.assertEquals(1, resp.getActualConfigs().size());
+  }
+
+  @Test
+  public void testGetServiceComponentHostsWithFilter() throws AmbariException {
+    String clusterName = "foo1";
+    createCluster(clusterName);
+    clusters.getCluster(clusterName)
+        .setDesiredStackVersion(new StackId("HDP-2.0.5"));
+    String serviceName = "HDFS";
+    createService(clusterName, serviceName, null);
+    String componentName1 = "NAMENODE";
+    String componentName2 = "DATANODE";
+    String componentName3 = "HDFS_CLIENT";
+
+    createServiceComponent(clusterName, serviceName, componentName1,
+        State.INIT);
+    createServiceComponent(clusterName, serviceName, componentName2,
+        State.INIT);
+    createServiceComponent(clusterName, serviceName, componentName3,
+        State.INIT);
+
+    String host1 = "h1";
+    clusters.addHost(host1);
+    clusters.getHost("h1").setOsType("centos5");
+    clusters.getHost("h1").setState(HostState.HEALTHY);
+    clusters.getHost("h1").persist();
+
+    clusters.mapHostToCluster(host1, clusterName);
+
+    createServiceComponentHost(clusterName, serviceName, componentName1,
+        host1, null);
+    createServiceComponentHost(clusterName, serviceName, componentName2,
+        host1, null);
+    createServiceComponentHost(clusterName, serviceName, componentName3,
+        host1, null);
+
+    // Install
+    installService(clusterName, serviceName, false, false);
+
+    // Create and attach config
+    Map<String, String> configs = new HashMap<String, String>();
+    configs.put("a", "b");
+
+    ConfigurationRequest cr1;
+    cr1 = new ConfigurationRequest(clusterName, "hdfs-site","version1",
+        configs);
+    ClusterRequest crReq = new ClusterRequest(null, clusterName, null, null);
+    crReq.setDesiredConfig(cr1);
+    controller.updateClusters(Collections.singleton(crReq), null);
+
+    // Start
+    startService(clusterName, serviceName, false, false);
+
+    //Update actual config
+    Service s1 = clusters.getCluster(clusterName).getService(serviceName);
+    ServiceComponentHost sch1 = s1.getServiceComponent(componentName1)
+        .getServiceComponentHost(host1);
+    ServiceComponentHost sch2 = s1.getServiceComponent(componentName2)
+        .getServiceComponentHost(host1);
+    ServiceComponentHost sch3 = s1.getServiceComponent(componentName3)
+        .getServiceComponentHost(host1);
+    sch1.updateActualConfigs(new HashMap<String, Map<String,String>>() {{
+      put("hdfs-site", new HashMap<String,String>() {{ put("tag", "version1"); }});
+    }});
+    sch2.updateActualConfigs(new HashMap<String, Map<String,String>>() {{
+      put("hdfs-site", new HashMap<String,String>() {{ put("tag", "version1"); }});
+    }});
+    sch3.updateActualConfigs(new HashMap<String, Map<String,String>>() {{
+      put("hdfs-site", new HashMap<String,String>() {{ put("tag", "version2"); }});
+    }});
+
+    ServiceComponentHostRequest r =
+        new ServiceComponentHostRequest(clusterName, null, null, null, null);
+    Set<ServiceComponentHostResponse> resps = controller.getHostComponents(Collections.singleton(r));
+    Assert.assertEquals(3, resps.size());
+
+    //Get all host components with stale config = true
+    r = new ServiceComponentHostRequest(clusterName, null, null, null, null);
+    r.setStaleConfig("true");
+    resps = controller.getHostComponents(Collections.singleton(r));
+    Assert.assertEquals(1, resps.size());
 
+    //Get all host components with stale config = false
+    r = new ServiceComponentHostRequest(clusterName, null, null, null, null);
+    r.setStaleConfig("false");
+    resps = controller.getHostComponents(Collections.singleton(r));
+    Assert.assertEquals(2, resps.size());
   }
 
+
   private Cluster setupClusterWithHosts(String clusterName, String stackId, List<String> hosts,
                              String osType) throws AmbariException {
     clusters.addCluster(clusterName);