Skip to content

Commit 6aa98f8

Browse files
tsegismontdoxlik
andauthored
Pipelining depth grows unbounded when prepared statement is retried with explicit parameter types (#1646) (#1668)
* Increment inflight counter on prepare-with-parametes-types query * test * Fix prepared statement reprepare inflight accounting * Fix prepared statement reprepare inflight accounting * Fix prepared statement reprepare inflight accounting * Update vertx-sql-client-codec/src/main/java/io/vertx/sqlclient/codec/SocketConnectionBase.java --------- Signed-off-by: doxlik <doxlikx@gmail.com> Co-authored-by: doxlik <168666511+doxlik@users.noreply.github.com>
1 parent 121f92a commit 6aa98f8

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.vertx.pgclient;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.ext.unit.Async;
5+
import io.vertx.ext.unit.TestContext;
6+
import io.vertx.pgclient.impl.PgSocketConnection;
7+
import io.vertx.sqlclient.Row;
8+
import io.vertx.sqlclient.RowSet;
9+
import io.vertx.sqlclient.Tuple;
10+
import io.vertx.sqlclient.impl.SqlConnectionInternal;
11+
import org.junit.After;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
15+
public class PreparedStatementReprepareTest extends PgTestBase {
16+
17+
private Vertx vertx;
18+
19+
@Before
20+
public void setup() throws Exception {
21+
super.setup();
22+
vertx = Vertx.vertx();
23+
}
24+
25+
@After
26+
public void tearDown(TestContext ctx) {
27+
vertx.close().onComplete(ctx.asyncAssertSuccess());
28+
}
29+
30+
@Test
31+
public void testReprepareDoesNotMakeInflightNegativeWithEnabledCache(TestContext ctx) {
32+
testReprepareDoesNotMakeInflightNegative(ctx, true);
33+
}
34+
35+
@Test
36+
public void testReprepareDoesNotMakeInflightNegativeWithDisabledCache(TestContext ctx) {
37+
testReprepareDoesNotMakeInflightNegative(ctx, false);
38+
}
39+
40+
private void testReprepareDoesNotMakeInflightNegative(TestContext ctx, boolean cachePreparedStatements) {
41+
Async async = ctx.async();
42+
43+
PgConnectOptions options = new PgConnectOptions(this.options)
44+
.setCachePreparedStatements(cachePreparedStatements);
45+
46+
PgConnection.connect(vertx, options).onComplete(ctx.asyncAssertSuccess(conn -> {
47+
PgSocketConnection socket = (PgSocketConnection) ((SqlConnectionInternal) conn).unwrap();
48+
49+
conn
50+
.preparedQuery("SELECT CONCAT('HELLO ', $1)")
51+
.execute(Tuple.of("WORLD"))
52+
.map(rows -> {
53+
RowSet<Row> result = rows;
54+
55+
ctx.assertEquals(1, result.size());
56+
ctx.assertEquals("HELLO WORLD", result.iterator().next().getString(0));
57+
58+
ctx.assertEquals(
59+
0,
60+
socket.inflight(),
61+
"Inflight count should be zero after reprepare query completion " +
62+
"(cachePreparedStatements=" + cachePreparedStatements + ")"
63+
);
64+
65+
return rows;
66+
})
67+
.eventually(() -> conn.close())
68+
.onComplete(ctx.asyncAssertSuccess(v -> async.complete()));
69+
}));
70+
}
71+
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/SocketConnectionBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public ClientMetrics metrics() {
105105
return metrics;
106106
}
107107

108+
// Visible for testing
109+
public int inflight() {
110+
return inflight;
111+
}
112+
108113
@Override
109114
public TracingPolicy tracingPolicy() {
110115
return connectOptions().getTracingPolicy();
@@ -305,6 +310,8 @@ private PrepareStatementCommand prepareCommand(ExtendedQueryCommand<?> queryCmd,
305310
Throwable cause = ar.cause();
306311
if (queryCmd.autoCommit() && isIndeterminatePreparedStatementError(cause) && !sendParameterTypes) {
307312
ChannelHandlerContext ctx = socket.channelHandlerContext();
313+
// We need to increment inflight because a new prepare command will be submitted
314+
inflight++;
308315
// We cannot cache this prepared statement because it might be executed with another type
309316
ctx.write(prepareCommand(queryCmd, false, true), ctx.voidPromise());
310317
ctx.flush();

0 commit comments

Comments
 (0)