feat: add new TAP group infra for PostgreSQL replication testing

pull/5422/head
Javier Jaramago Fernández 2 months ago
parent b14f88226c
commit b7e98f170d

@ -0,0 +1,3 @@
FROM postgres:17
RUN apt-get update && apt-get install -y iproute2

@ -0,0 +1,29 @@
# PostgreSQL Primary-Replica Infra
## Images build
```bash
docker build -t postgres-tc:17 .
```
This step is automatically performed by `docker-compose-init.bash`. Images should be reused between
executions.
## Start / Stop
To start the infra just execute the group startup script:
```bash
pre-proxysql.bash
```
To stop the infra just execute the group shutdown script:
```bash
post-proxysql.bash
```
## Folder structure
* `conf`: Config files for both infra and `ProxySQL`.
* `scripts`: Collection of scripts used to prepare the infra.

@ -0,0 +1,35 @@
#!/bin/bash
set -e
. constants
POSTGRE_SETUP_DIR=$(dirname $(realpath $0))/../scripts/
WAITED=0
TIMEOUT=300
RC=1
set +e
printf "[$(date)] Waiting for PostgreSQL service to initialize"
while [ $RC -ne 0 ]; do
if [ $WAITED -gt $TIMEOUT ]; then
echo "[ERROR] Timeout of $TIMEOUT seconds reached while connecting to MySQL"
exit 1
else
printf "."
PGPASSWORD=$PGSQL_PWD ON_ERROR_STOP=1 psql -h$PGSQL_HOST -p$PGSQL_PORT -U$PGSQL_DB -c"SELECT" > /dev/null 2>&1
RC=$?
WAITED=$((WAITED+1))
sleep 1
fi
done
printf "\n"
set -e
echo "[$(date)] Creating table structures for testing ..."
set -x
PGPASSWORD=$PGSQL_PWD ON_ERROR_STOP=1 psql -h$PGSQL_HOST -p$PGSQL_PORT -U$PGSQL_DB < $POSTGRE_SETUP_DIR/create_test_tables.sql
set +x

@ -0,0 +1,35 @@
#!/bin/bash
set -e
. constants
PROXY_CONF_DIR=$(dirname $(realpath $0))/../conf/proxysql
WAITED=0
TIMEOUT=300
RC=1
set +e
printf "[$(date)] Waiting for ProxySQL service to initialize"
while [ $RC -eq 1 ]; do
if [ $WAITED -gt $TIMEOUT ]; then
echo "[ERROR] Timeout of $TIMEOUT seconds reached while connecting to ProxySQL"
exit 1
else
printf "."
mysql -h$ADMIN_HOST -P$ADMIN_PORT -u$ADMIN_USER -p$ADMIN_PWD -e"\s" > /dev/null 2>&1
RC=$?
WAITED=$((WAITED+1))
sleep 1
fi
done
printf "\n"
set -e
echo "[$(date)] Applying initial config for ProxySQL ..."
set -x
mysql --prompt="admin> " -u$ADMIN_USER -p$ADMIN_PWD --table -h$ADMIN_HOST -P$ADMIN_PORT < $PROXY_CONF_DIR/config.sql
set +x

@ -0,0 +1,9 @@
CREATE USER repluser WITH replication encrypted password 'replpass';
SELECT pg_create_physical_replication_slot('replication_slot');
CREATE USER proxymon WITH encrypted password 'proxymon';
GRANT pg_monitor TO proxymon;
-- For testing 'pgsql-monitor_dbname'
CREATE DATABASE proxymondb;

@ -0,0 +1,36 @@
SET pgsql-monitor_password='proxymon';
SET pgsql-monitor_username='proxymon';
LOAD PGSQL VARIABLES TO RUNTIME;
SAVE PGSQL VARIABLES TO DISK;
DELETE FROM pgsql_users;
INSERT INTO pgsql_users (username,password,default_hostgroup) VALUES ('postgres','postgres',0);
LOAD PGSQL USERS TO RUNTIME;
SAVE PGSQL USERS TO DISK;
SET pgsql-monitor_replication_lag_interval=1000;
LOAD PGSQL VARIABLES TO RUNTIME;
SAVE PGSQL VARIABLES TO DISK;
DELETE FROM pgsql_replication_hostgroups;
INSERT INTO
pgsql_replication_hostgroups (writer_hostgroup, reader_hostgroup, check_type, comment)
VALUES
(0, 1, 'read_only', 'pg-replication');
LOAD PGSQL SERVERS TO RUNTIME;
SAVE PGSQL SERVERS TO DISK;
DELETE FROM pgsql_servers;
INSERT INTO
pgsql_servers (hostgroup_id, hostname, port, max_replication_lag, comment)
VALUES
(0, '127.0.0.1', 15432, 3, 'pg-primary'),
(1, '127.0.0.1', 15433, 3, 'pg-replica');
LOAD PGSQL SERVERS TO RUNTIME;
SAVE PGSQL SERVERS TO DISK;

@ -0,0 +1,12 @@
#!/bin/bash
export PGSQL_USER=${TAP_PGSQLROOT_USERNAME:=postgres}
export PGSQL_PWD=${TAP_PGSQLROOT_PASSWORD:=postgres}
export PGSQL_DB=${TAP_PGSQLROOT_DATABASE:=postgres}
export PGSQL_HOST=${TAP_PGSQLSERVER_HOST:=127.0.0.1}
export PGSQL_PORT=${TAP_PGSQLSERVER_PORT:=15432}
export ADMIN_HOST=${TAP_ADMINHOST:=127.0.0.1}
export ADMIN_PORT=${TAP_ADMINPORT:=6032}
export ADMIN_USER=${TAP_ADMINUSERNAME:=radmin}
export ADMIN_PWD=${TAP_ADMINPASSWORD:=radmin}

@ -0,0 +1,3 @@
#!/bin/bash
docker compose down -v

@ -0,0 +1,10 @@
#!/bin/bash
set -e
. constants
docker build -t postgres-tc:17 .
docker compose up -d
./bin/docker-pgsql-post.bash && ./bin/docker-proxy-post.bash

@ -0,0 +1,70 @@
x-pg-common:
&pg-common
image: postgres-tc:17
user: postgres
restart: always
healthcheck:
test: 'pg_isready -U postgres --dbname=postgres'
interval: 10s
timeout: 5s
retries: 5
services:
pg_primary:
<<: *pg-common
ports:
- 15432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
POSTGRES_INITDB_ARGS: "--auth-host=scram-sha-256"
networks:
default: {}
pg_backend:
aliases:
- pg_primary_int
cap_add:
- NET_ADMIN
command: |
postgres
-c wal_level=replica
-c hot_standby=on
-c max_wal_senders=10
-c max_connections=500
-c max_replication_slots=10
-c hot_standby_feedback=on
volumes:
- ./conf/postgres/00_init.sql:/docker-entrypoint-initdb.d/00_init.sql
pg_replica:
<<: *pg-common
ports:
- 15433:5432
environment:
PGUSER: repluser
PGPASSWORD: replpass
networks:
- default
- pg_backend
cap_add:
- NET_ADMIN
command: |
bash -c "
until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=replication_slot --host=pg_primary_int --port=5432
do
echo 'Waiting for primary to connect...'
sleep 1s
done
echo 'Backup done, starting replica...'
chmod 0700 /var/lib/postgresql/data
postgres -c max_connections=500
"
depends_on:
- pg_primary
networks:
pg_backend:
driver: bridge
internal: true

@ -0,0 +1,8 @@
#!/bin/bash
set -e
. constants
echo "[$(date)] Shutting down PGSQL_REPL testing infra ..."
./docker-compose-destroy.bash

@ -0,0 +1,11 @@
#!/bin/bash
set -e
. constants
echo "[$(date)] Cleaning infra prior to PGSQL_REPL group testing"
./docker-compose-destroy.bash
echo "[$(date)] Starting infra required for PGSQL_REPL group testing"
./docker-compose-init.bash

@ -0,0 +1,13 @@
\set ON_ERROR_STOP on
DROP DATABASE IF EXISTS sysbench;
CREATE DATABASE sysbench;
\connect sysbench;
CREATE TABLE sbtest1 (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
k integer DEFAULT 0 NOT NULL,
c character(120) DEFAULT ''::bpchar NOT NULL,
pad character(60) DEFAULT ''::bpchar NOT NULL
);
Loading…
Cancel
Save