Browse Source

Merge pull request #9665 from spericas/connection-timeouts-master2

[helidon] Should fix some connection issues
Mike Smith 4 months ago
parent
commit
4f1cb99e9a

+ 1 - 0
frameworks/Java/helidon/helidon-nima.dockerfile

@@ -12,5 +12,6 @@ COPY --from=maven /helidon/target/benchmark-nima.jar app.jar
 EXPOSE 8080
 
 CMD java -XX:+UseNUMA \
+    -server \
     -XX:+UseParallelGC \
     -jar app.jar

+ 18 - 16
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnection.java

@@ -1,8 +1,12 @@
 
 package io.helidon.benchmark.nima.models;
 
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
 import io.vertx.pgclient.PgConnection;
 import io.vertx.sqlclient.PreparedQuery;
+import io.vertx.sqlclient.PreparedStatement;
 import io.vertx.sqlclient.Row;
 import io.vertx.sqlclient.RowSet;
 
@@ -11,9 +15,9 @@ public class PgClientConnection implements AutoCloseable {
     private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
     private static String SELECT_FORTUNE = "SELECT * from FORTUNE";
 
-    private PreparedQuery<RowSet<Row>> worldQuery;
-    private PreparedQuery<RowSet<Row>> fortuneQuery;
-    private PreparedQuery<RowSet<Row>>[] updateQuery;
+    private CompletableFuture<PreparedStatement> worldQuery;
+    private CompletableFuture<PreparedStatement> fortuneQuery;
+    private CompletableFuture<PreparedStatement>[] updateQuery;
 
     private final PgConnection conn;
 
@@ -30,30 +34,28 @@ public class PgClientConnection implements AutoCloseable {
         conn.close();
     }
 
-    public PreparedQuery<RowSet<Row>> worldQuery() {
-        return worldQuery;
+    public PreparedQuery<RowSet<Row>> worldQuery() throws ExecutionException, InterruptedException {
+        return worldQuery.get().query();
     }
 
-    public PreparedQuery<RowSet<Row>> fortuneQuery() {
-        return fortuneQuery;
+    public PreparedQuery<RowSet<Row>> fortuneQuery() throws ExecutionException, InterruptedException {
+        return fortuneQuery.get().query();
     }
 
-    public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) {
-        return updateQuery[queryCount - 1];
+    public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) throws ExecutionException, InterruptedException {
+        return updateQuery[queryCount - 1].get().query();
     }
 
     @SuppressWarnings("unchecked")
     void prepare() {
         try {
-            worldQuery = conn.prepare(SELECT_WORLD)
-                    .toCompletionStage().toCompletableFuture().get().query();
-            fortuneQuery = conn.prepare(SELECT_FORTUNE)
-                    .toCompletionStage().toCompletableFuture().get().query();
-            updateQuery = (PreparedQuery<RowSet<Row>>[]) new PreparedQuery<?>[UPDATE_QUERIES];
+            worldQuery = conn.prepare(SELECT_WORLD).toCompletionStage().toCompletableFuture();
+            fortuneQuery = conn.prepare(SELECT_FORTUNE).toCompletionStage().toCompletableFuture();
+            updateQuery = (CompletableFuture<PreparedStatement>[]) new CompletableFuture<?>[UPDATE_QUERIES];
             for (int i = 0; i < UPDATE_QUERIES; i++) {
                 updateQuery[i] = conn.prepare(singleUpdate(i + 1))
-                        .toCompletionStage().toCompletableFuture().get().query();
-            }
+                                     .toCompletionStage().toCompletableFuture();
+                    }
         } catch (Exception e) {
             throw new RuntimeException(e);
         }

+ 17 - 13
frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnectionPoolArray.java

@@ -1,7 +1,8 @@
 
 package io.helidon.benchmark.nima.models;
 
-import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.logging.Logger;
 
 import io.helidon.config.Config;
@@ -13,7 +14,7 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
 
     private final int connections;
     private final PgClientConnection[] connectionArray;
-    private final ReentrantLock lock = new ReentrantLock();
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
 
     PgClientConnectionPoolArray(Vertx vertx, PgConnectOptions options, Config config) {
         super(vertx, options, config);
@@ -29,20 +30,23 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
     @Override
     public PgClientConnection clientConnection() {
         int index = Thread.currentThread().hashCode() % connections;
-        PgClientConnection connection = connectionArray[index];
-        if (connection == null) {
-            try {
-                lock.lock();
-                connection = connectionArray[index];
-                if (connection == null) {
-                    connection = newConnection();
-                    connectionArray[index] = connection;
+        if (connectionArray[index] == null) {
+            lock.readLock().lock();
+            if (connectionArray[index] == null) {
+                lock.readLock().unlock();
+                lock.writeLock().lock();
+                try {
+                    if (connectionArray[index] == null) {
+                        connectionArray[index] = newConnection();
+                    }
+                } finally {
+                    lock.writeLock().unlock();
                 }
-            } finally {
-                lock.unlock();
+            } else {
+                lock.readLock().unlock();
             }
         }
-        return connection;
+        return connectionArray[index];
     }
 
     @Override