Class JetStreamConsumerGroupPositioner
IConsumerGroupPositioner for Dapr's pubsub.jetstream, over a
NATS connection of its own to the component's server (workmanager#132). It reads
the topic's stream so that a subscriber that is behind is served from it
(TopicDaprSubscriptionRegistry) and replay_since works, as
on Redis. It moves nothing: where a new subscription starts is the component's
deliverPolicy (new: at the stream's tail).
public sealed class JetStreamConsumerGroupPositioner : IConsumerGroupPositioner, IAsyncDisposable
- Inheritance
-
JetStreamConsumerGroupPositioner
- Implements
- Inherited Members
Remarks
How Dapr subscribes (components-contrib 1.19 pubsub/jetstream): one stream
holds every topic as a subject, and each subscription adds an ephemeral push
consumer of its own (a random name, a _INBOX deliver subject, the topic as
its filter subject, explicit acks), handles one message at a time and acks it
once the gateway answered Success. Stream ids here are the stream's
sequence numbers in decimal, which StreamIds orders as it orders
ms-seq; Start stands for "before the first".
The consumer has no name the gateway could know. The positioner finds it as the
consumer that appears on the topic after PositionAtTailAsync(string, CancellationToken),
which the registry calls right before Dapr subscribes. Where more than one
appears (another gateway pod opening the same topic within the same moment) it
cannot tell, and a subscriber that falls behind then ends with
RESOURCE_EXHAUSTED, as with no positioner at all.
The consumer's position stands in for the Redis group's: its last delivered
stream sequence is LastDelivered, and every entry of
the topic after its ack floor up to there is reported pending. That includes
entries already handled whose ack the server has not seen yet; the registry
recognizes those by identity (they are among the entries it handled last), so
the subscriber gets each once. The consumer holds at most its
maxAckPending unacked, which therefore has to stay below
MaxPending (the positioner warns when not).
Entries are read with JetStream's batched direct get ($JS.API.DIRECT.GET,
NATS 2.11 or later, the stream needs allow_direct): one request returns a
page of the topic's messages from a sequence or a time on, without creating a
consumer. A stream that does not allow direct gets cannot be read: a subscriber
that falls behind then ends as without a positioner.
Constructors
JetStreamConsumerGroupPositioner(string, string?, JetStreamPositionerOptions, ILogger<JetStreamConsumerGroupPositioner>)
public JetStreamConsumerGroupPositioner(string url, string? stream, JetStreamPositionerOptions options, ILogger<JetStreamConsumerGroupPositioner> logger)
Parameters
urlstringThe component's
natsURL.streamstringThe component's
streamName; null or empty: the stream whose subjects cover the topic.optionsJetStreamPositionerOptionsTiming and page sizes (JetStreamPositionerOptions).
loggerILogger<JetStreamConsumerGroupPositioner>Logger.
Methods
AckAbandonedPendingAsync(IReadOnlyCollection<string>, TimeSpan, Func<string, RetainedEntry, ValueTask<bool>>?, CancellationToken)
Nothing to ack: each subscription has its own consumer, which takes what was pending to it along when it goes; no closed subscription takes a live subscription's message.
public Task<int> AckAbandonedPendingAsync(IReadOnlyCollection<string> topics, TimeSpan minIdle, Func<string, RetainedEntry, ValueTask<bool>>? redeliver, CancellationToken cancellationToken)
Parameters
topicsIReadOnlyCollection<string>minIdleTimeSpanredeliverFunc<string, RetainedEntry, ValueTask<bool>>cancellationTokenCancellationToken
Returns
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
PositionAtTailAsync(string, CancellationToken)
Notes the consumers the topic has now and starts looking for the one Dapr
adds for the subscription about to open. Positions nothing: returns null
(where the subscription starts is the component's deliverPolicy).
Never fails the subscription: without the consumer only catch-up is lost.
public Task<string?> PositionAtTailAsync(string topic, CancellationToken cancellationToken)
Parameters
topicstringcancellationTokenCancellationToken
Returns
ReadAfterAsync(string, string, int, CancellationToken)
AnchorPresent says whether nothing after
after was removed: the stream's first sequence, read
after the page, is at most one past it. A stream trims from its start
(max_age, max_bytes), so that is what the caller relies on;
the anchor itself may be another topic's message. As on Redis,
Start is never present.
public Task<StreamPage?> ReadAfterAsync(string topic, string after, int count, CancellationToken cancellationToken)
Parameters
topicstringafterstringcountintcancellationTokenCancellationToken
Returns
ReadFromAsync(string, string, int, CancellationToken)
Up to count entries of topic's stream
from the id fromInclusive on, oldest first; null when this
backend cannot read a stream. An entry without a payload comes with empty
Data, so the caller sees every id. Reads only:
no consumer group moves.
public Task<IReadOnlyList<RetainedEntry>?> ReadFromAsync(string topic, string fromInclusive, int count, CancellationToken cancellationToken)
Parameters
topicstringfromInclusivestringcountintcancellationTokenCancellationToken
Returns
ReadSinceAsync(string, DateTimeOffset, int, long, CancellationToken)
The entries topic's stream still retains from
since on (the stream's own clock), oldest first; null when
this backend cannot read a stream's history. Stops reading once it holds more
than max entries or more than maxBytes
payload bytes, so the caller sees the overrun without the whole stream in
memory. Reads only: no consumer group moves. For SubscribeRequest.replay_since.
public Task<IReadOnlyList<RetainedEntry>?> ReadSinceAsync(string topic, DateTimeOffset since, int max, long maxBytes, CancellationToken cancellationToken)
Parameters
topicstringsinceDateTimeOffsetmaxintmaxByteslongcancellationTokenCancellationToken
Returns
SnapshotAsync(string, int, CancellationToken)
Where this pod's consumer group stands on topic's stream:
the last id the sidecar read, and the ids up to it that are still pending
(read, not acked), oldest first, at most maxPending. Null
when this backend has no stream to read, or no group on the topic. Reads
only. For a subscriber that is behind
(TopicDaprSubscriptionRegistry): what the topic's delivery has
not handled yet is pending or comes after the last read id.
public Task<GroupSnapshot?> SnapshotAsync(string topic, int maxPending, CancellationToken cancellationToken)
Parameters
topicstringmaxPendingintcancellationTokenCancellationToken
Returns
UnblockSidecarReadersAsync(CancellationToken)
No blocking reader is left behind by a closed JetStream subscription: nothing to wake.
public Task<int> UnblockSidecarReadersAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationToken