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
_bulkrequest per write, every record acreateof 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
409by itscreate, 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
refreshistrue(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'screates of them answer409.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.
Properties
The most records one write takes, one _bulk request: see Sink.maxBatch.
Functions
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.