mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-09-17 13:37:26 +03:00
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:
parent
05c935a607
commit
69c238dc95
51
server/test-manual/postgres-replicas/README.md
Normal file
51
server/test-manual/postgres-replicas/README.md
Normal 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)"
|
58
server/test-manual/postgres-replicas/docker-compose.yaml
Normal file
58
server/test-manual/postgres-replicas/docker-compose.yaml
Normal 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:
|
10
server/test-manual/postgres-replicas/init-primary.sh
Executable file
10
server/test-manual/postgres-replicas/init-primary.sh
Executable 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"
|
Loading…
Reference in New Issue
Block a user