You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sw...@apache.org on 2015/06/04 22:55:09 UTC

ambari git commit: AMBARI-11702. Enhanced Dashboard: Server Error on toggling add widget.

Repository: ambari
Updated Branches:
  refs/heads/trunk 6a0d1e0a2 -> 98640c636


AMBARI-11702. Enhanced Dashboard: Server Error on toggling add widget.


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

Branch: refs/heads/trunk
Commit: 98640c63692960188d54a981665a2468769751e2
Parents: 6a0d1e0
Author: Siddharth Wagle <sw...@hortonworks.com>
Authored: Thu Jun 4 13:54:39 2015 -0700
Committer: Siddharth Wagle <sw...@hortonworks.com>
Committed: Thu Jun 4 13:54:44 2015 -0700

----------------------------------------------------------------------
 .../internal/WidgetLayoutResourceProvider.java  | 96 +++++++++++---------
 .../ambari/server/orm/dao/WidgetLayoutDAO.java  | 11 +++
 2 files changed, 63 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/98640c63/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java
index 174a106..7bee12c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/WidgetLayoutResourceProvider.java
@@ -50,6 +50,8 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 /**
  * Resource provider for widget layout resources.
@@ -80,6 +82,9 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv
   };
 
   @SuppressWarnings("serial")
+  private static ReadWriteLock lock = new ReentrantReadWriteLock();
+
+  @SuppressWarnings("serial")
   public static Set<String> propertyIds = new HashSet<String>() {
     {
       add(WIDGETLAYOUT_ID_PROPERTY_ID);
@@ -267,55 +272,58 @@ public class WidgetLayoutResourceProvider extends AbstractControllerResourceProv
           } catch (Exception ex) {
             throw new AmbariException("WidgetLayout should have numerical id");
           }
-          final WidgetLayoutEntity entity = widgetLayoutDAO.findById(layoutId);
-          if (entity == null) {
-            throw new ObjectNotFoundException("There is no widget layout with id " + layoutId);
-          }
-          if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)))) {
-            entity.setLayoutName(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString());
-          }
-          if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)))) {
-            entity.setSectionName(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString());
-          }
-          if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)))) {
-            entity.setDisplayName(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString());
-          }
-          if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID)))) {
-            entity.setScope(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString());
-          }
-
-          Set widgetsSet = (LinkedHashSet) propertyMap.get(WIDGETLAYOUT_WIDGETS_PROPERTY_ID);
+          lock.writeLock().lock();
+          try {
+            final WidgetLayoutEntity entity = widgetLayoutDAO.findById(layoutId);
+            if (entity == null) {
+              throw new ObjectNotFoundException("There is no widget layout with id " + layoutId);
+            }
+            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)))) {
+              entity.setLayoutName(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString());
+            }
+            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)))) {
+              entity.setSectionName(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString());
+            }
+            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)))) {
+              entity.setDisplayName(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString());
+            }
+            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID)))) {
+              entity.setScope(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString());
+            }
 
-          //Remove old relations from widget entities
-          for (WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) {
-            widgetLayoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity()
-                    .remove(widgetLayoutUserWidgetEntity);
-            widgetDAO.merge(widgetLayoutUserWidgetEntity.getWidget());
-          }
-          entity.setListWidgetLayoutUserWidgetEntity(new LinkedList<WidgetLayoutUserWidgetEntity>());
-          widgetLayoutDAO.merge(entity);
+            Set widgetsSet = (LinkedHashSet) propertyMap.get(WIDGETLAYOUT_WIDGETS_PROPERTY_ID);
 
-          List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>();
-          int order=0;
-          for (Object widgetObject : widgetsSet) {
-            HashMap<String, Object> widget = (HashMap) widgetObject;
-            long id = Integer.parseInt(widget.get("id").toString());
-            WidgetEntity widgetEntity = widgetDAO.findById(id);
-            if (widgetEntity == null) {
-              throw new AmbariException("Widget with id " + widget.get("id").toString() + " does not exists");
+            //Remove old relations from widget entities
+            for (WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) {
+              widgetLayoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity()
+                      .remove(widgetLayoutUserWidgetEntity);
+            }
+            entity.setListWidgetLayoutUserWidgetEntity(new LinkedList<WidgetLayoutUserWidgetEntity>());
+
+            List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<WidgetLayoutUserWidgetEntity>();
+            int order = 0;
+            for (Object widgetObject : widgetsSet) {
+              HashMap<String, Object> widget = (HashMap) widgetObject;
+              long id = Integer.parseInt(widget.get("id").toString());
+              WidgetEntity widgetEntity = widgetDAO.findById(id);
+              if (widgetEntity == null) {
+                throw new AmbariException("Widget with id " + widget.get("id").toString() + " does not exists");
+              }
+              WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity = new WidgetLayoutUserWidgetEntity();
+
+              widgetLayoutUserWidgetEntity.setWidget(widgetEntity);
+              widgetLayoutUserWidgetEntity.setWidgetOrder(order++);
+              widgetLayoutUserWidgetEntity.setWidgetLayout(entity);
+              widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidgetEntity);
+              widgetEntity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity);
+              entity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity);
             }
-            WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity = new WidgetLayoutUserWidgetEntity();
-
-            widgetLayoutUserWidgetEntity.setWidget(widgetEntity);
-            widgetLayoutUserWidgetEntity.setWidgetOrder(order++);
-            widgetLayoutUserWidgetEntity.setWidgetLayout(entity);
-            widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidgetEntity);
-            widgetEntity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity);
-          }
 
-          entity.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList);
 
-          widgetLayoutDAO.merge(entity);
+            widgetLayoutDAO.mergeWithFlush(entity);
+          } finally {
+            lock.writeLock().unlock();
+          }
         }
         return null;
       }

http://git-wip-us.apache.org/repos/asf/ambari/blob/98640c63/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java
index 9915fa8..2019e3b 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/WidgetLayoutDAO.java
@@ -25,8 +25,11 @@ import org.apache.ambari.server.orm.RequiresSession;
 import org.apache.ambari.server.orm.entities.WidgetLayoutEntity;
 
 import javax.persistence.EntityManager;
+import javax.persistence.LockModeType;
 import javax.persistence.TypedQuery;
 import java.util.List;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 @Singleton
 public class WidgetLayoutDAO {
@@ -92,6 +95,14 @@ public class WidgetLayoutDAO {
   }
 
   @Transactional
+  public WidgetLayoutEntity mergeWithFlush(WidgetLayoutEntity widgetLayoutEntity) {
+    EntityManager entityManager = entityManagerProvider.get();
+    widgetLayoutEntity = entityManager.merge(widgetLayoutEntity);
+    entityManager.flush();
+    return widgetLayoutEntity;
+  }
+
+  @Transactional
   public void remove(WidgetLayoutEntity widgetLayoutEntity) {
     entityManagerProvider.get().remove(merge(widgetLayoutEntity));
   }