Repository: ambari
Updated Branches:
  refs/heads/branch-rbac-sso f7ec3bc89 -> e8aeb7c5d


AMBARI-14418 - After Downgrade Cluster Desired Configurations Are Not Set 
(jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/e8aeb7c5
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/e8aeb7c5
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/e8aeb7c5

Branch: refs/heads/branch-rbac-sso
Commit: e8aeb7c5dc22dc53fa4356117a1b94d71bc6ab47
Parents: f7ec3bc
Author: Jonathan Hurley <[email protected]>
Authored: Thu Dec 17 10:26:37 2015 -0500
Committer: Jonathan Hurley <[email protected]>
Committed: Thu Dec 17 13:56:34 2015 -0500

----------------------------------------------------------------------
 .../server/state/cluster/ClusterImpl.java       |  55 ++++++----
 .../server/state/cluster/ClusterTest.java       | 100 +++++++++++++++++++
 2 files changed, 136 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/e8aeb7c5/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
index b2a8485..629c1b0 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java
@@ -37,6 +37,7 @@ import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import javax.annotation.Nullable;
+import javax.persistence.EntityManager;
 import javax.persistence.RollbackException;
 
 import com.google.common.collect.Maps;
@@ -63,7 +64,6 @@ import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.ClusterStateDAO;
 import org.apache.ambari.server.orm.dao.ClusterVersionDAO;
-import org.apache.ambari.server.orm.dao.ConfigGroupHostMappingDAO;
 import org.apache.ambari.server.orm.dao.HostConfigMappingDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostVersionDAO;
@@ -92,8 +92,6 @@ import org.apache.ambari.server.orm.entities.StackEntity;
 import org.apache.ambari.server.orm.entities.TopologyRequestEntity;
 import org.apache.ambari.server.security.authorization.AuthorizationException;
 import org.apache.ambari.server.security.authorization.AuthorizationHelper;
-import org.apache.ambari.server.security.authorization.ResourceType;
-import org.apache.ambari.server.security.authorization.RoleAuthorization;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.ClusterHealthReport;
 import org.apache.ambari.server.state.Clusters;
@@ -193,9 +191,15 @@ public class ClusterImpl implements Cluster {
   private final ReentrantReadWriteLock hostTransitionStateLock = new 
ReentrantReadWriteLock();
   private final Lock hostTransitionStateWriteLock = 
hostTransitionStateLock.writeLock();
 
+  /**
+   * The backing cluster entity - this should never be cached locally.
+   */
   private ClusterEntity clusterEntity;
 
-  private long clusterId;
+  /**
+   * The unique ID of the {@link #clusterEntity}.
+   */
+  private final long clusterId;
 
   @Inject
   private ClusterDAO clusterDAO;
@@ -225,9 +229,6 @@ public class ClusterImpl implements Cluster {
   private ConfigGroupFactory configGroupFactory;
 
   @Inject
-  private ConfigGroupHostMappingDAO configGroupHostMappingDAO;
-
-  @Inject
   private RequestExecutionFactory requestExecutionFactory;
 
   @Inject
@@ -279,6 +280,7 @@ public class ClusterImpl implements Cluster {
                      Injector injector) throws AmbariException {
     injector.injectMembers(this);
     this.clusterEntity = clusterEntity;
+    clusterId = clusterEntity.getClusterId();
 
     serviceComponentHosts = new HashMap<String,
       Map<String, Map<String, ServiceComponentHost>>>();
@@ -2166,8 +2168,9 @@ public class ClusterImpl implements Cluster {
           
c.setVersion(allConfigs.get(e.getType()).get(e.getTag()).getVersion());
 
           Set<DesiredConfig> configs = map.get(e.getType());
-          if (configs == null)
+          if (configs == null) {
             configs = new HashSet<>();
+          }
 
           configs.add(c);
 
@@ -2196,8 +2199,9 @@ public class ClusterImpl implements Cluster {
                 hostIdToName.get(mappingEntity.getHostId()), 
mappingEntity.getVersion()));
           }
 
-          for (DesiredConfig c: entry.getValue())
+          for (DesiredConfig c: entry.getValue()) {
             c.setHostOverrides(hostOverrides);
+          }
         }
       }
 
@@ -3018,20 +3022,27 @@ public class ClusterImpl implements Cluster {
   public void applyLatestConfigurations(StackId stackId) {
     clusterGlobalLock.writeLock().lock();
     try {
-      Collection<ClusterConfigMappingEntity> configMappingEntities = 
clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
+      ClusterEntity clusterEntity = getClusterEntity();
+      Collection<ClusterConfigMappingEntity> configMappingEntities = 
clusterEntity.getConfigMappingEntities();
 
-      // disable previous config
+      // disable all configs
       for (ClusterConfigMappingEntity e : configMappingEntities) {
         LOG.debug("{} with tag {} is unselected", e.getType(), e.getTag());
         e.setSelected(0);
       }
 
-      List<ClusterConfigMappingEntity> clusterConfigMappingEntities = 
clusterDAO.getClusterConfigMappingsByStack(clusterEntity.getClusterId(), 
stackId);
-      Collection<ClusterConfigMappingEntity> latestConfigMappingByStack = 
getLatestConfigMapping(clusterConfigMappingEntities);
+      List<ClusterConfigMappingEntity> clusterConfigMappingsForStack = 
clusterDAO.getClusterConfigMappingsByStack(
+          clusterEntity.getClusterId(), stackId);
 
+      Collection<ClusterConfigMappingEntity> latestConfigMappingByStack = 
getLatestConfigMapping(
+          clusterConfigMappingsForStack);
+
+      // loop through all configs and set the latest to enabled for the
+      // specified stack
       for(ClusterConfigMappingEntity e: configMappingEntities){
-        String type = e.getType(); //loop thru all the config mappings
+        String type = e.getType();
         String tag =  e.getTag();
+
         for (ClusterConfigMappingEntity latest : latestConfigMappingByStack) {
           String t = latest.getType();
           String tagLatest = latest.getTag();
@@ -3042,7 +3053,9 @@ public class ClusterImpl implements Cluster {
         }
       }
 
-      clusterDAO.mergeConfigMappings(clusterConfigMappingEntities);
+      clusterEntity.setConfigMappingEntities(configMappingEntities);
+      clusterEntity = clusterDAO.merge(clusterEntity);
+      clusterDAO.mergeConfigMappings(configMappingEntities);
 
       cacheConfigurations();
     } finally {
@@ -3274,13 +3287,20 @@ public class ClusterImpl implements Cluster {
     // Iterate through the topology requests associated with this cluster and 
look for PROVISION request
     for (TopologyRequestEntity topologyRequest: topologyRequests) {
       TopologyRequest.Type requestAction = 
TopologyRequest.Type.valueOf(topologyRequest.getAction());
-      if (requestAction == TopologyRequest.Type.PROVISION)
+      if (requestAction == TopologyRequest.Type.PROVISION) {
         return true;
+      }
     }
 
     return false;
   }
 
+  /**
+   * Gets the {@link ClusterEntity} for this {@link Cluster} from the
+   * {@link EntityManager} cache.
+   *
+   * @return
+   */
   private ClusterEntity getClusterEntity() {
     if (!clusterDAO.isManaged(clusterEntity)) {
       clusterEntity = clusterDAO.findById(clusterEntity.getClusterId());
@@ -3288,6 +3308,3 @@ public class ClusterImpl implements Cluster {
     return clusterEntity;
   }
 }
-
-
-

http://git-wip-us.apache.org/repos/asf/ambari/blob/e8aeb7c5/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
index b6eb8d0..05dbce1 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/state/cluster/ClusterTest.java
@@ -62,6 +62,7 @@ import org.apache.ambari.server.orm.dao.RepositoryVersionDAO;
 import org.apache.ambari.server.orm.dao.ResourceTypeDAO;
 import org.apache.ambari.server.orm.dao.StackDAO;
 import org.apache.ambari.server.orm.entities.ClusterConfigEntity;
+import org.apache.ambari.server.orm.entities.ClusterConfigMappingEntity;
 import org.apache.ambari.server.orm.entities.ClusterEntity;
 import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
 import org.apache.ambari.server.orm.entities.ClusterStateEntity;
@@ -2265,6 +2266,105 @@ public class ClusterTest {
     assertTrue(clusterConfigEntity.getData().contains("two"));
     assertTrue(clusterConfigEntity.getData().contains("three"));
     assertTrue(clusterConfigEntity.getData().contains("four"));
+  }
+
+  /**
+   * Tests that {@link Cluster#applyLatestConfigurations(StackId)} sets the
+   * right configs to enabled.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testApplyLatestConfigurations() throws Exception {
+    createDefaultCluster();
+    Cluster cluster = clusters.getCluster("c1");
+    ClusterEntity clusterEntity = clusterDAO.findByName("c1");
+    StackId stackId = cluster.getCurrentStackVersion();
+    StackId newStackId = new StackId("HDP-2.0.6");
+
+    StackEntity currentStack = stackDAO.find(stackId.getStackName(), 
stackId.getStackVersion());
+    StackEntity newStack = stackDAO.find(newStackId.getStackName(), 
newStackId.getStackVersion());
+
+    Assert.assertFalse( stackId.equals(newStackId) );
+
+    String configType = "foo-type";
+
+    ClusterConfigEntity clusterConfig = new ClusterConfigEntity();
+    clusterConfig.setClusterEntity(clusterEntity);
+    clusterConfig.setConfigId(1L);
+    clusterConfig.setStack(currentStack);
+    clusterConfig.setTag("version-1");
+    clusterConfig.setData("{}");
+    clusterConfig.setType(configType);
+    clusterConfig.setTimestamp(1L);
+    clusterConfig.setVersion(1L);
+
+    clusterDAO.createConfig(clusterConfig);
+    clusterEntity.getClusterConfigEntities().add(clusterConfig);
+    clusterEntity = clusterDAO.merge(clusterEntity);
+
+    ClusterConfigEntity newClusterConfig = new ClusterConfigEntity();
+    newClusterConfig.setClusterEntity(clusterEntity);
+    newClusterConfig.setConfigId(2L);
+    newClusterConfig.setStack(newStack);
+    newClusterConfig.setTag("version-2");
+    newClusterConfig.setData("{}");
+    newClusterConfig.setType(configType);
+    newClusterConfig.setTimestamp(2L);
+    newClusterConfig.setVersion(2L);
+
+    clusterDAO.createConfig(newClusterConfig);
+    clusterEntity.getClusterConfigEntities().add(newClusterConfig);
+    clusterEntity = clusterDAO.merge(clusterEntity);
+
+    // config mapping set to 1
+    ClusterConfigMappingEntity configMapping = new 
ClusterConfigMappingEntity();
+    configMapping.setClusterEntity(clusterEntity);
+    configMapping.setCreateTimestamp(1L);
+    configMapping.setSelected(1);
+    configMapping.setTag("version-1");
+    configMapping.setType(configType);
+    configMapping.setUser("admin");
+
+    // new config mapping set to 0
+    ClusterConfigMappingEntity newConfigMapping = new 
ClusterConfigMappingEntity();
+    newConfigMapping.setClusterEntity(clusterEntity);
+    newConfigMapping.setCreateTimestamp(2L);
+    newConfigMapping.setSelected(0);
+    newConfigMapping.setTag("version-2");
+    newConfigMapping.setType(configType);
+    newConfigMapping.setUser("admin");
+
+    clusterDAO.persistConfigMapping(configMapping);
+    clusterDAO.persistConfigMapping(newConfigMapping);
+    clusterEntity.getConfigMappingEntities().add(configMapping);
+    clusterEntity.getConfigMappingEntities().add(newConfigMapping);
+    clusterEntity = clusterDAO.merge(clusterEntity);
+
+    // check that the original mapping is enabled
+    Collection<ClusterConfigMappingEntity> clusterConfigMappings = 
clusterEntity.getConfigMappingEntities();
+    Assert.assertEquals(2, clusterConfigMappings.size());
+    for (ClusterConfigMappingEntity clusterConfigMapping : 
clusterConfigMappings) {
+      if (clusterConfigMapping.getTag().equals("version-1")) {
+        Assert.assertEquals(1, clusterConfigMapping.isSelected());
+      } else {
+        Assert.assertEquals(0, clusterConfigMapping.isSelected());
+      }
+    }
+
+    cluster.applyLatestConfigurations(newStackId);
+    clusterEntity = clusterDAO.findByName("c1");
+
+    // now check that the new config mapping is enabled
+    clusterConfigMappings = clusterEntity.getConfigMappingEntities();
+    Assert.assertEquals(2, clusterConfigMappings.size());
+    for (ClusterConfigMappingEntity clusterConfigMapping : 
clusterConfigMappings) {
+      if (clusterConfigMapping.getTag().equals("version-1")) {
+        Assert.assertEquals(0, clusterConfigMapping.isSelected());
+      } else {
+        Assert.assertEquals(1, clusterConfigMapping.isSelected());
+      }
+    }
 
   }
 }

Reply via email to