You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by mp...@apache.org on 2015/11/18 20:24:39 UTC

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

Repository: ambari
Updated Branches:
  refs/heads/branch-2.1 c0d0ea78c -> c5d6685c3


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


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

Branch: refs/heads/branch-2.1
Commit: c5d6685c3dd75aa752fcb12be59094c4f5d17074
Parents: c0d0ea7
Author: Myroslav Papirkovskyi <mp...@hortonworks.com>
Authored: Wed Nov 18 19:04:45 2015 +0200
Committer: Myroslav Papirkovskyi <mp...@hortonworks.com>
Committed: Wed Nov 18 21:22:43 2015 +0200

----------------------------------------------------------------------
 .../ambari/server/orm/dao/ClusterDAO.java       | 43 ++++++++++++++++++++
 .../server/orm/entities/ClusterEntity.java      |  2 +-
 .../server/state/cluster/ClusterImpl.java       | 38 +++++++++++------
 .../server/upgrade/UpgradeCatalog150.java       |  1 +
 .../server/upgrade/UpgradeCatalog170.java       |  1 +
 5 files changed, 72 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/c5d6685c/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
index 6ab3b4f..508a41c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/ClusterDAO.java
+++ b/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;
@@ -203,6 +205,17 @@ public class ClusterDAO {
     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
@@ -229,6 +242,32 @@ public class ClusterDAO {
   }
 
   /**
+   * 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.
    */
   @Transactional
@@ -323,4 +362,8 @@ public class ClusterDAO {
     remove(findById(id));
   }
 
+  @RequiresSession
+  public boolean isManaged(ClusterEntity entity) {
+    return entityManagerProvider.get().contains(entity);
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/c5d6685c/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
index 88eba07..2c4d695 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/ClusterEntity.java
+++ b/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)

http://git-wip-us.apache.org/repos/asf/ambari/blob/c5d6685c/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 2d3703a..79cc1cc 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
@@ -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
@@ -2505,7 +2510,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());
@@ -2525,7 +2530,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());
         }
@@ -2541,7 +2546,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;
         }
@@ -2931,7 +2936,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) {
@@ -2953,7 +2958,7 @@ public class ClusterImpl implements Cluster {
         }
       }
 
-      clusterEntity = clusterDAO.merge(clusterEntity);
+      clusterDAO.mergeConfigMappings(configMappingEntities);
 
       cacheConfigurations();
     } finally {
@@ -3018,7 +3023,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();
@@ -3152,6 +3158,14 @@ public class ClusterImpl implements Cluster {
       clusterGlobalLock.writeLock().unlock();
     }
   }
+
+  private ClusterEntity getClusterEntity() {
+    if (!clusterDAO.isManaged(clusterEntity)) {
+      clusterEntity = clusterDAO.findById(clusterEntity.getClusterId());
+    }
+    return clusterEntity;
+  }
 }
 
 
+

http://git-wip-us.apache.org/repos/asf/ambari/blob/c5d6685c/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog150.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog150.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog150.java
index 00baf28..b00b0e8 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog150.java
+++ b/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);
                 }
               }

http://git-wip-us.apache.org/repos/asf/ambari/blob/c5d6685c/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java b/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java
index c54039f..01b69db 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/upgrade/UpgradeCatalog170.java
+++ b/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);