Browse Source

Fix dropwizard postgres (#5371)

* Remove transactional + add unique random ids

* Fix missing queries in update

* remove broken tag

* Delete Test.java

* Fix typo!!!
jcheron 5 years ago
parent
commit
df0ae319ac

+ 1 - 1
frameworks/Java/dropwizard/benchmark_config.json

@@ -64,7 +64,7 @@
       "display_name": "dropwizard",
       "notes": "dropwizard PostgreSQL using hibernate",
       "versus": "servlet-postgres-raw",
-      "tags": ["broken"]
+      "tags": []
     },
     "jdbi-postgres": {
       "db_url": "/db",

+ 9 - 8
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/FortuneHibernateImpl.java

@@ -1,7 +1,5 @@
 package com.example.helloworld.db.hibernate;
 
-import io.dropwizard.hibernate.AbstractDAO;
-
 import java.util.List;
 
 import org.hibernate.SessionFactory;
@@ -10,13 +8,16 @@ import org.hibernate.query.Query;
 import com.example.helloworld.db.FortuneDAO;
 import com.example.helloworld.db.model.Fortune;
 
+import io.dropwizard.hibernate.AbstractDAO;
+
 public class FortuneHibernateImpl extends AbstractDAO<Fortune> implements FortuneDAO {
 
-    public FortuneHibernateImpl(SessionFactory factory) {
-        super(factory);
-    }
+	public FortuneHibernateImpl(SessionFactory factory) {
+		super(factory);
+	}
 
-    public List<Fortune> list() {
-        return list((Query<Fortune>) query("SELECT f FROM Fortune f"));
-    }
+	@Override
+	public List<Fortune> list() {
+		return list((Query<Fortune>) query("SELECT f FROM Fortune f"));
+	}
 }

+ 24 - 8
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/db/hibernate/WorldHibernateImpl.java

@@ -1,7 +1,5 @@
 package com.example.helloworld.db.hibernate;
 
-import io.dropwizard.hibernate.AbstractDAO;
-
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 
@@ -9,11 +7,14 @@ import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.World;
 import com.example.helloworld.resources.Helper;
 
+import io.dropwizard.hibernate.AbstractDAO;
+
 public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
 	public WorldHibernateImpl(SessionFactory factory) {
 		super(factory);
 	}
 
+	@Override
 	public World findById(int id) {
 		return get(id);
 	}
@@ -21,18 +22,24 @@ public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
 	@Override
 	public World[] findById(int[] ids) {
 		World[] worlds = new World[ids.length];
-		for(int i = 0; i < ids.length; i++) {
+		for (int i = 0; i < ids.length; i++) {
 			worlds[i] = get(ids[i]);
 		}
 		return worlds;
 	}
-	
+
+	@Override
 	public World findAndModify(int id, int newRandomNumber) {
 		final World world = get(id);
 		world.setRandomNumber(newRandomNumber);
 		return persist(world);
 	}
 
+	private World modify(World world, int newRandomNumber) {
+		world.setRandomNumber(newRandomNumber);
+		return persist(world);
+	}
+
 	/**
 	 * Using manual transaction handling and JDBC batch updates
 	 */
@@ -44,9 +51,17 @@ public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
 		try {
 			txn = currentSession().beginTransaction();
 
-			// using write batching. See the data source properties provided in the configuration .yml file
-			for (int i = 0; i < totalQueries; i++) {
-				worlds[i] = findAndModify(Helper.randomWorld(), Helper.randomWorld());
+			// using write batching. See the data source properties provided in the
+			// configuration .yml file
+			int[] ids = Helper.getRandomInts(totalQueries);
+			int i = 0;
+			for (int id : ids) {
+				int newNumber;
+				World world = findById(id);
+				do {
+					newNumber = Helper.randomWorld();
+				} while (world.getRandomNumber() == newNumber);
+				worlds[i++] = modify(world, newNumber);
 			}
 			currentSession().flush();
 			currentSession().clear();
@@ -56,7 +71,8 @@ public class WorldHibernateImpl extends AbstractDAO<World> implements WorldDAO {
 				txn.rollback();
 			throw e;
 		}
-		// The cleaning of the session should happen in the dropwizard provided aspect code
+		// The cleaning of the session should happen in the dropwizard provided aspect
+		// code
 
 		return worlds;
 	}

+ 1 - 1
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/FortuneResource.java

@@ -25,7 +25,7 @@ public class FortuneResource {
 	}
 
 	@GET
-	@UnitOfWork(transactional = false) // Needed only for Hibernate - not for Mongo or JDBI
+	@UnitOfWork // Needed only for Hibernate - not for Mongo or JDBI
 	public FortuneView dbTest() {
 		final List<Fortune> fortunes = fortuneDAO.list();
 

+ 29 - 23
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/Helper.java

@@ -4,31 +4,37 @@ import java.util.Optional;
 import java.util.concurrent.ThreadLocalRandom;
 
 /**
- * Provides utility methods for the benchmark tests.
- * Taken from undertow-edge project.
+ * Provides utility methods for the benchmark tests. Taken from undertow-edge
+ * project.
  */
 public final class Helper {
-    private Helper() {
-        throw new AssertionError();
-    }
+	private final static int MAX_RANDOM_NUMBER = 10000;
 
-    static int getQueries(Optional<String> queries) {
-        if (!queries.isPresent()) {
-            return 1;
-        }
-        return getQueries(queries.get());
-    }
+	private Helper() {
+		throw new AssertionError();
+	}
 
-    static int getQueries(String queries) {
-    	try {
-            int parsedValue = Integer.parseInt(queries);
-            return Math.min(500, Math.max(1, parsedValue));
-        } catch (NumberFormatException e) {
-            return 1;
-        }
-    }
-    
-    public static int randomWorld() {
-        return 1 + ThreadLocalRandom.current().nextInt(10000);
-    }
+	static int getQueries(Optional<String> queries) {
+		if (!queries.isPresent()) {
+			return 1;
+		}
+		return getQueries(queries.get());
+	}
+
+	static int getQueries(String queries) {
+		try {
+			int parsedValue = Integer.parseInt(queries);
+			return Math.min(500, Math.max(1, parsedValue));
+		} catch (NumberFormatException e) {
+			return 1;
+		}
+	}
+
+	public static int randomWorld() {
+		return 1 + ThreadLocalRandom.current().nextInt(MAX_RANDOM_NUMBER);
+	}
+
+	public static int[] getRandomInts(int count) {
+		return ThreadLocalRandom.current().ints(1, MAX_RANDOM_NUMBER + 1).distinct().limit(count).sorted().toArray();
+	}
 }

+ 7 - 11
frameworks/Java/dropwizard/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -2,8 +2,6 @@ package com.example.helloworld.resources;
 
 import java.util.Optional;
 
-import io.dropwizard.hibernate.UnitOfWork;
-
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
@@ -13,6 +11,8 @@ import javax.ws.rs.core.MediaType;
 import com.example.helloworld.db.WorldDAO;
 import com.example.helloworld.db.model.World;
 
+import io.dropwizard.hibernate.UnitOfWork;
+
 @Path("/db")
 @Produces(MediaType.APPLICATION_JSON)
 public class WorldResource {
@@ -21,23 +21,19 @@ public class WorldResource {
 	public WorldResource(WorldDAO worldDAO) {
 		this.worldDAO = worldDAO;
 	}
-	
+
 	@GET
-	@UnitOfWork(transactional = false, readOnly = true) // Needed only for Hibernate - not for Mongo or JDBI
+	@UnitOfWork(readOnly = true) // Needed only for Hibernate - not for Mongo or JDBI
 	public Object db() {
 		return worldDAO.findById(Helper.randomWorld());
 	}
-	
+
 	@GET
 	@Path("/query")
-	@UnitOfWork(transactional = false,  readOnly = true) // Needed only for Hibernate - not for Mongo or JDBI
+	@UnitOfWork(readOnly = true) // Needed only for Hibernate - not for Mongo or JDBI
 	public Object query(@QueryParam("queries") String queries) {
 		int totalQueries = Helper.getQueries(queries); // Optional check is done inside
-		int[] ids = new int[totalQueries];
-		for (int i = 0; i < totalQueries; i++) {
-			ids[i] = Helper.randomWorld();
-		}
-		return worldDAO.findById(ids);
+		return worldDAO.findById(Helper.getRandomInts(totalQueries));
 	}
 
 	@GET