KafkaPartitionedSource

class KafkaPartitionedSource(consumers: (String) -> Consumer<String, ByteArray>, pollTimeout: Duration = KafkaOptions.DEFAULT_POLL_TIMEOUT, dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, val offsetGap: OffsetGapPolicy = OffsetGapPolicy.FAIL, observer: Observer = Observer.NONE, topicIds: suspend (String) -> String? = { null }) : PartitionedSource, AutoCloseable

The PartitionedSource over Kafka consumers: made by create from KafkaOptions, consumers without a group that commit nothing, since the runtime assigns the partitions and keeps the offsets.

  • partitions and offsets ask one consumer of their own (consumers makes it on the first call), one call at a time, since a Kafka consumer is not safe for concurrent use.

  • partitionOf is the producer's default partitioner: murmur2 of the key over the partition count.

  • read of a partition the source does not read yet joins the topic's shared consumer (SharedTopicConsumer): the tails of every partition of a topic this node owns go through one consumer, assigned those partitions by hand. A second read of a partition already joined, such as a subscription catching up, and a bounded read run a consumer of their own for as long as their flow is collected: assigned the one partition, it seeks to from and polls, each poll at most pollTimeout on a virtual thread, emitting what it got; the consumer closes when the flow ends or is cancelled, without waiting for a fetch still in flight.

  • readAt runs one consumer of its own for the call, whatever the number of offsets: it seeks to each offset still wanted, in ascending order, and takes every wanted one a poll returns before it seeks again. Offsets the partition does not hold are left out before it seeks, so they are no gap. It closes the consumer without waiting for a fetch still in flight. On a broker that stops answering after the offsets, its polls come back empty and it ends only when its caller is cancelled, between two polls; the offsets requests themselves fail after the client's default.api.timeout.ms. Its callers in the runtime have no bound of their own: a run's start (ParkedAtStart) and the retry scan read in the run's scope, which the run's stop cancels, and a reconcile stuck on the start fences the group after wow.cluster.reconcile-timeout and fails the node's liveness after wow.health.liveness.stall-timeout.

Either way, an offset before the partition's earliest retained offset is a gap: the read never steps over it silently, but logs an error, counts it in offsetGaps and does what offsetGap says, by default failing with an OffsetGapException. A partition that ends before the read's offset, and a record at a lower leader epoch than the one before it, are another topic of the same name: whatever offsetGap says, the read ends, counted and logged the same way (OffsetGapPolicy), the shared tail and a read of its own alike (LeaderEpochs).

Each consumer is named after what it does (consumers takes the name, client.id with KafkaOptions): metadata for partitions and offsets, tail.<topic> for a topic's shared consumer and read.<topic>-<partition> for a read of its own, each with a number unique in the process, so an operator tells them apart in the client's logs and metrics, and the client registers each once.

close closes the shared consumers and the consumer of partitions and offsets, each before it returns: a metadata call in flight is woken out of its wait, so the consumer closes under its lock once the call ends. The other reads close their own. Every call after close is refused, and opens nothing.

Constructors

Link copied to clipboard
constructor(consumers: (String) -> Consumer<String, ByteArray>, pollTimeout: Duration = KafkaOptions.DEFAULT_POLL_TIMEOUT, dispatcher: CoroutineDispatcher = VirtualThreads.dispatcher, offsetGap: OffsetGapPolicy = OffsetGapPolicy.FAIL, observer: Observer = Observer.NONE, topicIds: suspend (String) -> String? = { null })

Types

Link copied to clipboard

Properties

Link copied to clipboard

What a read does when its partition does not hold the offset it is to read from.

Link copied to clipboard

The offset gaps this source's reads met, each logged as an error: a counter to alert on.

Functions

Link copied to clipboard
open override fun close()
Link copied to clipboard
open suspend override fun incarnation(topic: String): String?

The topic's id, which the broker gives each creation of it: another once it is deleted and created again.

Link copied to clipboard
open suspend override fun offsets(topic: String, partition: Int): PartitionOffsets
Link copied to clipboard
open suspend override fun partitionOf(topic: String, key: String): Int

The partition the producer's default partitioner puts a message keyed key in: murmur2 of the key's UTF-8 bytes over the topic's partition count, as KafkaTopicProducer sends with no partition of its own.

Link copied to clipboard
open suspend override fun partitions(topic: String): List<Int>
Link copied to clipboard
open override fun read(topic: String, partition: Int, from: Long): Flow<SourceMessage>

open override fun read(topic: String, partition: Int, from: Long, until: Long): Flow<SourceMessage>

Polls a consumer of its own until its position reaches until, so a partition ending in a gap completes too.

Link copied to clipboard
open suspend override fun readAt(topic: String, partition: Int, offsets: Collection<Long>): Map<Long, SourceMessage>