mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -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() {
|
||||
|
||||
@@ -4,9 +4,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.floci.aws.services.rds.container.RdsBackendGate;
|
||||
import io.floci.aws.services.rds.model.DatabaseEngine;
|
||||
import io.floci.aws.services.secretsmanager.SecretsManagerService;
|
||||
import io.floci.aws.services.secretsmanager.model.SecretVersion;
|
||||
import io.floci.aws.testing.ManualClock;
|
||||
import org.h2.Driver;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -21,6 +23,7 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -47,6 +50,7 @@ class RdsDataTransactionConcurrencyTest {
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final CountDownLatch started = new CountDownLatch(1);
|
||||
private final CountDownLatch release = new CountDownLatch(1);
|
||||
private final ManualClock clock = new ManualClock(Instant.EPOCH);
|
||||
private String jdbcUrl;
|
||||
private RdsDataService service;
|
||||
|
||||
@@ -71,8 +75,8 @@ class RdsDataTransactionConcurrencyTest {
|
||||
when(secrets.getSecretValue(any(), any(), any(), any())).thenReturn(version);
|
||||
RdsDataConnectionFactory factory = mock(RdsDataConnectionFactory.class);
|
||||
when(factory.open(any(), any(), any(), any(), any())).thenAnswer(invocation -> connection());
|
||||
return new RdsDataService(resolver, secrets, mapper, factory, ttl,
|
||||
(runtimeId, engine) -> new EndpointInfo("127.0.0.1", 5432));
|
||||
return new RdsDataService(resolver, secrets, mapper, factory, ttl, RdsBackendGate.OPEN,
|
||||
(runtimeId, engine) -> new EndpointInfo("127.0.0.1", 5432), false, clock);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -147,7 +151,7 @@ class RdsDataTransactionConcurrencyTest {
|
||||
Future<ObjectNode> first = executor.submit(() -> service.executeStatement(request(tx, BLOCKED_QUERY), REGION));
|
||||
try {
|
||||
assertTrue(started.await(5, TimeUnit.SECONDS));
|
||||
Thread.sleep(1100);
|
||||
clock.advance(Duration.ofSeconds(2));
|
||||
Future<String> independent = executor.submit(this::begin);
|
||||
String other = independent.get(5, TimeUnit.SECONDS);
|
||||
service.rollbackTransaction(request(other, null), REGION);
|
||||
@@ -157,7 +161,9 @@ class RdsDataTransactionConcurrencyTest {
|
||||
first.get(5, TimeUnit.SECONDS);
|
||||
}
|
||||
assertEquals(2, query(tx));
|
||||
service.rollbackTransaction(request(tx, null), REGION);
|
||||
clock.advance(Duration.ofSeconds(2));
|
||||
assertEquals("TransactionNotFoundException",
|
||||
assertThrows(AwsException.class, () -> query(tx)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user