A cluster id is an address - one octet of the subnet, one byte of the MAC - so it is bounded at 254 and has to be recycled. Until now it was recycled by me, by reading the running clusters and typing the next free number into a tfvars. That works exactly as long as one person does it at a time. The store hands them out instead. Choosing a free address and claiming it are a single statement, so there is no window between the two, and an advisory lock keeps concurrent allocators from wasting attempts on the same number. Neither of those is the guarantee: the partial unique index is, and it also covers a writer that never took the lock. Tested against a real PostgreSQL, because everything worth testing here is the database's behaviour. Twenty simultaneous callers get twenty different addresses. A deliberately naive allocator, turned loose at the same width, had 17 of its 20 inserts rejected by the index and still could not seat two live clusters at one address - which is what says the passing case above passed for a reason and not by luck. The tests found one thing: a lease of zero meant "no expiry", but nothing said so, and there was no way to attach an expiry afterwards. Both fixed - SetLease is how a lease gets extended anyway. Each test builds the schema in a scratch schema and drops it, and aborts if current_schema() is not that one, so this runs against the production database without touching it. Verified afterwards: no scratch schemas left, both real tables untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1.5 KiB
Control plane database
001-schema.sql is the whole of it. Apply it with psql -f; every statement
is IF NOT EXISTS, so re-applying is safe.
The DSN is not in this repository. It lives on the database host in
/etc/hpcod/db.env (mode 0640, root:hpcod) as HPCOD_DB_DSN, which is also
where the service reads it from.
Running the store tests
internal/store is tested against a real PostgreSQL, because what it relies
on - a partial unique index, a generated column, an advisory lock - is
database behaviour that a fake would not reproduce. The tests skip unless
HPCOD_TEST_DSN is set.
Each test builds the schema in a scratch schema of its own and drops it
afterwards, and refuses to run if current_schema() is not that scratch
schema. So the DSN may point at the production database: the tests cannot
see or touch the clusters recorded there.
PostgreSQL listens on loopback only, so the tests run where it does. Ship a cross-compiled binary rather than installing a Go toolchain on that host:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c -o store.test ./internal/store/
ssh $DBHOST mkdir -p /tmp/hpcodstore/internal/store /tmp/hpcodstore/db
scp store.test $DBHOST:/tmp/hpcodstore/internal/store/
scp db/001-schema.sql $DBHOST:/tmp/hpcodstore/db/
ssh $DBHOST "cd /tmp/hpcodstore/internal/store && \
sudo sh -c 'set -a; . /etc/hpcod/db.env; set +a; \
HPCOD_TEST_DSN=\"\$HPCOD_DB_DSN\" ./store.test -test.v'"
ssh $DBHOST rm -rf /tmp/hpcodstore
The password never leaves the host: the DSN is read from the env file there.