Read status key at startup to warm up FoundationDB connections

This commit is contained in:
Ameya Lokare authored and Jon Chambers committed 2026-09-08 10:35:20 -04:00
1 parent c1d8ca3c14
commit 93ade07c57
2 files changed
+68

No files matched your search

@@ -295,6 +295,7 @@ import org.whispersystems.textsecuregcm.storage.devicecheck.AppleDeviceCheckMana
import org.whispersystems.textsecuregcm.storage.devicecheck.AppleDeviceCheckTrustAnchor;
import org.whispersystems.textsecuregcm.storage.devicecheck.AppleDeviceChecks;
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStore;
import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDBWarmup;
import org.whispersystems.textsecuregcm.storage.foundationdb.VersionstampUUIDCipher;
import org.whispersystems.textsecuregcm.subscriptions.AppleAppStoreClient;
import org.whispersystems.textsecuregcm.subscriptions.AppleAppStoreManager;
@@ -523,6 +524,8 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
entry -> entry.getValue().stream()
.map(databasesByName::get)
.toList()));
environment.lifecycle().manage(new FoundationDBWarmup(databasesByName));
}
final AwsCredentialsProvider cdnCredentialsProvider = config.getCdnConfiguration().credentials().build();
@@ -0,0 +1,65 @@
package org.whispersystems.textsecuregcm.storage.foundationdb;
import com.apple.foundationdb.Database;
import io.dropwizard.lifecycle.Managed;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FoundationDBWarmup implements Managed {
private final Map<String, Database> databasesByName;
private static final Duration RETRY_DELAY = Duration.ofMillis(500);
private static final int MAX_ATTEMPTS = 3;
private static final Logger LOGGER = LoggerFactory.getLogger(FoundationDBWarmup.class);
private static final byte[] STATUS_JSON_KEY;
static {
final byte[] suffix = "/status/json".getBytes(StandardCharsets.US_ASCII);
STATUS_JSON_KEY = new byte[suffix.length + 2];
STATUS_JSON_KEY[0] = (byte) 0xff;
STATUS_JSON_KEY[1] = (byte) 0xff;
System.arraycopy(suffix, 0, STATUS_JSON_KEY, 2, suffix.length);
}
public FoundationDBWarmup(final Map<String, Database> databasesByName) {
this.databasesByName = databasesByName;
}
@Override
public void start() throws Exception {
databasesByName.forEach(this::readStatusKey);
}
/// Warm up the connection to a FoundationDB database by reading its status key. We retry a limited number of times so
/// that we don't block the readiness probe in case a single shard is unavailable.
///
/// @param databaseName the name of the database to read from
/// @param database the FoundationDB [Database] instance
private void readStatusKey(final String databaseName, final Database database) {
int attempts = 0;
while (true) {
try {
database.readAsync(transaction -> transaction.get(STATUS_JSON_KEY)).join();
return;
} catch (final Exception e) {
attempts++;
if (attempts == MAX_ATTEMPTS) {
LOGGER.warn("Failed to read status key at startup for shard {}", databaseName, e);
return;
}
try {
Thread.sleep(RETRY_DELAY);
} catch (final InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}
}
}