#!/usr/bin/env bash set -euo pipefail # Only called against the disposable postgres-check cluster. Two independently # connected regions contend for one world allocator, then retry after reconnect. work="$(mktemp -d /tmp/evo-life-reservations.XXXXXXXX)" psql -X -v ON_ERROR_STOP=1 > /dev/null <<'SQL' INSERT INTO evolution.life_allocators VALUES (9751,1); INSERT INTO evolution.region_leases(world_id,region_id,frame_id) VALUES (9751,1,7),(9751,2,7); SQL runtime() { psql -XAt -v ON_ERROR_STOP=1 -U "$EVO_PG_RUNTIME_USER" "$@"; } IFS='|' read -r holder_a epoch_a frame_a duration_a < <(runtime -c "SELECT * FROM evolution.acquire_region_lease(9751,1,60000,decode(repeat('d5',32),'hex'))") IFS='|' read -r holder_b epoch_b frame_b duration_b < <(runtime -c "SELECT * FROM evolution.acquire_region_lease(9751,2,60000,decode(repeat('d5',32),'hex'))") account_a=22222222-2222-2222-2222-222222222222 account_b=33333333-3333-3333-3333-333333333333 runtime -c "SELECT * FROM evolution.claim_account_control(9751,1,'$holder_a',$epoch_a,'$account_a',1,1,60000)" > /dev/null runtime -c "SELECT * FROM evolution.claim_account_control(9751,2,'$holder_b',$epoch_b,'$account_b',1,1,60000)" > /dev/null reserve_a="SELECT life_identity FROM evolution.reserve_primitive_life(9751,1,'$holder_a',$epoch_a,'$account_a',1,1,1)" reserve_b="SELECT life_identity FROM evolution.reserve_primitive_life(9751,2,'$holder_b',$epoch_b,'$account_b',1,1,1)" # Hold allocator row so both actual independent connections queue for it. psql -X -v ON_ERROR_STOP=1 -c "BEGIN; SELECT * FROM evolution.life_allocators WHERE world_id=9751 FOR UPDATE; SELECT pg_sleep(1); COMMIT" > "$work/lock" & locker=$! # No correctness assumption on which client wins; the shared counter must issue # exactly two distinct identities, even if the lock process starts last. runtime -c "$reserve_a" > "$work/a" & first=$! runtime -c "$reserve_b" > "$work/b" & second=$! wait "$first"; wait "$second"; wait "$locker" test "$(cat "$work/a")" != "$(cat "$work/b")" test "$(runtime -c "$reserve_a")" = "$(cat "$work/a")" test "$(runtime -c "$reserve_b")" = "$(cat "$work/b")" test "$(psql -XAtc 'SELECT next_identity FROM evolution.life_allocators WHERE world_id=9751')" = 3 # Same account/request on two independent connections retains exactly one ID. runtime -c "$reserve_a" > "$work/retry1" & first=$! runtime -c "$reserve_a" > "$work/retry2" & second=$! wait "$first"; wait "$second" cmp "$work/a" "$work/retry1"; cmp "$work/a" "$work/retry2" test "$(psql -XAtc 'SELECT count(*) FROM evolution.life_reservations WHERE world_id=9751')" = 2 account_c=44444444-4444-4444-4444-444444444444 runtime -c "SELECT * FROM evolution.claim_account_control(9751,2,'$holder_b',$epoch_b,'$account_c',1,2,1000)" > /dev/null psql -X -v ON_ERROR_STOP=1 > "$work/expiry-lock" <<'SQL' & BEGIN; SELECT * FROM evolution.life_allocators WHERE world_id=9751 FOR UPDATE; \echo life-lock-held SELECT pg_sleep(2); COMMIT; SQL locker=$! locked=false for attempt in $(seq 1 200); do if grep -q life-lock-held "$work/expiry-lock"; then locked=true; break; fi sleep 0.01 done test "$locked" = true if runtime -c "SELECT * FROM evolution.reserve_primitive_life(9751,2,'$holder_b',$epoch_b,'$account_c',1,1,1)" > "$work/expired" 2>&1; then echo 'Expired controller allocated an identity after waiting' >&2; exit 1 fi wait "$locker" grep -q 'life control fenced' "$work/expired" test "$(psql -XAtc 'SELECT next_identity FROM evolution.life_allocators WHERE world_id=9751')" = 3 test "$(psql -XAtc 'SELECT count(*) FROM evolution.life_reservations WHERE world_id=9751')" = 2 psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.region_leases SET expires_at=clock_timestamp()-interval '1 second' WHERE world_id=9751" > /dev/null printf '%s\n' 'PASS independent life reservation connections, shared allocator and reconnect retries'