mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(rds-data): make transaction expiry tests independent of scheduling (#7630)
Use an injectable UTC clock for transaction deadlines. Advance the test clock only after the blocking query starts, then check idle expiry after it finishes. This removes the one-second wall-clock race without changing runtime TTLs. test(rds-data): reuse the shared manual clock Use ManualClock for the transaction concurrency fixture and advance it through its existing duration-based interface. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
1ce40a1c79
commit
cb07f9f773
2 files changed
+32
-13
No files matched your search
@@ -30,6 +30,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
@@ -72,6 +73,7 @@ public class RdsDataService implements Resettable {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RdsDataConnectionFactory connectionFactory;
|
||||
private final Duration transactionTtl;
|
||||
private final Clock clock;
|
||||
private final ConcurrentMap<String, TransactionContext> transactions = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService transactionCleanupExecutor;
|
||||
private final RdsBackendGate backendGate;
|
||||
@@ -120,11 +122,25 @@ public class RdsDataService implements Resettable {
|
||||
RdsBackendGate backendGate,
|
||||
RdsBackendEndpointResolver backendEndpointResolver,
|
||||
boolean databaseResumingExceptionEnabled) {
|
||||
this(resourceResolver, secretsManagerService, objectMapper, connectionFactory, transactionTtl,
|
||||
backendGate, backendEndpointResolver, databaseResumingExceptionEnabled, Clock.systemUTC());
|
||||
}
|
||||
|
||||
RdsDataService(RdsDataResourceResolver resourceResolver,
|
||||
SecretsManagerService secretsManagerService,
|
||||
ObjectMapper objectMapper,
|
||||
RdsDataConnectionFactory connectionFactory,
|
||||
Duration transactionTtl,
|
||||
RdsBackendGate backendGate,
|
||||
RdsBackendEndpointResolver backendEndpointResolver,
|
||||
boolean databaseResumingExceptionEnabled,
|
||||
Clock clock) {
|
||||
this.resourceResolver = resourceResolver;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.objectMapper = objectMapper;
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.transactionTtl = transactionTtl;
|
||||
this.clock = clock;
|
||||
this.backendGate = backendGate;
|
||||
this.backendEndpointResolver = backendEndpointResolver;
|
||||
this.databaseResumingExceptionEnabled = databaseResumingExceptionEnabled;
|
||||
@@ -220,7 +236,8 @@ public class RdsDataService implements Resettable {
|
||||
connection.setAutoCommit(false);
|
||||
String transactionId = UUID.randomUUID().toString();
|
||||
transactions.put(transactionId, new TransactionContext(
|
||||
transactionId, connection, target.engine(), target.arn(), database, region, transactionTtl,
|
||||
transactionId, connection, target.engine(), target.arn(), database, region,
|
||||
clock.instant().plus(transactionTtl),
|
||||
lease));
|
||||
leaseHandedOver = true;
|
||||
|
||||
@@ -307,7 +324,7 @@ public class RdsDataService implements Resettable {
|
||||
requireActiveTransaction(transactionId, tx);
|
||||
return work.execute(tx.connection, tx.engine);
|
||||
} finally {
|
||||
tx.refresh(transactionTtl);
|
||||
tx.expiresAt = clock.instant().plus(transactionTtl);
|
||||
tx.lock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -719,7 +736,7 @@ public class RdsDataService implements Resettable {
|
||||
}
|
||||
|
||||
private void cleanupExpiredTransactions() {
|
||||
Instant now = Instant.now();
|
||||
Instant now = clock.instant();
|
||||
transactions.forEach((id, tx) -> {
|
||||
if (tx.expiresAt.isBefore(now) && tx.lock.tryLock()) {
|
||||
try {
|
||||
@@ -887,7 +904,7 @@ public class RdsDataService implements Resettable {
|
||||
|
||||
private TransactionContext(
|
||||
String id, Connection connection, DatabaseEngine engine, String resourceArn,
|
||||
String database, String region, Duration ttl, RdsBackendGate.Lease lease) {
|
||||
String database, String region, Instant expiresAt, RdsBackendGate.Lease lease) {
|
||||
this.id = id;
|
||||
this.connection = connection;
|
||||
this.engine = engine;
|
||||
@@ -895,11 +912,7 @@ public class RdsDataService implements Resettable {
|
||||
this.database = database;
|
||||
this.region = region;
|
||||
this.lease = lease;
|
||||
refresh(ttl);
|
||||
}
|
||||
|
||||
private void refresh(Duration ttl) {
|
||||
this.expiresAt = Instant.now().plus(ttl);
|
||||
this.expiresAt = expiresAt;
|
||||
}
|
||||
|
||||
private void close() {
|
||||
|
||||
Reference in new issue
Block a user