InMemoryTopics

class InMemoryTopics(partitions: Int = DEFAULT_PARTITIONS) : TopicProducer, PartitionedSource, TopicAdmin

Topics in memory, a TopicProducer and a PartitionedSource at once, for tests and samples, as the other in-memory backends: a topic is created, or created with partitions partitions when it is first written or read; a message goes to the partition it names, else to the one its key's hash picks (partitionOf), and every partition keeps what it was sent, from offset 0, up to its last retention messages, until retain drops its oldest messages as a broker's retention would.

failure, when set, makes every send throw it without writing anything, as a broker that does not acknowledge.

As a TopicAdmin, it describes the topics created so far and creates the ones asked for; grow adds partitions to a topic, as an operator does to a Kafka topic.

Public, unlike the other in-memory backends: the test server takes one to share between the nodes it starts (wowServer(topics = …)) and gives each node's (server.bean<InMemoryTopics>()), so a test reads what the sagas sent and the streams published.

Constructors

Link copied to clipboard
constructor(partitions: Int = DEFAULT_PARTITIONS)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Thrown by every send while set.

Link copied to clipboard

The most messages a partition keeps, at least one (DEFAULT_RETENTION unless set): once a write takes a partition past it, the partition drops its oldest messages, as a broker's size retention drops them, and the topic says so once, the first time. A read from an offset no longer kept starts at the oldest one kept, the offset gap a reader handles as it does on a broker (PartitionedSource.read). So the topics stay bounded however long the node runs: they cannot keep a message until its readers have read it, since a source knows no reader's position, which each keeps in its own checkpoint.

Functions

Link copied to clipboard
open suspend override fun create(topics: Map<String, Int>)

fun create(topic: String, partitions: Int = this.partitions)

Creates topic with partitions partitions, unless it exists.

Link copied to clipboard
fun grow(topic: String, partitions: Int)

Gives topic partitions partitions, the new ones empty; a topic never has fewer than it had.

Link copied to clipboard

Every message topic holds, partition by partition, each in offset order.

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 of key in topic: its String.hashCode, not the log's hash (me.ahoo.wow.spi.log.Partitioner). For an aggregate's key in ASCII, as the ids of the test fixtures are (the text the log hashes when its names need no escape), the two hashes' low bits always differ: String.hashCode's is the parity of the characters' low bits, FNV-1a's, over the same bytes, that parity flipped by its odd offset basis. Beyond ASCII they need not (String.hashCode reads UTF-16 units, FNV-1a UTF-8 bytes). So with an even number of partitions here, the log's being a power of two, no such aggregate is in the same partition of a topic and of the log: a test that needs one in both gives the topic an odd number of partitions (3, say) and picks an aggregate the two agree on.

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

Drops the messages of partition of topic before from, as a broker's retention does: they are no longer read, and the partition's offsets start at from. Offsets do not move.

Link copied to clipboard
open suspend override fun send(topic: String, messages: List<TopicMessage>)