Skip to content

Clearing the last retention or tiering window removes the table TTL it set - #222

Merged
zlareb1 merged 5 commits into
mainfrom
fix/retention-remove-ttl
Nov 3, 2026
Merged

zlareb1 merged 5 commits into
mainfrom
fix/retention-remove-ttl

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Critical Bug Fix (data loss, cross-tenant read, auth bypass)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Setting a namespace's retention_days or warm_days back to 0, or removing the last LOOM_RETENTION_<TABLE>_DAYS / LOOM_TIERING_<TABLE>_WARM_DAYS setting, now removes the table TTL. Before this fix the old TTL stayed on the table and kept deleting rows.

Why

Failing case: with no global window, namespace ns1 registers retention_days=30. The reconcile pass sets a multiIf TTL on loom.memory and the six other managed tables. The owner then sets retention_days=0 ("keep forever"). On the next pass compute_alters finds no desired TTL for any table and returns an empty plan, apply logs "schema defaults apply" and issues nothing. Nothing in server/ ever issued REMOVE TTL, so the 30-day TTL stayed on every table and ns1's rows kept being dropped at merges. The same happens when an operator removes the last global retention or tiering setting. This breaks "Loom deletes only what its writer asked to delete".

Pattern: the reconcile loop only emitted "set" statements and had no statement for an empty desired state, so the last applied state stayed in force.

Fix: apply now walks every managed table. A table with a composed clause takes the existing compare-then-MODIFY TTL path. A table without one reads its current TTL from system.tables.engine_full (the read current_ttl_matches already did, now current_ttl). If the table still carries a TTL, apply issues ALTER TABLE <t> REMOVE TTL, with the same unfinished-mutation deferral. None of the seven managed tables has a TTL in schemas/schema.sql, so removing the TTL restores the schema default and does not strip a schema-defined one.

Scope and risk

Touches server/storage/retention_overrides.py only, plus two doc paragraphs. REMOVE TTL changes only metadata. It deletes nothing and rewrites no part. If the system.tables read fails for a table that should have no TTL, the removal is skipped and retried on the next pass, because REMOVE TTL on a table without a TTL is an error. Every pass now reads system.tables once per managed table, seven point reads. Before, it read only the tables that had a planned clause. If an operator added a TTL by hand to one of these seven tables, the leader now removes it when no Loom window is set, just as it already replaced such a TTL whenever a window was set.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none. On the first pass after deploy, the leader removes any TTL left behind by a window that was already cleared.
  • Rollback: reverting leaves no rows or migrations. A TTL that was removed stays removed until a window is set again.
  • During a rolling upgrade, an N-1 leader issues no ALTER for a table with no window and an N leader removes the stale TTL, so the two builds never undo each other.

Tests

  • tests/unit/test_retention_overrides.py::test_clearing_the_last_override_removes_the_ttl_it_left failed before the fix (0 ALTERs issued) and passes now, with REMOVE TTL sent to exactly the tables that still carry a TTL.
  • test_a_table_ttl_is_not_removed_when_its_lookup_fails covers the deferral when the read fails. test_apply_zero_state_issues_no_alter_on_tables_without_ttl checks that no ALTER is sent when no table has a TTL.
  • test_a_ttl_removal_waits_for_unfinished_mutations covers the mutation-queue deferral on the removal path. test_a_ttl_an_overlapping_pass_removed_first_is_not_an_error covers two overlapping passes. test_no_managed_table_has_a_schema_ttl fails if a migration gives one of the seven tables a schema TTL, which this removal would strip.
  • test_a_cleared_window_removes_the_ttl_on_real_tables runs on clickhouse-local (skipped without the binary). It creates the seven managed tables from schemas/schema.sql, applies a namespace window, clears it, and checks that every table has a TTL after the first pass and none after the second. A second REMOVE TTL fails with Code: 36 ... Table doesn't have any table TTL expression, cannot remove, the text the overlap handling matches.
  • make format lint typecheck, tests/unit/test_retention_overrides.py, test_run_retention.py, test_clickhouse_mutation_inventory.py, test_public_copy.py and test_doc_links.py pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. I checked the schema for TTLs on the managed tables and traced the reconcile path end to end.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 5 commits November 3, 2026 13:42
…t set

When a table's composed TTL is empty (no namespace override and no
global LOOM_RETENTION_* / LOOM_TIERING_* window), compute_alters skips
it, and apply issued nothing, so the TTL from an earlier pass stayed on
the table and kept deleting rows. apply now walks every managed table
and issues ALTER TABLE ... REMOVE TTL when the table has no desired TTL
but still carries one. None of the managed tables has a schema TTL, so
the removal restores the schema default. A failed system.tables read
defers the removal to the next pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… test pins that no managed table has a schema TTL

Also keeps the TTL read private, logs each branch's change from the
branch, and covers a removal deferred behind unfinished mutations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
… runs set-then-clear on loom.audit_log

The stub-based apply tests read each table through _DiffCH, so a table
without a window takes the no-TTL branch, not the lookup-failure one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…h KeptCH

So both passes run on real ClickHouse for every table, including the
loom.memory coalesce TTL; the schema check reads schema.sql from ROOT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 3d27944 into main Nov 3, 2026
22 checks passed
@zlareb1
zlareb1 deleted the fix/retention-remove-ttl branch November 3, 2026 18:04
@pufit pufit mentioned this pull request Nov 4, 2026
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant