server/test-manual: A way to start PostgreSQL with a read replica.

This is for local testing. Details are in the README.

[DSF-218]: https://hasurahq.atlassian.net/browse/DSF-218?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8449
GitOrigin-RevId: 758aa9600fe79ed5c2917def3c86ac185267259e
This commit is contained in:
Samir Talwar 2023-03-28 11:19:10 +02:00 committed by hasura-bot
parent 05c935a607
commit 69c238dc95
3 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,51 @@
# Testing PostgreSQL read replication
This provides a Docker Compose file that starts up a PostgreSQL primary server and a single read replica.
We can use this to test how the HGE Server and Console behave when communicating with a read replica.
## Starting and stopping
To start them, run the following from this directory:
docker compose up --wait
To stop them, run:
docker compose down
(Add `-v` to delete the data too.)
## Connecting
### Ports
To connect to the databases, you will need to get the ports.
The primary port can be found by running:
docker compose port postgres-primary 5432
The replica port can be found by running:
docker compose port postgres-replica 5432
Note that these ports may change if you restart the server.
### Credentials
The database username is `postgres`, and the password is `password`, for both the primary and the replica.
## Experimenting with the Hasura GraphQL Engine
Run HGE as follows:
cabal run graphql-engine:exe:graphql-engine -- \
--metadata-database-url="postgresql://postgres:password@$(docker compose --project-directory=server/test-manual/postgres-replicas port postgres-primary 5432)" \
serve \
--enable-console \
--console-assets-dir=$PWD/frontend/dist/apps/server-assets-console
Then run the following to get the replica database connection URL, and add it as the "default" data source.
echo "postgresql://postgres:password@$(docker compose --project-directory=server/test-manual/postgres-replicas port postgres-primary 5432)"

View File

@ -0,0 +1,58 @@
version: "3"
name: hge-postgres-replicas
services:
# The primary server, which supports reads and writes.
postgres-primary:
extends:
service: postgres
file: ../../../docker-compose/databases.yaml
command:
# for some reason this is necessary, even if we don't actually use an archive
- "--archive_mode=on"
volumes:
- primary-data:/var/lib/postgresql/data
# this script enables replication for all users
- ./init-primary.sh:/docker-entrypoint-initdb.d/init-primary.sh:ro
# Initializes the replica by creating a backup of the primary into the
# replica's data directory, then marking it as "standby" by creating the
# "standby.signal" file.
init-replica:
extends:
service: postgres
file: ../../../docker-compose/databases.yaml
command:
- su
- postgres
- -c
- |
set -ex
PGPASSWORD=password pg_basebackup --host=postgres-primary --username=postgres --no-password --pgdata=/var/lib/postgresql/data
touch /var/lib/postgresql/data/standby.signal
volumes:
- replica-data:/var/lib/postgresql/data
depends_on:
postgres-primary:
condition: service_healthy
# The replica server, which streams writes from the primary, and supports reads.
postgres-replica:
extends:
service: postgres
file: ../../../docker-compose/databases.yaml
command:
- "--primary_conninfo=host=postgres-primary port=5432 user=postgres password=password options='-c wal_sender_timeout=5000'"
- "--hot_standby=on"
volumes:
- replica-data:/var/lib/postgresql/data
depends_on:
postgres-primary:
condition: service_started
init-replica:
condition: service_completed_successfully
volumes:
primary-data:
replica-data:

View File

@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e
set -x
# Allow all users to replicate the database.
# This is very insecure.
auth="$(postgres -C password_encryption)"
echo "host replication all all ${auth}" >> "$PGDATA/pg_hba.conf"