Skip to content

Pull replication endpoint (gen_statem) - phase 1#213

Merged
benoitc merged 1 commit into
masterfrom
feature/pull-replicator-phase1
Jun 10, 2026
Merged

Pull replication endpoint (gen_statem) - phase 1#213
benoitc merged 1 commit into
masterfrom
feature/pull-replicator-phase1

Conversation

@benoitc

@benoitc benoitc commented Jun 10, 2026

Copy link
Copy Markdown
Owner

Phase 1 of 2. Adds a client-side CouchDB pull replicator. Phase 2 will add attachment bodies; the target behaviour contract is already final so phase 2 is additive.

What it does

couchbeam_replicator is a gen_statem that reads the changes feed and document revisions from a source CouchDB and writes them into a pluggable local target (a behaviour the app implements). It preserves each document's full revision history (_revisions), handles deletions, and is resumable. Pull only (CouchDB -> app).

The source side reuses existing couchbeam APIs: couchbeam_changes:follow_once/2 (with style=all_docs, since, limit), couchbeam:open_doc/3 ({rev,R},{revs,true} to fetch a revision with its history), and db_info/1. The target does the revs_diff/write_docs, so no push path is involved.

API

  • start_link(Source, TargetMod, TargetArgs, Opts); status/1, pause/1, resume/1, stop/1.
  • Opts: mode (oneshot|continuous), batch_size, interval, repl_id, notify, changes_options.
  • Behaviour callbacks: init/1, revs_diff/2, write_docs/2, read_checkpoint/2, write_checkpoint/3, optional terminate/2. write_docs takes [{Doc, Atts}] (Atts #{} in phase 1, populated in phase 2).

Resume

Stable repl_id + target-persisted source last_seq + revs_diff-gated idempotent writes. No CouchDB _local doc needed. With the dets backend, resume survives a restart.

Reference target + docs

couchbeam_replicator_ets (ets or dets backend) stores every revision, leaf revs, and the checkpoint. guides/replication.md documents usage, resume, and writing a target.

Tests

New e2e group pull_replication_ops (wired into run-e2e.sh and the CI CouchDB matrix): replicates docs with multi-rev history and a deletion into an ets target and asserts the import; a second run with the same repl_id resumes from the checkpoint and imports only the new changes.

Verified: rebar3 eunit (32), xref, dialyzer clean; make e2e green (all 16 groups) against CouchDB 3.5.x.

couchbeam_replicator is a gen_statem that pulls documents and their full
revision history from a source CouchDB into a pluggable local target behaviour.
Pull only, resumable via a target-stored checkpoint. Attachment bodies are
deferred to phase 2 (target entries are {Doc, #{}} for now).

- couchbeam_replicator: engine + target behaviour (revs_diff, write_docs,
  read/write_checkpoint); oneshot and continuous modes; status/pause/resume
- couchbeam_replicator_ets: ets/dets reference target preserving _revisions
- guides/replication.md, ex_doc Replication group
- e2e group pull_replication_ops (docs + history + deletion + resume), wired
  into run-e2e.sh and the CI CouchDB matrix
- CHANGELOG
@benoitc benoitc merged commit 1f967b9 into master Jun 10, 2026
9 checks passed
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