Add circuit breakers to FoundationDB operations

This commit is contained in:
Katherine authored and GitHub committed 2026-09-08 14:36:52 -04:00
1 parent 4d48c40ce1
commit 100ab61c82
17 files changed
+258 -136

No files matched your search

@@ -28,7 +28,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isEveryEpochClusterConfigured());
assertFalse(new FoundationDbMessagesConfiguration(
@@ -41,7 +42,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isEveryEpochClusterConfigured());
}
@@ -57,7 +59,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isEveryEpochFreeOfDuplicates());
assertFalse(new FoundationDbMessagesConfiguration(
@@ -70,7 +73,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isEveryEpochFreeOfDuplicates());
}
@@ -86,7 +90,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isActiveEpochConfigured());
assertFalse(new FoundationDbMessagesConfiguration(
@@ -99,7 +104,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isActiveEpochConfigured());
}
@@ -115,7 +121,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isCurrentVersionstampCipherKeyConfigured());
assertFalse(new FoundationDbMessagesConfiguration(
@@ -128,7 +135,8 @@ class FoundationDbMessagesConfigurationTest {
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT,
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_TIMEOUT.multipliedBy(2),
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2
FoundationDbMessagesConfiguration.DEFAULT_TRANSACTION_RETRY_LIMIT * 2,
null
).isCurrentVersionstampCipherKeyConfigured());
}
}
@@ -5,21 +5,20 @@
package org.whispersystems.textsecuregcm.storage;
import com.apple.foundationdb.Database;
import com.apple.foundationdb.FDB;
import java.io.IOException;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.io.IOException;
import org.whispersystems.textsecuregcm.storage.foundationdb.FaultTolerantDatabase;
public class FoundationDbClusterExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
private FoundationDbDatabaseLifecycleManager[] databaseLifecycleManagers;
private Database[] databases;
private FaultTolerantDatabase[] databases;
public FoundationDbClusterExtension(final int numInstances) {
this.databaseLifecycleManagers = new FoundationDbDatabaseLifecycleManager[numInstances];
this.databases = new Database[numInstances];
this.databases = new FaultTolerantDatabase[numInstances];
}
@Override
@@ -33,13 +32,13 @@ public class FoundationDbClusterExtension implements BeforeAllCallback, Extensio
: new TestcontainersFoundationDbDatabaseLifecycleManager();
databaseLifecycleManager.initializeDatabase(FDB.selectAPIVersion(FoundationDbVersion.getFoundationDbApiVersion()));
databaseLifecycleManagers[i] = databaseLifecycleManager;
databases[i] = databaseLifecycleManager.getDatabase();
databases[i] = new FaultTolerantDatabase(databaseLifecycleManager.getDatabase(), String.format("messages-%d", i), null);
}
}
}
public Database[] getDatabases() {
public FaultTolerantDatabase[] getDatabases() {
return databases;
}
@@ -0,0 +1,66 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.storage.foundationdb;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.apple.foundationdb.Database;
import com.apple.foundationdb.FDBException;
import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import java.time.Duration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.whispersystems.textsecuregcm.configuration.CircuitBreakerConfiguration;
import org.whispersystems.textsecuregcm.util.ResilienceUtil;
public class FaultTolerantDatabaseTest {
private Database database;
private FaultTolerantDatabase ftDatabase;
@BeforeEach
void setUp() {
database = mock(Database.class);
}
@Test
void circuitBreaker() throws InterruptedException {
final String circuitBreakerConfigurationName = "testCircuitBreaker";
final CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration();
circuitBreakerConfiguration.setSlidingWindowSize(2);
circuitBreakerConfiguration.setSlidingWindowMinimumNumberOfCalls(2);
circuitBreakerConfiguration.setPermittedNumberOfCallsInHalfOpenState(1);
circuitBreakerConfiguration.setFailureRateThreshold(50);
circuitBreakerConfiguration.setWaitDurationInOpenState(Duration.ofSeconds(1));
ResilienceUtil.getCircuitBreakerRegistry()
.addConfiguration(circuitBreakerConfigurationName, circuitBreakerConfiguration.toCircuitBreakerConfig());
ftDatabase = new FaultTolerantDatabase(database, getClass().getSimpleName() + "testCircuitBreaker",
circuitBreakerConfigurationName);
when(database.read(any())).thenThrow(new FDBException("error", 1031));
assertThrows(FDBException.class, () -> ftDatabase.read(t ->
t.get("test_key".getBytes())));
assertThrows(FDBException.class, () -> ftDatabase.read(t ->
t.get("test_key".getBytes())));
assertThrows(CallNotPermittedException.class, () -> ftDatabase.read(t ->
t.get("test_key".getBytes())));
Thread.sleep(1001);
assertThrows(FDBException.class, () -> ftDatabase.read(t ->
t.get("test_key".getBytes())));
assertThrows(CallNotPermittedException.class, () -> ftDatabase.read(t ->
t.get("test_key".getBytes())));
}
}
@@ -56,6 +56,7 @@ import reactor.test.StepVerifier;
class FoundationDbMessagePublisherTest {
private Database database;
private FaultTolerantDatabase ftDatabase;
private List<FoundationDbMessagePublisher.State> stateTransitions;
private static ScheduledExecutorService presenceRenewalExecutorService;
@@ -80,6 +81,8 @@ class FoundationDbMessagePublisherTest {
@BeforeEach
void setUp() {
database = mock(Database.class);
ftDatabase = new FaultTolerantDatabase(database, getClass().getSimpleName() + "-testCircuitBreaker",
null);
stateTransitions = new ArrayList<>();
final byte[] messageGuidCodecKey = new byte[16];
@@ -123,7 +126,7 @@ class FoundationDbMessagePublisherTest {
});
final FoundationDbMessagePublisher finitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
@@ -200,7 +203,7 @@ class FoundationDbMessagePublisherTest {
.thenReturn(CompletableFuture.completedFuture(FoundationDbMessageStore.getPresenceValue(CLOCK.instant(), STREAM_ID)));
final FoundationDbMessagePublisher infinitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
@@ -290,7 +293,7 @@ class FoundationDbMessagePublisherTest {
.thenReturn(CompletableFuture.completedFuture(FoundationDbMessageStore.getPresenceValue(CLOCK.instant(), STREAM_ID + 1)));
final FoundationDbMessagePublisher infinitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
@@ -344,7 +347,7 @@ class FoundationDbMessagePublisherTest {
.thenReturn(CompletableFuture.completedFuture(FoundationDbMessageStore.getPresenceValue(CLOCK.instant(), STREAM_ID)));
final FoundationDbMessagePublisher infinitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
@@ -392,7 +395,7 @@ class FoundationDbMessagePublisherTest {
@SuppressWarnings({"unchecked", "resource"})
void watchCanceledOnSubscriptionCancel() {
final FoundationDbMessagePublisher infinitePublisher = FoundationDbMessagePublisher.createInfinitePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterThan(SUBSPACE_RANGE.end),
@@ -454,7 +457,7 @@ class FoundationDbMessagePublisherTest {
});
final FoundationDbMessagePublisher infinitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
@@ -520,7 +523,7 @@ class FoundationDbMessagePublisherTest {
final CountDownLatch latch = new CountDownLatch(1);
final FoundationDbMessagePublisher finitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
@@ -578,7 +581,7 @@ class FoundationDbMessagePublisherTest {
});
final FoundationDbMessagePublisher infinitePublisher = new FoundationDbMessagePublisher(
database,
ftDatabase,
CLOCK,
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
@@ -12,7 +12,6 @@ import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.apple.foundationdb.Database;
import com.apple.foundationdb.KeyValue;
import com.apple.foundationdb.async.AsyncUtil;
import com.apple.foundationdb.subspace.Subspace;
@@ -110,7 +109,7 @@ class FoundationDbMessageStoreTest {
versionstampUUIDCipher = new VersionstampUUIDCipher(0, versionstampCipherKey);
final List<Database> databases = Arrays.asList(FOUNDATION_DB_EXTENSION.getDatabases());
final List<FaultTolerantDatabase> databases = Arrays.asList(FOUNDATION_DB_EXTENSION.getDatabases());
foundationDbMessageStore = new FoundationDbMessageStore(
// Simulate a topology change by reversing the order of the (two) databases in the group in a second epoch. This
@@ -841,7 +840,7 @@ class FoundationDbMessageStoreTest {
while (!presenceCleared && Instant.now().isBefore(deadline)) {
presenceCleared = foundationDbMessageStore.getShardForAci(aci, DEFAULT_EPOCH).runAsync(transaction ->
transaction.get(FoundationDbMessageStore.getPresenceKey(aci, Device.PRIMARY_ID)))
transaction.get(FoundationDbMessageStore.getPresenceKey(aci, Device.PRIMARY_ID)), FoundationDbUtil.Context.GET_PRESENCE)
.thenApply(Objects::isNull)
.join();
}
@@ -905,7 +904,8 @@ class FoundationDbMessageStoreTest {
while (!presenceCleared && Instant.now().isBefore(deadline)) {
presenceCleared = foundationDbMessageStore.getShardForAci(aci, DEFAULT_EPOCH).runAsync(transaction ->
transaction.get(FoundationDbMessageStore.getPresenceKey(aci, Device.PRIMARY_ID)))
transaction.get(FoundationDbMessageStore.getPresenceKey(aci, Device.PRIMARY_ID)),
FoundationDbUtil.Context.GET_PRESENCE)
.thenApply(Objects::isNull)
.join();
}
@@ -12,7 +12,6 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.apple.foundationdb.Database;
import com.apple.foundationdb.KeyValue;
import com.apple.foundationdb.Range;
import com.apple.foundationdb.async.AsyncUtil;
@@ -43,6 +42,7 @@ import org.whispersystems.textsecuregcm.storage.AccountLockManager;
import org.whispersystems.textsecuregcm.storage.AccountsManager;
import org.whispersystems.textsecuregcm.storage.Device;
import org.whispersystems.textsecuregcm.storage.FoundationDbClusterExtension;
import org.whispersystems.textsecuregcm.storage.foundationdb.FaultTolerantDatabase;
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
import org.whispersystems.textsecuregcm.storage.foundationdb.VersionstampUUIDCipher;
import org.whispersystems.textsecuregcm.util.TestClock;
@@ -65,7 +65,7 @@ class ClearOrphanedFoundationDbQueuesCommandTest {
final byte[] versionstampCipherKey = new byte[16];
new SecureRandom().nextBytes(versionstampCipherKey);
final List<Database> databases = Arrays.asList(FOUNDATION_DB_EXTENSION.getDatabases());
final List<FaultTolerantDatabase> databases = Arrays.asList(FOUNDATION_DB_EXTENSION.getDatabases());
accountsManager = mock(AccountsManager.class);
accountLockManager = mock(AccountLockManager.class);
@@ -132,7 +132,7 @@ class ClearOrphanedFoundationDbQueuesCommandTest {
final List<AciServiceIdentifier> acis = IntStream.range(0, 128)
.mapToObj(_ -> new AciServiceIdentifier(UUID.randomUUID()))
.toList();
final Database database = FOUNDATION_DB_EXTENSION.getDatabases()[0];
final FaultTolerantDatabase database = FOUNDATION_DB_EXTENSION.getDatabases()[0];
database.run(transaction -> {
acis.forEach(aci -> {
transaction.set(FoundationDbMessageStore.getAccountSubspace(testMessagesSubspace, aci).pack(Tuple.from("foo")),
@@ -178,7 +178,7 @@ class ClearOrphanedFoundationDbQueuesCommandTest {
private boolean queueExists(final Subspace accountSpace) {
final Range accountRange = accountSpace.range();
for (final Database database : FOUNDATION_DB_EXTENSION.getDatabases()) {
for (final FaultTolerantDatabase database : FOUNDATION_DB_EXTENSION.getDatabases()) {
final List<KeyValue> keyValues = database.readAsync(transaction ->
AsyncUtil.collect(transaction.getRange(accountRange, 1))).join();