ElasticsearchEventSink

The event search Sink of one aggregate type: writes each commit the sink subscription hands it as one stream document (ElasticsearchEventDocuments) of the type's events index (ElasticsearchNames.events, ElasticsearchEventIndex), which the events queries of an Elasticsearch store read.

  • Batch: one _bulk request per write, every record a create of its document. The sink subscription hands write the records of every aggregate's commits waiting together, at most maxBatch (Sink.maxBatch), so a sustained load is written in a few large requests instead of one per commit, and one stamp per aggregate whose attributes changed serves the whole write.

  • Idempotent by id: a document's id is the commit's identity (ElasticsearchNames.streamId: tenant, aggregate id and version), and a record written before is answered 409 by its create, which counts as written: a record written again changes nothing, whether a retry, a replay or an old owner of the partition writes it.

  • Durable on return: Elasticsearch answers a bulk request once each document is in its shard's translog. A write does not wait for the next refresh (searches see it within the index's refresh interval, 1 s by default), unless the store's refresh is true (an immediate refresh, for tests): waiting for one would hold each aggregate's next commit for up to the interval.

  • Failure: a request that fails, or any item it did not write, fails the call (write, stampAll) as the store's other writes fail (ElasticsearchFailures.itemFailure), the item a retry may get past soonest deciding: an unavailable cluster (502, 503, 504, a connection failure) as a StorageUnavailableException, which the runtime classifies systemic; a rejection (429, a full queue) as a StorageBusyException, backpressure; a document refused (400) as a StorageRefusedException, which parks its aggregate; any other as an IllegalStateException. A conflict an update's retries did not get past fails as a StorageUnavailableException too, where the store's guarded writes answer it themselves: the stamp's retry reads the document again. The records a failed write wrote stay; the retry's creates of them answer 409.

  • Access attributes: a document holds its record's attributes with the version they are of (StateEventRecord.accessVersion). After a write, each record whose attributes differ from those of the aggregate's stream before it (the record before it in the write, the last one this Sink wrote of the aggregate, capacity of them remembered, or else that stream's document) stamps them over the aggregate's earlier documents (stamp), the last such record of each aggregate only. A stamp writes each document by id, read in real time (stampAll), so the documents just written are among them without a refresh. Records are written in log order, so the Sink follows the history it writes as the log does (me.ahoo.wow.spi.access.AccessStamps): a record read before a stamp of the log and one read after it differ, and the later version wins.

The index is an alias of one concrete index (ElasticsearchIndices), made sure of before the first write as the store's DDL mode says (auto creates the pair, validate requires it, none assumes it), and its primary waited for, as the snapshot indices are. Every _bulk carries require_alias, so a write to a missing alias is refused, naming it, instead of having Elasticsearch create an index with a mapping it guesses; under auto the next write creates the pair again, with a warning. A stamp's updates by id reach the alias's write index alone, so the alias holds one index: auto and validate refuse an alias of several, and under none nothing checks it.

Types

Link copied to clipboard

Properties

Link copied to clipboard

The type's events index.

Link copied to clipboard
open override val maxBatch: Int

The most records one write takes, one _bulk request: see Sink.maxBatch.

Link copied to clipboard

Functions

Link copied to clipboard
suspend fun prepare()

Makes sure of the events index, once, as the DDL mode says.

Link copied to clipboard
open suspend override fun stamp(ref: AggregateRef, access: Map<String, Any?>, asOf: Int, from: Int, until: Int): Int

Writes access as of asOf over the documents of ref from from until until: stampAll of one stamp.

Link copied to clipboard
open suspend override fun stampAll(stamps: List<AccessStamp>): Int

Writes each of stamps over its documents, each by its id (ElasticsearchNames.streamId): _bulk requests of at most maxBatch updates, each guarded in its script, which writes a document that has no ElasticsearchEventIndex.ACCESS_VERSION, one before the stamp's asOf, or asOf with other attributes (a new model's). Returns how many it wrote.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
open suspend override fun write(records: List<StateEventRecord>)