diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java index 0510fc1f8..51b528d7c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/WhisperServerService.java @@ -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 entry.getValue().stream() .map(databasesByName::get) .toList())); + + environment.lifecycle().manage(new FoundationDBWarmup(databasesByName)); } final AwsCredentialsProvider cdnCredentialsProvider = config.getCdnConfiguration().credentials().build(); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDBWarmup.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDBWarmup.java new file mode 100644 index 000000000..300a4b4b8 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDBWarmup.java @@ -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 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 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); + } + } + } + } + +}