Search and contents

A stand-in target database and the row player

A pair of small commands for standing up a stand-in for a target system's own database while you build or test a Database Log Scanning config (see database-log-scanning.md), before the site's database is reachable. init creates one of three table layouts an integrator will meet and fills it with a few rows. play then applies a file of inserts and updates to it, at the pace you choose, so the connector has rows to read as they arrive.

This is test tooling, not part of the public API. The options, the file formats and the exit codes may change in any release, and the stability policy (public-api-stability.md) does not cover them.

It writes to a database. Every other Kindred connector and tool only reads one. This one creates tables and inserts and updates rows, so it has three guards, and they are why you can run it without fear for a database that matters:

  • It refuses a database without its marker table. init creates a table called kindred_row_player_marker, with one row for each layout it has created. play refuses a database that has none, and writes only to the tables of the layouts the marker table lists. A database the tool did not set up is never written to.
  • It refuses a host that is not a private-network address, by the rule the connector applies (see the host rule).
  • It never reads a password from a command line. There is no option for one. The password is in the target file or in the environment variable KINDRED_PLAYER_PASSWORD, which wins. A target file that holds a password is refused unless only its owner can read it (chmod 600). Nothing the commands print includes a credential.

It does not touch the Node's own database, and init never creates over a table that exists.

Setup and the commands in this document

The commands run in order, from the repository root with the project's Python environment active, and each one relies on the ones before it. The block below makes a working directory with the files they use: the target files for a database on this machine and for three that cannot be used, and the statement files. Run it first, in the same shell. It needs a Postgres server you may create tables in on this machine's standard port, with a database called standin and an account called kindred_player; change the target file if yours are named differently.

WORK=$(mktemp -d)
cat > "$WORK/target.json" <<'JSON'
{"host": "127.0.0.1", "port": 5432, "database": "standin", "user": "kindred_player", "schema": "standin"}
JSON
echo '{"host": "8.8.8.8", "database": "standin", "user": "kindred_player"}' > "$WORK/public.json"
echo '{"host": "127.0.0.1", "database": "standin", "user": "kindred_player", "schema": "standin_unmarked"}' > "$WORK/unmarked.json"
echo '{"host": "127.0.0.1", "port": 1, "database": "standin", "user": "kindred_player"}' > "$WORK/nobody.json"
cat > "$WORK/lots.jsonl" <<'JSON'
{"op": "update", "table": "order_status", "key": {"order_id": "MD-2027-0142"}, "set": {"status_code": 3, "updated_at": "2027-03-08 10:05:00"}}
{"op": "delay", "seconds": 0.5}
{"op": "insert", "table": "audit_log", "row": {"event_uid": "5b0e1c1e-0000-4000-8000-000000000005", "event_code": 2, "entity_id": "MD-2027-0142", "old_value": "flagged", "new_value": "closed", "logged_at": "2027-03-08T11:00:00+00:00"}}
JSON
cat > "$WORK/late-commit.jsonl" <<'JSON'
{"op": "insert", "table": "audit_log", "transaction": "slow", "row": {"event_uid": "5b0e1c1e-0000-4000-8000-000000000011", "event_code": 1, "entity_id": "MD-2027-0301", "new_value": "blocked", "logged_at": "2027-03-08T12:00:00+00:00"}}
{"op": "insert", "table": "audit_log", "row": {"event_uid": "5b0e1c1e-0000-4000-8000-000000000012", "event_code": 1, "entity_id": "MD-2027-0302", "new_value": "in-progress", "logged_at": "2027-03-08T12:00:05+00:00"}}
{"op": "insert", "table": "audit_log", "row": {"event_uid": "5b0e1c1e-0000-4000-8000-000000000013", "event_code": 1, "entity_id": "MD-2027-0303", "new_value": "in-progress", "logged_at": "2027-03-08T12:00:10+00:00"}}
{"op": "delay", "seconds": 2}
{"op": "commit", "transaction": "slow"}
JSON
echo '{"op": "update", "table": "order_status", "key": {"order_id": "NO-SUCH-ORDER"}, "set": {"status_code": 1}}' > "$WORK/nothing.jsonl"
echo '{"op": "insert", "table": "audit_log"}' > "$WORK/bad.jsonl"

The three layouts

init takes the name of one. Each is a table that carries the awkward parts a real one has, on purpose: integer codes where you would want words, NULL, numeric values, padded char columns and timestamps with no time zone.

LayoutTableShapeWatermark and keyMappingChecked against
audit_log audit_log Append-only. Each row takes the next number from an identity sequence and records an integer event code, an entity, and its old and new value audit_id, event_uid state_transition_rules: code 1 is a status change, code 2 a completion the MES dictionary
order_status order_status One wide row per order, updated in place. The status is a small integer code, updated_at has no time zone and the application sets it updated_at, order_id state_transition_rules on the status code, with change_detection set to row_hash and a source_timezone the MES dictionary
inventory_ledger inventory_ledger Every receipt, issue and adjustment of a material is a row. Quantities are signed, a unit cost can be NULL, and the unit of measure is padded txn_id, txn_ref field_map, with a source_timezone of America/Chicago the ERP dictionary

Each layout is a directory of three files under engine/db_archetypes: schema.sql (the table), seed.jsonl (the rows init inserts, in the statement format below) and mapping.json (the Node Type, the canonical events the mapping produces, and the database_log section's table, columns and mapping, without the connection). A test checks each mapping against the data dictionary for its Node Type (see the data dictionary): a mapping that names a canonical field the dictionary lacks, or leaves out one an event requires, or gives a status the dictionary does not allow, fails.

The order_status layout also keeps a weld strength (weld_strength_n) and a cycle time in seconds (cycle_seconds) on each order's row, which the MES as a database log reads. A stand-in made before the cycle time column was added lacks it: drop the schema and run init again. The order_status seed holds two orders, MD-2027-0142 with a vendor and a padded vendor lot and MD-2027-0203 with NULL in both, so a scan of it meets NULL and padding at once. inventory_ledger is scanned and mapped against the ERP dictionary and is not wired to an ERP Node: an ERP Node reads its rest source only. It is there so a ledger-shaped log can be tried.

The target file

--target names a JSON file describing the database.

KeyMeaning
host Required. A host name or a literal IP address; every address it gives must be private.
database Required.
user Required.
port Default 5432.
schema The schema the tables and the marker table are in. Default public. init creates it if it is missing.
password Secret. Prefer KINDRED_PLAYER_PASSWORD.
ca_bundle_path A CA certificate file. Setting it turns on verify-full: the server's certificate must chain to it and name the host.

With no ca_bundle_path the server's certificate is not checked, so keep a password off a network you do not trust. Any other key is an error.

Creating a layout

python -m engine.db_row_player_cli init audit_log --target "$WORK/target.json"

This creates the schema if it is missing, the marker table, the table and its seed rows, all in one transaction: if anything fails, nothing is created. --no-seed creates the table without its rows.

python -m engine.db_row_player_cli init order_status --target "$WORK/target.json"
python -m engine.db_row_player_cli init inventory_ledger --target "$WORK/target.json" --no-seed

A layout that is already there is refused, and so is a table that already exists under its name; the player never creates over one:

python -m engine.db_row_player_cli init audit_log --target "$WORK/target.json"

To start again, drop the schema (DROP SCHEMA standin CASCADE) and run init again. Nothing here drops anything for you.

The statement file

JSON lines: one statement per line, played in order. A JSON array of the same objects is read too.

KeyMeaning
op Required. insert, update, delay, commit or rollback.
table insert and update: one of the tables in the table above, and one whose layout the marker table lists.
row insert: an object of column to value.
key update: an object of column to value; the row to change is the one where every column equals its value. A value of null is refused, since no row equals it.
set update: an object of column to the new value. The player does not set a column for you, so an update to a table whose updated_at the application sets must set it.
seconds delay: seconds to wait, from 0 to 3600.
transaction insert, update, commit and rollback: the name of a transaction, a short lower-case word.

A value is a string, a number, true, false or null. Give a numeric or a timestamp as text ("9.10", "2027-03-08 10:05:00"), so no precision is lost on the way. An update that changes no row is an error, not a quiet no-op.

A statement with no transaction is committed at once. One with a name runs in that transaction, which opens at its first statement and stays open, holding its row locks and the sequence numbers it took, until a commit or rollback with the same name. This is what makes a late commit repeatable: a row inserted in an open transaction takes its number from the sequence at once, and the rows that are inserted and committed after it have higher numbers and are visible first. A transaction must be closed in the same file, because one the player left open would be rolled back when it stopped, and a name cannot be used again once it is closed.

The whole file is checked before anything is written, and an error names the statement's number, never its content. A file may hold at most 10 000 statements and 10 MB, and at most 16 transactions open at once.

Playing a file

python -m engine.db_row_player_cli play "$WORK/lots.jsonl" --target "$WORK/target.json"

--speed FACTOR scales the delays: 2 is twice as fast, 0.5 half as fast, and the factor is above 0 and at most 1000.

A transaction that commits late

python -m engine.db_row_player_cli play "$WORK/late-commit.jsonl" --target "$WORK/target.json" --speed 4

The first row is inserted inside the transaction slow, and takes the lowest number. Two more rows are committed after it, and a scan that runs during the delay reads them and remembers their number as the watermark. The row in slow is committed after, with a number below the watermark, so only the connector's lookback can find it (see lookback): a lookback that reaches back past the two rows delivers it once, and one that does not misses it. That is what the documentation of the connector says will happen, and this file is how to see it.

Exit codes

CodeMeaning
0 Done.
1 A statement failed, or init could not create the layout: the database refused it, or an update changed no row. The run stopped there, and every transaction still open was rolled back. Statements committed before it stay.
2 The file, the target or the options could not be used, or the player refused the database; nothing was written.
3 The database could not be reached, or refused the connection.
4 Stopped by SIGINT or SIGTERM before the end. Transactions still open are rolled back.

A file with a bad statement writes nothing:

python -m engine.db_row_player_cli play "$WORK/bad.jsonl" --target "$WORK/target.json"

A host that is not on a private network is refused before any connection is made:

python -m engine.db_row_player_cli play "$WORK/lots.jsonl" --target "$WORK/public.json"

So is a database with no marker table:

python -m engine.db_row_player_cli play "$WORK/lots.jsonl" --target "$WORK/unmarked.json"

A database that cannot be reached is a different code:

python -m engine.db_row_player_cli play "$WORK/lots.jsonl" --target "$WORK/nobody.json" --connect-timeout 2

An update that changes no row stops the run:

python -m engine.db_row_player_cli play "$WORK/nothing.jsonl" --target "$WORK/target.json"

--connect-timeout SECONDS bounds the wait for the database (default 5, at most 600), on both commands. The player waits for a row lock for at most 10 seconds and runs no statement for more than 30, so a statement that is blocked by another session fails and does not hang.

Limits

  • It is not a copy of anyone's database. The three layouts are made up to carry the awkward parts; a real one has more columns, more event codes and its own habits. Write the mapping against the dictionary and the real table, and use these to try the connector's behavior.
  • The connector and the player are different accounts. The player needs to create tables and write rows. The connector reads, with an account that can only read (see what the site provides). Give each its own.
  • A held transaction is held by the player's process. If the player is stopped, its open transactions are rolled back and their rows never appear. There is no way to hand one to another process.
  • It is Postgres only, as the connector is.

Release: v0.11.0