Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5uEyg58znuchZ8C8i
59 lines
2.7 KiB
Bash
Executable File
59 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# What ordering does CDC to Kafka really guarantee? Per key, yes. Across keys, no. Inside one transaction, only in the WAL.
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
OUT=output/04-ordering.txt
|
|
mkdir -p output
|
|
{
|
|
echo "### Topic layout"
|
|
docker compose exec -T kafka $KT/kafka-topics.sh --bootstrap-server kafka:19092 --describe --topic shop.public.orders 2>/dev/null | head -1 | tr '\t' ' '
|
|
|
|
echo
|
|
echo "### Write: 6 orders (ids 10..15), then 4 rounds of single-row updates, then one transaction that updates 10, then 12, then 13"
|
|
for id in 10 11 12 13 14 15; do sql -c "insert into orders(id, customer, status, amount_cents) values ($id, 'c$id', 'NEW', 100)"; done
|
|
for round in 1 2 3 4; do
|
|
for id in 10 11 12 13 14 15; do sql -c "update orders set status = 'R$round' where id = $id"; done
|
|
done
|
|
sql <<'SQL'
|
|
begin;
|
|
update orders set status = 'TX-A' where id = 10;
|
|
update orders set status = 'TX-B' where id = 12;
|
|
update orders set status = 'TX-C' where id = 13;
|
|
commit;
|
|
SQL
|
|
sleep 5
|
|
|
|
kafka_consume shop.public.orders 8000 | grep -E '\|\{"id":1[0-5]\}\|' > /tmp/cdc04.txt
|
|
python3 -I - <<'PY'
|
|
import json, collections
|
|
by_part = collections.OrderedDict((p, []) for p in "012")
|
|
for line in open("/tmp/cdc04.txt"):
|
|
part, key, value = line.rstrip("\n").split("|", 2)
|
|
if value == "null": continue
|
|
v = json.loads(value)
|
|
by_part[part].append((json.loads(key)["id"], v["op"], v["source"]["lsn"], v["source"]["txId"], v["after"]["status"] if v["after"] else None))
|
|
|
|
print("### Which partition each key landed on, and whether its events stayed in WAL order inside that partition")
|
|
keys = collections.defaultdict(list)
|
|
for part, evs in by_part.items():
|
|
for e in evs: keys[e[0]].append((part, e[2]))
|
|
for k in sorted(keys):
|
|
parts = {p for p, _ in keys[k]}
|
|
lsns = [l for _, l in keys[k]]
|
|
print(f"key {k}: partitions={sorted(parts)} events={len(lsns)} lsn strictly increasing in offset order={lsns == sorted(lsns) and len(set(lsns)) == len(lsns)}")
|
|
|
|
print()
|
|
print("### Reading partition 0, then 1, then 2 (what the broker stores): is the LSN sequence globally increasing?")
|
|
seq = [e[2] for evs in by_part.values() for e in evs]
|
|
print("lsn sequence head:", seq[:8])
|
|
print("globally increasing across partitions:", seq == sorted(seq))
|
|
print("events per partition:", {p: len(e) for p, e in by_part.items()})
|
|
|
|
print()
|
|
print("### The multi-statement transaction (statuses TX-A, TX-B, TX-C): one txId, increasing lsn, three different keys")
|
|
tx_rows = [(lsn, st, k, part, tx) for part, evs in by_part.items() for k, op, lsn, tx, st in evs if st and st.startswith("TX-")]
|
|
for lsn, st, k, part, tx in sorted(tx_rows): # sorted by LSN: the order Postgres committed the statements in
|
|
print(f"status={st} key={k} partition={part} lsn={lsn} txId={tx}")
|
|
PY
|
|
} 2>&1 | tee "$OUT"
|