Improve handling of FoundationDB errors in reactive streams

This commit is contained in:
Ameya Lokare committed 2026-09-10 10:49:45 -07:00
1 parent 78c15d2a9a
commit fd36a8909c
2 files changed
+9 -8

No files matched your search

@@ -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);
}
}
@@ -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);
}