Skip to content

Commit 4f1cb99

Browse files
Merge pull request #9665 from spericas/connection-timeouts-master2
[helidon] Should fix some connection issues
2 parents 7aefc37 + f230e8a commit 4f1cb99

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

frameworks/Java/helidon/helidon-nima.dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ COPY --from=maven /helidon/target/benchmark-nima.jar app.jar
1212
EXPOSE 8080
1313

1414
CMD java -XX:+UseNUMA \
15+
-server \
1516
-XX:+UseParallelGC \
1617
-jar app.jar

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnection.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

22
package io.helidon.benchmark.nima.models;
33

4+
import java.util.concurrent.CompletableFuture;
5+
import java.util.concurrent.ExecutionException;
6+
47
import io.vertx.pgclient.PgConnection;
58
import io.vertx.sqlclient.PreparedQuery;
9+
import io.vertx.sqlclient.PreparedStatement;
610
import io.vertx.sqlclient.Row;
711
import io.vertx.sqlclient.RowSet;
812

@@ -11,9 +15,9 @@ public class PgClientConnection implements AutoCloseable {
1115
private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
1216
private static String SELECT_FORTUNE = "SELECT * from FORTUNE";
1317

14-
private PreparedQuery<RowSet<Row>> worldQuery;
15-
private PreparedQuery<RowSet<Row>> fortuneQuery;
16-
private PreparedQuery<RowSet<Row>>[] updateQuery;
18+
private CompletableFuture<PreparedStatement> worldQuery;
19+
private CompletableFuture<PreparedStatement> fortuneQuery;
20+
private CompletableFuture<PreparedStatement>[] updateQuery;
1721

1822
private final PgConnection conn;
1923

@@ -30,30 +34,28 @@ public void close() {
3034
conn.close();
3135
}
3236

33-
public PreparedQuery<RowSet<Row>> worldQuery() {
34-
return worldQuery;
37+
public PreparedQuery<RowSet<Row>> worldQuery() throws ExecutionException, InterruptedException {
38+
return worldQuery.get().query();
3539
}
3640

37-
public PreparedQuery<RowSet<Row>> fortuneQuery() {
38-
return fortuneQuery;
41+
public PreparedQuery<RowSet<Row>> fortuneQuery() throws ExecutionException, InterruptedException {
42+
return fortuneQuery.get().query();
3943
}
4044

41-
public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) {
42-
return updateQuery[queryCount - 1];
45+
public PreparedQuery<RowSet<Row>> updateQuery(int queryCount) throws ExecutionException, InterruptedException {
46+
return updateQuery[queryCount - 1].get().query();
4347
}
4448

4549
@SuppressWarnings("unchecked")
4650
void prepare() {
4751
try {
48-
worldQuery = conn.prepare(SELECT_WORLD)
49-
.toCompletionStage().toCompletableFuture().get().query();
50-
fortuneQuery = conn.prepare(SELECT_FORTUNE)
51-
.toCompletionStage().toCompletableFuture().get().query();
52-
updateQuery = (PreparedQuery<RowSet<Row>>[]) new PreparedQuery<?>[UPDATE_QUERIES];
52+
worldQuery = conn.prepare(SELECT_WORLD).toCompletionStage().toCompletableFuture();
53+
fortuneQuery = conn.prepare(SELECT_FORTUNE).toCompletionStage().toCompletableFuture();
54+
updateQuery = (CompletableFuture<PreparedStatement>[]) new CompletableFuture<?>[UPDATE_QUERIES];
5355
for (int i = 0; i < UPDATE_QUERIES; i++) {
5456
updateQuery[i] = conn.prepare(singleUpdate(i + 1))
55-
.toCompletionStage().toCompletableFuture().get().query();
56-
}
57+
.toCompletionStage().toCompletableFuture();
58+
}
5759
} catch (Exception e) {
5860
throw new RuntimeException(e);
5961
}

frameworks/Java/helidon/nima/src/main/java/io/helidon/benchmark/nima/models/PgClientConnectionPoolArray.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
package io.helidon.benchmark.nima.models;
33

4-
import java.util.concurrent.locks.ReentrantLock;
4+
import java.util.concurrent.locks.ReadWriteLock;
5+
import java.util.concurrent.locks.ReentrantReadWriteLock;
56
import java.util.logging.Logger;
67

78
import io.helidon.config.Config;
@@ -13,7 +14,7 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
1314

1415
private final int connections;
1516
private final PgClientConnection[] connectionArray;
16-
private final ReentrantLock lock = new ReentrantLock();
17+
private final ReadWriteLock lock = new ReentrantReadWriteLock();
1718

1819
PgClientConnectionPoolArray(Vertx vertx, PgConnectOptions options, Config config) {
1920
super(vertx, options, config);
@@ -29,20 +30,23 @@ class PgClientConnectionPoolArray extends PgClientConnectionPool {
2930
@Override
3031
public PgClientConnection clientConnection() {
3132
int index = Thread.currentThread().hashCode() % connections;
32-
PgClientConnection connection = connectionArray[index];
33-
if (connection == null) {
34-
try {
35-
lock.lock();
36-
connection = connectionArray[index];
37-
if (connection == null) {
38-
connection = newConnection();
39-
connectionArray[index] = connection;
33+
if (connectionArray[index] == null) {
34+
lock.readLock().lock();
35+
if (connectionArray[index] == null) {
36+
lock.readLock().unlock();
37+
lock.writeLock().lock();
38+
try {
39+
if (connectionArray[index] == null) {
40+
connectionArray[index] = newConnection();
41+
}
42+
} finally {
43+
lock.writeLock().unlock();
4044
}
41-
} finally {
42-
lock.unlock();
45+
} else {
46+
lock.readLock().unlock();
4347
}
4448
}
45-
return connection;
49+
return connectionArray[index];
4650
}
4751

4852
@Override

0 commit comments

Comments
 (0)