53  Operating it — backup and restore, health, metrics and traces

Verbs: pg_dump · verify_materialization · reconcile · list_sources · /healthz · /readyz · /metrics · [observability] otlp_endpoint · InferenceObserver (Rust) · jammi.testing.LiveServer · Background: a backup is a pair of consistent snapshots — the catalog and the bytes its rows name — taken in an order that never leaves a row without its bytes (Kleppmann 2017) · Checked: measurement (every restored table re-verifies against its recorded materialization; every request the chapter makes is counted in the metrics and exported as a span; every inferred row passes the observer once).

The deployment of the previous chapter — a Postgres catalog and broker, a server, and the result tables its work writes — is what an operator runs. This chapter does the operator’s two jobs on it, live: back it up and restore it into a fresh deployment, and read what it reports about itself — liveness, readiness, metrics, traces, and, inside a process that links the engine, every batch its models produce.

import json
import shutil
import subprocess
import tempfile
import time
import urllib.error
import urllib.request
from concurrent import futures
from pathlib import Path

import grpc
import jammi
import pgserver
from jammi.testing import LiveServer
from jammi_cookbook import fixtures
from jammi_cookbook.claims import claim
from opentelemetry.proto.collector.trace.v1 import trace_service_pb2, trace_service_pb2_grpc

MODEL = fixtures.model("tiny_bert")
CORPUS = fixtures.url("tiny_corpus.parquet")
QUERY = "how does quantum computing work?"
PG_BIN = Path(pgserver.__file__).parent / "pginstall" / "bin"

work = Path(tempfile.mkdtemp())
ARTIFACTS = work / "artifacts"


def catalog_url(postgres) -> str:
    """The engine's URL for `postgres`: the libpq URI it hands out, reached over its
    Unix socket (an empty host, the socket directory in `host`)."""
    return postgres.get_uri()


def configure(postgres, **sections: str) -> Path:
    """A deployment's config: the catalog and broker on `postgres`, plus `sections`."""
    path = work / f"jammi-{len(list(work.glob('jammi-*.toml')))}.toml"
    extra = "".join(f"\n[{name}]\n{body}\n" for name, body in sections.items())
    path.write_text(f"""
[catalog.postgres]
url = "{catalog_url(postgres)}"

[broker.postgres]
idle_poll_secs = 1

[gpu]
device = -1
{extra}""")
    return path


def ranked(db) -> list[tuple[str, float]]:
    query = db.encode_query(model=MODEL, query=QUERY)
    return [(h["_row_id"], round(h["similarity"], 6))
            for h in db.search("corpus", query=query, k=5).to_pylist()]
/__w/jammi-ai/jammi-ai/.venv/lib64/python3.12/site-packages/platformdirs/api.py:459: RuntimeDirWarning: XDG_RUNTIME_DIR is not set, falling back to /tmp/runtime-0
  return Path(self.user_runtime_dir)

53.1 A deployment with something to lose

A server on a Postgres catalog embeds the corpus and builds a neighbor graph over the embeddings — two result tables, each a catalog row naming Parquet under the result root.

original = pgserver.get_server(work / "pgdata")
config = configure(original)
with LiveServer(ARTIFACTS, env={"JAMMI_CONFIG": str(config)}) as server, \
        jammi.connect(server.endpoint) as db:
    db.add_source("corpus", url=CORPUS, format="parquet")
    embeddings = db.generate_embeddings(
        source="corpus", model=MODEL, columns=["content"], key="id", modality="text")
    graph = db.build_neighbor_graph(source="corpus", k=3)
    before = ranked(db)
    tables = sorted(t["table_name"] for s in db.list_sources() for t in s["result_tables"])
print(f"{len(tables)} result tables:", *tables, sep="\n  ")
2 result tables:
  corpus__neighbor_graph__20261003T074244051020143_e48b76a6
  corpus__text_embedding_____w_jammi-ai_jammi-ai_cookbook_book_jammi_cookbook__fixtures_ti__20261003T074244029182669_6154c124

53.2 Back up: the bytes first, then the catalog

The catalog and the result root are two systems backed up with their own tools — pg_dump for Postgres, a copy (or a bucket snapshot) for the bytes. The order is the rule that keeps them consistent: storage no later than the catalog. A catalog row is a claim that its bytes exist, so the storage snapshot is taken first and the catalog snapshot after it — every row the backup holds then names bytes the backup also holds. Postgres serves a dump while the server keeps running; nothing stops.

backup = work / "backup"
with LiveServer(ARTIFACTS, env={"JAMMI_CONFIG": str(config)}):
    shutil.copytree(ARTIFACTS / "jammi_db", backup / "jammi_db",
                    ignore=shutil.ignore_patterns("cache"))
    dump = subprocess.run(
        [PG_BIN / "pg_dump", "--no-owner", "--dbname", original.get_uri()],
        check=True, capture_output=True, text=True).stdout
(backup / "catalog.sql").write_text(dump)
objects = sum(1 for p in (backup / "jammi_db").rglob("*") if p.is_file())
print(f"backed up {objects} result-root objects and a {len(dump):,}-byte catalog dump")
backed up 7 result-root objects and a 35,205-byte catalog dump

The cache/ directory is left out: it holds only content-addressed copies the store rebuilds on demand, never the sole copy of anything.

53.3 Lose it, then restore into a fresh deployment

The deployment is gone — the database and the result root both. A fresh Postgres takes the dump; the result root is put back where the rows say it is. A server opened on the restored pair runs its startup recovery, and is then asked two questions: does every table still match what was recorded when it was materialized, and does the catalog agree with the object store?

original.cleanup()
shutil.rmtree(work / "pgdata")
shutil.rmtree(ARTIFACTS)

restored = pgserver.get_server(work / "pgdata-restored")
subprocess.run([PG_BIN / "psql", "--quiet", "--dbname", restored.get_uri(),
                "--file", backup / "catalog.sql"], check=True, capture_output=True)
shutil.copytree(backup / "jammi_db", ARTIFACTS / "jammi_db")

with LiveServer(ARTIFACTS, env={"JAMMI_CONFIG": str(configure(restored))}) as server, \
        jammi.connect(server.endpoint) as db:
    verdicts = {t: db.verify_materialization(t)["verdict"] for t in tables}
    report = db.reconcile(apply=False)
    after = ranked(db)

for table, verdict in verdicts.items():
    print(f"  {verdict:<28} {table}")
print(f"reconcile (dry run): {report['orphan_count']} orphans, "
      f"{report['rows_failed_count']} rows failed, {report['damaged_count']} damaged")
print(f"search after the restore returns the same ranking: {after == before}")
  match                        corpus__neighbor_graph__20261003T074244051020143_e48b76a6
  match_with_unpinned_inputs   corpus__text_embedding_____w_jammi-ai_jammi-ai_cookbook_book_jammi_cookbook__fixtures_ti__20261003T074244029182669_6154c124
reconcile (dry run): 0 orphans, 0 rows failed, 0 damaged
search after the restore returns the same ranking: True
claim("every restored table verifies against its manifest",
      set(verdicts.values()) <= {"match", "match_with_unpinned_inputs"}, f"{len(verdicts)} tables")
claim("the restore leaves no orphan, failed or damaged row",
      report["orphan_count"] == report["rows_failed_count"] == report["damaged_count"] == 0)
claim("and the restored deployment answers the search as the lost one did", after == before)
✓ every restored table verifies against its manifest (2 tables)
✓ the restore leaves no orphan, failed or damaged row
✓ and the restored deployment answers the search as the lost one did

Each verdict recomputes the table’s artifact digest and compares it with the one its .materialization.json recorded; a table whose inputs were registered sources (never pinned to a version) matches with its unpinned inputs named. The dry-run reconcile mutates nothing — it is the step to read before any --apply after a restore whose two snapshots were not taken at one instant.

53.4 What the server says about itself

A server exposes a side channel beside its gRPC and Flight SQL surfaces: /healthz (the process is up), /readyz (it can reach its catalog — the load balancer’s question), and /metrics (a Prometheus snapshot). Configured with an OTLP endpoint, it also exports a span for every request. The collector here is a few lines of Python in this process, receiving over the OTLP protocol any collector speaks.

class Collector(trace_service_pb2_grpc.TraceServiceServicer):
    """An OTLP trace receiver keeping every span it is sent."""

    def __init__(self) -> None:
        self.spans: list[dict] = []

    def Export(self, request, context):
        for resource_spans in request.resource_spans:
            resource = {a.key: a.value.string_value for a in resource_spans.resource.attributes}
            for scope in resource_spans.scope_spans:
                for span in scope.spans:
                    self.spans.append({
                        "service": resource.get("service.name"),
                        "name": span.name,
                        "attributes": {a.key: a.value.string_value for a in span.attributes},
                    })
        return trace_service_pb2.ExportTraceServiceResponse()


collector = Collector()
receiver = grpc.server(futures.ThreadPoolExecutor(max_workers=2))
trace_service_pb2_grpc.add_TraceServiceServicer_to_server(collector, receiver)
otlp_port = receiver.add_insecure_port("127.0.0.1:0")
receiver.start()


def get(url: str) -> tuple[int, str]:
    try:
        with urllib.request.urlopen(url, timeout=10) as response:
            return response.status, response.read().decode()
    except urllib.error.HTTPError as refused:
        return refused.code, refused.read().decode()


def metric(text: str, name: str) -> float:
    return sum(float(line.split()[-1]) for line in text.splitlines()
               if line.startswith(name) and not line.startswith("#"))
SEARCHES = 4
observed = configure(restored, observability=f'otlp_endpoint = "http://127.0.0.1:{otlp_port}"')
with LiveServer(ARTIFACTS, env={"JAMMI_CONFIG": str(observed)}) as server, \
        jammi.connect(server.endpoint) as db:
    side = server.health_endpoint
    health = get(f"{side}/healthz")
    ready = get(f"{side}/readyz")
    before_metrics = get(f"{side}/metrics")[1]
    for _ in range(SEARCHES):
        ranked(db)
    after_metrics = get(f"{side}/metrics")[1]

    # The catalog goes away: the process stays live, and stops being ready.
    restored.cleanup()
    unready = get(f"{side}/readyz")
    still_live = get(f"{side}/healthz")
    restored = pgserver.get_server(work / "pgdata-restored")
    deadline = time.time() + 30
    while (ready_again := get(f"{side}/readyz"))[0] != 200 and time.time() < deadline:
        time.sleep(0.5)
# Stopping the server flushes the spans it still holds to the collector.
receiver.stop(grace=None)

searched = metric(after_metrics, "jammi_search_latency_seconds_count") \
    - metric(before_metrics, "jammi_search_latency_seconds_count")
spans = [s for s in collector.spans
         if s["name"] == "grpc_request"
         and s["attributes"].get("path") == "/jammi.v1.embedding.EmbeddingService/Search"]
print(f"/healthz  {health[0]}  {health[1]}")
print(f"/readyz   {ready[0]}  {ready[1]}")
print(f"catalog stopped: /readyz {unready[0]} {unready[1]}  ·  /healthz {still_live[0]}")
print(f"catalog back:    /readyz {ready_again[0]}")
print(f"/metrics  jammi_search_latency_seconds_count +{searched:.0f} over {SEARCHES} searches")
print(f"exported  {len(spans)} Search spans, service.name = {spans[0]['service'] if spans else None}")
I1003 07:42:45.323184    1270 fork_posix.cc:71] Other threads are currently calling into gRPC, skipping fork() handlers
I1003 07:42:45.434377    1270 fork_posix.cc:71] Other threads are currently calling into gRPC, skipping fork() handlers
/healthz  200  {"status":"ok","version":"0.54.0","lease_keeper":true,"claim_loop":"running"}
/readyz   200  {"status":"ready"}
catalog stopped: /readyz 503 {"status":"not_ready","detail":"sqlx backend error: error communicating with database: No such file or directory (os error 2)"}  ·  /healthz 200
catalog back:    /readyz 200
/metrics  jammi_search_latency_seconds_count +4 over 4 searches
exported  4 Search spans, service.name = jammi
claim("a serving server is live and ready",
      health[0] == 200 and json.loads(health[1])["status"] == "ok"
      and ready[0] == 200 and json.loads(ready[1])["status"] == "ready")
claim("with its catalog stopped, it stays live but reports itself not ready",
      unready[0] == 503 and json.loads(unready[1])["status"] == "not_ready" and still_live[0] == 200)
claim("and is ready again once the catalog is back", ready_again[0] == 200)
claim("every search is traced as one span of the jammi service",
      searched == SEARCHES and len(spans) == SEARCHES and all(s["service"] == "jammi" for s in spans),
      f"{len(spans)} spans")
restored.cleanup()
✓ a serving server is live and ready
✓ with its catalog stopped, it stays live but reports itself not ready
✓ and is ready again once the catalog is back
✓ every search is traced as one span of the jammi service (4 spans)

/healthz never touches a dependency, so it answers the orchestrator’s should this container be restarted? — and stayed 200 while the catalog was down. /readyz pings the catalog, so it answers the load balancer’s should traffic come here? — 503 while Postgres was stopped, 200 once it was back, with no restart of the server.

53.5 Watching inference from inside the process

The side channel reports on a server from outside. Inside a process that links the engine, an inference observer is shown every output batch of every model run, with the model that produced it and how long it took — the hook for logging, progress, and quality checks. It is a Rust trait: this program implements it, opens an InferenceSession with it, and embeds the corpus.

import os
from importlib.metadata import version as dist_version

import pyarrow.parquet as pq
from jammi_cookbook import programs

VERSION = dist_version("jammi-ai")
print((programs.program("monitor_inference") / "main.rs").read_text())
//! An inference observer watching an embedding run — the "Monitor Inference"
//! guide page, run: an `InferenceSession` opened with an `InferenceObserver`,
//! which the engine calls once per output batch of every model run.
//!
//! Usage: `monitor_inference <corpus-url> <model>`. The artifact directory and
//! every other setting come from `jammi.toml` or the `JAMMI_*` environment.
//! Prints one JSON line per observed batch: `rows`, `errors`, `model`,
//! `latency_ms`.

use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use arrow::array::{Array, RecordBatch, StringArray};
use jammi_ai::inference::InferenceObserver;
use jammi_ai::session::InferenceSession;
use jammi_db::config::JammiConfig;
use jammi_db::source::{FileFormat, SourceConnection, SourceType};
use jammi_db::store::CachePolicy;

/// One observed batch: its rows, how many of them failed, the model that
/// produced it, and how long the batch took.
struct Observed {
    rows: usize,
    errors: usize,
    model: String,
    latency: Duration,
}

/// Records every batch it is shown.
#[derive(Default)]
struct Recorder {
    batches: Mutex<Vec<Observed>>,
}

impl InferenceObserver for Recorder {
    fn on_batch(&self, batch: &RecordBatch, model_id: &str, latency: Duration) {
        let errors = batch
            .column_by_name("_status")
            .and_then(|status| status.as_any().downcast_ref::<StringArray>())
            .map_or(0, |status| {
                status.iter().filter(|s| *s == Some("error")).count()
            });
        self.batches
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .push(Observed {
                rows: batch.num_rows(),
                errors,
                model: model_id.to_string(),
                latency,
            });
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut args = std::env::args().skip(1);
    let (Some(corpus), Some(model)) = (args.next(), args.next()) else {
        return Err("usage: monitor_inference <corpus-url> <model>".into());
    };

    let recorder = Arc::new(Recorder::default());
    let observer: Arc<dyn InferenceObserver> = recorder.clone();
    let session = InferenceSession::with_observer(JammiConfig::load(None)?, Some(observer)).await?;

    let connection = SourceConnection::parse(&corpus, FileFormat::Parquet)?;
    session
        .add_source("corpus", SourceType::File, connection)
        .await?;
    session
        .generate_text_embeddings(
            "corpus",
            &model,
            &["content".to_string()],
            "id",
            CachePolicy::Bypass,
            None,
        )
        .await?;

    let batches = recorder
        .batches
        .lock()
        .unwrap_or_else(PoisonError::into_inner);
    for seen in batches.iter() {
        println!(
            r#"{{"rows": {}, "errors": {}, "model": "{}", "latency_ms": {:.3}}}"#,
            seen.rows,
            seen.errors,
            seen.model,
            seen.latency.as_secs_f64() * 1000.0
        );
    }
    Ok(())
}

It builds against the crates of this release — the workspace when the book renders in a checkout, crates.io otherwise — and runs on the embedded default (a SQLite catalog in its own artifact directory, from the environment). The first build compiles the engine, so it takes minutes.

project = programs.rust_program(work, VERSION, "monitor_inference")
run = subprocess.run(
    [programs.cargo(), "run", "-q", "--manifest-path", str(project / "Cargo.toml"),
     "--", CORPUS, MODEL],
    capture_output=True, text=True,
    env=dict(os.environ, JAMMI_ARTIFACT_DIR=str(work / "observed"), JAMMI_GPU__DEVICE="-1"))
assert run.returncode == 0, run.stderr[-4000:]
observed = [json.loads(line) for line in run.stdout.splitlines()]
corpus_rows = pq.read_table(fixtures.path("tiny_corpus.parquet")).num_rows
print(f"{len(observed)} batches observed, {sum(b['rows'] for b in observed)} rows "
      f"of {corpus_rows} in the corpus, {sum(b['errors'] for b in observed)} errors")
for batch in observed:
    print(" ", batch)
1 batches observed, 20 rows of 20 in the corpus, 0 errors
  {'rows': 20, 'errors': 0, 'model': '/__w/jammi-ai/jammi-ai/cookbook/book/jammi_cookbook/_fixtures/tiny_bert', 'latency_ms': 51.493}
claim("the observer sees every row of the corpus, in batches with no errors",
      sum(b["rows"] for b in observed) == corpus_rows and all(b["errors"] == 0 for b in observed))
claim("each batch names the one model and a measured latency",
      len({b["model"] for b in observed}) == 1 and all(b["latency_ms"] > 0 for b in observed))
✓ the observer sees every row of the corpus, in batches with no errors
✓ each batch names the one model and a measured latency

Every row the embedding run produced passed the observer exactly once, in batches, each tagged with the one model that ran. The observer costs nothing when none is attached — the engine checks for one per batch.

53.6 What this shows

  • A restore is two snapshots in the right order. Storage first, catalog after, so every restored row names bytes that were restored; every table re-verified against the materialization it recorded, and reconcile found nothing to reconcile.
  • Liveness and readiness are different questions. One is the process, the other is its dependencies; a catalog outage makes the server unready, not dead.
  • Every request is measured and traced. Each search moved the latency histogram’s count by one and reached the collector as a span, from a server that needed only an endpoint in its configuration.
  • Inference is observable from inside. An observer attached to a session saw every row a model run produced, once, with its model and its latency.
Kleppmann, Martin. 2017. Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems. O’Reilly Media.