Skip to content

Add draft replication script and config for the GeoParquet mirror (#287) - #288

Open
smnorris wants to merge 22 commits into
mainfrom
replicate
Open

smnorris wants to merge 22 commits into
mainfrom
replicate

Conversation

@smnorris

@smnorris smnorris commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

scripts/replicate.py replicates the tables in data/replication.toml to a destination: an object store URL (s3://, az://, gs://, via obstore) or a local folder, from --dest or BCDATA_REPLICATION_DEST.

For each table that is due (per its schedule and <dest>/state.json), it downloads the whole table with bcdata.to_parquet and compares the content_hash and catalogue metadata with the footer of the file at the destination (read with range requests):

  • unchanged: the check is recorded in state.json
  • only the catalogue metadata changed: the new file is uploaded and a metadata row is added to the change log
  • changed: the previous and new files are diffed with fit_changedetector, a changed row is added to the change log, and the new file is uploaded

If the download has no catalogue record but the mirror file does (eg the BC Data Catalogue was unavailable), an unchanged table is recorded as unchanged, and a changed table fails, leaving the mirror as it was.

Destination layout, a folder per table:

<dest>/<table>/<table>.parquet               latest copy, as written by bcdata.to_parquet
<dest>/<table>/changes.parquet               change log: one row per change (counts, file
                                             summaries, schema changes, keys of changed records)
<dest>/<table>/changes.json                  the change log without the keys
<dest>/<table>/changes/<timestamp>.parquet   changed records as full rows (keep_change_records only),
                                             laid out as a Delta Lake change data feed
<dest>/state.json                            last check of each table

Each change rewrites the table's change log (S3 has no append), so runs must not overlap: .github/workflows/replicate.yml runs them in a concurrency group.

replication.toml options per table:

  • schedule: daily, weekly, monthly or quarterly
  • keep_change_records: also write the changed records (default false)
  • hash_columns: for tables without a primary key in table_metadata.toml, the columns hashed to generate a key for the diff (the key exists only in the diff and change records, not the mirror). Numeric columns hash the same whether bcdata inferred int64 or double
  • ignore_columns: columns left out of the comparison, eg regenerated ids
  • allow_duplicates: compare a table whose generated key isn't unique (otherwise it fails)
  • rekey: accept a change to a table's key since its last logged change

replication.toml lists the 21 tables bcfishpass replicates today (less FISS obstacles).

Nothing in the bcdata package changes. Other changes:

  • scripts/validate_tables.py also validates replication.toml against the live WFS (tables exist, hash_columns / ignore_columns present)
  • fit_changedetector and obstore are a replicate dependency group, not package dependencies
  • CI installs the replicate group and runs tests/test_replicate.py (against a local folder and an in-memory object store)
  • .github/workflows/replicate.yml (draft) runs the script on manual dispatch, for all or named tables, optionally forcing tables that aren't due, with the versions in uv.lock. The destination and endpoint come from the BCDATA_REPLICATION_DEST and AWS_ENDPOINT_URL repository variables, and the credentials from the NRS_OBJECTSTORE_ACCESS_KEY_ID / NRS_OBJECTSTORE_SECRET_ACCESS_KEY secrets. Its daily schedule is commented out until a manual run has been checked

scripts/replicate.py replicates the tables in data/replication.toml to a
destination (any pyarrow URI, eg s3://..., from --dest or
BCDATA_REPLICATION_DEST). For each table that is due per its schedule and
<dest>/state.json, it downloads the whole table with bcdata.to_parquet and
compares the content_hash with the file at the destination. A changed table
is diffed with fit_changedetector, a change record is written to
<dest>/changes/<table>/<timestamp>/ (change.json counts, plus changes.parquet
laid out as a Delta Lake change data feed: full rows with _change_type and
_commit_timestamp), and the new file is uploaded. Mirror files are not
modified.

Tables without a primary key in table_metadata.toml set hash_columns to
generate a key for the diff (only in the diff, not the mirror), with
ignore_columns for regenerated ids and allow_duplicates for known duplicate
records. FISS observations use bcfishobs's observation_key columns.

replication.toml lists the 21 tables bcfishpass replicates today (less FISS
obstacles). validate_tables.py also checks it against the live WFS.
fit_changedetector is a "replicate" dependency group, not a package
dependency.

Co-Authored-By: Claude <noreply@anthropic.com>
@smnorris

smnorris commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

Note that this PR is meant only as a proof of concept at this point - it is 100% vibed/prompt generated and is as yet unchecked.

Also, note that the PR includes writing every change to file (state before/after a change for each changed record). I think this is probably unnecessary/overkill - simple change counts should be plenty (applications can manage their own snapshots/versions, they just need to know which records have changed - and full change recording could be added at some later date if needed)

smnorris and others added 19 commits November 25, 2026 10:26
change.json now lists the keys of inserted, deleted and updated records
(and, with allow_duplicates, of the duplicate records not compared).
changes.parquet, with the full changed rows, is written only for tables
with keep_change_records = true in replication.toml (default false).

Co-Authored-By: Claude <noreply@anthropic.com>
Replace the per-change change.json folders with a change log per table,
changes/<table>.parquet: one row per change, with counts, file
summaries, schema changes and the keys of changed records as list
columns (summary queries don't read the keys). Full changed records,
with keep_change_records, go to changes/<table>/<timestamp>.parquet.
Everything is written locally before uploading, so a failed log write
leaves no unlisted records file.

A change to a table's key (key column or hash_columns) since its last
logged change now fails the table unless rekey = true is set, as keys
before and after would not refer to the same records. If the key type
changes, the log's key columns are converted to strings so the history
stays in one file.

Co-Authored-By: Claude <noreply@anthropic.com>
Destination now transfers files with obstore instead of pyarrow.fs, so
--dest can be an S3, Azure or GCS URL (or a local folder), with
credentials and endpoints from each service's environment variables.
Footers are read with range requests. obstore is added to the replicate
dependency group, and imported optionally so validate_tables.py still
reads the config without that group.

Tests run against both a local folder and an in-memory object store.

Co-Authored-By: Claude <noreply@anthropic.com>
Install the replicate dependency group, without which
tests/test_replicate.py is skipped, and run the tests when
scripts/replicate.py or data/replication.toml change.

Co-Authored-By: Claude <noreply@anthropic.com>
scripts/check_objectstore.py writes, reads and deletes a few files under
a test prefix with replicate.py's Destination: json, a multipart upload,
footer-only reads and a download. It also reports whether the store
enforces conditional writes. Run it before replicating to a new store
(eg the NRS objectstore or Azure).

Co-Authored-By: Claude <noreply@anthropic.com>
Each table's change log is now written back with a conditional put on
the ETag it was read with (or only if it still doesn't exist), so two
overlapping runs can't each drop the other's row. A conflict fails the
table with ConcurrentWriteError, keeping the other run's log, deleting
this run's records file and leaving the mirror unchanged. Stores without
conditional writes (obstore's LocalStore) are overwritten as before. The
NRS objectstore enforces them (checked with check_objectstore.py).

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
Layout is now <table>/<table>.parquet, <table>/changes.parquet,
<table>/changes.json (the change log without key columns) and
<table>/changes/<timestamp>.parquet (keep_change_records only).

If content_hash is unchanged but the catalogue footer fields (description,
comments, columns, catalogue) differ, the new file is uploaded and a
kind = "metadata" row added to the change log (data_changed false). A new
metadata_changed column names the fields that differ, on metadata and
changed rows.

Co-Authored-By: Claude <noreply@anthropic.com>
bcdata.to_parquet writes the file without catalogue metadata if the BC
Data Catalogue lookup fails. If the mirror file has a catalogue record
and the download doesn't, an unchanged table is now recorded as
unchanged (with a warning) rather than logged as a metadata change and
uploaded, and a changed table fails with CatalogueMissingError, leaving
the mirror as it was, to be retried on the next run.

Co-Authored-By: Claude <noreply@anthropic.com>
diff_files now loads both files into DuckDB (geometries as WKB), outer
joins them on the key and compares the common columns with IS DISTINCT
FROM. The changed records are assembled there too (UNION ALL BY NAME)
and written with the files' GeoParquet metadata. geopandas, pandas and
fit_changedetector are no longer used, so read_snapshot,
harmonize_types and add_key are gone: int64 and double compare as
numbers.

Generated keys are the sha256 of the hash_columns values as a JSON list
of text, with whole numbers written as integers whatever type bcdata
inferred, so a key no longer changes when a column flips between int64
and double. Records without geometry are hashed like any other (no
longer dropped), and count_duplicates checks the generated key, so a
null in a single hash column is not a duplicate.

Behaviour otherwise follows fit_changedetector: objectid is always
ignored (unless in hash_columns), and with allow_duplicates the first
record with each key, in file order, is compared. Geometries are now
compared exactly (WKB), not normalized to 1 cm. The change log loses
dropped_null_geometry_* and fit_changedetector_version. The replicate
group requires duckdb>=2.0 (GeoParquet read as the GEOMETRY type).

Co-Authored-By: Claude <noreply@anthropic.com>
fit_changedetector hashes str() of each value, after pandas has
converted the row to a common type, so a generated key depended on
whether bcdata inferred a column as int64 or double in that download.
Each diff casts a retyped column to Float64 in both files, so its counts
were right, but a record's key could differ between change log rows (eg
"3" in an int64/int64 diff, "3.0" in a later int64/double diff).

Numeric hash_columns are now hashed as text copies, whole numbers
written as integers. The compared and written columns keep their types.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
read_bcdata_metadata requested the last 8 bytes before checking the
size, so a file under 8 bytes failed with a range error rather than
"not a parquet file". A footer length larger than the file is refused
too, rather than requesting a range from before the start of the file.

Co-Authored-By: Claude <noreply@anthropic.com>
… writes

Destination.replace overwrote the change log, logging only at debug
level, on any store that doesn't support conditional writes, so on a new
store overlapping runs could silently drop each other's rows. Only a
local folder (obstore's LocalStore) is now written to unconditionally;
any other store raises ConditionalWriteError, failing the table, and the
records file uploaded before the log is removed again.

Co-Authored-By: Claude <noreply@anthropic.com>
Runs are to be kept from overlapping by running them from one GitHub
Actions workflow with a concurrency group, so the change log no longer
needs replacing conditionally on its ETag. Removes Destination.replace,
Destination.delete, ConcurrentWriteError, ConditionalWriteError and
their tests; the change log is uploaded like the other files. The
module docstring says runs must not overlap.

read_change_log is now a single download that handles a missing log,
rather than a HEAD then a GET.

Co-Authored-By: Claude <noreply@anthropic.com>
Runs scripts/replicate.py on manual dispatch (optionally for named
tables, or forcing tables that aren't due), with the dependency versions
in uv.lock. A concurrency group keeps runs from overlapping, which the
change log now relies on. Failed tables are added to the job summary.

The destination comes from the BCDATA_REPLICATION_DEST variable and the
NRS objectstore credentials from secrets. The daily schedule is
commented out until a manual run has been checked.

Co-Authored-By: Claude <noreply@anthropic.com>
AWS_ENDPOINT_URL and AWS_REGION come from repository variables of the
same names rather than being written into the workflow. Either is unset
if its variable is: an unset repository variable is an empty string,
and an empty endpoint makes obstore fail every request.

Co-Authored-By: Claude <noreply@anthropic.com>
obstore's S3 store defaults to us-east-1 when no region is set.

Co-Authored-By: Claude <noreply@anthropic.com>
@smnorris
smnorris marked this pull request as ready for review November 26, 2026 05:02
smnorris and others added 2 commits November 26, 2026 13:46
Tables that fail in the replicate job (after bcdata's retries of each
request) are passed to a retry job, which waits 5 minutes and checks them
again on a new runner: a refusal lasting that long probably means DataBC is
blocking the first runner's IP (#264). The run fails only if a table fails
again there. Any other error still fails the replicate job.

replicate.py --failed-tables writes the failed tables, comma separated, for
the retry job's --tables.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
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