airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scnakand...@apache.org
Subject airavata git commit: fixing race condition in sharing registry
Date Fri, 16 Dec 2016 22:02:33 GMT
Repository: airavata
Updated Branches:
  refs/heads/develop b33d72be7 -> 2cb5ebc3c


fixing race condition in sharing registry


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

Branch: refs/heads/develop
Commit: 2cb5ebc3c628e69cd1801020bd17aa5be034774f
Parents: b33d72b
Author: scnakandala <supun.nakandala@gmail.com>
Authored: Fri Dec 16 17:02:26 2016 -0500
Committer: scnakandala <supun.nakandala@gmail.com>
Committed: Fri Dec 16 17:02:26 2016 -0500

----------------------------------------------------------------------
 .../sharing-registry-server/pom.xml             |  5 ++++
 .../db/repositories/AbstractRepository.java     | 24 ++++++++++++++++----
 .../db/repositories/EntityRepository.java       |  2 +-
 .../sharing/registry/db/utils/JPAUtils.java     | 21 +----------------
 .../sharing/registry/server/ServerMain.java     |  1 -
 .../registry/SharingRegistryServiceTest.java    |  9 +++++++-
 6 files changed, 34 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/pom.xml
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/pom.xml b/modules/sharing-registry/sharing-registry-server/pom.xml
index 69b16fd..2e3756d 100644
--- a/modules/sharing-registry/sharing-registry-server/pom.xml
+++ b/modules/sharing-registry/sharing-registry-server/pom.xml
@@ -56,6 +56,11 @@
             <artifactId>libthrift</artifactId>
             <version>${thrift.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.databene</groupId>
+            <artifactId>contiperf</artifactId>
+            <version>2.3.4</version>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/AbstractRepository.java
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/AbstractRepository.java
b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/AbstractRepository.java
index 271fef3..8f1fcb3 100644
--- a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/AbstractRepository.java
+++ b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/AbstractRepository.java
@@ -20,6 +20,7 @@
 */
 package org.apache.airavata.sharing.registry.db.repositories;
 
+import org.apache.airavata.sharing.registry.db.utils.Committer;
 import org.apache.airavata.sharing.registry.db.utils.DBConstants;
 import org.apache.airavata.sharing.registry.db.utils.JPAUtils;
 import org.apache.airavata.sharing.registry.db.utils.ObjectMapperSingleton;
@@ -28,6 +29,7 @@ import org.dozer.Mapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.persistence.EntityManager;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -54,7 +56,7 @@ public abstract class AbstractRepository<T, E, Id> {
     public  T update(T t) throws SharingRegistryException {
         Mapper mapper = ObjectMapperSingleton.getInstance();
         E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = (new JPAUtils()).execute(entityManager -> entityManager.merge(entity));
+        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
         return mapper.map(persistedCopy, thriftGenericClass);
     }
 
@@ -66,7 +68,7 @@ public abstract class AbstractRepository<T, E, Id> {
     }
 
     public boolean delete(Id id) throws SharingRegistryException {
-        (new JPAUtils()).execute(entityManager -> {
+        execute(entityManager -> {
              E entity = entityManager.find(dbEntityGenericClass, id);
              entityManager.remove(entity);
              return entity;
@@ -81,7 +83,7 @@ public abstract class AbstractRepository<T, E, Id> {
     }
 
     public T get(Id id) throws SharingRegistryException {
-        E entity =  (new JPAUtils()).execute(entityManager -> entityManager
+        E entity = execute(entityManager -> entityManager
                 .find(dbEntityGenericClass, id));
         Mapper mapper = ObjectMapperSingleton.getInstance();
         if(entity == null)
@@ -117,7 +119,7 @@ public abstract class AbstractRepository<T, E, Id> {
         query += " ORDER BY p.createdTime DESC";
         String queryString = query;
         int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-        List resultSet = (new JPAUtils()).execute(entityManager -> {
+        List resultSet = execute(entityManager -> {
             javax.persistence.Query q = entityManager.createQuery(queryString);
             for (int i = 0; i < parameters.size(); i++) {
                 q.setParameter(i + 1, parameters.get(i));
@@ -132,11 +134,23 @@ public abstract class AbstractRepository<T, E, Id> {
 
     public List<T> select(String queryString, int offset, int limit) throws SharingRegistryException
{
         int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-        List resultSet = (new JPAUtils()).execute(entityManager -> entityManager.createQuery(queryString).setFirstResult(offset)
+        List resultSet = execute(entityManager -> entityManager.createQuery(queryString).setFirstResult(offset)
                 .setMaxResults(newLimit).getResultList());
         Mapper mapper = ObjectMapperSingleton.getInstance();
         List<T> gatewayList = new ArrayList<>();
         resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
         return gatewayList;
     }
+
+    public <R> R execute(Committer<EntityManager, R> committer) throws SharingRegistryException
{
+        EntityManager entityManager = JPAUtils.getEntityManager();
+        try {
+            entityManager.getTransaction().begin();
+            R r = committer.commit(entityManager);
+            entityManager.getTransaction().commit();
+            return r;
+        } finally {
+            entityManager.close();
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/EntityRepository.java
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/EntityRepository.java
b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/EntityRepository.java
index 2fe15ef..f445f36 100644
--- a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/EntityRepository.java
+++ b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/repositories/EntityRepository.java
@@ -107,7 +107,7 @@ public class EntityRepository extends AbstractRepository<Entity, EntityEntity,
E
         final String nativeQuery = query;
         int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
 
-        List<Object[]> temp = (new JPAUtils()).execute(entityManager -> entityManager.createNativeQuery(nativeQuery).setFirstResult(offset)
+        List<Object[]> temp = execute(entityManager -> entityManager.createNativeQuery(nativeQuery).setFirstResult(offset)
                 .setMaxResults(newLimit).getResultList());
         List<Entity> resultSet = new ArrayList<>();
 

http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
index 3cfd60a..ed495ce 100644
--- a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
+++ b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/db/utils/JPAUtils.java
@@ -63,14 +63,13 @@ public class JPAUtils {
     @PersistenceContext(unitName = PERSISTENCE_UNIT_NAME)
     private static EntityManager entityManager;
 
-    private EntityManager getEntityManager() throws SharingRegistryException {
+    public synchronized static EntityManager getEntityManager() throws SharingRegistryException
{
         if (factory == null) {
             String connectionProperties = "DriverClassName=" + readServerProperties(SHARING_REG_JDBC_DRIVER)
+ "," +
                     "Url=" + readServerProperties(SHARING_REG_JDBC_URL) + "?autoReconnect=true,"
+
                     "Username=" + readServerProperties(SHARING_REG_JDBC_USER) + "," +
                     "Password=" + readServerProperties(SHARING_REG_JDBC_PWD) +
                     ",validationQuery=" + readServerProperties(SHARING_REG_VALIDATION_QUERY);
-//
 
 //            String connectionProperties = "DriverClassName=com.mysql.jdbc.Driver," +
 //                    "Url=jdbc:mysql://localhost:3306/airavata_sharing_catalog?autoReconnect=true,"
+
@@ -101,24 +100,6 @@ public class JPAUtils {
         return entityManager;
     }
 
-    public <R> R execute(Committer<EntityManager, R> committer) throws SharingRegistryException
{
-        EntityManager entityManager = getEntityManager();
-        try {
-            entityManager.getTransaction().begin();
-            R r = committer.commit(entityManager);
-            entityManager.getTransaction().commit();
-            return  r;
-        }finally {
-            if (entityManager != null) {
-                if (entityManager.isOpen() && entityManager.getTransaction().isActive())
{
-                    entityManager.getTransaction().rollback();
-                }
-                if(entityManager.isOpen())
-                    entityManager.close();
-            }
-        }
-    }
-
     public static void initializeDB() throws SharingRegistryException {
         jdbcDriver = readServerProperties(SHARING_REG_JDBC_DRIVER);
         jdbcURl = readServerProperties(SHARING_REG_JDBC_URL);

http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/server/ServerMain.java
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/server/ServerMain.java
b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/server/ServerMain.java
index fbe5443..db5ee0b 100644
--- a/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/server/ServerMain.java
+++ b/modules/sharing-registry/sharing-registry-server/src/main/java/org/apache/airavata/sharing/registry/server/ServerMain.java
@@ -67,7 +67,6 @@ public class ServerMain {
             SHARING_REGISTRY_HOME = "/tmp";
         else
             SHARING_REGISTRY_HOME = SHARING_REGISTRY_HOME + "/bin";
-        logger.info(SHARING_REGISTRY_HOME);
         return new File(SHARING_REGISTRY_HOME, serverStartedFileNamePrefix + "_" + Integer.toString(serverPID)).toString();
     }
 

http://git-wip-us.apache.org/repos/asf/airavata/blob/2cb5ebc3/modules/sharing-registry/sharing-registry-server/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServiceTest.java
----------------------------------------------------------------------
diff --git a/modules/sharing-registry/sharing-registry-server/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServiceTest.java
b/modules/sharing-registry/sharing-registry-server/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServiceTest.java
index 47c3001..d289c46 100644
--- a/modules/sharing-registry/sharing-registry-server/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServiceTest.java
+++ b/modules/sharing-registry/sharing-registry-server/src/test/java/org/apache/airavata/sharing/registry/SharingRegistryServiceTest.java
@@ -28,8 +28,11 @@ import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TProtocol;
 import org.apache.thrift.transport.TSocket;
 import org.apache.thrift.transport.TTransport;
+import org.databene.contiperf.PerfTest;
+import org.databene.contiperf.junit.ContiPerfRule;
 import org.junit.Assert;
 import org.junit.BeforeClass;
+import org.junit.Rule;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -40,6 +43,9 @@ import java.util.Arrays;
 public class SharingRegistryServiceTest {
     private final static Logger logger = LoggerFactory.getLogger(SharingRegistryServiceTest.class);
 
+    @Rule
+    public ContiPerfRule i = new ContiPerfRule();
+
     @BeforeClass
     public static void setUp() throws InterruptedException {
         ServerMain serverMain = new ServerMain();
@@ -49,6 +55,7 @@ public class SharingRegistryServiceTest {
 
 
     @Test
+    @PerfTest(invocations = 50, threads = 10)
     public void test() throws TException, InterruptedException {
         String serverHost = "localhost";
         int serverPort = 7878;
@@ -60,7 +67,7 @@ public class SharingRegistryServiceTest {
 
         Domain domain = new Domain();
         //has to be one word
-        domain.setName("test-domain"+System.currentTimeMillis());
+        domain.setName("test-domain" + Math.random());
         //optional
         domain.setDescription("test domain description");
 


Mime
View raw message