Skip to content

Commit 10e7ea7

Browse files
committed
use hash for better transaction table partitioning
1 parent 038c0d3 commit 10e7ea7

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

jdbc/src/main/java/tech/ydb/jdbc/context/QueryServiceExecutorExt.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ public QueryServiceExecutorExt(YdbContext ctx, int transactionLevel, boolean aut
3131
}
3232

3333
private Status upsertAndCommit(QueryTransaction localTx) {
34-
String sql = "DECLARE $trans_id AS Text; "
34+
String sql = "DECLARE $trans_hash AS Int32; DECLARE $trans_id AS Text; "
3535
+ "UPSERT INTO `" + processUndeterminedTable
36-
+ "` (trans_id, trans_tv) VALUES ($trans_id, CurrentUtcTimestamp());";
37-
Params params = Params.of("$trans_id", PrimitiveValue.newText(localTx.getId()));
36+
+ "` (trans_hash, trans_id, trans_tv) "
37+
+ "VALUES ($trans_hash, $trans_id, CurrentUtcTimestamp());";
38+
Params params = Params.of(
39+
"$trans_id", PrimitiveValue.newText(localTx.getId()),
40+
"$trans_hash", PrimitiveValue.newInt32(localTx.getId().hashCode())
41+
);
3842
return localTx.createQueryWithCommit(sql, params)
3943
.execute()
4044
.join()
@@ -43,10 +47,13 @@ private Status upsertAndCommit(QueryTransaction localTx) {
4347

4448
private boolean checkTransaction(YdbContext ctx, String transId,
4549
YdbValidator validator, YdbTracer tracer) throws SQLException {
46-
String sql = "DECLARE $trans_id AS Text; "
50+
String sql = "DECLARE $trans_hash AS Int32; DECLARE $trans_id AS Text; "
4751
+ "SELECT trans_id, trans_tv FROM `" + processUndeterminedTable
48-
+ "` WHERE trans_id=$trans_id;";
49-
Params params = Params.of("$trans_id", PrimitiveValue.newText(transId));
52+
+ "` WHERE trans_hash=$trans_hash AND trans_id=$trans_id;";
53+
Params params = Params.of(
54+
"$trans_id", PrimitiveValue.newText(transId),
55+
"$trans_hash", PrimitiveValue.newInt32(transId.hashCode())
56+
);
5057
Result<DataQueryResult> result = ctx.getRetryCtx().supplyResult(
5158
session -> session.executeDataQuery(sql, TxControl.onlineRo(), params))
5259
.join();

jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,13 @@ public void ensureTransactionTableExists() throws SQLException {
355355
}
356356
LOGGER.log(Level.FINE, "Using table {} for UNDETERMINED processing", tableName);
357357
String sqlCreate = "CREATE TABLE IF NOT EXISTS `" + tableName
358-
+ "` (trans_id Text NOT NULL, trans_tv Timestamp,"
359-
+ " PRIMARY KEY (trans_id)) WITH ("
358+
+ "` (trans_hash Int32 NOT NULL, trans_id Text NOT NULL, trans_tv Timestamp,"
359+
+ " PRIMARY KEY (trans_hash, trans_id)) WITH ("
360360
+ "TTL=Interval('PT60M') ON trans_tv,"
361+
/*
361362
+ "AUTO_PARTITIONING_MIN_PARTITIONS_COUNT=100,"
362363
+ "AUTO_PARTITIONING_MAX_PARTITIONS_COUNT=150,"
364+
*/
363365
+ "AUTO_PARTITIONING_BY_LOAD=ENABLED,"
364366
+ "AUTO_PARTITIONING_BY_SIZE=ENABLED,"
365367
+ "AUTO_PARTITIONING_PARTITION_SIZE_MB=100"

0 commit comments

Comments
 (0)