Prepared Statement initialization lock

This commit is contained in:
Andrii Shvaika 2021-04-14 13:49:09 +03:00
parent ae5632f8ab
commit 123457f8eb

View File

@ -62,6 +62,8 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal;
@ -107,6 +109,7 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
private PreparedStatement[] fetchStmtsDesc;
private PreparedStatement deleteStmt;
private PreparedStatement deletePartitionStmt;
private final Lock stmtCreationLock = new ReentrantLock();
private boolean isInstall() {
return environment.acceptsProfiles(Profiles.of("install"));
@ -544,6 +547,9 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
}
private PreparedStatement getDeleteStmt() {
if (deleteStmt == null) {
stmtCreationLock.lock();
try {
if (deleteStmt == null) {
deleteStmt = prepare("DELETE FROM " + ModelConstants.TS_KV_CF +
" WHERE " + ModelConstants.ENTITY_TYPE_COLUMN + EQUALS_PARAM
@ -553,6 +559,10 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
+ "AND " + ModelConstants.TS_COLUMN + " >= ? "
+ "AND " + ModelConstants.TS_COLUMN + " < ?");
}
} finally {
stmtCreationLock.unlock();
}
}
return deleteStmt;
}
@ -584,6 +594,9 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
}
private PreparedStatement getDeletePartitionStmt() {
if (deletePartitionStmt == null) {
stmtCreationLock.lock();
try {
if (deletePartitionStmt == null) {
deletePartitionStmt = prepare("DELETE FROM " + ModelConstants.TS_KV_PARTITIONS_CF +
" WHERE " + ModelConstants.ENTITY_TYPE_COLUMN + EQUALS_PARAM
@ -591,10 +604,17 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
+ "AND " + ModelConstants.PARTITION_COLUMN + EQUALS_PARAM
+ "AND " + ModelConstants.KEY_COLUMN + EQUALS_PARAM);
}
} finally {
stmtCreationLock.unlock();
}
}
return deletePartitionStmt;
}
private PreparedStatement getSaveStmt(DataType dataType) {
if (saveStmts == null) {
stmtCreationLock.lock();
try {
if (saveStmts == null) {
saveStmts = new PreparedStatement[DataType.values().length];
for (DataType type : DataType.values()) {
@ -608,10 +628,17 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
" VALUES(?, ?, ?, ?, ?, ?)");
}
}
} finally {
stmtCreationLock.unlock();
}
}
return saveStmts[dataType.ordinal()];
}
private PreparedStatement getSaveTtlStmt(DataType dataType) {
if (saveTtlStmts == null) {
stmtCreationLock.lock();
try {
if (saveTtlStmts == null) {
saveTtlStmts = new PreparedStatement[DataType.values().length];
for (DataType type : DataType.values()) {
@ -625,10 +652,17 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
" VALUES(?, ?, ?, ?, ?, ?) USING TTL ?");
}
}
} finally {
stmtCreationLock.unlock();
}
}
return saveTtlStmts[dataType.ordinal()];
}
private PreparedStatement getPartitionInsertStmt() {
if (partitionInsertStmt == null) {
stmtCreationLock.lock();
try {
if (partitionInsertStmt == null) {
partitionInsertStmt = prepare(INSERT_INTO + ModelConstants.TS_KV_PARTITIONS_CF +
"(" + ModelConstants.ENTITY_TYPE_COLUMN +
@ -637,10 +671,17 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
"," + ModelConstants.KEY_COLUMN + ")" +
" VALUES(?, ?, ?, ?)");
}
} finally {
stmtCreationLock.unlock();
}
}
return partitionInsertStmt;
}
private PreparedStatement getPartitionInsertTtlStmt() {
if (partitionInsertTtlStmt == null) {
stmtCreationLock.lock();
try {
if (partitionInsertTtlStmt == null) {
partitionInsertTtlStmt = prepare(INSERT_INTO + ModelConstants.TS_KV_PARTITIONS_CF +
"(" + ModelConstants.ENTITY_TYPE_COLUMN +
@ -649,6 +690,10 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
"," + ModelConstants.KEY_COLUMN + ")" +
" VALUES(?, ?, ?, ?) USING TTL ?");
}
} finally {
stmtCreationLock.unlock();
}
}
return partitionInsertTtlStmt;
}
@ -712,14 +757,28 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD
private PreparedStatement getFetchStmt(Aggregation aggType, String orderBy) {
switch (orderBy) {
case ASC_ORDER:
if (fetchStmtsAsc == null) {
stmtCreationLock.lock();
try {
if (fetchStmtsAsc == null) {
fetchStmtsAsc = initFetchStmt(orderBy);
}
} finally {
stmtCreationLock.unlock();
}
}
return fetchStmtsAsc[aggType.ordinal()];
case DESC_ORDER:
if (fetchStmtsDesc == null) {
stmtCreationLock.lock();
try {
if (fetchStmtsDesc == null) {
fetchStmtsDesc = initFetchStmt(orderBy);
}
} finally {
stmtCreationLock.unlock();
}
}
return fetchStmtsDesc[aggType.ordinal()];
default:
throw new RuntimeException("Not supported" + orderBy + "order!");