This file is for the next agent (or human) extending mirror-v3. Read this first; it explains the design decisions, invariants you must not violate, and the workflow expected for changes.
.
├── Cargo.toml workspace, profiles, shared deps
├── rust-toolchain.toml stable channel + rustfmt + clippy
├── Dockerfile multi-stage; runtime = distroless/cc-debian12
├── README.md end-user docs (config reference)
├── AGENTS.md this file
├── crates/
│ ├── mirror-config/ Config structs + serde + schemars
│ ├── mirror-bin/ the `mirror-v3` binary (CLI + main loop)
│ └── xtask/ cargo run -p xtask -- (gen|check)-schema
├── examples/ YAML configs that double as bin tests
└── schemas/
└── mirror-v3.config.schema.json golden file, gated in CI
Phase 1 added mirror-core and mirror-kafka. Phase 2 added the mirror-e2e crate at e2e/ with testcontainers-driven Docker stacks and the Provisioner/ProvisionedStack trait seam for plugging in new test infra. Phase 3 added mirror-fs (atomic-rename filesystem sink, <from>-<to>.ndjson naming, scan-validate on startup). Phase 4 added mirror-s3 (same wire format as mirror-fs, object_store-backed, PutMode::Create + scan-validate two-layer atomicity). Phase 5 generalised run_mirror to accept a shutdown future, added Sink::flush (default no-op; FS/S3 implementations call their flush_now), and wired SIGINT/SIGTERM in mirror-v3 run. Phase 5b added the end-to-end restart-correctness test — the proof that "destination is the truth" holds across a real process crash.
Each row is a separate change set / PR. Do not skip phases.
| Phase | Scope | Done when |
|---|---|---|
| 0 | Workspace + config model + JSON Schema gate + CLI stub + Dockerfile | cargo test --workspace green, schema committed |
| 1 | mirror-core (Source/Sink traits, loop) + mirror-kafka source+sink with end-offset gate + mirror-v3 run supervisor |
Builds + 17 tests green; loop invariants exhaustively unit-tested with mocks |
| 2 | Docker e2e harness (mirror-e2e crate) + kafka-native → redpanda happy-path test |
First real Kafka e2e green: 100 records, byte-identical, offsets preserved |
| 3 | mirror-fs sink + flush triggers + scan-validate on startup + e2e |
FS sink unit-tested (corrupt-chain, restart, crashed .tmp, flush triggers); kafka→fs e2e green |
| 4 | mirror-s3 sink via object_store + VersityGW e2e + conditional-PUT spike |
S3 sink unit-tested against InMemory (incl. PutMode::Create enforcement); kafka→VersityGW e2e green; spike answer recorded |
| 5 | Multi-mirror in one process + graceful shutdown + e2e | Loop accepts a shutdown future; Sink::flush is wired to flush_now on FS/S3; SIGINT/SIGTERM trigger a graceful exit; parallel-mirrors e2e green |
| 5b | Restart-correctness e2e | e2e/tests/restart_correctness.rs proves abort + restart resumes at max(to)+1 with no gaps or duplicates against real Kafka |
| 2b (deferred) | Toxiproxy fault injection in the Docker stack | The Provisioner/ProvisionedStack::inject_fault seam is in place; a Toxiproxy-based impl is the next infra investment. Restart-correctness is now covered without it (see 5b) so this is lower priority than originally framed. |
| 6 (handoff) | Cutover: replace the Java worker image in checkit/mirror-v3 |
Done by the operator, not this repo |
| 4 | mirror-s3 sink via object_store, redpanda → versitygw e2e |
Concurrent writer race produces hard exit, never a silently-overlapping blob |
| 5 | Supervisor for N mirrors in one process; per-mirror metrics | Two mirrors run side-by-side under fault injection |
| 6 | Cutover: replace the Java worker image in checkit/mirror-v3 | Dev site running Rust binary; Java module archived |
These are not style preferences. Breaking any of them defeats the whole point of the rewrite.
- Restart correctness derives from the destination. Never persist source position to a local file, lock file, sidecar DB, or consumer-group commit as the truth. Group commits are monitoring-only.
- Before producing source offset N, verify the destination is exactly at N. For Kafka sinks: read target
end_offsetsand require it equals N. For blob sinks: derive next-expected offset from the prefix listing, require it equals N. Any mismatch is a hard exit. - Atomic writes only. Filesystem = same-directory
rename(2). S3 = singlePutObject, ideally withPutMode::Create(If-None-Match: *). Multi-step writes that could leave the destination in an inconsistent state are unacceptable. - Naming encodes both
fromandtosource offsets for blob destinations (from= previousto+ 1, so the chain is contiguous across source offset holes; see README "Offset holes"). Listing →max(to)+1is the next-expected offset. Two objects sharing afromis a corruption-detection signal — exit and alert. The chain starts at 0, or at its floor: the highestfloor-*marker, written only bymirror-v3 trim(README "Retention"). Below the floor objects may be missing; above it the chain must be contiguous as everywhere else. Nothing inrunwrites a marker or deletes an object. Withlock-days, an object's retention lock is set only in the PUT that creates it and is read back before the object counts as written; nothing sets the retention of an object that exists (README "Retention lock"). - One process = one writer per
(topic, partition). Deployments run a single replica withRecreate. Don't add leader election or coordination; the orchestrator owns singleton-ness. - Correctness > performance, always. If you have to choose, choose to exit and let k8s restart you.
Every change should land with a failing test first, then the fix. Three layers:
- Unit tests in
crates/*/src/(private helpers). - Integration tests in
crates/*/tests/(public API of one crate). Themirror-configschema golden test (crates/mirror-config/tests/schema.rs) is here. - E2e tests in (Phase 2+)
e2e/workspace member. These spin Docker containers viatestcontainers-rs, optionally fronted by Toxiproxy for fault injection. TheTestEnvironmenttrait must remain implementable by future provisioners (kind, real cloud).
Run locally:
# Fast loop (no Docker, ~seconds):
cargo test --workspace --exclude mirror-e2e
cargo fmt --all -- --check
cargo clippy --workspace --all-targets --locked -- -Dwarnings
cargo run -p xtask -- check-schema # fails if structs changed without regen
# Full e2e (requires Docker; pulls broker images on first run):
cargo test -p mirror-e2eCI in .github/workflows/ci.yml runs all of the above on every PR.
The committed JSON Schema at schemas/mirror-v3.config.schema.json is load-bearing. Editors validate users' YAML against it.
- Edit structs in
crates/mirror-config/src/lib.rs. - Add tests in
crates/mirror-config/tests/loading.rsfor the new field. cargo run -p xtask -- gen-schemato regenerate the committed JSON file.cargo test --workspace— thecommitted_schema_matches_current_structstest intests/schema.rsmust pass.- Update
examples/if a new field is mandatory. - Update the field reference table in
README.md.
#[serde(deny_unknown_fields, rename_all = "kebab-case")] is set on every struct so YAML uses kebab-case keys and typos fail loudly. Keep it that way.
Base: gcr.io/distroless/cc-debian12:latest (glibc + libgcc + libstdc++; enough for librdkafka in Phase 1). Pin by @sha256:<digest> once we publish images. Update both stages of the Dockerfile together.
The image runs as nonroot. The binary needs no extra capabilities.
rdkafka(Phase 1+): librdkafka bindings, built from source with CMake (cmake-build). The build packages are in README.md, "Development on Ubuntu", the same set the Dockerfile's builder installs.object_store(Phase 3+): one trait coversLocalFileSystem+AmazonS3.PutMode::Createis the preferred atomicity primitive; fall back to scan-validate on backends that ignore it.schemarsv1: derive macro +schema_for!. Always re-runxtask gen-schemaafter touching structs.
- Phase 4 (resolved): Does VersityGW (POSIX backend) honor
If-None-Match: *? Only when it stores metadata.e2e/tests/kafka_to_versitygw.rs::versitygw_conditional_put_spikefirst found that v1.4.1 silently overwrites the second PUT, against a gateway run with--nometa. On v1.6.0 a gateway with--nometastill overwrites, and one that stores metadata (xattrs, or--sidecar, which the e2e harness uses because trim needsx-amz-meta-*) rejects the second PUT with 412. AWS S3 honors it (Aug 2024 feature). Consequence: when the destination is a VersityGW with--nometa, the only protection against the two-writer overlap race is the deployment guarantee (k8sRecreate, single replica) plus scan-validate on startup. The S3 sink still issuesPutMode::Createbecause the cost is the same and backends that honor it get API-level atomicity from it. - Phase 6: Cutover plan for checkit/mirror-v3 — env-var shim so
mirror-v3-worker-deployment.yamldoesn't need to change in lockstep.