From 4443c31ee0080bea5b1545ad82642300895b0aa8 Mon Sep 17 00:00:00 2001 From: Katherine Date: Mon, 21 Sep 2026 16:09:03 -0400 Subject: [PATCH] Augment FoundationDB metrics to help debug epoch drill mystery --- .../foundationdb/FaultTolerantDatabase.java | 39 +++++++++++++------ .../FoundationDbMessagePublisher.java | 7 ++-- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FaultTolerantDatabase.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FaultTolerantDatabase.java index b0711f49b..73e75188c 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FaultTolerantDatabase.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FaultTolerantDatabase.java @@ -28,8 +28,13 @@ public class FaultTolerantDatabase { private final Bulkhead bulkhead; private final LongAdder concurrentTransactionCount; + private final String name; private static final String TRANSACTION_ERRORS_COUNTER = MetricsUtil.name(FaultTolerantDatabase.class, "transactionErrors"); + private static final String TRANSACTION_COUNTER_NAME = MetricsUtil.name(FaultTolerantDatabase.class, "transactions"); + private static final String CONTEXT_TAG_NAME = "context"; + private static final String DATABASE_TAG_NAME = "name"; + private static final String ERROR_CODE_TAG_NAME = "code"; public FaultTolerantDatabase( final Database database, @@ -37,7 +42,7 @@ public class FaultTolerantDatabase { @Nullable final String circuitBreakerConfigurationName, @Nullable final String bulkheadConfigurationName) { this.database = database; - + this.name = name; this.circuitBreaker = circuitBreakerConfigurationName != null ? ResilienceUtil.getCircuitBreakerRegistry().circuitBreaker(name, circuitBreakerConfigurationName) : ResilienceUtil.getCircuitBreakerRegistry().circuitBreaker(name); @@ -47,18 +52,24 @@ public class FaultTolerantDatabase { : ResilienceUtil.getBulkheadRegistry().bulkhead(name); this.concurrentTransactionCount = - Metrics.gauge(MetricsUtil.name(getClass(), "concurrentTransactions"), Tags.of("name", name), new LongAdder()); + Metrics.gauge(MetricsUtil.name(getClass(), "concurrentTransactions"), Tags.of(DATABASE_TAG_NAME, name), new LongAdder()); + } + + public String getName() { + return name; } public T run(final Function retryable, final Context context) { try { + Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment(); concurrentTransactionCount.increment(); return bulkhead.executeSupplier(() -> circuitBreaker.executeSupplier(() -> database.run(retryable))); } catch (final Exception e) { if (e instanceof final FDBException fdbException) { Metrics.counter(TRANSACTION_ERRORS_COUNTER, - "context", context.getName(), - "code", String.valueOf(fdbException.getCode()) + CONTEXT_TAG_NAME, context.getName(), + DATABASE_TAG_NAME, name, + ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode()) ).increment(); } throw e; @@ -77,15 +88,16 @@ public class FaultTolerantDatabase { /// @return a cancellation-safe version of the future returned from [Database#runAsync(Function)] public CompletableFuture runAsync(final Function> retryable, final Context context) { - + Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment(); concurrentTransactionCount.increment(); final CompletableFuture runAsyncFuture = bulkhead.executeCompletionStage(() -> circuitBreaker.executeCompletionStage(() -> database.runAsync(retryable) .whenComplete((_, throwable) -> { if (throwable != null && ExceptionUtils.unwrap(throwable) instanceof final FDBException fdbException) { Metrics.counter(TRANSACTION_ERRORS_COUNTER, - "context", context.getName(), - "code", String.valueOf(fdbException.getCode()) + CONTEXT_TAG_NAME, context.getName(), + DATABASE_TAG_NAME, name, + ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode()) ).increment(); } }) @@ -99,13 +111,15 @@ public class FaultTolerantDatabase { public T read(final Function retryable, final Context context) { try { + Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment(); concurrentTransactionCount.increment(); return bulkhead.executeSupplier(() -> circuitBreaker.executeSupplier(() -> database.read(retryable))); } catch (final Exception e) { if (e instanceof final FDBException fdbException) { Metrics.counter(TRANSACTION_ERRORS_COUNTER, - "context", context.getName(), - "code", String.valueOf(fdbException.getCode()) + CONTEXT_TAG_NAME, context.getName(), + DATABASE_TAG_NAME, name, + ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode()) ).increment(); } throw e; @@ -117,15 +131,16 @@ public class FaultTolerantDatabase { public CompletableFuture readAsync( final Function> retryable, final Context context) { - + Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment(); concurrentTransactionCount.increment(); final CompletableFuture readAsyncFuture = bulkhead.executeCompletionStage(() -> circuitBreaker.executeCompletionStage(() -> database.readAsync(retryable) .whenComplete((_, throwable) -> { if (throwable != null && ExceptionUtils.unwrap(throwable) instanceof final FDBException fdbException) { Metrics.counter(TRANSACTION_ERRORS_COUNTER, - "context", context.getName(), - "code", String.valueOf(fdbException.getCode()) + CONTEXT_TAG_NAME, context.getName(), + DATABASE_TAG_NAME, name, + ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode()) ).increment(); } }))) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDbMessagePublisher.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDbMessagePublisher.java index bd6c92be1..1d6c78cde 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDbMessagePublisher.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/foundationdb/FoundationDbMessagePublisher.java @@ -91,6 +91,7 @@ class FoundationDbMessagePublisher { private static final String WATCHES_COUNTER = MetricsUtil.name(FoundationDbMessagePublisher.class, "watches"); private static final String ACTION_TAG = "action"; + private static final String DATABASE_TAG = "database"; enum State { /// Messages are likely available in the queue. Initial state. @@ -420,9 +421,9 @@ class FoundationDbMessagePublisher { // again (if there is demand). When we run out of messages, this method will be called again, setting another watch, // and so on, thus achieving a "watch for new messages -> read -> publish" loop. watchFuture = transaction.watch(messagesAvailableWatchKey); - Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "set").increment(); + Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "set", DATABASE_TAG, database.getName()).increment(); watchFuture.thenRun(() -> { - Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "triggered").increment(); + Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "triggered", DATABASE_TAG, database.getName()).increment(); transitionStateOnEvent(Event.MESSAGE_AVAILABLE_WATCH_TRIGGERED); }); } @@ -432,7 +433,7 @@ class FoundationDbMessagePublisher { private synchronized void cancelWatch() { if (watchFuture != null) { if (watchFuture.cancel(true)) { - Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "cancelled").increment(); + Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "cancelled", DATABASE_TAG, database.getName()).increment(); } watchFuture = null; }