Quellcode durchsuchen

Make Java/RestExpress to conform to #4622 (#4632)

* Add check to distinguish between db and multiple queries

* Handle non-numeric parameter value

* Add separate implementations for db and query tests for MySQL

* Disable SSL for MySQL connections

* Add separate handlers for db and queries for MongoDB
Radoslav Petrov vor 6 Jahren
Ursprung
Commit
1d83a3a670

+ 4 - 4
frameworks/Java/restexpress/benchmark_config.json

@@ -4,8 +4,8 @@
     "default": {
       "json_url": "/restexpress/json",
       "plaintext_url": "/restexpress/plaintext",
-      "db_url": "/restexpress/mongodb",
-      "query_url": "/restexpress/mongodb?queries=",
+      "db_url": "/restexpress/mongo/db",
+      "query_url": "/restexpress/mongo/query?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Micro",
@@ -23,8 +23,8 @@
       "versus": "netty"
     },
     "mysql-raw": {
-      "db_url": "/restexpress/mysql",
-      "query_url": "/restexpress/mysql?queries=",
+      "db_url": "/restexpress/mysql/db",
+      "query_url": "/restexpress/mysql/query?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Micro",

+ 1 - 1
frameworks/Java/restexpress/config/dev/environment.properties

@@ -6,7 +6,7 @@ executor.threadPool.size = 300
 mongodb.uri = mongodb://tfb-database:27017/hello_world?maxPoolSize=300
 
 # A MySQL URI/Connection string
-mysql.uri = jdbc:mysql://tfb-database:3306/hello_world
+mysql.uri = jdbc:mysql://tfb-database:3306/hello_world?useSSL=false
 
 # MySQL useConfigs value, See section "21.3.5.1.1. Properties Files for the useConfigs Option" of:
 # http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html

+ 21 - 24
frameworks/Java/restexpress/src/main/java/hello/Main.java

@@ -14,44 +14,41 @@ import org.jboss.netty.handler.codec.http.HttpMethod;
 import com.strategicgains.restexpress.RestExpress;
 import com.strategicgains.restexpress.util.Environment;
 
-public class Main
-{
-	public static void main(String[] args) throws Exception
-	{
+public class Main {
+	public static void main(String[] args) throws Exception {
 		Configuration config = loadEnvironment(args);
-		RestExpress server = new RestExpress()
-			.setName("RestExpress Benchmark")
-		    .setExecutorThreadCount(config.getExecutorThreadPoolSize())
-		    .alias("HelloWorld", HelloWorld.class);
+		RestExpress server = new RestExpress().setName("RestExpress Benchmark")
+				.setExecutorThreadCount(config.getExecutorThreadPoolSize())
+				.alias("HelloWorld", HelloWorld.class);
 
-		server.uri("/restexpress/json", config.getJsonController())
-			.action("helloWorld", HttpMethod.GET);
+		server.uri("/restexpress/json", config.getJsonController()).action("helloWorld",
+				HttpMethod.GET);
 
-                server.uri("/restexpress/plaintext", config.getPlaintextController())
-                        .action("helloWorld", HttpMethod.GET)
-                        .noSerialization();
+		server.uri("/restexpress/plaintext", config.getPlaintextController())
+				.action("helloWorld", HttpMethod.GET).noSerialization();
 
-		server.uri("/restexpress/mysql", config.getMysqlController())
-			.method(HttpMethod.GET);
+		server.uri("/restexpress/mysql/db", config.getDbMysqlController()).method(HttpMethod.GET);
 
-		server.uri("/restexpress/mongodb", config.getMongodbController())
-		    .method(HttpMethod.GET);
+		server.uri("/restexpress/mysql/query", config.getQueriesMysqlController()).method(
+				HttpMethod.GET);
+
+		server.uri("/restexpress/mongo/db", config.getDbMongodbController()).method(HttpMethod.GET);
+		
+		server.uri("/restexpress/mongo/query", config.getQueriesMongodbController()).method(HttpMethod.GET);
 
 		server.addPostprocessor((request, response) -> {
 			response.addHeader("Server", "RestExpress");
-			response.addHeader("Date", DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));
+			response.addHeader("Date",
+					DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));
 		});
 
 		server.bind(config.getPort());
 		server.awaitShutdown();
 	}
 
-	private static Configuration loadEnvironment(String[] args)
-	throws FileNotFoundException,
-	    IOException
-	{
-		if (args.length > 0)
-		{
+	private static Configuration loadEnvironment(String[] args) throws FileNotFoundException,
+			IOException {
+		if (args.length > 0) {
 			return Environment.from(args[0], Configuration.class);
 		}
 

+ 46 - 44
frameworks/Java/restexpress/src/main/java/hello/config/Configuration.java

@@ -1,10 +1,12 @@
 package hello.config;
 
+import hello.controller.DbMongodbController;
+import hello.controller.DbMysqlController;
 import hello.controller.JsonController;
-import hello.controller.MongodbController;
-import hello.controller.MysqlController;
-import hello.controller.persistence.WorldsMongodbRepository;
 import hello.controller.PlaintextController;
+import hello.controller.QueriesMongodbController;
+import hello.controller.QueriesMysqlController;
+import hello.controller.persistence.WorldsMongodbRepository;
 
 import java.util.Properties;
 
@@ -13,9 +15,7 @@ import com.strategicgains.repoexpress.exception.InvalidObjectIdException;
 import com.strategicgains.restexpress.Format;
 import com.strategicgains.restexpress.util.Environment;
 
-public class Configuration
-extends Environment
-{
+public class Configuration extends Environment {
 	private static final String DEFAULT_EXECUTOR_THREAD_POOL_SIZE = "20";
 
 	private static final String PORT_PROPERTY = "port";
@@ -29,76 +29,78 @@ extends Environment
 	private int executorThreadPoolSize;
 
 	private JsonController jsonController;
-	private MysqlController mysqlController;
-	private MongodbController mongodbController;
-        private PlaintextController plaintextController;
+	private DbMysqlController dbMysqlController;
+	private QueriesMysqlController queriesMysqlController;
+	private DbMongodbController dbMongodbController;
+	private QueriesMongodbController queriesMongodbController;
+	private PlaintextController plaintextController;
 
 	@Override
-	protected void fillValues(Properties p)
-	{
+	protected void fillValues(Properties p) {
 		this.port = Integer.parseInt(p.getProperty(PORT_PROPERTY, "8080"));
 		this.defaultFormat = p.getProperty(DEFAULT_FORMAT_PROPERTY, Format.JSON);
 		this.baseUrl = p.getProperty(BASE_URL_PROPERTY, "http://localhost:" + String.valueOf(port));
-		this.executorThreadPoolSize = Integer.parseInt(p.getProperty(EXECUTOR_THREAD_POOL_SIZE, DEFAULT_EXECUTOR_THREAD_POOL_SIZE));
+		this.executorThreadPoolSize = Integer.parseInt(p.getProperty(EXECUTOR_THREAD_POOL_SIZE,
+				DEFAULT_EXECUTOR_THREAD_POOL_SIZE));
 		MongoConfig mongoSettings = new MongoConfig(p);
 		MysqlConfig mysqlSettings = new MysqlConfig(p);
 		initialize(mysqlSettings, mongoSettings);
 	}
 
-	private void initialize(MysqlConfig mysqlSettings, MongoConfig mongo)
-	{
+	private void initialize(MysqlConfig mysqlSettings, MongoConfig mongo) {
 		jsonController = new JsonController();
-                plaintextController = new PlaintextController();
-		mysqlController = new MysqlController(mysqlSettings.getDataSource());
-        WorldsMongodbRepository worldMongodbRepository = new WorldsMongodbRepository(mongo.getClient(), mongo.getDbName());
-        worldMongodbRepository.setIdentifierAdapter(new IdentiferAdapter<Long>()
-		{
+		plaintextController = new PlaintextController();
+		dbMysqlController = new DbMysqlController(mysqlSettings.getDataSource());
+		queriesMysqlController = new QueriesMysqlController(mysqlSettings.getDataSource());
+		WorldsMongodbRepository worldMongodbRepository = new WorldsMongodbRepository(
+				mongo.getClient(), mongo.getDbName());
+		worldMongodbRepository.setIdentifierAdapter(new IdentiferAdapter<Long>() {
 			@Override
-			public Long convert(String id) throws InvalidObjectIdException
-			{
+			public Long convert(String id) throws InvalidObjectIdException {
 				return Long.valueOf(id);
 			}
 		});
-		mongodbController = new MongodbController(worldMongodbRepository);
+		dbMongodbController = new DbMongodbController(worldMongodbRepository);
+		queriesMongodbController = new QueriesMongodbController(worldMongodbRepository);
 	}
 
-	public String getDefaultFormat()
-	{
+	public String getDefaultFormat() {
 		return defaultFormat;
 	}
 
-	public int getPort()
-	{
+	public int getPort() {
 		return port;
 	}
-	
-	public String getBaseUrl()
-	{
+
+	public String getBaseUrl() {
 		return baseUrl;
 	}
-	
-	public int getExecutorThreadPoolSize()
-	{
+
+	public int getExecutorThreadPoolSize() {
 		return executorThreadPoolSize;
 	}
 
-	public JsonController getJsonController()
-	{
+	public JsonController getJsonController() {
 		return jsonController;
 	}
 
-	public MysqlController getMysqlController()
-	{
-		return mysqlController;
+	public DbMysqlController getDbMysqlController() {
+		return dbMysqlController;
 	}
 
-	public MongodbController getMongodbController()
-	{
-		return mongodbController;
+	public QueriesMysqlController getQueriesMysqlController() {
+		return queriesMysqlController;
 	}
 
-        public PlaintextController getPlaintextController()
-        {
-            return plaintextController;
-        }
+	public DbMongodbController getDbMongodbController() {
+		return dbMongodbController;
+	}
+	
+	public QueriesMongodbController getQueriesMongodbController() {
+		return queriesMongodbController;
+	}
+
+	public PlaintextController getPlaintextController() {
+		return plaintextController;
+	}
 }

+ 27 - 0
frameworks/Java/restexpress/src/main/java/hello/controller/DbMongodbController.java

@@ -0,0 +1,27 @@
+package hello.controller;
+
+import hello.controller.persistence.WorldsMongodbRepository;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+import com.strategicgains.restexpress.Request;
+import com.strategicgains.restexpress.Response;
+
+public class DbMongodbController {
+	// Database details.
+	private static final int DB_ROWS = 10000;
+
+	private WorldsMongodbRepository worldRepo;
+
+	public DbMongodbController(WorldsMongodbRepository worldsRepository) {
+		super();
+		this.worldRepo = worldsRepository;
+	}
+
+	public Object read(Request request, Response response) {
+		// Fetch some rows from the database.
+		final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
+
+		return worldRepo.find(random);
+	}
+}

+ 47 - 0
frameworks/Java/restexpress/src/main/java/hello/controller/DbMysqlController.java

@@ -0,0 +1,47 @@
+package hello.controller;
+
+import hello.domain.World;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.concurrent.ThreadLocalRandom;
+
+import javax.sql.DataSource;
+
+import com.strategicgains.restexpress.Request;
+import com.strategicgains.restexpress.Response;
+
+public class DbMysqlController {
+	// Database details.
+	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
+	private static final int DB_ROWS = 10000;
+
+	private DataSource mysqlDataSource;
+
+	public DbMysqlController(DataSource dataSource) {
+		super();
+		this.mysqlDataSource = dataSource;
+	}
+
+	public Object read(Request request, Response response) throws SQLException {
+		final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
+
+		World world = null;
+
+		// Fetch some rows from the database.
+		try (Connection conn = mysqlDataSource.getConnection()) {
+			try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
+					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
+				statement.setInt(1, random);
+				try (ResultSet results = statement.executeQuery()) {
+					results.next(); // Here the expectation is ONLY one
+									// result row
+					world = new World(results.getLong("id"), results.getInt("randomNumber"));
+				}
+			}
+		}
+		return world;
+	}
+}

+ 0 - 64
frameworks/Java/restexpress/src/main/java/hello/controller/MongodbController.java

@@ -1,64 +0,0 @@
-package hello.controller;
-
-import hello.controller.persistence.WorldsMongodbRepository;
-import hello.domain.World;
-
-import java.util.Random;
-import java.util.concurrent.ThreadLocalRandom;
-
-import com.strategicgains.restexpress.Request;
-import com.strategicgains.restexpress.Response;
-
-public class MongodbController
-{
-	// Database details.
-	private static final int DB_ROWS = 10000;
-
-	private WorldsMongodbRepository worldRepo;
-
-	public MongodbController(WorldsMongodbRepository worldsRepository)
-	{
-		super();
-		this.worldRepo = worldsRepository;
-	}
-
-	public Object read(Request request, Response response)
-	{
-    // Get the count of queries to run.
-    int count = 1;
-    try
-    {
-      count = Integer.parseInt(request.getHeader("queries"));
-
-      // Bounds check.
-      if (count > 500)
-      {
-        count = 500;
-      }
-      if (count < 1)
-      {
-        count = 1;
-      }
-    }
-    catch(NumberFormatException nfexc)
-    {
-      // do nothing
-    }
-
-		// Fetch some rows from the database.
-		final World[] worlds = new World[count];
-		final Random random = ThreadLocalRandom.current();
-
-		for (int i = 0; i < count; i++)
-		{
-			worlds[i] = worldRepo.find(random.nextInt(DB_ROWS) + 1);
-		}
-
-		if (count == 1)
-		{
-			return worlds[0];
-		}
-
-		return worlds;
-	}
-}

+ 0 - 86
frameworks/Java/restexpress/src/main/java/hello/controller/MysqlController.java

@@ -1,86 +0,0 @@
-package hello.controller;
-
-import hello.domain.World;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Random;
-import java.util.concurrent.ThreadLocalRandom;
-
-import javax.sql.DataSource;
-
-import com.strategicgains.restexpress.Request;
-import com.strategicgains.restexpress.Response;
-
-public class MysqlController
-{
-	// Database details.
-	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
-	private static final int DB_ROWS = 10000;
-
-	private DataSource mysqlDataSource;
-
-	public MysqlController(DataSource dataSource)
-	{
-		super();
-		this.mysqlDataSource = dataSource;
-	}
-
-	public Object read(Request request, Response response)
-	throws SQLException
-	{
-		final DataSource source = mysqlDataSource;
-
-		// Get the count of queries to run.
-		int count = 1;
-		try
-		{
-			count = Integer.parseInt(request.getHeader("queries"));
-		
-			// Bounds check.
-			if (count > 500)
-			{
-				count = 500;
-			}
-			if (count < 1)
-			{
-				count = 1;
-			}
-		}
-		catch(NumberFormatException nfexc)
-		{
-			// do nothing
-		}
-
-		// Fetch some rows from the database.
-		final World[] worlds = new World[count];
-		final Random random = ThreadLocalRandom.current();
-
-		Connection conn = source.getConnection();
-		PreparedStatement statement = conn.prepareStatement(DB_QUERY, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
-
-		// Run the query the number of times requested.
-		for (int i = 0; i < count; i++)
-		{
-			final Long id = (long) (random.nextInt(DB_ROWS) + 1);
-			statement.setLong(1, id);
-
-			try (ResultSet results = statement.executeQuery())
-			{
-				if (results.next())
-				{
-					worlds[i] = new World(id, results.getInt("randomNumber"));
-				}
-			}
-		}
-
-		if (count == 1)
-		{
-			return worlds[0];
-		}
-
-		return worlds;
-	}
-}

+ 48 - 0
frameworks/Java/restexpress/src/main/java/hello/controller/QueriesMongodbController.java

@@ -0,0 +1,48 @@
+package hello.controller;
+
+import hello.controller.persistence.WorldsMongodbRepository;
+import hello.domain.World;
+
+import java.util.concurrent.ThreadLocalRandom;
+
+import com.strategicgains.restexpress.Request;
+import com.strategicgains.restexpress.Response;
+
+public class QueriesMongodbController {
+	// Database details.
+	private static final int DB_ROWS = 10000;
+
+	private WorldsMongodbRepository worldRepo;
+
+	public QueriesMongodbController(WorldsMongodbRepository worldsRepository) {
+		super();
+		this.worldRepo = worldsRepository;
+	}
+
+	public Object read(Request request, Response response) {
+		// Get the count of queries to run.
+		int count = 1;
+		try {
+			count = Integer.parseInt(request.getHeader("queries"));
+
+			// Bounds check.
+			if (count > 500) {
+				count = 500;
+			}
+			if (count < 1) {
+				count = 1;
+			}
+		} catch (NumberFormatException nfexc) {
+			// do nothing
+		}
+
+		// Fetch some rows from the database.
+		final World[] worlds = new World[count];
+		final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
+		for (int i = 0; i < count; i++) {
+			worlds[i] = worldRepo.find(random);
+		}
+
+		return worlds;
+	}
+}

+ 67 - 0
frameworks/Java/restexpress/src/main/java/hello/controller/QueriesMysqlController.java

@@ -0,0 +1,67 @@
+package hello.controller;
+
+import hello.domain.World;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.concurrent.ThreadLocalRandom;
+
+import javax.sql.DataSource;
+
+import com.strategicgains.restexpress.Request;
+import com.strategicgains.restexpress.Response;
+
+public class QueriesMysqlController
+{
+	// Database details.
+	private static final String DB_QUERY = "SELECT * FROM World WHERE id = ?";
+	private static final int DB_ROWS = 10000;
+
+	private DataSource mysqlDataSource;
+
+	public QueriesMysqlController(DataSource dataSource)
+	{
+		super();
+		this.mysqlDataSource = dataSource;
+	}
+
+	public Object read(Request request, Response response)
+	throws SQLException
+	{
+		int count = 1;
+		try {
+			count = Integer.parseInt(request.getHeader("queries"));
+			// Bounds checks
+			if (count > 500) {
+				count = 500;
+			} else if (count < 1) {
+				count = 1;
+			}
+		} catch (NumberFormatException nfexc) {
+			//do nothing
+		}		
+		final World[] worlds = new World[count];
+		final int random = 1 + ThreadLocalRandom.current().nextInt(DB_ROWS);
+
+		// Fetch some rows from the database.
+		try (Connection conn = mysqlDataSource.getConnection()) {
+			try (PreparedStatement statement = conn.prepareStatement(DB_QUERY,
+					ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
+				// Run the query the number of times requested.
+				for (int i = 0; i < count; i++) {
+					final int id = random;
+					statement.setInt(1, id);
+
+					try (ResultSet results = statement.executeQuery()) {
+						results.next();
+						worlds[i] = new World(new Long(id), results.getInt("randomNumber"));
+					}
+				}
+			}
+		}
+
+		return worlds;
+	}
+}