feat(ttp): E.3.6 DatabaseRuleStore — ttp_rule/ttp_rule_state + master sync
Implements the DB-backed rule store body left empty at contract phase:
load_compiled reads from ttp_rule + ttp_rule_state; get_state /
set_state hit ttp_rule_state with the same expires_at auto-revert and
bus-event semantics as the FS backend; subscribe_changes returns a
per-subscriber queue. State persists across process restarts — the
swarm property the FS backend deliberately doesn't have.
Also lands two swarm-mode helpers:
- sync_from_filesystem(fs_store) — master-side, subscribes to a
FilesystemRuleStore and projects each RuleChange onto a ttp_rule
upsert/delete.
- tail_db(poll_interval) — worker-side, watermark poll over
ttp_rule.updated_at; emits RuleChange("definition", ...) for each
row that moved.
Why: swarm mode needs rule definitions and operator state to
propagate across hosts. The filesystem backend (E.3.5) was the
single-host-dev variant; this one survives restart and serves N
workers from a shared DB.
Notes:
- DatabaseRuleStore() with no args lazy-inits an in-memory SQLite
repo so the conformance fixture works without test plumbing. In
production the worker bootstrap (E.3.14) passes an explicit repo.
- The conftest.py rule_store fixture became async (pytest_asyncio),
per-backend creates/initializes a SQLite repo for the DB run.
- Adds a `seed_rule(store, rule_id, yaml)` helper to bridge backend
semantics: drop a YAML file (FS) vs insert a ttp_rule row (DB).
Used by the parametrized load_compiled conformance test.
- Late-bound _tracer() in both backends (was module-level get_tracer
binding) so test_tracing's monkeypatch of decnet.telemetry.get_tracer
actually affects span output.
xfails flipped: tests/ttp/store/test_database.py set_state-writes-to-
ttp_rule_state + filesystem-to-DB sync; tests/ttp/store/test_conformance.py
DB-side load_compiled / set_state isolation / round-trip / per-rule
fan-out / expired-state revert / set_state failure / get_state default
(was xfail-only-on-DB); tests/ttp/test_tracing.py set_state span
hierarchy.
Tests: 208 passed, 25 xfailed (gated on E.3.7 + lifters).
mypy: clean on all touched files.
This commit is contained in:
@@ -50,10 +50,10 @@ from typing import TYPE_CHECKING, Any, Final, Type
|
||||
|
||||
import yaml
|
||||
|
||||
from decnet import telemetry as _telemetry
|
||||
from decnet.bus import topics as _topics
|
||||
from decnet.bus.publish import publish_safely
|
||||
from decnet.logging import get_logger
|
||||
from decnet.telemetry import get_tracer
|
||||
from decnet.ttp.impl.rule_engine import CompiledRule, RuleSchema
|
||||
from decnet.ttp.store.base import RuleChange, RuleState, RuleStore
|
||||
|
||||
@@ -62,7 +62,13 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
_log = get_logger("ttp.store.filesystem")
|
||||
_tracer = get_tracer("ttp.store")
|
||||
|
||||
|
||||
def _tracer() -> Any:
|
||||
# Late binding: tests monkeypatch ``decnet.telemetry.get_tracer``
|
||||
# at fixture setup; capturing the tracer at import time would freeze
|
||||
# the no-op tracer into the module forever.
|
||||
return _telemetry.get_tracer("ttp.store")
|
||||
|
||||
|
||||
# ── Filename allowlist ──────────────────────────────────────────────
|
||||
@@ -324,7 +330,7 @@ class FilesystemRuleStore(RuleStore):
|
||||
# Operational state changes are NOT a tolerated-absence path.
|
||||
# Failures here MUST raise rather than silently drop — the
|
||||
# E.2.14b conformance test pins this.
|
||||
with _tracer.start_as_current_span("ttp.rule.state.change") as span:
|
||||
with _tracer().start_as_current_span("ttp.rule.state.change") as span:
|
||||
# Defensive set_attribute: real OTEL spans accept str/int/etc;
|
||||
# the no-op tracer's _NoOpSpan ignores attributes silently. A
|
||||
# caller-side wrapper keeps both paths green without leaking
|
||||
@@ -336,10 +342,10 @@ class FilesystemRuleStore(RuleStore):
|
||||
set_by=set_by,
|
||||
)
|
||||
stamped = replace(state, set_by=set_by, set_at=_utcnow())
|
||||
with _tracer.start_as_current_span("ttp.store.write_state"):
|
||||
with _tracer().start_as_current_span("ttp.store.write_state"):
|
||||
self._state[rule_id] = stamped
|
||||
self._restamp_compiled(rule_id, stamped)
|
||||
with _tracer.start_as_current_span("ttp.rule.publish"):
|
||||
with _tracer().start_as_current_span("ttp.rule.publish"):
|
||||
await self._emit_change(
|
||||
RuleChange("state", rule_id, stamped),
|
||||
bus_topic=_topics.ttp_rule_state(rule_id),
|
||||
|
||||
Reference in New Issue
Block a user