mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-01 10:16:47 +00:00
Limit message range size based on outstanding demand
This commit is contained in:
1 parent
4f8ca893f0
commit
07444bffb4
5 files changed
+99
-102
No files matched your search
+49
-35
@@ -18,7 +18,6 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.BiConsumer;
|
||||
import javax.annotation.Nullable;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
@@ -42,9 +41,6 @@ class FoundationDbMessagePublisher {
|
||||
/// time the publisher was created. For an infinite publisher, this is the end of subspace range.
|
||||
private final KeySelector endKeyExclusive;
|
||||
|
||||
/// The maximum number of messages we will fetch per range query operation to avoid excessive memory consumption
|
||||
private final int maxMessagesPerScan;
|
||||
|
||||
/// The key that stores the server ID/timestamp indicating when/where the connected client was last known to be
|
||||
/// present. Active publishers refresh this key at regular intervals (see [#renewPresenceFuture]).
|
||||
@Nullable private final byte[] presenceKey;
|
||||
@@ -80,14 +76,16 @@ class FoundationDbMessagePublisher {
|
||||
/// A future that represents the next pending presence renewal.
|
||||
@Nullable private CompletableFuture<?> renewPresenceFuture;
|
||||
|
||||
private final AtomicLong totalRequested = new AtomicLong(0);
|
||||
private final AtomicLong totalEmitted = new AtomicLong(0);
|
||||
private long totalRequested;
|
||||
private long totalEmitted;
|
||||
|
||||
private static final AtomicInteger NEXT_STREAM_ID = new AtomicInteger();
|
||||
|
||||
private static final long RENEWAL_INTERVAL_MILLIS =
|
||||
FoundationDbMessageStore.PRESENCE_STALE_THRESHOLD.multipliedBy(4).dividedBy(5).toMillis();
|
||||
|
||||
private static final int MAX_MESSAGES_PER_PAGE = 1024;
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FoundationDbMessagePublisher.class);
|
||||
|
||||
private static final String WATCHES_COUNTER = MetricsUtil.name(FoundationDbMessagePublisher.class, "watches");
|
||||
@@ -132,7 +130,6 @@ class FoundationDbMessagePublisher {
|
||||
final Clock clock,
|
||||
final KeySelector beginKeyInclusive,
|
||||
final KeySelector endKeyExclusive,
|
||||
final int maxMessagesPerScan,
|
||||
@Nullable final byte[] presenceKey,
|
||||
@Nullable final ScheduledExecutorService presenceRenewalExecutorService,
|
||||
@Nullable final byte[] messagesAvailableWatchKey,
|
||||
@@ -142,7 +139,6 @@ class FoundationDbMessagePublisher {
|
||||
this.clock = clock;
|
||||
this.beginKeyCursor = beginKeyInclusive;
|
||||
this.endKeyExclusive = endKeyExclusive;
|
||||
this.maxMessagesPerScan = maxMessagesPerScan;
|
||||
this.presenceKey = presenceKey;
|
||||
this.presenceRenewalExecutorService = presenceRenewalExecutorService;
|
||||
this.messagesAvailableWatchKey = messagesAvailableWatchKey;
|
||||
@@ -165,12 +161,7 @@ class FoundationDbMessagePublisher {
|
||||
this.messagePublisher = establishPresence
|
||||
.thenMany(Flux.create(emitter -> {
|
||||
this.emitter = emitter;
|
||||
emitter.onRequest(n -> {
|
||||
final long totalRequestedCount = totalRequested.addAndGet(n);
|
||||
if (totalRequestedCount > totalEmitted.get()) {
|
||||
transitionStateOnEvent(Event.DEMAND_REQUESTED);
|
||||
}
|
||||
});
|
||||
emitter.onRequest(this::addDemand);
|
||||
emitter.onDispose(this::onDispose);
|
||||
}));
|
||||
}
|
||||
@@ -183,14 +174,12 @@ class FoundationDbMessagePublisher {
|
||||
final Database database,
|
||||
final Clock clock,
|
||||
final KeySelector beginKeyInclusive,
|
||||
final KeySelector endKeyExclusive,
|
||||
final int maxMessagesPerScan) {
|
||||
final KeySelector endKeyExclusive) {
|
||||
|
||||
return new FoundationDbMessagePublisher(database,
|
||||
clock,
|
||||
beginKeyInclusive,
|
||||
endKeyExclusive,
|
||||
maxMessagesPerScan,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
@@ -205,7 +194,6 @@ class FoundationDbMessagePublisher {
|
||||
final Clock clock,
|
||||
final KeySelector beginKeyInclusive,
|
||||
final KeySelector endKeyExclusive,
|
||||
final int maxMessagesPerScan,
|
||||
final byte[] presenceKey,
|
||||
final ScheduledExecutorService presenceRenewalExecutorService,
|
||||
final byte[] messagesAvailableWatchKey) {
|
||||
@@ -214,7 +202,6 @@ class FoundationDbMessagePublisher {
|
||||
clock,
|
||||
beginKeyInclusive,
|
||||
endKeyExclusive,
|
||||
maxMessagesPerScan,
|
||||
presenceKey,
|
||||
presenceRenewalExecutorService,
|
||||
messagesAvailableWatchKey,
|
||||
@@ -305,24 +292,30 @@ class FoundationDbMessagePublisher {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetch messages using a range query limiting batch size to [#maxMessagesPerScan]. If the query returns fewer than
|
||||
/// [#maxMessagesPerScan], emit [Event#FETCHED_ALL_AVAILABLE_MESSAGES]. In the case of an infinite publisher, also set
|
||||
/// Fetch messages using a range query limiting batch size to `maxMessages`. If the query returns fewer than
|
||||
/// `maxMessages`, emit [Event#FETCHED_ALL_AVAILABLE_MESSAGES]. In the case of an infinite publisher, also set
|
||||
/// a watch for new messages. Additionally, the cursor is updated so that we begin fetching from the right key on
|
||||
/// subsequent scans.
|
||||
///
|
||||
/// @return a future of a list of [FoundationDbMessageStreamEntry.Message] with a max size of [#maxMessagesPerScan]
|
||||
private CompletableFuture<List<FoundationDbMessageStreamEntry.Message>> getMessagesBatch() {
|
||||
/// @param maxMessages the maximum number of messages to retrieve
|
||||
///
|
||||
/// @return a future of a list of [FoundationDbMessageStreamEntry.Message] with a max size of `maxMessages`
|
||||
private CompletableFuture<List<FoundationDbMessageStreamEntry.Message>> getMessagesBatch(final int maxMessages) {
|
||||
if (maxMessages <= 0) {
|
||||
throw new IllegalArgumentException("Max messages must be positive");
|
||||
}
|
||||
|
||||
return FoundationDbUtil.safeRunAsync(database, transaction ->
|
||||
transaction.getRange(beginKeyCursor, endKeyExclusive, maxMessagesPerScan, false, StreamingMode.EXACT).asList()
|
||||
transaction.getRange(beginKeyCursor, endKeyExclusive, maxMessages, false, StreamingMode.EXACT).asList()
|
||||
.thenApply(keyValues -> {
|
||||
if (keyValues.size() < maxMessagesPerScan && !terminateOnQueueEmpty) {
|
||||
if (keyValues.size() < maxMessages && !terminateOnQueueEmpty) {
|
||||
setWatch(transaction);
|
||||
}
|
||||
|
||||
return keyValues;
|
||||
}))
|
||||
.thenApply(keyValues -> {
|
||||
if (keyValues.size() < maxMessagesPerScan) {
|
||||
if (keyValues.size() < maxMessages) {
|
||||
transitionStateOnEvent(Event.FETCHED_ALL_AVAILABLE_MESSAGES);
|
||||
}
|
||||
|
||||
@@ -347,18 +340,39 @@ class FoundationDbMessagePublisher {
|
||||
});
|
||||
}
|
||||
|
||||
/// Fetch and publish messages. Messages are fetched in batches of [#maxMessagesPerScan] to avoid excessive memory
|
||||
/// consumption. After each fetch operation, [#beginKeyCursor] is updated to the next key we need to start reading
|
||||
/// from. If the fetch 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()]
|
||||
/// 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.
|
||||
private synchronized void addDemand(final long n) {
|
||||
totalRequested += n;
|
||||
|
||||
assert totalRequested > totalEmitted;
|
||||
|
||||
transitionStateOnEvent(Event.DEMAND_REQUESTED);
|
||||
}
|
||||
|
||||
private synchronized void handleEntriesEmitted(final long n) {
|
||||
totalEmitted += n;
|
||||
}
|
||||
|
||||
/// Returns the unmet demand requested by subscribers. Values larger than [Integer#MAX_VALUE] are clamped to
|
||||
/// [Integer#MAX_VALUE].
|
||||
///
|
||||
/// @return the unmet demand requested by publishers as an `int`
|
||||
private synchronized int getOutstandingDemand() {
|
||||
return (int) Math.min(totalRequested - totalEmitted, Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
/// Fetch and publish messages. Messages are fetched in batches to avoid excessive memory consumption. After each
|
||||
/// fetch operation, [#beginKeyCursor] is updated to the next key we need to start reading from. If the fetch
|
||||
/// 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.
|
||||
private void emitMessages() {
|
||||
getMessagesBatch()
|
||||
final int maxMessages = Math.min(getOutstandingDemand(), MAX_MESSAGES_PER_PAGE);
|
||||
|
||||
getMessagesBatch(maxMessages)
|
||||
.thenAccept(messageStreamEntries -> {
|
||||
messageStreamEntries.forEach(emitter::next);
|
||||
totalEmitted.addAndGet(messageStreamEntries.size());
|
||||
handleEntriesEmitted(messageStreamEntries.size());
|
||||
transitionStateOnEvent(Event.PUBLISHED_MESSAGES);
|
||||
})
|
||||
.exceptionally(t -> {
|
||||
|
||||
+1
-3
@@ -424,7 +424,7 @@ public class FoundationDbMessageStore {
|
||||
}
|
||||
|
||||
public FoundationDbMessageStream getMessages(final AciServiceIdentifier aci, final byte deviceId) {
|
||||
return getMessages(aci, deviceId, FoundationDbMessageStream.DEFAULT_MAX_MESSAGES_PER_SCAN, Util.NOOP);
|
||||
return getMessages(aci, deviceId, Util.NOOP);
|
||||
}
|
||||
|
||||
Database[] getDatabasesByEpochForAci(final AciServiceIdentifier aci) {
|
||||
@@ -441,7 +441,6 @@ public class FoundationDbMessageStore {
|
||||
@VisibleForTesting
|
||||
FoundationDbMessageStream getMessages(final AciServiceIdentifier aci,
|
||||
final byte deviceId,
|
||||
final int maxMessagesPerScan,
|
||||
final Runnable doAfterCleanup) {
|
||||
|
||||
final Database[] databasesForQueueByEpoch = getDatabasesByEpochForAci(aci);
|
||||
@@ -451,7 +450,6 @@ public class FoundationDbMessageStore {
|
||||
deviceId,
|
||||
databasesForQueueByEpoch,
|
||||
new MessageGuidCodec(aci.uuid(), deviceId, versionstampUUIDCipher),
|
||||
maxMessagesPerScan,
|
||||
doAfterCleanup,
|
||||
presenceRenewalExecutorService,
|
||||
clock);
|
||||
|
||||
+5
-10
@@ -44,7 +44,6 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
private final Database[] databasesByEpoch;
|
||||
private final MessageGuidCodec messageGuidCodec;
|
||||
/// The maximum number of messages we will fetch per range query operation to avoid excessive memory consumption
|
||||
private final int maxMessagesPerScan;
|
||||
private final Flow.Publisher<MessageStreamEntry> messageStreamPublisher;
|
||||
private final Runnable doAfterCleanup;
|
||||
private final ScheduledExecutorService presenceRenewalExecutorService;
|
||||
@@ -59,11 +58,11 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
private final Counter staleEphemeralMessagesCounter =
|
||||
Metrics.counter(name(FoundationDbMessageStream.class, "staleEphemeralMessages"));
|
||||
|
||||
static final int DEFAULT_MAX_MESSAGES_PER_SCAN = 100;
|
||||
|
||||
private static final Comparator<FoundationDbMessageStreamEntry.Message> STREAM_ENTRY_TIMESTAMP_COMPARATOR =
|
||||
Comparator.comparingLong(streamEntry -> streamEntry.partialEnvelope().getServerTimestamp());
|
||||
|
||||
private static final int PREFETCH = 128;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FoundationDbMessageStream.class);
|
||||
|
||||
FoundationDbMessageStream(final FoundationDbMessageStore foundationDbMessageStore,
|
||||
@@ -71,7 +70,6 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
final byte deviceId,
|
||||
final Database[] databasesByEpoch,
|
||||
final MessageGuidCodec messageGuidCodec,
|
||||
final int maxMessagesPerScan,
|
||||
final Runnable doAfterCleanup,
|
||||
final ScheduledExecutorService presenceRenewalExecutorService,
|
||||
final Clock clock) {
|
||||
@@ -83,7 +81,6 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
this.messagesAvailableWatchKey = FoundationDbMessageStore.getMessagesAvailableWatchKey(aciServiceIdentifier);
|
||||
this.databasesByEpoch = databasesByEpoch;
|
||||
this.messageGuidCodec = messageGuidCodec;
|
||||
this.maxMessagesPerScan = maxMessagesPerScan;
|
||||
this.messageStreamPublisher = JdkFlowAdapter.publisherToFlowPublisher(createMessagePublisher());
|
||||
this.doAfterCleanup = doAfterCleanup;
|
||||
this.presenceRenewalExecutorService = presenceRenewalExecutorService;
|
||||
@@ -144,8 +141,7 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
database,
|
||||
clock,
|
||||
KeySelector.firstGreaterOrEqual(deviceQueueSubspace.range().begin),
|
||||
endOfQueueKeyExclusive,
|
||||
maxMessagesPerScan)
|
||||
endOfQueueKeyExclusive)
|
||||
.getMessages())
|
||||
.orElseGet(Flux::empty)
|
||||
.handle((fdbMessageStreamEntry, sink) -> {
|
||||
@@ -177,7 +173,6 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
clock,
|
||||
infinitePublisherBeginKey,
|
||||
KeySelector.firstGreaterThan(deviceQueueSubspace.range().end),
|
||||
maxMessagesPerScan,
|
||||
presenceKey,
|
||||
presenceRenewalExecutorService,
|
||||
messagesAvailableWatchKey).getMessages();
|
||||
@@ -185,7 +180,7 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
.toArray(Flux[]::new);
|
||||
|
||||
return Flux.concat(
|
||||
Flux.mergeComparing(maxMessagesPerScan, STREAM_ENTRY_TIMESTAMP_COMPARATOR, finitePublishers),
|
||||
Flux.mergeComparing(PREFETCH, STREAM_ENTRY_TIMESTAMP_COMPARATOR, finitePublishers),
|
||||
Mono.just(new FoundationDbMessageStreamEntry.QueueEmpty()),
|
||||
|
||||
// Note that we use `mergePriority` instead of `mergeComparing` for the "live"/non-terminating publishers
|
||||
@@ -196,7 +191,7 @@ public class FoundationDbMessageStream implements MessageStream {
|
||||
// where messages arrive at different servers while somebody is connected and a migration is in progress,
|
||||
// but that should be (again) exceedingly rare and also minimally-disruptive (i.e. it would self-correct
|
||||
// so quickly that end users would likely never even notice).
|
||||
Flux.mergePriority(maxMessagesPerScan, STREAM_ENTRY_TIMESTAMP_COMPARATOR, infinitePublishers));
|
||||
Flux.mergePriority(PREFETCH, STREAM_ENTRY_TIMESTAMP_COMPARATOR, infinitePublishers));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+36
-37
@@ -2,7 +2,6 @@ package org.whispersystems.textsecuregcm.storage.foundationdb;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
@@ -27,7 +26,9 @@ import java.nio.ByteBuffer;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -125,14 +126,14 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
|
||||
2, // With 3 messages and batch size set to 2, we'll need to grab 2 batches.
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
(_, newState) -> stateTransitions.add(newState)
|
||||
);
|
||||
|
||||
StepVerifier.create(finitePublisher.getMessages())
|
||||
// With 3 messages and batch size set to 2, we'll need to grab 2 batches.
|
||||
StepVerifier.create(finitePublisher.getMessages().limitRate(2))
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue1))
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue2))
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue3))
|
||||
@@ -198,7 +199,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
|
||||
2,
|
||||
FoundationDbMessageStore.getPresenceKey(SERVICE_IDENTIFIER, Device.PRIMARY_ID),
|
||||
presenceRenewalExecutorService,
|
||||
MESSAGES_AVAILABLE_WATCH_KEY,
|
||||
@@ -282,7 +282,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
|
||||
2,
|
||||
FoundationDbMessageStore.getPresenceKey(SERVICE_IDENTIFIER, Device.PRIMARY_ID),
|
||||
presenceRenewalExecutorService,
|
||||
MESSAGES_AVAILABLE_WATCH_KEY,
|
||||
@@ -330,7 +329,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterThan(SUBSPACE_RANGE.end),
|
||||
100,
|
||||
FoundationDbMessageStore.getPresenceKey(SERVICE_IDENTIFIER, Device.PRIMARY_ID),
|
||||
presenceRenewalExecutorService,
|
||||
MESSAGES_AVAILABLE_WATCH_KEY);
|
||||
@@ -389,7 +387,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(new byte[]{(byte) 10}),
|
||||
2,
|
||||
FoundationDbMessageStore.getPresenceKey(SERVICE_IDENTIFIER, Device.PRIMARY_ID),
|
||||
presenceRenewalExecutorService,
|
||||
MESSAGES_AVAILABLE_WATCH_KEY,
|
||||
@@ -423,23 +420,30 @@ class FoundationDbMessagePublisherTest {
|
||||
final KeyValue keyValue2 = mockKeyValue((byte) 6, message2);
|
||||
final KeyValue keyValue3 = mockKeyValue((byte) 7, message3);
|
||||
|
||||
final AsyncIterable<KeyValue> batch1 = mock(AsyncIterable.class);
|
||||
when(batch1.asList()).thenReturn(CompletableFuture.completedFuture(List.of(keyValue1, keyValue2)));
|
||||
|
||||
final AsyncIterable<KeyValue> batch2 = mock(AsyncIterable.class);
|
||||
when(batch2.asList()).thenReturn(CompletableFuture.completedFuture(List.of(keyValue3)));
|
||||
final Deque<KeyValue> keyValues = new ArrayDeque<>(List.of(keyValue1, keyValue2, keyValue3));
|
||||
|
||||
final Transaction transaction = mock(Transaction.class);
|
||||
when(transaction.getRange(any(KeySelector.class), any(KeySelector.class), anyInt(), anyBoolean(), any(
|
||||
StreamingMode.class)))
|
||||
.thenReturn(batch1)
|
||||
.thenReturn(batch2);
|
||||
when(transaction.getRange(any(KeySelector.class), any(), anyInt(), anyBoolean(), any(StreamingMode.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
final int limit = invocation.getArgument(2);
|
||||
final List<KeyValue> results = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < limit && !keyValues.isEmpty(); i++) {
|
||||
results.add(keyValues.removeFirst());
|
||||
}
|
||||
|
||||
final AsyncIterable<KeyValue> asyncIterable = mock(AsyncIterable.class);
|
||||
when(asyncIterable.asList()).thenReturn(CompletableFuture.completedFuture(results));
|
||||
|
||||
return asyncIterable;
|
||||
});
|
||||
|
||||
when(database.runAsync(any(Function.class))).thenAnswer(
|
||||
(Answer<CompletableFuture<List<? extends MessageStreamEntry>>>) invocationOnMock -> {
|
||||
final Function<Transaction, CompletableFuture<List<? extends MessageStreamEntry>>> f = invocationOnMock.getArgument(
|
||||
0);
|
||||
return f.apply(transaction);
|
||||
(Answer<CompletableFuture<List<? extends MessageStreamEntry>>>) invocation -> {
|
||||
final Function<Transaction, CompletableFuture<List<? extends MessageStreamEntry>>> retryable =
|
||||
invocation.getArgument(0);
|
||||
|
||||
return retryable.apply(transaction);
|
||||
});
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
@@ -448,7 +452,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
|
||||
2,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
@@ -460,24 +463,21 @@ class FoundationDbMessagePublisherTest {
|
||||
}
|
||||
);
|
||||
|
||||
// Request two messages one at a time and verify that we only fetch a single batch (batch size = 2) and not two batches
|
||||
StepVerifier.create(finitePublisher.getMessages(), 1)
|
||||
StepVerifier.create(finitePublisher.getMessages(), 2)
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue1))
|
||||
.then(() -> {
|
||||
// Wait for the whole batch to have completed publishing before requesting the next message
|
||||
try {
|
||||
assertTrue(latch.await(500, TimeUnit.MILLISECONDS));
|
||||
} catch (final InterruptedException e) {
|
||||
fail(e);
|
||||
}
|
||||
})
|
||||
.thenRequest(1)
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue2))
|
||||
.verifyTimeout(Duration.ofMillis(500));
|
||||
|
||||
verify(transaction, times(1)).getRange(any(KeySelector.class), any(KeySelector.class), anyInt(), anyBoolean(),
|
||||
any());
|
||||
.thenRequest(1)
|
||||
.expectNext(getExpectedMessageStreamEntry(keyValue3))
|
||||
.thenRequest(1)
|
||||
.verifyComplete();
|
||||
|
||||
// We expect three invocations:
|
||||
//
|
||||
// 1. The first batch of 2 messages
|
||||
// 2. The second batch of a single message
|
||||
// 3. The third request for a single message that returns nothing and causes the stream to complete
|
||||
verify(transaction, times(3))
|
||||
.getRange(any(KeySelector.class), any(KeySelector.class), anyInt(), anyBoolean(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -509,7 +509,6 @@ class FoundationDbMessagePublisherTest {
|
||||
CLOCK,
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.begin),
|
||||
KeySelector.firstGreaterOrEqual(SUBSPACE_RANGE.end),
|
||||
1,
|
||||
FoundationDbMessageStore.getPresenceKey(SERVICE_IDENTIFIER, Device.PRIMARY_ID),
|
||||
presenceRenewalExecutorService,
|
||||
MESSAGES_AVAILABLE_WATCH_KEY,
|
||||
|
||||
+8
-17
@@ -599,10 +599,10 @@ class FoundationDbMessageStoreTest {
|
||||
.toList();
|
||||
|
||||
final MessageStream messageStream =
|
||||
foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID, batchSize, Util.NOOP);
|
||||
foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID, Util.NOOP);
|
||||
|
||||
final List<MessageStreamEntry> retrievedEntries = new ArrayList<>();
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages()))
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages()).limitRate(batchSize))
|
||||
.recordWith(() -> retrievedEntries)
|
||||
.expectNextCount(numMessages)
|
||||
.expectNext(new MessageStreamEntry.QueueEmpty())
|
||||
@@ -911,9 +911,7 @@ class FoundationDbMessageStoreTest {
|
||||
.toList();
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID,
|
||||
FoundationDbMessageStream.DEFAULT_MAX_MESSAGES_PER_SCAN,
|
||||
latch::countDown);
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID, latch::countDown);
|
||||
final List<CompletableFuture<Void>> acknowledgeFutures = new ArrayList<>();
|
||||
final AtomicInteger messageCounter = new AtomicInteger(0);
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages())
|
||||
@@ -979,12 +977,7 @@ class FoundationDbMessageStoreTest {
|
||||
|
||||
{
|
||||
final CountDownLatch cleanupLatch = new CountDownLatch(1);
|
||||
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci,
|
||||
deviceId,
|
||||
FoundationDbMessageStream.DEFAULT_MAX_MESSAGES_PER_SCAN,
|
||||
cleanupLatch::countDown);
|
||||
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, deviceId, cleanupLatch::countDown);
|
||||
final List<MessageStreamEntry> retrievedEntries = new ArrayList<>();
|
||||
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages()))
|
||||
@@ -1057,9 +1050,9 @@ class FoundationDbMessageStoreTest {
|
||||
}
|
||||
});
|
||||
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID,
|
||||
FoundationDbMessageStream.DEFAULT_MAX_MESSAGES_PER_SCAN,
|
||||
cleanUpLatch::countDown);
|
||||
final MessageStream messageStream =
|
||||
foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID, cleanUpLatch::countDown);
|
||||
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages()))
|
||||
// We should have discarded the already-inserted ephemeral message
|
||||
.expectNext(new MessageStreamEntry.QueueEmpty())
|
||||
@@ -1216,9 +1209,7 @@ class FoundationDbMessageStoreTest {
|
||||
final long lastTimestampInInactiveEpoch = inactiveEpochMessages.getLast().getServerTimestamp();
|
||||
final List<MessageStreamEntry> retrievedEntries = new ArrayList<>();
|
||||
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID,
|
||||
FoundationDbMessageStream.DEFAULT_MAX_MESSAGES_PER_SCAN,
|
||||
Util.NOOP);
|
||||
final MessageStream messageStream = foundationDbMessageStore.getMessages(aci, Device.PRIMARY_ID, Util.NOOP);
|
||||
StepVerifier.create(JdkFlowAdapter.flowPublisherToFlux(messageStream.getMessages()))
|
||||
.recordWith(() -> retrievedEntries)
|
||||
.expectNextMatches(entry -> entry instanceof MessageStreamEntry.Envelope(MessageProtos.Envelope message)
|
||||
|
||||
Reference in new issue
Block a user