Skip to content
539 changes: 539 additions & 0 deletions docs/dev/specs/itl-1446-spiral-columns.md

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions docs/dev/specs/itl-1446/lease_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""ITL-1446: exhaustive check of the named-table lease (section 4 of the design).

One table name; creating it succeeds for one caller only (atomic, measured live). A writer:
creates; on a lost reply looks the name up and owns the table only when its own token is in
it; writes; drops **its own table id**. Every interleaving, with a crash after any step.

Variants, each adding one thing:

* ``late``: a create whose reply was lost may land after the writer looked (a ghost lease).
* ``late_drop``: a drop may be sent again after it already succeeded.
* ``clear``: a person drops the current table's id while its holder is alive (forbidden).
* ``by_name``: drops go by name instead of by id (the rule the design rests on, removed).

Run: ``uv run python docs/dev/specs/itl-1446/lease_model.py``
"""

from __future__ import annotations

from typing import NamedTuple

HOLDING = ("hold", "wrote")


class State(NamedTuple):
"""Writers' phases, the table's (owner, id) or ``None``, next id, own ids, in flight."""

phases: tuple[str, ...]
table: tuple[int, int] | None
next_id: int
ids: tuple[int, ...]
flying: frozenset[int]


def steps(
s: State, *, late: bool, late_drop: bool, clear: bool, by_name: bool
) -> list[State]:
"""Every state one step away."""
out: list[State] = []
for i, phase in enumerate(s.phases):

def to(new: str, i: int = i) -> tuple[str, ...]:
return (*s.phases[:i], new, *s.phases[i + 1 :])

own = (*s.ids[:i], s.next_id, *s.ids[i + 1 :])
if phase == "start":
if s.table is None:
made = s._replace(table=(i, s.next_id), next_id=s.next_id + 1)
out.append(made._replace(phases=to("hold"), ids=own))
out.append(made._replace(phases=to("unsure"))) # reply lost
else:
out.append(s._replace(phases=to("refused")))
out.append(s._replace(phases=to("unsure"))) # refusal lost
if late:
out.append(s._replace(phases=to("unsure"), flying=s.flying | {i}))
elif phase == "unsure":
if s.table is not None and s.table[0] == i:
ids = (*s.ids[:i], s.table[1], *s.ids[i + 1 :])
out.append(s._replace(phases=to("hold"), ids=ids))
else:
out.append(s._replace(phases=to("refused")))
elif phase == "hold":
out.append(s._replace(phases=to("wrote")))
elif phase == "wrote" or (late_drop and phase == "done"):
mine = s.table is not None and (by_name or s.table[1] == s.ids[i])
out.append(s._replace(phases=to("done"), table=None if mine else s.table))
if phase not in ("start", "refused", "done", "crashed"):
out.append(s._replace(phases=to("crashed")))
if i in s.flying:
landed = s._replace(flying=s.flying - {i})
if s.table is None:
landed = landed._replace(table=(i, s.next_id), next_id=s.next_id + 1)
out.append(landed)
if clear and s.table is not None:
out.append(s._replace(table=None))
return out


def explore(writers: int, **variant: bool) -> None:
"""Visit every reachable state; report two holders at once, and a stuck lease."""
options = {"late": False, "late_drop": False, "clear": False, "by_name": False}
options.update(variant)
start = State(("start",) * writers, None, 0, (-1,) * writers, frozenset())
visited, queue, two, stuck = {start}, [start], False, False
while queue:
s = queue.pop()
two = two or sum(p in HOLDING for p in s.phases) > 1
ended = all(p in ("refused", "done", "crashed") for p in s.phases)
stuck = stuck or (ended and not s.flying and s.table is not None)
for new in steps(s, **options):
if new not in visited:
visited.add(new)
queue.append(new)
print(
f"{writers} writers, {', '.join(variant) or 'base'}: {len(visited)} states, "
+ f"two holders possible: {two}, a lease left behind possible: {stuck}"
)


if __name__ == "__main__":
for count in (2, 3, 4):
explore(count)
explore(3, late=True)
explore(3, late_drop=True)
explore(3, late=True, late_drop=True)
explore(2, clear=True)
explore(3, late_drop=True, by_name=True)
55 changes: 55 additions & 0 deletions docs/dev/specs/itl-1446/prototype-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Prototype output (generated: `PYTHONPATH=. uv run python docs/dev/specs/itl-1446/prototype.py`)

```text
codec: 200000 distinct names -> 200000 stored names, all decoded
codec: a.b and ox_a;2Eb side by side keep their own values
job: first run calls {'add': 6, 'mean': 2, 'notify': 2}, rerun calls {}; rows, schemas, system tags and content hashes equal the in-memory store's
order (6 rows): pipeline table equal False, as_source() equal True
order (60 rows): pipeline table equal False, as_source() equal True
cached source, in memory: k [0, 1, 2, 3, 4, 5], Batch(2) gives y [[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]
cached source, Delta: k [0, 1, 2, 3, 4, 5], Batch(2) gives y [[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]]
cached source, SpiralDB: k [4, 5, 1, 2, 3, 0], Batch(2) gives y [[4.0, 5.0], [1.0, 2.0], [3.0, 0.0]]
record ids, in memory, 4 rows, key None: ['row_0', 'row_1', 'row_2', 'row_3']
record ids, in memory, 4 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']
record ids, in memory, 8 rows, key None: ['row_0', 'row_1', 'row_2', 'row_3']
record ids, in memory, 8 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']
record ids, Delta, 4 rows, key None: ['row_0', 'row_1', 'row_2', 'row_3']
record ids, Delta, 4 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']
record ids, Delta, 8 rows, key None: ['row_4', 'row_5', 'row_6', 'row_7']
record ids, Delta, 8 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']
record ids, SpiralDB, 4 rows, key None: ['row_3', 'row_0', 'row_2', 'row_1']
record ids, SpiralDB, 4 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']
record ids, SpiralDB, 8 rows, key None: ['row_5', 'row_1', 'row_4', 'row_2']
record ids, SpiralDB, 8 rows, key ['k']: ['{"k":0}', '{"k":1}', '{"k":2}', '{"k":3}']

| Rows | Run | list tables | look up table | get metadata | set metadata | create table | write transaction | whole-table scan |
|---|---|---|---|---|---|---|---|---|
| 2 | first | 35 | 15 | 5 | 5 | 5 | 5 | 3 |
| 2 | unchanged rerun | 10 | 14 | 7 | 0 | 0 | 0 | 7 |
| 2 | 10 rows added | 48 | 89 | 48 | 0 | 5 | 5 | 34 |
| 10 | first | 67 | 15 | 5 | 5 | 5 | 5 | 3 |
| 10 | unchanged rerun | 10 | 14 | 7 | 0 | 0 | 0 | 7 |
| 10 | 10 rows added | 48 | 89 | 48 | 0 | 5 | 5 | 34 |
| 50 | first | 227 | 15 | 5 | 5 | 5 | 5 | 3 |
| 50 | unchanged rerun | 10 | 14 | 7 | 0 | 0 | 0 | 7 |
| 50 | 10 rows added | 48 | 89 | 48 | 0 | 5 | 5 | 34 |

| Path A | Path B | Today's one name | Proposed names differ |
|---|---|---|---|
| `job/_result/my-fn/<64 hex>/v0/python.function.v1/rev1/rdb_v1` | `job/_result/my_fn/<64 hex>/v0/python.function.v1/rev1/rdb_v1` | `job___result__my_fn__<64 hex>__v0__python_function_v1__rev1__rdb_v1` | True |
| `job/_result/café/<64 hex>/v0/python.function.v1/rev1/rdb_v1` | `job/_result/cafè/<64 hex>/v0/python.function.v1/rev1/rdb_v1` | `job___result__caf___<64 hex>__v0__python_function_v1__rev1__rdb_v1` | True |
| `run-1/add/pdb_v1` | `run_1/add/pdb_v1` | `run_1__add__pdb_v1` | True |
| `a/b__c` | `a__b/c` | `a__b__c` | True |
| `a_/b` | `a/_b` | `a___b` | True |
| `1x` | `t_1x` | `t_1x` | True |

| Stored table | Today's length | Proposed length |
|---|---|---|
| `proto___result__add__<64 hex>__v0__python_function_v1__rev1__rdb_v1` | 123 | at most 124 |
| `proto___result__mean__<64 hex>__v0__python_function_v1__rev1__rdb_v1` | 124 | at most 124 |
| `proto__add__<64 hex>__v0__python_function_v1__schema_<64 hex>__pdb_v1` | 181 | at most 124 |
| `proto__mean__<64 hex>__v0__python_function_v1__schema_<64 hex>__pdb_v1` | 182 | at most 124 |
| `proto__side_effects__notify__schema_<64 hex>__tdb_v1` | 108 | at most 124 |

wrong result: my-fn doubles [0.0, 2.0]; my_fn triples [0.0, 1.0] and is given [0.0, 2.0]
```
Loading
Loading