Complete future with exception if message availability subscriptions fail

This commit is contained in:
Chris Eager authored and Chris Eager committed 2026-09-30 16:21:45 -04:00
1 parent 54807b22a3
commit f618da855f
4 files changed
+126 -5

No files matched your search

@@ -180,15 +180,15 @@ public class RedisMessageAvailabilityManager extends RedisClusterPubSubAdapter<b
}
return subscribeFuture.get()
.thenCompose(ignored -> clusterClient.withBinaryCluster(connection -> connection.async()
.spublish(eventChannel, CLIENT_CONNECTED_EVENT_BYTES)))
.handle((ignored, throwable) -> {
.thenCompose(_ -> clusterClient.withBinaryCluster(connection -> connection.async()
.spublish(eventChannel, CLIENT_CONNECTED_EVENT_BYTES))
.handle((_, throwable) -> {
if (throwable != null) {
PUBLISH_CLIENT_CONNECTION_EVENT_ERROR_COUNTER.increment();
}
return null;
});
}));
}
/**
@@ -9,6 +9,7 @@ import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;
import io.micrometer.core.instrument.Metrics;
import java.util.UUID;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Flow;
import javax.annotation.Nullable;
import org.reactivestreams.Publisher;
@@ -155,11 +156,15 @@ class RedisDynamoDbMessagePublisher implements MessageAvailabilityListener, Flow
this.subscriber = subscriber;
@Nullable final CompletionStage<Void> clientConnectedFuture;
if (!terminateOnQueueEmpty) {
// If we're trying to follow a live stream of messages, then listen for signals indicating that new messages are
// available in Redis, that messages have been persisted from Redis to DynamoDB, or that there's a conflicting
// message reader connected somewhere else
redisMessageAvailabilityManager.handleClientConnected(accountIdentifier, device.getId(), this);
clientConnectedFuture = redisMessageAvailabilityManager.handleClientConnected(accountIdentifier, device.getId(), this);
} else {
clientConnectedFuture = null;
}
subscriber.onSubscribe(new Flow.Subscription() {
@@ -173,6 +178,16 @@ class RedisDynamoDbMessagePublisher implements MessageAvailabilityListener, Flow
terminate();
}
});
// Defer the client connected handling until after onSubscribe() is called. Otherwise, onError() might be called
// first, which violates the Reactive Streams spec.
if (clientConnectedFuture != null) {
clientConnectedFuture.whenComplete((_, throwable) -> {
if (throwable != null) {
handleMessageSourceError(throwable);
}
});
}
}
@Override
@@ -5,7 +5,10 @@
package org.whispersystems.textsecuregcm.push;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
@@ -13,13 +16,16 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.lettuce.core.RedisException;
import io.lettuce.core.cluster.SlotHash;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import io.lettuce.core.cluster.event.ClusterTopologyChangedEvent;
import io.lettuce.core.cluster.models.partitions.RedisClusterNode;
import io.lettuce.core.cluster.pubsub.api.async.RedisClusterPubSubAsyncCommands;
import io.lettuce.core.cluster.pubsub.api.sync.RedisClusterPubSubCommands;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -184,6 +190,7 @@ class RedisMessageAvailabilityManagerTest {
final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder()
.binaryPubSubCommands(pubSubCommands)
.binaryPubSubAsyncCommands(pubSubAsyncCommands)
.binaryAsyncCommands(buildBinaryAsyncCommands())
.build();
final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager(
@@ -250,6 +257,7 @@ class RedisMessageAvailabilityManagerTest {
final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder()
.binaryPubSubAsyncCommands(pubSubAsyncCommands)
.binaryAsyncCommands(buildBinaryAsyncCommands())
.build();
final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager(
@@ -281,4 +289,82 @@ class RedisMessageAvailabilityManagerTest {
verify(pubSubAsyncCommands)
.sunsubscribe(RedisMessageAvailabilityManager.getClientEventChannel(noListenerAccountIdentifier, noListenerDeviceId));
}
@Test
void handleClientConnectedSubscribeFailure() {
@SuppressWarnings("unchecked") final RedisClusterPubSubAsyncCommands<byte[], byte[]> pubSubAsyncCommands =
mock(RedisClusterPubSubAsyncCommands.class);
final RedisException exception = new RedisException("Currently not connected. Commands are rejected.");
when(pubSubAsyncCommands.ssubscribe(any()))
.thenReturn(MockRedisFuture.failedFuture(exception));
final RedisAdvancedClusterAsyncCommands<byte[], byte[]> binaryAsyncCommands = buildBinaryAsyncCommands();
final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder()
.binaryPubSubAsyncCommands(pubSubAsyncCommands)
.binaryAsyncCommands(binaryAsyncCommands)
.build();
final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager(
clusterClient,
Runnable::run,
Runnable::run);
eventManager.start();
final CompletionException completionException = assertThrows(CompletionException.class, () ->
eventManager.handleClientConnected(UUID.randomUUID(), Device.PRIMARY_ID, new MessageAvailabilityAdapter())
.toCompletableFuture()
.join());
assertEquals(exception, completionException.getCause());
// We shouldn't announce a client connection if we couldn't subscribe to events for that client
verify(binaryAsyncCommands, never()).spublish(any(), any());
}
@Test
void handleClientConnectedPublishFailure() {
@SuppressWarnings("unchecked") final RedisClusterPubSubAsyncCommands<byte[], byte[]> pubSubAsyncCommands =
mock(RedisClusterPubSubAsyncCommands.class);
when(pubSubAsyncCommands.ssubscribe(any())).thenReturn(MockRedisFuture.completedFuture(null));
@SuppressWarnings("unchecked") final RedisAdvancedClusterAsyncCommands<byte[], byte[]> binaryAsyncCommands =
mock(RedisAdvancedClusterAsyncCommands.class);
when(binaryAsyncCommands.spublish(any(), any()))
.thenReturn(MockRedisFuture.failedFuture(new RedisException("Currently not connected. Commands are rejected.")));
final FaultTolerantRedisClusterClient clusterClient = RedisClusterHelper.builder()
.binaryPubSubAsyncCommands(pubSubAsyncCommands)
.binaryAsyncCommands(binaryAsyncCommands)
.build();
final RedisMessageAvailabilityManager eventManager = new RedisMessageAvailabilityManager(
clusterClient,
Runnable::run,
Runnable::run);
eventManager.start();
final UUID accountIdentifier = UUID.randomUUID();
final byte deviceId = Device.PRIMARY_ID;
assertDoesNotThrow(() -> eventManager.handleClientConnected(accountIdentifier, deviceId, new MessageAvailabilityAdapter())
.toCompletableFuture()
.join());
assertTrue(eventManager.isLocallyPresent(accountIdentifier, deviceId));
}
private static RedisAdvancedClusterAsyncCommands<byte[], byte[]> buildBinaryAsyncCommands() {
@SuppressWarnings("unchecked") final RedisAdvancedClusterAsyncCommands<byte[], byte[]> binaryAsyncCommands =
mock(RedisAdvancedClusterAsyncCommands.class);
when(binaryAsyncCommands.spublish(any(), any())).thenReturn(MockRedisFuture.completedFuture(0L));
return binaryAsyncCommands;
}
}
@@ -361,6 +361,26 @@ class RedisDynamoDbMessagePublisherTest {
verify(redisMessageAvailabilityManager, timeout(1_000)).handleClientDisconnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher);
}
@Test
void publishMessagesClientConnectedFailure() {
final CompletableFuture<Void> clientConnectedFuture = new CompletableFuture<>();
final RuntimeException exception = new RuntimeException("OH NO");
clientConnectedFuture.completeExceptionally(exception);
final RedisDynamoDbMessagePublisher messagePublisher =
new RedisDynamoDbMessagePublisher(messagesDynamoDb, messagesCache, redisMessageAvailabilityManager, DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice, false);
when(redisMessageAvailabilityManager.handleClientConnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher))
.thenReturn(clientConnectedFuture);
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messagePublisher))
.expectErrorMatches(exception::equals)
.verify();
verify(redisMessageAvailabilityManager).handleClientDisconnected(DESTINATION_SERVICE_IDENTIFIER.uuid(), destinationDevice.getId(), messagePublisher);
}
@ParameterizedTest
@CsvSource({
"207, 173",