From fd36a8909ca6d9bfdbbb0b78ccac1a28a06f1594 Mon Sep 17 00:00:00 2001 From: Ameya Lokare Date: Wed, 9 Sep 2026 15:22:31 -0700 Subject: [PATCH] Improve handling of FoundationDB errors in reactive streams --- .../textsecuregcm/storage/MirroringMessageStream.java | 7 ++++++- .../foundationdb/FoundationDbMessagePublisher.java | 10 +++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java index bf6c50590..4ec2f9404 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/MirroringMessageStream.java @@ -5,6 +5,7 @@ package org.whispersystems.textsecuregcm.storage; +import com.apple.foundationdb.FDBException; import com.google.common.hash.HashCode; import com.google.common.hash.Hashing; import com.google.protobuf.CodedOutputStream; @@ -31,6 +32,7 @@ import org.whispersystems.textsecuregcm.experiment.ExperimentEnrollmentManager; import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier; import org.whispersystems.textsecuregcm.metrics.MetricsUtil; import org.whispersystems.textsecuregcm.storage.foundationdb.FoundationDbMessageStream; +import org.whispersystems.textsecuregcm.util.ExceptionUtils; import org.whispersystems.textsecuregcm.util.UUIDUtil; import reactor.adapter.JdkFlowAdapter; import reactor.core.publisher.BaseSubscriber; @@ -128,8 +130,11 @@ public class MirroringMessageStream implements MessageStream { @Override protected void hookOnError(final Throwable throwable) { - switch (throwable) { + switch (ExceptionUtils.unwrap(throwable)) { case ConflictingMessageConsumerException _ -> CONFLICTING_CONSUMER_COUNTER.increment(); + case FDBException _ -> { + // FDBExceptions are already instrumented by metrics, so do nothing + } default -> super.hookOnError(throwable); } } 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 037c28c0d..bd6c92be1 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 @@ -122,8 +122,6 @@ class FoundationDbMessagePublisher { MESSAGE_AVAILABLE_WATCH_TRIGGERED, /// Internal self-trigger used to immediately transition to the next state. INTERNAL_TRIGGER, - /// An error occurred during fetching from FoundationDB or publishing messages to the sink. - FETCH_OR_PUBLISH_ERROR_OCCURRED } FoundationDbMessagePublisher( @@ -251,7 +249,6 @@ class FoundationDbMessagePublisher { transitionStateOnEvent(Event.INTERNAL_TRIGGER); } case MESSAGE_AVAILABLE_WATCH_TRIGGERED -> setState(State.MESSAGE_AVAILABLE_SIGNAL_BUFFERED, event); - case FETCH_OR_PUBLISH_ERROR_OCCURRED -> setState(State.ERROR, event); default -> knownTransition = false; } } @@ -390,7 +387,7 @@ class FoundationDbMessagePublisher { /// operation returns fewer items than the batch size, we infer that we have fetched all available messages and /// [Event#FETCHED_ALL_AVAILABLE_MESSAGES] is sent to the state machine. See [#getMessagesBatch(int)] for details. /// Additionally, after we successfully publish the batch of messages, {@link Event#PUBLISHED_MESSAGES} is emitted. If - /// there's an error while fetching or publishing, [Event#FETCH_OR_PUBLISH_ERROR_OCCURRED] is emitted instead. + /// there's an error while fetching or publishing, the stream is terminated with the error. private void emitMessages() { final int maxMessages = Math.min(getOutstandingDemand(), MAX_MESSAGES_PER_PAGE); @@ -401,8 +398,7 @@ class FoundationDbMessagePublisher { transitionStateOnEvent(Event.PUBLISHED_MESSAGES); }) .exceptionally(t -> { - transitionStateOnEvent(Event.FETCH_OR_PUBLISH_ERROR_OCCURRED); - emitter.error(ExceptionUtils.unwrap(t)); + terminateWithError(ExceptionUtils.unwrap(t)); return null; }); } @@ -525,7 +521,7 @@ class FoundationDbMessagePublisher { @VisibleForTesting synchronized void terminateWithError(final Throwable throwable) { - if (state != State.TERMINATED && state != State.ERROR) { + if (state != State.TERMINATED && state != State.ERROR && !emitter.isCancelled()) { setState(State.ERROR, Event.INTERNAL_TRIGGER); emitter.error(throwable); }