Merge pull request #4114 from omnivore-app/fix/read-replica

fix/read replica
This commit is contained in:
Hongbo Wu
2024-06-26 17:38:53 +08:00
committed by GitHub
4 changed files with 84 additions and 36 deletions

View File

@ -1,22 +1,61 @@
version: '3' version: '3'
x-postgres:
&postgres-common
image: "ankane/pgvector:v0.5.1"
user: postgres
healthcheck:
test: "exit 0"
interval: 2s
timeout: 12s
retries: 3
services: services:
postgres: postgres:
image: "ankane/pgvector:v0.5.1" <<: *postgres-common
container_name: "omnivore-postgres" container_name: "omnivore-postgres"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=omnivore
- PG_POOL_MAX=20
healthcheck:
test: "exit 0"
interval: 2s
timeout: 12s
retries: 3
expose: expose:
- 5432 - 5432
ports: ports:
- "5432:5432" - "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: omnivore
PG_POOL_MAX: 20
POSTGRES_HOST_AUTH_METHOD: "scram-sha-256\nhost replication all 0.0.0.0/0 md5"
POSTGRES_INITDB_ARGS: --auth-host=scram-sha-256
command: |
postgres
-c wal_level=replica
-c hot_standby=on
-c max_wal_senders=10
-c max_replication_slots=10
-c hot_standby_feedback=on
postgres-replica:
<<: *postgres-common
container_name: "omnivore-postgres-replica"
expose:
- 5433
ports:
- "5433:5432"
environment:
PGUSER: replicator
PGPASSWORD: replicator_password
command: |
bash -c "
until pg_basebackup --pgdata=/var/lib/postgresql/data -R --slot=replication_slot --host=postgres --port=5432
do
echo 'Waiting for primary to connect...'
sleep 1s
done
echo 'Backup done, starting replica...'
chmod 0700 /var/lib/postgresql/data
postgres
"
depends_on:
- postgres
migrate: migrate:
build: build:

View File

@ -23,27 +23,24 @@ export const appDataSource = new DataSource({
max: env.pg.pool.max, max: env.pg.pool.max,
idleTimeoutMillis: 10000, // 10 seconds idleTimeoutMillis: 10000, // 10 seconds
}, },
}) replication: env.pg.replication
? {
if (env.pg.replication) { master: {
appDataSource.setOptions({ host: env.pg.host,
replication: { port: env.pg.port,
master: { username: env.pg.userName,
host: env.pg.host, password: env.pg.password,
port: env.pg.port, database: env.pg.dbName,
username: env.pg.userName,
password: env.pg.password,
database: env.pg.dbName,
},
slaves: [
{
host: env.pg.replica.host,
port: env.pg.replica.port,
username: env.pg.replica.userName,
password: env.pg.replica.password,
database: env.pg.replica.dbName,
}, },
], slaves: [
}, {
}) host: env.pg.replica.host,
} port: env.pg.replica.port,
username: env.pg.replica.userName,
password: env.pg.replica.password,
database: env.pg.replica.dbName,
},
],
}
: undefined,
})

View File

@ -36,6 +36,7 @@ export class CustomTypeOrmLogger
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) { logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
this.logger.info(query, { this.logger.info(query, {
isReplicated: queryRunner?.connection?.driver?.isReplicated,
parameters, parameters,
}) })
} }
@ -45,7 +46,12 @@ export class CustomTypeOrmLogger
message: any, message: any,
queryRunner?: QueryRunner queryRunner?: QueryRunner
): void { ): void {
this.logger.log(level, message) if (level === 'warn') {
this.logger.log('warning', message)
return
}
this.logger.info(message)
} }
} }

View File

@ -6,6 +6,12 @@ echo "create $PG_DB database"
psql --host $PG_HOST --username $POSTGRES_USER --command "CREATE USER app_user WITH ENCRYPTED PASSWORD '$PG_PASSWORD';" || true psql --host $PG_HOST --username $POSTGRES_USER --command "CREATE USER app_user WITH ENCRYPTED PASSWORD '$PG_PASSWORD';" || true
echo "created app_user" echo "created app_user"
psql --host $PG_HOST --username $POSTGRES_USER --command "CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'replicator_password';" || true
echo "created replicator"
psql --host $PG_HOST --username $POSTGRES_USER --command "SELECT pg_create_physical_replication_slot('replication_slot');" || true
echo "created replication_slot"
PG_USER=$POSTGRES_USER PG_PASSWORD=$PGPASSWORD yarn workspace @omnivore/db migrate PG_USER=$POSTGRES_USER PG_PASSWORD=$PGPASSWORD yarn workspace @omnivore/db migrate
psql --host $PG_HOST --username $POSTGRES_USER --dbname $PG_DB --command "GRANT omnivore_user TO app_user;" || true psql --host $PG_HOST --username $POSTGRES_USER --dbname $PG_DB --command "GRANT omnivore_user TO app_user;" || true
@ -17,4 +23,4 @@ if [ -z "${NO_DEMO_USER}" ]; then
PASSWORD='$2a$10$41G6b1BDUdxNjH1QFPJYDOM29EE0C9nTdjD1FoseuQ8vZU1NWtrh6' PASSWORD='$2a$10$41G6b1BDUdxNjH1QFPJYDOM29EE0C9nTdjD1FoseuQ8vZU1NWtrh6'
psql --host $PG_HOST --username $POSTGRES_USER --dbname $PG_DB --command "INSERT INTO omnivore.user (id, source, email, source_user_id, name, password) VALUES ('$USER_ID', 'EMAIL', 'demo@omnivore.app', 'demo@omnivore.app', 'Demo User', '$PASSWORD'); INSERT INTO omnivore.user_profile (user_id, username) VALUES ('$USER_ID', 'demo_user');" psql --host $PG_HOST --username $POSTGRES_USER --dbname $PG_DB --command "INSERT INTO omnivore.user (id, source, email, source_user_id, name, password) VALUES ('$USER_ID', 'EMAIL', 'demo@omnivore.app', 'demo@omnivore.app', 'Demo User', '$PASSWORD'); INSERT INTO omnivore.user_profile (user_id, username) VALUES ('$USER_ID', 'demo_user');"
echo "created demo user with email: demo@omnivore.app, password: demo_password" echo "created demo user with email: demo@omnivore.app, password: demo_password"
fi fi