Path: redb-route/redb.Route.Kafka/KafkaConsumedOffsets.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Kafka/KafkaConsumedOffsets.cs
----
using Confluent.Kafka;
using Confluent.Kafka.Admin;
using redb.Route.Abstractions;
namespace redb.Route.Kafka;
///
/// The offsets a Kafka consumer is about to commit for the exchange it handed the route, offered to a Kafka-transactional
/// producer of the same cluster in that route. Committed inside the producer's transaction
/// (SendOffsetsToTransaction), they become visible together with its sends — exactly-once within Kafka — and the
/// consumer does not commit them itself.
///
/// Claimable only while the consumer is still waiting for the route: a copy the route handed on asynchronously (seda,
/// .Threads()) commits after the consumer moved on, with a group generation that may no longer be current, and
/// must not take them. One transaction takes them at most.
///
///
/// Claimable only by the exchange the consumer handed the route. A split part, a multicast branch or any other copy
/// inherits the exchange's properties, this one included, but it is one part of the unit of work: its transaction
/// committing the offsets would commit the whole record or batch before the other parts ran. The consumer commits them
/// once, after the route returned.
///
///
internal sealed class KafkaConsumedOffsets
{
/// Exchange property the consumer offers the offsets under.
internal const string PropertyKey = "KafkaConsumedOffsets";
private const int Open = 0, Claimed = 1, Closed = 2;
private readonly IConsumer _consumer;
private readonly KafkaCommitAction _commit;
private readonly IExchange _owner;
private int _state;
public KafkaConsumedOffsets(IConsumer consumer, KafkaCommitAction commit, string? cluster, IExchange owner)
{
_consumer = consumer;
_commit = commit;
Cluster = cluster;
_owner = owner;
}
/// The consumer's cluster id (); null when the consumer could not read it.
public string? Cluster { get; }
/// The positions to commit: the next offset to read, per partition.
public IReadOnlyList Offsets => _commit.NextOffsets;
/// The consumer's group metadata, read when the transaction commits: it carries the current generation.
public IConsumerGroupMetadata GroupMetadata => _consumer.ConsumerGroupMetadata;
/// Takes the offsets for a transaction; false when another took them or the consumer has finished.
public bool TryClaim() => Interlocked.CompareExchange(ref _state, Claimed, Open) == Open;
/// The transaction that took them did not commit: they are the consumer's again.
public void Release() => Interlocked.CompareExchange(ref _state, Open, Claimed);
/// The transaction committed them: the consumer must not commit them again.
public void MarkCommitted() => _commit.MarkCommitted();
/// The route returned to the consumer: no transaction may take them any more.
public void Close() => Interlocked.CompareExchange(ref _state, Closed, Open);
/// True once the route returned to the consumer: a transaction that did not take them before cannot now.
public bool IsClosed => Volatile.Read(ref _state) == Closed;
///
/// The offsets a Kafka consumer offered on itself, of whatever cluster, if any. Offsets
/// only inherited from the exchange they were offered on are reported through
/// and not returned.
///
public static KafkaConsumedOffsets? OfferedOn(IExchange exchange, out bool inherited)
{
inherited = false;
if (!exchange.Properties.TryGetValue(PropertyKey, out var value) || value is not KafkaConsumedOffsets offsets)
return null;
if (ReferenceEquals(offsets._owner, exchange))
return offsets;
inherited = true;
return null;
}
///
/// A cluster's identity for matching a consumer with a producer: the cluster id its brokers report, read through the
/// client's own connection (). Brokers listed differently on the two ends are still one
/// cluster; a producer of another cluster cannot commit these offsets, they belong to the consumer group of this one.
///
public static async Task ClusterIdOf(Handle handle, TimeSpan timeout)
{
using var admin = new DependentAdminClientBuilder(handle).Build();
var cluster = await admin.DescribeClusterAsync(new DescribeClusterOptions { RequestTimeout = timeout })
.ConfigureAwait(false);
return cluster.ClusterId ?? throw new InvalidOperationException("The Kafka cluster reported no cluster id.");
}
}