Skip to Content
OpenETL
Internals

Data engine and state

Explain engine lanes, exchanges, watermarks, keyed state, group windows, OVER and dedupe.

Edit on GitHub

This page assumes knowledge of distributed data processing. First, read Control plane and runners.

The engine

openetl-engine is the Rust data engine. The runner starts one engine process per job and communicates through two socket types. The control channel uses EngineService from proto/openetl/engine/v1. Data edges carry frames with a 12-byte header and Arrow IPC payload.

The engine can encode an Arrow batch as metadata and references to the batch's existing buffers (crates/engine/src/ipc.rs). Vectored writes send the frame header and those buffers without combining them into an additional payload buffer (crates/engine/src/frame.rs).

The runner sits between the connectors and the engine. Its source pump is the source connector's emitter, and sends what the source reads to the engine's inbound edges. On each lane, the engine:

  • turns raw rows into Arrow, inferring each stream's schema from what it sees and only ever widening it, and coerces values to the types a source declares (crates/normalize);
  • sets aside the records it cannot take, each with a reason and its code. They are JSON lines under the pipeline's _quarantine/ in object storage (format). It counts them by cause in each epoch's report;
  • applies the pipeline's schema policy, gives tables and columns their destination names, which never change once given, and adds the metadata columns, _openetl_id among them;
  • holds back the columns and cells the destination cannot take, and keeps their values for a later release;
  • hands the rows to writer lanes, which write to the runner, with barriers in band, or stage Parquet files, for a destination that loads them and for the built-in parquet destination.

The runner's sink pumps hand each writer lane's batches to the destination connector's writer.

A stream's schema comes from its rows, so a read with no rows gives it none, and a destination has no table to replace for an overwrite. So the source pump marks the barrier where a stream's read ends (Barrier.ends_read), at least on the last of its lanes. Once every lane has passed that barrier, an overwrite stream (StreamPlan.overwrite) with declared types and no schema takes the schema that an empty batch of its declared columns shapes to (Shaper::type_declared). The lane that passed last shapes it before it sends the barrier on. On workers, it proposes the schema to the coordinator, as it does any change. EpochReady carries the schema, so the coordinator applies it before that epoch's commit, and the destination replaces the table with an empty one. One worker reads all of an overwrite stream, so its engine sees every row of the read, and a stream with rows on any lane keeps the schema they give. A transform's outputs, and streams without declared types, get none.

Partitioning and exchanges

internal/plan plans each stream's path from its source to the destination's writer. Each edge of the path has a partitioning: how its rows are spread over the lanes of the operator it feeds. A source's stream is split by its primary key where the catalog says that its splits partition it by key (splits_by_key), unless it is read with cdc, and is unpartitioned otherwise. Each operator states what it needs of its input. The writer of a keyed stream needs its primary key, so that one writer lane gets all of a key's rows. The writer of a stream whose writer lanes keep the versions of its records needs its key by key group (Need.KeyGroups), which splits do not meet, so on more than one writer lane it always has an exchange. A statement's need comes from the engine's plan of it (need), and a row-wise statement needs nothing.

An exchange by key group is placed only before an operator whose need its input does not meet. A partitioning by columns K meets a need for columns J when K is a subset of J. So an operator that exchanged rows by its keys k feeds a writer that needs (window_start, k) with no second exchange. A statement keeps a partitioning while its columns pass through unchanged, renamed or not (passes in the engine's plan of it); otherwise its output is unpartitioned. A destination with one writer lane takes every lane's rows, which meets every need.

The same plan gives openetl plan its partitioning section, and the runner the exchanges of each attempt (StreamPlan.exchange in StartJob). With one writer lane, the runner sends none. The engine sends each row of a stream with an exchange to the writer lane that owns the key group (openetl.kg.v1) of its values of the exchange's columns, which must be of the stream's primary key. On workers, that writer lane may be another engine's, as the exchange between engines describes.

Lanes and writer lanes own contiguous ranges of the pipeline's key groups, picked together (crates/engine/src/layout.rs, and plan.NewLayout for the runner). Writer lane w owns the w-th of the writer lanes' equal ranges. Each writer lane's range is cut again among the lanes that feed it, in lane order: each writer lane gets one lane, and each other lane goes to the writer lane with the most key groups per lane. A lane without an exchange writes to the writer lane whose range holds its own. An attempt has no more lanes than key groups.

Watermarks

A stream that a SQL transform reads can have event time (StreamPlan.event_time). For such a stream, the runner's source pump gives each split a lane as the read starts, and the split joins that lane with a SPLIT frame before anything else of the stream. The frame carries the watermark the attempt starts the split at. A split that another split adds joins that split's lane. Before a batch of another split than the last one, the runner sends a SPLIT frame that names it. The pump also sends SPLIT frames to raise a split's watermark: when the split is idle in a continuous read, and when it completes. These go through the pump as the split's events, so a split that waits at a barrier has them sent after the lane's barrier.

A retry within a sync does not read again a stream that completed earlier in the sync. If the stream is read in the order of its time, its splits still join their lanes, at the watermarks the record has for them. A lane with no split holds nothing back, so without them the stream's windows would close as at the end of input.

crates/engine/src/watermark.rs has the two halves. Splits is a lane's splits of one stream: each split's watermark is the latest event time it has read less the delay, or what its SPLIT frames raise it to, and the lane's is the least of them. The splits a read starts with join together, so a restore never starts a lane above one of its splits. Senders is whatever takes a stream from several senders: it starts at the stream's floor, the least watermark of the attempt's splits, and takes the least of every sender, counting each one from the start. A sender's watermark never goes back.

On a lane, each advance reaches the statements that read the stream through Stage::watermark, after the batch that moved it, and then goes to the writer lanes in the outputs' Out::Watermark, as barriers do. A writer lane aligns a watermark with the barriers as it aligns rows. So, at a barrier, it has the least watermark that each lane had at its own barrier. At each barrier of the stream, a lane reports its splits' watermarks, which EpochReady.watermarks carries to the runner, and the checkpoint record keeps with each split. A worker's share carries them in its EpochReady.

Keyed state

crates/state is the state layer for stateful operators. The group window operator uses it (Group windows).

Operators reach state through one trait, StateBackend, which has four primitives: a value per key, rows per key by the time they fall due, a set with expiry, and timers. Each call names the operator and the key's group. A Layer also puts a key's one row at a time (Layer::put_row), in place of the one before, which Layer::row reads by its key in one batched read of the store, as get reads values; rows reads a key's rows at a time by a scan. A lane opens a Layer for its range of key groups. The engine opens one for each stateful operator of the lane, on runs in a directory of the operator's own, since each operator aligns its barriers on its own.

The layer keeps the open epoch's changes in memory. If the epoch fails, the layer discards them. At a barrier, it adds them to its pending set: its changes since it last committed to its store, which reads see over the store. While the pending set's encoding is within the lane's share, the barrier commits nothing to the store, and the snapshots that the store returns carry the set in StateSnapshot.pending (versioned and checksummed, crates/state/src/pending.rs). Past the share, the barrier commits the set to the store as that epoch's changes, and the set starts empty. A record so carries every change since the lane's last run, and a lane never lags its record. The shares add up to 1 MiB, or a 64th of Limits.memory_bytes if that is less (tests set another limit in StartJobRequest.state_pending_bytes, 0 for a run at every barrier), split evenly by the job's stateful operators and by each one's lanes as their key groups are of the pipeline's (PendingLimit), so every engine of an attempt computes its own lanes' shares, and the pending sets in the coordinator's record take at most that many bytes of encoding together. The operators that keep streams' versions share one pool instead (Versions at one cursor value). The bound is on the pending sets alone: the rest of the record, the runs its snapshots name, watermarks and positions among it, is beside it. At each barrier, the pending set drops what the horizons passed, and deletes of keys the store cannot hold (Store::may_hold: a run not opened yet may hold anything). A worker's share of an epoch carries its lanes' snapshots, pending sets with them, to the coordinator.

A restore opens only the snapshots that a record names, for any range of the pipeline's key groups, and takes the entries of their pending sets in its own key groups as its pending set. It opens a run as a read first needs it. So the state of a key group can move to another lane, or to another worker. A redo restores the record before its epoch, pending sets too, and writes nothing.

Keys start with their key group, so a range of key groups is a range of keys. In each key group, rows sort by the time they fall due, so the rows that a watermark releases are one range. Timers sort by time across the lane's key groups, so a restore reads no timer before it is due.

The engine's store is RunStores (crates/state/src/runs): runs of its own on object storage, at the job's state URL, {org}/{pipeline}/_state/ next to the checkpoints (crates/engine/src/state.rs). Each stateful operator has a RunStores of its own, whose block cache its lanes share, in a directory named by its ID (state::dir): the ID itself where it has only letters, digits, - and _, else x. and the ID in hex. A name over 255 bytes, which file systems refuse, is h., the ID's first 64 bytes, and a hash of the ID. A lane's snapshot is its key-group range, the layout, the sections of runs it reads and each operator's horizon in the run store's encoding, and the names of those runs, which retention reads without decoding the rest. A record names an operator's snapshots on every lane once any of its lanes names a run.

A snapshot leaves out each section that is dead, and a lane merges some of the others (Merges). A section is dead in these cases:

  • An operator moves its horizon with expire, as its watermark moves. Its rows and timers at or before the horizon, and its set's entries that expire at or before it, are gone. Reads pass over them, and the layer writes no change of them. The snapshot keeps each operator's horizon in each key group.
  • A section of rows, timers or a set is dead once the horizon of each of its operators, in each of its key groups, is at or past its latest entry. A section that holds entries on both sides of a horizon stays, and reads leave out the entries the horizon passed. A set's entry that never expires never dies.
  • A values section is dead once each of its entries is dead: a key that a newer section the lane names also holds, or a delete whose key no older section the lane names holds. A lane knows what a values section holds once it writes it, as 128-bit hashes of its keys. It reads the values sections it restored, each whole, at its first barrier that writes a run. Until then, it takes such a section to hold every key. The reads count as restores, and hold no more at once than the block cache's bound.
  • A barrier whose run would hold only dead sections writes no run, and the operator's epoch reports no barrier request.

An object stays while any kept record names one of its sections. So after a rescale, it stays while any lane names its part of it.

Merges

A lane merges sections by kind, since each kind dies in its own way (crates/state/src/runs/merge.rs):

  • Timers are never merged. They die whole as the horizon passes them.
  • Rows merge only among sections of one time, whose rows are all at that time, whichever operators' rows they are: a read's versions (Versions at one cursor value), a window's rows at its end, or the rows of OVER or a dedupe at a coarse event time. Such sections merge by size, and the output dies whole with them as the horizon passes their time. A section of rows at several times is left to die whole. A merge leaves out the rows that the horizon has passed. It drops deletes where no older section of rows may hold a row at that time.
  • Values merge only while a lane names more than 8 values sections, since most windows' values are dead before a merge would reach them. A merge that takes the lane's oldest values section drops deletes.
  • A set whose entries expire merges only within a time bucket. The lane cuts its live expiries into 8 buckets, aligned in time and a power of two wide. Sections of the newest bucket with at most 256 KiB stored merge. Older buckets are left to die whole. A merge leaves out the entries that the horizon has passed.
  • A set kept for good merges by size.

A merge by size takes sections of one tier: sections within a factor of 4 of each other in stored bytes. Values merge once two of one tier are next to each other, and a set or rows of one time once four are. A lookup of a row passes over each section whose least and greatest time leave out the row's time, without its filter. A merge takes sections that are next to each other in the lane's order, newest first, and never part of an epoch's sections. Its output holds the newest entry of each key, as of its newest input's epoch, so reads find it where they found its inputs.

Each lane runs at most one merge at a time, in a task of the engine, which starts after a barrier. The merges in flight of an operator's lanes take at most the block cache's bound of input bytes together, and the lanes take that room in turns: a lane refused for want of room waits in line, the first in line takes room once its merge fits, and the others take only room its merge leaves. So a lane whose barrier comes late in each epoch still merges, rather than the lanes before it taking whatever their merges release. A lane asks at each of its barriers, so a place whose lane has not asked for a whole epoch lapses, and a lane that stops coming to barriers holds no one up. A merge holds its output in memory until a barrier writes it. A merge only saves later reads, so one whose reads fail is dropped, of any kind of section, and the barrier goes on. A merge whose storage failed (Error::Store) is tried again at a later barrier: after 2 epochs, then 4, 8 and so on, at most 64. After five such failures in a row, the lane merges those runs no more until the next attempt, as it does an unreadable input's. The store counts both (Counts::merges_failed, merges_abandoned), and the engine logs a warning once for each lane that gives up. A merge that finds an input's run gone or unreadable is dropped, and that run's sections merge no more: the reads that need the run find it so, as they would without the merge. A merge's own mistake still fails the barrier, as Error::Invalid, which the engine reports as INTERNAL: a merge task that panicked, or one that lost track of its keys. That barrier writes the merge's output as g{generation}/e{epoch}/kg{first}-{last}.merge.run, beside its own run, and the snapshot names the output in place of the inputs. A merge that is not done waits for a later barrier, which names the output for its own epoch. A merge whose input dies first is dropped.

A merge's output counts only once a committed record names it. Retention keeps a run of the attempt's own generation from an epoch after its newest record, so an output that a barrier is about to name stays. After a failed attempt, an output that no record names is an orphan, and retention deletes it. The inputs stay while a kept record names them, so a restore of the record before reads them.

Each operator's epoch reports the merges' writes (merge_write) and reads (merge_read) beside the barrier's. Write amplification is the bytes that barriers and merges write for each kind of state, over the bytes of its changes. crates/state/tests/workloads.rs measures it for each kind.

Early flushes and the disk cache

A lane flushes its pending set and its open epoch of changes early past a threshold on the two together, which Layer::set_flush_at sets to its part of the job's memory: an eighth of Limits.memory_bytes, divided among the operators and the engine's keyed lanes, and at least 64 KiB (JobState::flush_at). With no memory limit, flush_at is 0, and no threshold flushes. The threshold is not a hard bound: writes can pass it, and the layer's next read writes the changes as a run of their own, g{generation}/e{epoch}/kg{first}-{last}.earlyN.run, through Store::flush. The pending set goes first, as a run of its own (Store::flush with kept), filtered only by the horizons the store holds, since a discard undoes the open epoch's moves; then the open epoch's changes, as another. So reads take the open epoch's changes, the pending set (empty after a flush until the barrier), the early runs newest first, and older runs. The barrier's snapshot names the early runs. It carries what the lane changed after the flush as its pending set while that fits the lane's share; past the share, the barrier commits those changes to the store as its own run. Layer::discard stops reading the epoch's own early runs and keeps the pending set's, which the next barrier names. The keyed lane also flushes once it holds 256 KiB of changes and pending set, and the allocator holds more than seven eighths of the limit (memory::pressure). The lane reads an early run's sections from then on, ordered within their epoch by N, under the barrier's run. The barrier's snapshot names them with its own, so they count only once a record does. Early runs that no committed record names, as after a failed attempt, are orphans for retention, as a merge's unnamed output is. A merge takes sections of one kind, and never part of an epoch's sections of that kind. So it takes that kind's sections of all of an epoch's early runs and its barrier's run, or of none of them, and leaves their sections of other kinds where they are. A redo writes nothing, so it never flushes early. Each keyed lane reports what its state holds to JobState::hold, which also sets the allocator's count of state (memory::set_state). If the engine is near its limit and the job's state holds more than half of the limit, the lane fails the job with RESOURCE_EXHAUSTED, before the allocator ends the engine.

With Limits.scratch_dir and a scratch_bytes of at least 2, the job's RunStores share a DiskCache (crates/state/src/runs/disk.rs) in scratch_dir/state, bounded by half of scratch_bytes. With neither, or if the cache's directory cannot be made, there is no disk cache: the engine logs a warning and reads object storage for every block that is not in memory. Each operator's stores use a namespace of their own. The cache keeps byte ranges of runs: blocks that a read fetched or a barrier, flush or merge wrote, and the indexes and filters of runs that a lane opened. It keeps one only if it is within the cache's bound and its write succeeds; otherwise a later read of it goes to object storage. Entries go in segment files with an XXH3 checksum. A short read or a failed checksum is a miss, and the block's own checksum is checked again when it is decoded. Past the bound, the oldest segment goes whole, and an entry read from it is written again to the newest. The cache's index is in memory only, and the cache empties its directory when it opens and removes it when it is dropped. Reads that the disk answered count as disk, not as requests. The runner makes each engine's scratch directory, oe-* in the scratch folder of its --cache, which only the runner's user may write (internal/engineclient/process.go). It holds an flock on the directory's lock file while the engine runs, and removes the directory when the engine stops. Before it makes one, it removes each oe-* directory there whose lock is a regular file that no process holds. It opens a lock without blocking, so a FIFO cannot hold it up.

The record keeps an operator's snapshots also when its lanes name no run. A snapshot holds the operator's horizons beside its runs, and a group window's floor is one of them. At a barrier where a group window's watermark is the end of input, its keyed lanes report no snapshot. Every window has then closed and gone, at every lane's barrier alike. A later read of the stream, as of a stream read whole at each sync, starts the windows again. OVER and a first-row dedupe report none there either, nor does a latest-row dedupe over a stream read whole. A latest-row dedupe over another read keeps its snapshot at the end of input: its keys' latest rows never close, and a stream read incremental by a cursor other than its event time reaches the end of input at each sync's end, so the next sync goes on from them.

A lane restored from lanes whose horizons differ keeps each key group's own horizon. In each key group, an operator takes the horizon there as the least of its watermark (StateBackend::horizon). So it never sets a timer that the horizon passed.

A pipeline version's SQL plan lists the operators of earlier versions that it dropped or replaced (SqlPlan.dropped). An attempt, and a redo, restores the record without their snapshots (internal/runner/operators.go). So the engine never opens them, the records after it stop naming their runs, and retention deletes them. Each mirrored checkpoint lists the operators whose state it names, which the plan reports.

Group windows

A statement whose GROUP BY has a window plans as a GroupWindow (crates/sql/src/window.rs): the row-wise steps before it, the window, the group keys, the aggregates, and the steps after it. Its operator ID hashes what its state means: the stream it reads, the steps, the window, the keys, the aggregates and the state's layout. A change to any of them gives a new ID, which restores no state.

The statement runs on keyed lanes, past an exchange inside the engine (crates/engine/src/dataflow/keyed.rs). Each lane sends each row of the stream to the keyed lane that owns the key group of the row's group keys, by the layout's lane ranges. It sends its barriers, its watermark advances and its end to every keyed lane. A keyed lane aligns its senders at each barrier, as a writer lane does. Its range of key groups lies within one writer lane's range, so its output reaches that writer lane with no second exchange. A keyed lane opens a Layer for each group window it runs, on the window's runs (crates/state): object storage below the job's state URL, in a directory of the window's operator ID. At a barrier, a lane that changed state writes one run of its changes, named by generation, epoch and key-group range, and never written twice. The lane's snapshot names each section of a run that it reads and that is not dead, and goes in EpochReady and from there into the checkpoint record. A run counts once the record that names it is committed. A restore gives each lane the snapshots that overlap its range, on any number of lanes, and a lane opens a run when it first reads from it. A redo opens the state of the record before its epoch, and writes none. If the epoch is the first of an attempt that took a reset, its record names the streams that the reset read again (ResetStreams), and the redo leaves out their positions, their watermarks and the state of the statements that read them, as the attempt did. A restore fails with CHECKPOINT_CORRUPT when the record names an operator's state but none of its snapshots holds a key group of a lane, or names a run that is missing.

A keyed lane's watermark is the least of its senders' watermarks, through Senders, starting at the stream's floor. A lane's watermark advance comes after the rows that moved it. At open, a keyed lane advances its windows to the floor, so a window that closed before the checkpoint, or before the read that the floor carries over, stays closed. In a key group whose state's horizon is later than the window's watermark, the window takes the horizon as its watermark there: a window whose end the horizon passed is closed. Each advance fires the timers it reaches: a window gives its row, and its state stays until the watermark passes its end plus the lateness. The window then moves its state's horizon to the watermark, and the keyed lane sends the watermark to its writer lane as the output's.

On workers with lane commits, each keyed lane records the steps of each window's watermark that its rows meet, for a redo (StartJobRequest.record_window_steps, watermark::Steps). A lane counts the rows it reads of each split from each barrier, so each row has a place in its split's part of the epoch. A batch's rows go to a keyed lane with their split and the place of the first. The keyed lane takes a step where the window's watermark is not the last one that rows of that split met: the place, and the watermark. Without lateness, a tumbling or hopping window takes as its watermark the latest end of a window at or before it (Operator::level), as rows and timers tell no other watermarks apart. At its barrier, each keyed lane reports its steps by split, with its key groups, in OperatorEpoch.steps. The coordinator keeps them in the record's stream, by operator (steps), encoded against the record's splits and floor, after a byte that names the encoding's format (internal/checkpoint/steps.go).

After 1,024 steps of a window's watermark in an epoch (watermark::STEPS), a keyed lane holds the window's watermark until the barrier, and there it catches up with its senders'. Its rows then meet the held watermark, which the steps already have, but for one more step of each split whose rows had not met it yet. So a keyed lane's steps of an epoch are at most 1,024 and one for each split whose rows reach it. A hard cap would need the epoch's splits up front, and a read can add splits.

A redo gives the engine the steps of the keyed lanes whose key groups its committable holds (StartJobRequest.window_steps). A lane that reads a split again counts its rows the same way. It finds for each row the watermark that the row met in the epoch, by the keyed lane that owned the row's key group then. The redo's keyed lane stays at the floor while it reads, takes at once the rows that met the floor, and keeps the others until the barrier. There it advances to each watermark that rows met, in order, and takes those rows after each advance, then advances to closes_at. The rows it keeps count against watermark::Deferred, half of Limits.memory_bytes for the engine's keyed lanes together, by their Arrow arrays' bytes. Rows past that fail the redo with RESOURCE_EXHAUSTED. The cap bounds only these rows, as the mesh's and the block caches' shares bound theirs, and reserves nothing: the allocator (crates/engine/src/memory.rs) counts the whole process against the full limit, and still ends the engine there if the rest of the job holds more than the other half. So each key's rows meet the watermark and the closed windows that they met in the epoch. Rows that met the same watermark may come in another order, which a window without lateness cannot tell apart.

A window's floor, in each key group, is the latest the group's watermark less the lateness has been. A tumbling or hopping window that ends at or before the floor is gone. A session is gone when its end, its last row's time plus the gap, is before the floor. A row of a gone window is late, and a gone session is dropped, with no row, when its key's sessions are next read. A row that would give a session its start is late too, if its own time plus the gap is before the floor. A gone session could have had that start, and its row is in the output.

The floor is the horizon of an operator ID of the window's own in its layer: the window's ID with its lowest bit flipped. That operator holds only the set of rows a tumbling or hopping window took, below, and the floor only moves forward. Every snapshot keeps it by key group, on any number of lanes and workers. Each advance moves it to the watermark less the lateness, where that is later. At open, each key group that a restore brings a horizon gets that horizon less the lateness (StateBackend::expire_in). That horizon is the group's watermark until the operator's watermark passes it.

A group window over a stream whose reads may give a record again, and whose read isn't held to its end, drops the rows a read gives again (keyed::drops_repeats). The runner says so in EventTime.rereads: superset replay, and a primary key or collapse (delivery.Stream.Rereads). Over a stream replayed exactly, the window keeps no rows' IDs, and its state stays its windows'. The lane that reads a row sends, after the record ID, a hash of the row's columns that aren't null, each with its name, but the stream's volatile columns (StreamPlan.volatile_columns, from volatileColumns), as the versions' hash (openetl_normalize::versions::row_hashes), so a column the stream gained since, null in the row, doesn't change it. A row is a repeat where its ID and hash are those of a row the window took while a window it went to is kept. A tumbling or hopping window asks the set under the floor's operator with insert_new, at i64::MIN, to expire at its latest window's end: an entry lives until that window is gone, as the floor says. A session keeps its rows' IDs and hashes in the key's sessions, in a layout of its own (SESSIONS_SEEN), and a row is a repeat where a session that holds its time has them. They merge with their sessions and go with them. So this state grows with the rows of the windows kept, not only with the windows. A row that comes after its windows have gone is late, as any row is. OperatorEpoch.repeats counts the rows dropped. Over a held read, which for a window is one with full_refresh, every window stays open to the end of input, and the set would hold every row read, so the window drops no repeats there.

So a raised watermark.lateness leaves the windows gone under the old one gone, until the watermark less the new one passes the floor. No change of lateness needs the engine to know the old value. A window's expiry timer is set when it closes, by the lateness then. One that fires before a raised lateness has passed is set again. After a lowered lateness, a window's state can stay until its old expiry, and the floor keeps it from taking rows.

crates/engine/src/window is the operator. A window's state is its accumulators and how many times its row has been given, under its group keys and start. A session's state is every session of its keys. Each output row has an _openetl_id and a seq from its key and that count, so a replay gives the same rows.

At each barrier, a keyed lane adds its part of the operator's epoch to EpochReady.operators: the rows it took and wrote, the rows it quarantined as late, the windows whose state it began and stopped keeping, its watermark, what its state held in memory, and the runs its snapshot names with their sizes. The engine adds up its lanes' parts, takes the least watermark, and adds the operator's block cache and its run store's requests since the last epoch (Counts). A worker's share carries its engine's in its EpochReady, and the coordinator adds every worker's into the record (checkpoint.Operators). The record counts an operator's windows on from the record it restored, and from 0 where that holds none of the operator's state, as after a reset. A run named by several lanes counts once. The runner reports the record's numbers to the control plane with the checkpoint, and records the openetl.operator.* metrics at each commit.

OVER and dedupe

OVER and dedupe by ROW_NUMBER() … = 1 plan as an Over and a Dedupe (crates/sql/src/over.rs), and run on keyed lanes as a group window does, exchanged by their PARTITION BY columns. Both take their input through Partitioned (crates/engine/src/over/partitioned.rs): each row with its key, key group, event time and record ID, a row written in state under the schema it came with, which state keeps beside it, and the take of due rows that keeps a key's rows of one time together, ordered by record ID, then values.

OVER over a stream with EventTime.rereads drops a row a read gives again while its copy waits, held read or not: each waiting row's record ID and hash, as a group window's, are in a set under a second operator ID, the operator's with its lowest bit flipped, each to expire at the row's time, and the set's horizon moves with the operator's. A row whose ID and hash are in the set is a repeat, and doesn't wait twice. One whose copy has left is late. OperatorEpoch.repeats counts the rows dropped.

A first-row dedupe (crates/engine/src/dedupe) keeps its kept keys in the set, each with its first row's time as its expiry, and the rows of keys not kept as rows at their times, under a second operator ID, whose horizon is where rows have left. A row of a kept key is a repeat as it comes: insert_new with an expiry of i64::MIN only asks. When the watermark passes a row, the rows due leave in order, and each is written where insert_new takes its key. With a retention, insert_new asks at the row's time less the retention, and the set's horizon is the watermark less the retention, so a changed retention applies to every key, and a key a horizon passed stays gone. Unbounded, a key is kept with NEVER, which the set's merges keep for good. Keys forgotten are counted by buckets of first times, each a value and a timer under the second operator. A latest-row dedupe keeps a value for each key: its latest time, record ID, a hash of its values, which orders rows of one time and ID, and whether the output has its row. It writes upserts as rows come, takes no watermark (Operator::level is constant, so it records no steps), and writes a delete of the key alone, its key columns and nulls, where the steps after it drop a key's latest row. OperatorEpoch reports a dedupe's kept and forgotten keys as windows_opened and windows_dropped, and the rows it dropped as repeats. Its keyed lanes flush early, count their state and fail with RESOURCE_EXHAUSTED as a group window's do (Early flushes and the disk cache).

Versions at one cursor value

A keyed stream read with a cursor has record IDs of its stream, key and cursor value, so two changes of a row within one cursor value give it one ID. A destination that dedupes at apply would skip the second as a repeat. So where the job keeps state, the runner gives each such stream, not past a transform, an operator that keeps its versions (StreamPlan.versions_operator, versions.{stream}). Normalization adds each row's hash to its batch as a hidden column: of the names and values of its columns, but the metadata columns and the stream's volatile columns (volatile_columns), with each version the evolver sent one to ({name}__v{k}), which the schema's version leaves out too, in one encoding across widenings, with a held cell's raw text for its value. Lists, structs, maps and unions are encoded by their structure, at any depth, with a tag for each and for a null within one, so a row's hash is of its values alone, not of its arrays' layout or the rows beside it (crates/normalize/src/versions.rs). The stream's writer lanes take the column off (crates/engine/src/dataflow/versions.rs). Each keeps, for each record ID of its key groups, the hash and schema version of the version that holds the ID, at the read that last gave it: the ID's one row at the read's start (Layer::put_row), so a batch's lookups, of each row's ID at the last two reads, are one batched read by key, whose filters rule out the runs without them. A lookup at one read passes over the other read's runs by their times, and a lane merges each read's runs by size (Merges). So the filters a lookup asks grow with the logarithm of a read's epochs, not with the epochs: about three runs of each size, up to the largest that a merge may take. Record IDs are hashes, so once the entries outgrow the operator's block cache, a row read again costs about one block read: from the disk cache, or from object storage without one. A row whose ID has a live entry of its schema's version and another hash gets an ID of its own from the base ID and its hash (openetl.id.v1, tag 4), in its delivered and held rows alike: where the stream has an exchange, its held rows go to the writer lane its row went to, on workers in another engine as a FRAME_KIND_HELD frame. Any other row keeps its ID, and its hash becomes the entry. So does a row of a key group that its writer lane does not keep: it keeps its ID, so its second version is skipped as without the operator. The stream's exchange by key group gives each writer lane the rows of the key groups it keeps, and a batch that crosses engines carries the hash column with it.

The entries of the stream's last two reads stay. A read's first row at a writer lane moves each of the lane's key groups on to the read, and the entries of the reads before the last expire, so a read's runs leave the record two reads after it, whichever key groups those give rows of. A lane that a read gives no row doesn't move on, and keeps its entries and their runs named until a read gives it one: after a first read of a whole table on eight writer lanes, reads that give only a few rows, as the rows at the cursor where the last read ended, leave the other lanes their first read's entries for as long as they get none. That is a read's entries at most, in storage alone: a lane that gets no rows reads none. A key group's reads are horizons, which the record carries, and not a value, which would keep the run it's in named after the run's entries had gone.

The versions operators of a job share one pool (JobState::pooled, openetl_state::Pool), rather than each having a block cache, a merge budget and a pending share of its own. The pool's block cache, and the input bytes of its merges in flight, take the versions' part of a sixteenth of Limits.memory_bytes, as their operators are of the job's. A record carries at most their part of the pending bytes, as this engine's writer lanes' key groups are of the pipeline's. A lane's barrier takes its pending set's bytes of its epoch's record while they fit, in whatever order the lanes' barriers come, and otherwise commits the set as a run. So 300 streams on the smallest engine (768 MiB) share one block cache of about 48 MiB, rather than taking 1 MiB each, and a record carries the pending sets of the lanes that have changes. A lane whose pending set does not fit still writes a run at the barrier, one for each stream and writer lane, and its open changes flush early past their own part of the job's memory, at least 64 KiB, as before. A stream's versions on a writer lane go when the stream ends there, which is the last the writer lane gets of it: the lane's place in the pool's line for merge room, and a merge in flight, which no record will name, with the room it took. A writer lane flushes its entries early past its part of the job's memory, as a keyed lane does (Early flushes and the disk cache), so a read's entries do not wait in memory for the barrier. Memory pressure, which also flushes a keyed lane, does not flush them, and the job's count of what its state holds in memory leaves them out. Their snapshots name the versions' layout with the state's (StateSnapshot.layout), and an engine that finds another starts the versions again with a warning, as it does where it can't read them: a row read again keeps its ID until they're kept again. So does an engine whose versions' runs are gone, as after a person deletes one that can't be read: the lookup that finds a run missing, on its read and on a look after 200 milliseconds and again after 1 second, forgets the run, as if it held nothing, rather than fail the attempt with CHECKPOINT_CORRUPT, and the writer lane reads on with the record's other runs and the epoch's own changes. A lookup opens and reads the runs it needs at once, and each read runs to its end, so it finds every one of them that is gone in one pass (Error::Gone names them all), and the lane forgets them together: a stream whose runs were all deleted waits for one confirmation, not one a run. A run among them that can't be forgotten fails the attempt, and the error names only such runs. Where a lookup's reads fail in several ways, the error names the one that needs a person first: state that can't be read before storage that failed. A row whose entry the run held keeps its ID, a row of another run's entry still gets its version, held values get the IDs their rows got, and the record no longer names the run. A redo of the epoch restores the epoch's own record, which no longer names the run, and gives the same IDs. A store can answer a read with not found while it holds the object, as some do in an outage, so a look that finds the run reads it again, and a look that fails otherwise fails the attempt with TRANSIENT (ObjectStorage::confirm_gone). The looks protect only against a not found that ends within about 1.2 seconds. A store that answers not found for longer, for an object it holds, makes the lane forget the run, as if it were gone. Only a run that is gone and that no read of the lane's has taken a block of is forgotten: one a read took a block of, which a cache can answer after the object has gone, or one the lane wrote, or one that is there but can't be read, fails the attempt, as other state does. A run that a merge alone opened and read, or whose filters alone answered a lookup, held nothing a read gave, so it goes too, and a merge in flight that reads it goes with it, unadopted. A run that is there but can't be read (Error::Unreadable: damaged bytes, an object shorter than the record's run, whose reads the store refuses as of an invalid range, or not the run the record names) fails it with CHECKPOINT_CORRUPT and an error that names the stream and the run's URL (JobState::url), and says to delete it. Nothing else clears it: a reset keeps the versions, and a rewind restores a record that names the run too. Once a person deletes it, the next attempt finds it gone and forgets it, as above. A record keeps the operators' snapshots with its others, and an attempt restores those of the streams it keeps versions of. A reset keeps the operator's state, as it keeps the stream's schema. A redo reads its epoch's record's state and writes none. So a redo whose source changed a row since its epoch gives the new version an ID of its own, and the version the epoch read never lands. A later read that finds no entry for the row gives the new version the base ID, and it lands again.

On this page