Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b5uEyg58znuchZ8C8i
78 lines
3.7 KiB
Bash
Executable File
78 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# The embedded engine: same connector, same envelope, running inside the Spring Boot JVM. Then the at-least-once proof.
|
|
set -eu
|
|
. "$(dirname "$0")/lib.sh"
|
|
OUT=output/07-embedded.txt
|
|
mkdir -p output target
|
|
JAR=target/debezium-cdc-1.0.jar
|
|
[ -f "$JAR" ] || mvn -B -q package -DskipTests >/dev/null 2>&1
|
|
rm -f target/embedded-offsets.dat target/run1.log target/run2.log
|
|
|
|
start_app() { # start_app <logfile> -> sets APP_PID
|
|
java -jar "$JAR" --cdc.embedded.enabled=true --cdc.listener.enabled=true \
|
|
--cdc.listener.topic=embedded.public.orders --cdc.embedded.show-raw=true \
|
|
> "$1" 2>&1 &
|
|
APP_PID=$!
|
|
}
|
|
wait_for() { # wait_for <logfile> <regex> <seconds>
|
|
for i in $(seq 1 "$3"); do grep -q -E "$2" "$1" 2>/dev/null && return 0; sleep 1; done
|
|
echo "timed out waiting for $2"; return 1
|
|
}
|
|
status_lines() { # one line per update of order 1 to A0..A2: its LSN and the status it set
|
|
grep -E '^EMBEDDED op=u key=\{"id":1\}' "$1" | sed -E 's/^EMBEDDED op=u key=\{"id":1\} lsn=([0-9]+) .*after=.*"status":"(A[0-9])".*/lsn=\1 status set to \2/' | grep 'status set to A'
|
|
}
|
|
events() { grep -E '^(EMBEDDED|LISTENER) ' "$1" | sed -e 's/ txId=[0-9]*//'; }
|
|
|
|
{
|
|
echo "### Start the Spring Boot app with the embedded engine (slot embedded_orders, topic prefix embedded)"
|
|
start_app target/run1.log
|
|
wait_for target/run1.log '^EMBEDDED op=r' 90
|
|
sleep 8
|
|
echo "snapshot rows delivered by the engine: $(grep -c '^EMBEDDED op=r' target/run1.log)"
|
|
sqlp -c "select slot_name, plugin, active from pg_replication_slots where slot_name = 'embedded_orders'"
|
|
|
|
echo
|
|
echo "### Insert, update and delete order 30, and the app's own view of them (EMBEDDED = engine callback, LISTENER = @KafkaListener on embedded.public.orders)"
|
|
sql -c "insert into orders(id, customer, status, amount_cents) values (30, 'tara', 'NEW', 300)"
|
|
sql -c "update orders set status = 'PAID' where id = 30"
|
|
sql -c "delete from orders where id = 30"
|
|
wait_for target/run1.log 'LISTENER .*TOMBSTONE' 30
|
|
sleep 2
|
|
events target/run1.log | grep -E 'key=\{"id":30\}' | cut -c1-230
|
|
|
|
echo
|
|
echo "### Same UPDATE, seen by Kafka Connect (topic shop.public.orders) and by the embedded engine (topic embedded.public.orders): what differs?"
|
|
sql -c "update orders set status = 'SHIPPED' where id = 2"
|
|
sleep 6
|
|
norm='del(.ts_ms,.ts_us,.ts_ns,.source.ts_ms,.source.ts_us,.source.ts_ns) | .source'
|
|
kafka_consume shop.public.orders 8000 | awk -F'|' '$2 == "{\"id\":2}" && $3 ~ /SHIPPED/ {print $3}' | jq -S "$norm" > /tmp/connect-src.json
|
|
grep '^EMBEDDED-RAW' target/run1.log | sed 's/^EMBEDDED-RAW //' | grep SHIPPED | jq -S "$norm" > /tmp/embedded-src.json
|
|
diff /tmp/connect-src.json /tmp/embedded-src.json || true
|
|
|
|
echo
|
|
echo "### At-least-once: offsets are flushed every 60 s (Debezium's default). Wait 65 s, then three updates, then kill -9 the JVM"
|
|
sleep 65
|
|
sql -c "update orders set status = 'A0' where id = 1"
|
|
sleep 2
|
|
sql -c "update orders set status = 'A1' where id = 1"
|
|
sql -c "update orders set status = 'A2' where id = 1"
|
|
wait_for target/run1.log 'LISTENER .*status":"A2"' 30
|
|
kill -9 "$APP_PID"; wait "$APP_PID" 2>/dev/null || true
|
|
echo "killed. Events the first run delivered for key id=1 after the wait:"
|
|
status_lines target/run1.log
|
|
|
|
echo
|
|
echo "### Restart the app (same offset file, same slot)"
|
|
start_app target/run2.log
|
|
wait_for target/run2.log '^EMBEDDED ' 90
|
|
sleep 10
|
|
echo "EMBEDDED events in the second run (anything here is a redelivery):"
|
|
status_lines target/run2.log
|
|
kill "$APP_PID"; wait "$APP_PID" 2>/dev/null || true
|
|
|
|
echo
|
|
echo "### What a downstream consumer sees on embedded.public.orders: records per (key, lsn) for the three A updates"
|
|
kafka_consume embedded.public.orders 8000 | summarize | grep -E 'after=\{"id":1,.*"status":"A[012]"' \
|
|
| sed -E 's/.* lsn=([0-9]+) .*"status":"(A[012])".*/status=\2 lsn=\1/' | sort | uniq -c
|
|
} 2>&1 | tee "$OUT"
|