Bläddra i källkod

AMBARI-13953. Config types, incorrect number of selected=1. (mpapirkovskyy)

Myroslav Papirkovskyi 9 år sedan
förälder
incheckning
f90c2b0435

+ 45 - 2
ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java

@@ -18,11 +18,13 @@
 
 package org.apache.ambari.server.orm.dao;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
 import javax.persistence.EntityManager;
 import javax.persistence.NoResultException;
+import javax.persistence.Query;
 import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
@@ -227,8 +229,19 @@ public class ClusterDAO {
     query.setParameter("stack", stackEntity);
 
     return daoUtils.selectList(query);
-  }  
-  
+  }
+
+  @RequiresSession
+  public List<ClusterConfigMappingEntity> getClusterConfigMappingEntitiesByCluster(long clusterId) {
+    TypedQuery<ClusterConfigMappingEntity> query = entityManagerProvider.get().createQuery(
+      "SELECT mapping FROM ClusterConfigMappingEntity mapping " +
+        "WHERE mapping.clusterId = :clusterId", ClusterConfigMappingEntity.class);
+
+    query.setParameter("clusterId", clusterId);
+
+    return daoUtils.selectList(query);
+  }
+
   /**
    * Create Cluster entity in Database
    * @param clusterEntity entity to create
@@ -254,6 +267,32 @@ public class ClusterDAO {
     entityManagerProvider.get().remove(entity);
   }
 
+  /**
+   * Bulk update config mappings in DB
+   */
+  @Transactional
+  public void mergeConfigMappings(Collection<ClusterConfigMappingEntity> mappingEntities) {
+    for (ClusterConfigMappingEntity mappingEntity : mappingEntities) {
+      entityManagerProvider.get().merge(mappingEntity);
+    }
+  }
+
+  /**
+   * Update config mapping in DB
+   */
+  @Transactional
+  public void mergeConfigMapping(ClusterConfigMappingEntity mappingEntity) {
+    entityManagerProvider.get().merge(mappingEntity);
+  }
+
+  /**
+   * Create cluster config mapping in DB
+   */
+  @Transactional
+  public void persistConfigMapping(ClusterConfigMappingEntity entity) {
+    entityManagerProvider.get().persist(entity);
+  }
+
   /**
    * Remove a cluster configuration mapping from the DB.
    */
@@ -349,4 +388,8 @@ public class ClusterDAO {
     remove(findById(id));
   }
 
+  @RequiresSession
+  public boolean isManaged(ClusterEntity entity) {
+    return entityManagerProvider.get().contains(entity);
+  }
 }

+ 4 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterConfigEntity.java

@@ -52,7 +52,10 @@ import javax.persistence.UniqueConstraint;
     @NamedQuery(name = "ClusterConfigEntity.findNextConfigVersion", query = "SELECT COALESCE(MAX(clusterConfig.version),0) + 1 as nextVersion FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.type=:configType AND clusterConfig.clusterId=:clusterId"),
     @NamedQuery(name = "ClusterConfigEntity.findAllConfigsByStack", query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.stack=:stack"),
     @NamedQuery(name = "ClusterConfigEntity.findLatestConfigsByStack", query = "SELECT clusterConfig FROM ClusterConfigEntity clusterConfig WHERE clusterConfig.clusterId=:clusterId AND clusterConfig.timestamp = (SELECT MAX(clusterConfig2.timestamp) FROM ClusterConfigEntity clusterConfig2 WHERE clusterConfig2.clusterId=:clusterId AND clusterConfig2.stack=:stack AND clusterConfig2.type = clusterConfig.type)"),
-    @NamedQuery(name = "ClusterConfigEntity.findClusterConfigMappingsByStack", query = "SELECT clusterConfigMapping FROM ClusterConfigMappingEntity clusterConfigMapping WHERE clusterConfigMapping.clusterId=:clusterId and clusterConfigMapping.tag in ( select clusterConfig.tag from ClusterConfigEntity clusterConfig where clusterConfig.stack = :stack)")
+    @NamedQuery(name = "ClusterConfigEntity.findClusterConfigMappingsByStack",
+      query = "SELECT mapping FROM ClusterConfigMappingEntity mapping " +
+        "JOIN ClusterConfigEntity config ON mapping.typeName = config.type AND mapping.tag = config.tag " +
+        "WHERE mapping.clusterId = :clusterId AND config.stack = :stack")
 })
 
 public class ClusterConfigEntity {

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java

@@ -115,7 +115,7 @@ public class ClusterEntity {
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.ALL)
   private Collection<ClusterConfigEntity> configEntities;
 
-  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.ALL)
+  @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.REMOVE)
   private Collection<ClusterConfigMappingEntity> configMappingEntities;
 
   @OneToMany(mappedBy = "clusterEntity", cascade = CascadeType.ALL)

+ 26 - 12
ambari-server/src/main/java/org/apache/ambari/server/state/cluster/ClusterImpl.java

@@ -187,6 +187,8 @@ public class ClusterImpl implements Cluster {
 
   private ClusterEntity clusterEntity;
 
+  private long clusterId;
+
   @Inject
   private ClusterDAO clusterDAO;
 
@@ -2078,7 +2080,7 @@ public class ClusterImpl implements Cluster {
       Map<String, DesiredConfig> map = new HashMap<String, DesiredConfig>();
       Collection<String> types = new HashSet<String>();
 
-      for (ClusterConfigMappingEntity e : clusterEntity.getConfigMappingEntities()) {
+      for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
         if (e.isSelected() > 0) {
           DesiredConfig c = new DesiredConfig();
           c.setServiceName(null);
@@ -2370,12 +2372,14 @@ public class ClusterImpl implements Cluster {
     //disable all configs related to service
     if (serviceConfigEntity.getGroupId() == null) {
       Collection<String> configTypes = serviceConfigTypes.get(serviceName);
-      for (ClusterConfigMappingEntity entity : clusterEntity.getConfigMappingEntities()) {
+      List<ClusterConfigMappingEntity> mappingEntities =
+          clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
+      for (ClusterConfigMappingEntity entity : mappingEntities) {
         if (configTypes.contains(entity.getType()) && entity.isSelected() > 0) {
           entity.setSelected(0);
+          clusterDAO.mergeConfigMapping(entity);
         }
       }
-      clusterEntity = clusterDAO.merge(clusterEntity);
 
       for (ClusterConfigEntity configEntity : serviceConfigEntity.getClusterConfigEntities()) {
         selectConfig(configEntity.getType(), configEntity.getTag(), user);
@@ -2432,12 +2436,14 @@ public class ClusterImpl implements Cluster {
 
   @Transactional
   void selectConfig(String type, String tag, String user) {
-    Collection<ClusterConfigMappingEntity> entities = clusterEntity.getConfigMappingEntities();
+    Collection<ClusterConfigMappingEntity> entities =
+        clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
 
     //disable previous config
     for (ClusterConfigMappingEntity e : entities) {
       if (e.isSelected() > 0 && e.getType().equals(type)) {
         e.setSelected(0);
+        clusterDAO.mergeConfigMapping(e);
       }
     }
 
@@ -2449,9 +2455,8 @@ public class ClusterImpl implements Cluster {
     entity.setUser(user);
     entity.setType(type);
     entity.setTag(tag);
-    entities.add(entity);
+    clusterDAO.persistConfigMapping(entity);
 
-    clusterEntity = clusterDAO.merge(clusterEntity);
   }
 
   @Transactional
@@ -2506,7 +2511,7 @@ public class ClusterImpl implements Cluster {
 
     //add configs from this service
     Collection<String> configTypes = serviceConfigTypes.get(serviceName);
-    for (ClusterConfigMappingEntity mappingEntity : clusterEntity.getConfigMappingEntities()) {
+    for (ClusterConfigMappingEntity mappingEntity : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
       if (mappingEntity.isSelected() > 0 && configTypes.contains(mappingEntity.getType())) {
         ClusterConfigEntity configEntity =
           clusterDAO.findConfig(getClusterId(), mappingEntity.getType(), mappingEntity.getTag());
@@ -2526,7 +2531,7 @@ public class ClusterImpl implements Cluster {
     loadConfigurations();
     clusterGlobalLock.readLock().lock();
     try {
-      for (ClusterConfigMappingEntity e : clusterEntity.getConfigMappingEntities()) {
+      for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
         if (e.isSelected() > 0 && e.getType().equals(configType)) {
           return getConfig(e.getType(), e.getTag());
         }
@@ -2542,7 +2547,7 @@ public class ClusterImpl implements Cluster {
   public boolean isConfigTypeExists(String configType) {
     clusterGlobalLock.readLock().lock();
     try {
-      for (ClusterConfigMappingEntity e : clusterEntity.getConfigMappingEntities()) {
+      for (ClusterConfigMappingEntity e : clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId())) {
         if (e.getType().equals(configType)) {
           return true;
         }
@@ -2932,7 +2937,7 @@ public class ClusterImpl implements Cluster {
   public void applyLatestConfigurations(StackId stackId) {
     clusterGlobalLock.writeLock().lock();
     try {
-      Collection<ClusterConfigMappingEntity> configMappingEntities = clusterEntity.getConfigMappingEntities();
+      Collection<ClusterConfigMappingEntity> configMappingEntities = clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
 
       // disable previous config
       for (ClusterConfigMappingEntity e : configMappingEntities) {
@@ -2956,7 +2961,7 @@ public class ClusterImpl implements Cluster {
         }
       }
 
-      clusterEntity = clusterDAO.merge(clusterEntity);
+      clusterDAO.mergeConfigMappings(clusterConfigMappingEntities);
 
       cacheConfigurations();
     } finally {
@@ -3040,7 +3045,8 @@ public class ClusterImpl implements Cluster {
     clusterEntity = clusterDAO.merge(clusterEntity);
 
     // remove config mappings
-    Collection<ClusterConfigMappingEntity> configMappingEntities = clusterEntity.getConfigMappingEntities();
+    Collection<ClusterConfigMappingEntity> configMappingEntities =
+        clusterDAO.getClusterConfigMappingEntitiesByCluster(getClusterId());
     for (ClusterConfigEntity removedClusterConfig : removedClusterConfigs) {
       String removedClusterConfigType = removedClusterConfig.getType();
       String removedClusterConfigTag = removedClusterConfig.getTag();
@@ -3174,6 +3180,14 @@ public class ClusterImpl implements Cluster {
       clusterGlobalLock.writeLock().unlock();
     }
   }
+
+  private ClusterEntity getClusterEntity() {
+    if (!clusterDAO.isManaged(clusterEntity)) {
+      clusterEntity = clusterDAO.findById(clusterEntity.getClusterId());
+    }
+    return clusterEntity;
+  }
 }
 
 
+

+ 1 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog150.java

@@ -777,6 +777,7 @@ public class UpgradeCatalog150 extends AbstractUpgradeCatalog {
                   clusterConfigMappingEntity.setUser(defaultUser);
                   clusterConfigMappingEntity.setTag(configEntity.getTag());
                   entities.add(clusterConfigMappingEntity);
+                  clusterDAO.persistConfigMapping(clusterConfigMappingEntity);
                   clusterDAO.merge(clusterEntity);
                 }
               }

+ 1 - 0
ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java

@@ -714,6 +714,7 @@ public class UpgradeCatalog170 extends AbstractUpgradeCatalog {
       for (ClusterConfigMappingEntity configMapping : cluster.getConfigMappingEntities()) {
         if (configMapping.getType().equals(Configuration.MAPREDUCE2_LOG4J_CONFIG_TAG)) {
           configMapping.setSelected(0);
+          clusterDAO.mergeConfigMapping(configMapping);
         }
       }
       clusterDAO.merge(cluster);