Macaw.Data.Manager (macaw_data v0.21.1)

View Source

A source manager's process (spec/01 §6.4.6, dco-tek/macaw/macaw#108): the one owner of a source's data on a host, running the Macaw.Data.Source.Manager value. Moved from macaw_live's manager (dco-tek/macaw/macaw#478 decision 6a, #450, #461), so a headless pipeline runs a named source's manager with no macaw_live: what it took from macaw_live is its supervisor's (Macaw.Data.Manager.Supervisor) or a value passed in.

  • One per source per host, keyed (key/4) by the source's name, the digest of its entry in the host's sources (entry_digest/1), the digest of what every read asks (its params and as_of), and the viewer, nil for a source that declares itself viewer_independent, so a viewer-dependent source is never served across viewers. Started under the supervisor's <name>.Managers, :transient, by the first reader that finds none (ensure/3), and registered in its Registry with its table.
  • A writer only. It describes and subscribes its source, and runs each read its value plans (take_reads/1, request/3, the source's fetch, answer/4 with what the read cost in reductions), each in a turn of its own under <name>.Tasks, bounded by timeout:, a failed one tried again after tick: doubled per failure up to 30 s. It buffers its source's pushes (receive/2) and drains them once per burst (a gap in them closed by the snapshot read the value plans, run as any read), polls a source that doesn't push every tick: (poll/1), and applies a change notice of its source's name (spec/01 §5), heard through the supervisor's notify:, as notify/2, a read of exactly what it names.
  • A change detector the supervisor's detectors: names for its source (spec/01 §5) is made once the source is described and run in a turn every every: over the held slots, its notices applied as a writer's; a source it watches is never polled.
  • It publishes each change in its table (ETS, read_concurrency): the chunks put under {id, write_seq}, then one object of the index (its served facts the value's own, spec/01 §6.4.3), the held slots, the status and the params its reads ask, then the versions superseded deleted. A reader reads with Macaw.Data.Manager.Reader.read/3 from the table alone, never calling the manager. The table's heir is the supervisor's <name>.Tables, which keeps it a grace past the manager's end, so a reader racing that end reads a whole table.
  • It messages only the readers a change concerns: after each publication, each process of a reader whose window covers a chunk put or dropped, of a reader whose window has become held, and of every reader when the status moved, is sent {:macaw_data, :source, key, %{reader => status}}, once per process.
  • Lifetime: while a reader is subscribed and linger: (5,000 ms) after the last leaves, then it stops and unsubscribes its source. A crashed manager is restarted by its supervisor; its readers' processes see its :DOWN and subscribe again.

A reader is {pid, name}: the process it notifies and the reader's name there. A reader's process is monitored while it holds a reader, and its readers leave at its :DOWN.

Summary

Types

A manager's key: the source's name, its entry's digest, the digest of what every read asks, and the viewer's digest, nil for a viewer-independent source.

A manager's status, as a monitor's reader's is: what a reader of it draws.

Functions

Returns a specification to start this module under a supervisor.

The manager of key under the supervisor sup (Macaw.Data.Manager.Supervisor's name), started when none runs: {:ok, pid}, the one started first when two race, or {:error, reason}. opts: entry: (the source, or {source, context}), params:, as_of: and viewer: (what every read asks, the viewer nil for a viewer-independent source), the first reader's clock:, tick:, timeout: and stale_after:, and linger: (5,000 ms).

The digest of a source's entry in the host's sources, as a manager's key names it (spec/01 §6.4.6): the SHA-256 of its deterministic external form, lowercase hex. A manager is per node, so its key need only be one for equal entries on its node; an entry that holds its rows (an in-memory source of the host's own) is digested at the cost of writing it out once, never of walking each of its values as a document: a 50,000-row entry cost 38,500,000 reductions so, once for every key read, and costs 2,840,000 written out (measured 2026-11-23, macaw_live#110).

The manager's present state: its key, status, publication version, readers by pid, the reads it made (calls) and the rows they answered (rows), the asks its readers finished and what finishing them cost (finished, Finished.counted/1), its value's info/1 (chunks, bytes, held, stale, in flight, evicted, re-read), its push subscription's reference and position, the faults its source answered, its turns running and whether work waits in its mailbox. {:ok, info}, or {:error, reason} for a manager that ended or didn't answer within timeout (5,000 ms).

The key of the manager a reader of name reads (spec/01 §6.4.6): the source's name, the digest of its entry in the host's sources, the digest of the request's params and as_of, which every read asks, and the digest of its viewer unless the source declares itself viewer_independent. Everything else a request asks (its window, box, region, latest, order, budget and reducer) is finished by each reader from what the manager holds.

The manager of key and its table: {:ok, pid, table}, or :error for none running. Never calls the manager.

The host's memory as its managers see it (spec/01 §6.4.6), read in the caller's process, never a manager's: budget, the supervisor's memory: (nil for none); managers, each manager's chunk bytes by key, as each last put them (only with a budget); binary, the BEAM's binary memory (:erlang.memory(:binary)), which counts a chunk until every process holding it has dropped it and been collected; and holders, the ten processes holding the most binary memory (Macaw.Data.Manager.Budget.holders/1), so a reader keeping evicted chunks alive is named. Each manager's evictions and reads of evicted slots again are its info/2's manager.evicted and manager.reread.

reader, {pid, name}, subscribed to window (a request's window: nil or [from, to]) with budget, or its window and budget moved when it is subscribed already: :ok, never waiting. The manager answers reader's pid once its window is held.

reader unsubscribed: :ok, never waiting.

Types

key()

@type key() :: {String.t(), String.t(), String.t(), String.t() | nil}

A manager's key: the source's name, its entry's digest, the digest of what every read asks, and the viewer's digest, nil for a viewer-independent source.

status()

@type status() :: :loading | :live | :stale | {:down, term()}

A manager's status, as a monitor's reader's is: what a reader of it draws.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

ensure(sup, key, opts)

@spec ensure(atom(), key(), keyword()) :: {:ok, pid()} | {:error, term()}

The manager of key under the supervisor sup (Macaw.Data.Manager.Supervisor's name), started when none runs: {:ok, pid}, the one started first when two race, or {:error, reason}. opts: entry: (the source, or {source, context}), params:, as_of: and viewer: (what every read asks, the viewer nil for a viewer-independent source), the first reader's clock:, tick:, timeout: and stale_after:, and linger: (5,000 ms).

entry_digest(entry)

@spec entry_digest(term()) :: String.t()

The digest of a source's entry in the host's sources, as a manager's key names it (spec/01 §6.4.6): the SHA-256 of its deterministic external form, lowercase hex. A manager is per node, so its key need only be one for equal entries on its node; an entry that holds its rows (an in-memory source of the host's own) is digested at the cost of writing it out once, never of walking each of its values as a document: a 50,000-row entry cost 38,500,000 reductions so, once for every key read, and costs 2,840,000 written out (measured 2026-11-23, macaw_live#110).

info(manager, timeout \\ 5000)

@spec info(pid(), timeout()) :: {:ok, map()} | {:error, term()}

The manager's present state: its key, status, publication version, readers by pid, the reads it made (calls) and the rows they answered (rows), the asks its readers finished and what finishing them cost (finished, Finished.counted/1), its value's info/1 (chunks, bytes, held, stale, in flight, evicted, re-read), its push subscription's reference and position, the faults its source answered, its turns running and whether work waits in its mailbox. {:ok, info}, or {:error, reason} for a manager that ended or didn't answer within timeout (5,000 ms).

key(name, entry, request, independent?)

@spec key(String.t(), term(), Macaw.Data.Source.Request.t(), boolean()) :: key()

The key of the manager a reader of name reads (spec/01 §6.4.6): the source's name, the digest of its entry in the host's sources, the digest of the request's params and as_of, which every read asks, and the digest of its viewer unless the source declares itself viewer_independent. Everything else a request asks (its window, box, region, latest, order, budget and reducer) is finished by each reader from what the manager holds.

lookup(sup, key)

@spec lookup(atom(), key()) :: {:ok, pid(), :ets.tid()} | :error

The manager of key and its table: {:ok, pid, table}, or :error for none running. Never calls the manager.

memory(sup)

@spec memory(atom()) :: %{
  budget: pos_integer() | nil,
  managers: %{required(key()) => non_neg_integer()},
  binary: non_neg_integer(),
  holders: [map()]
}

The host's memory as its managers see it (spec/01 §6.4.6), read in the caller's process, never a manager's: budget, the supervisor's memory: (nil for none); managers, each manager's chunk bytes by key, as each last put them (only with a budget); binary, the BEAM's binary memory (:erlang.memory(:binary)), which counts a chunk until every process holding it has dropped it and been collected; and holders, the ten processes holding the most binary memory (Macaw.Data.Manager.Budget.holders/1), so a reader keeping evicted chunks alive is named. Each manager's evictions and reads of evicted slots again are its info/2's manager.evicted and manager.reread.

subscribe(manager, reader, window, budget)

@spec subscribe(pid(), {pid(), term()}, [term()] | nil, pos_integer() | nil) :: :ok

reader, {pid, name}, subscribed to window (a request's window: nil or [from, to]) with budget, or its window and budget moved when it is subscribed already: :ok, never waiting. The manager answers reader's pid once its window is held.

unsubscribe(manager, reader)

@spec unsubscribe(
  pid(),
  {pid(), term()}
) :: :ok

reader unsubscribed: :ok, never waiting.