Skip to content

Commit 6521f36

Browse files
committed
New connection pool using carrier threads.
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
1 parent cc9ef0e commit 6521f36

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

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

4+
import java.util.HashMap;
5+
import java.util.Map;
46
import java.util.concurrent.locks.ReentrantLock;
57

68
import io.vertx.core.Vertx;
@@ -12,59 +14,64 @@
1214

1315
class PgClientConnectionPool implements AutoCloseable {
1416

15-
private final int size;
1617
private final Vertx vertx;
1718
private final PgConnectOptions options;
18-
private final PgClientConnection[] connections;
1919
private final ReentrantLock lock = new ReentrantLock();
20+
private final Map<String, PgClientConnection> connectionMap = new HashMap<>();
2021

21-
public PgClientConnectionPool(Vertx vertx, int size, PgConnectOptions options) {
22-
this.size = size;
22+
public PgClientConnectionPool(Vertx vertx, PgConnectOptions options) {
2323
this.vertx = vertx;
2424
this.options = options;
25-
this.connections = new PgClientConnection[size];
2625
}
2726

2827
public PgClientConnection clientConnection() {
29-
int bucket = Thread.currentThread().hashCode() % size;
30-
if (connections[bucket] == null) {
28+
String carrierThread = carrierThread();
29+
PgClientConnection connection = connectionMap.get(carrierThread);
30+
if (connection == null) {
3131
try {
3232
lock.lock();
33-
if (connections[bucket] == null) {
34-
connect(bucket);
33+
connection = connectionMap.get(carrierThread);
34+
if (connection == null) {
35+
connection = newConnection();
36+
connectionMap.put(carrierThread, connection);
3537
}
3638
} finally {
3739
lock.unlock();
3840
}
3941
}
40-
return connections[bucket];
42+
return connection;
4143
}
4244

4345
@Override
4446
public void close() {
4547
try {
46-
for (PgClientConnection connection : connections) {
48+
for (PgClientConnection connection : connectionMap.values()) {
4749
connection.close();
4850
}
4951
} catch (Exception e) {
5052
throw new RuntimeException(e);
5153
}
5254
}
5355

54-
private void connect(int bucket) {
56+
private PgClientConnection newConnection() {
5557
try {
5658
PgConnection conn = PgConnection.connect(vertx, options)
5759
.toCompletionStage().toCompletableFuture().get();
5860
PgClientConnection clientConn = new PgClientConnection(conn);
5961
clientConn.prepare();
60-
connections[bucket] = clientConn;
62+
return clientConn;
6163
} catch (Exception e) {
6264
throw new RuntimeException(e);
6365
}
6466
}
6567

68+
static String carrierThread() {
69+
String threadName = Thread.currentThread().toString();
70+
return threadName.substring(threadName.indexOf('@') + 1);
71+
}
72+
6673
public static class PgClientConnection implements AutoCloseable {
67-
private static final int UPDATE_QUERIES = 500;
74+
static final int UPDATE_QUERIES = 500;
6875
private static String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
6976
private static String SELECT_FORTUNE = "SELECT id, message from FORTUNE";
7077

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.vertx.sqlclient.Tuple;
1616

1717
import static io.helidon.benchmark.nima.models.DbRepository.randomWorldNumber;
18+
import static io.helidon.benchmark.nima.models.PgClientConnectionPool.PgClientConnection.UPDATE_QUERIES;
1819

1920
public class PgClientRepository implements DbRepository {
2021
private static final Logger LOGGER = Logger.getLogger(PgClientRepository.class.getName());
@@ -23,17 +24,24 @@ public class PgClientRepository implements DbRepository {
2324

2425
@SuppressWarnings("unchecked")
2526
public PgClientRepository(Config config) {
26-
Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
27+
VertxOptions vertxOptions = new VertxOptions()
28+
.setPreferNativeTransport(true)
29+
.setBlockedThreadCheckInterval(100000);
30+
Vertx vertx = Vertx.vertx(vertxOptions);
2731
PgConnectOptions connectOptions = new PgConnectOptions()
2832
.setPort(config.get("port").asInt().orElse(5432))
29-
.setCachePreparedStatements(config.get("cache-prepared-statements").asBoolean().orElse(true))
3033
.setHost(config.get("host").asString().orElse("tfb-database"))
3134
.setDatabase(config.get("db").asString().orElse("hello_world"))
3235
.setUser(config.get("username").asString().orElse("benchmarkdbuser"))
3336
.setPassword(config.get("password").asString().orElse("benchmarkdbpass"))
37+
.setCachePreparedStatements(true)
38+
.setPreparedStatementCacheMaxSize(UPDATE_QUERIES + 2)
39+
.setPreparedStatementCacheSqlFilter(s -> true) // cache all
40+
.setTcpNoDelay(true)
41+
.setTcpQuickAck(true)
42+
.setTcpKeepAlive(true)
3443
.setPipeliningLimit(100000);
35-
int sqlPoolSize = config.get("sql-pool-size").asInt().orElse(Runtime.getRuntime().availableProcessors());
36-
connectionPool = new PgClientConnectionPool(vertx, sqlPoolSize, connectOptions);
44+
connectionPool = new PgClientConnectionPool(vertx, connectOptions);
3745
}
3846

3947
@Override

0 commit comments

Comments
 (0)