Skip to content

Commit a2c2979

Browse files
committed
wip on replication
1 parent fd10890 commit a2c2979

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ COMPOSE_FILE_RUN=docker-compose.yml,docker-compose-run.yml
22
COMPOSE_FILE=${COMPOSE_FILE_RUN},docker/compose-dev.yml
33
COMPOSE_PROJECT_NAME=off-query
44
COMPOSE_PATH_SEPARATOR=,
5+
COMPOSE_PROFILES=app
56
RESTART_POLICY=no
67
TAG=latest
78
QUERY_PORT=127.0.0.1:5511
@@ -10,6 +11,8 @@ QUERY_PORT=127.0.0.1:5511
1011
POSTGRES_IMAGE=pgautoupgrade/pgautoupgrade:16-alpine
1112
POSTGRES_EXPOSE=127.0.0.1:5512
1213
POSTGRES_SHM_SIZE=256m
14+
POSTGRES_CONFIG_FILE=./confs/postgresql/dev.conf
15+
#POSTGRES_HBA_FILE=./confs/postgresql/dev_hba.conf
1316
# This is the default. Use 16G in production
1417
QUERY_POSTGRES_SHARED_BUFFERS=128MB
1518
# This is the default. Use 1G in production
@@ -33,4 +36,4 @@ LOG_LEVEL=debug
3336
DEPS=openfoodfacts-shared-services
3437

3538
# Test settings
36-
USE_TESTCONTAINERS=1
39+
USE_TESTCONTAINERS=1

.github/workflows/container-deploy.yml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
# FIXME name you envs, see reuse below
1717
# it's also the name of the directory for the application
1818
- ${{ startsWith(github.ref, 'refs/tags/v') && 'off-query-org' || 'off-query-net' }}
19+
- ${{ startsWith(github.ref, 'refs/tags/v') && 'off-query-replica-org' || 'NONE' }}
20+
exclude:
21+
- env: 'NONE'
1922
environment: ${{ matrix.env }}
2023
concurrency: ${{ matrix.env }}
2124
steps:
@@ -43,6 +46,7 @@ jobs:
4346
echo "POSTGRES_SHM_SIZE=512m" >> $GITHUB_ENV
4447
echo "QUERY_POSTGRES_SHARED_BUFFERS=4GB" >> $GITHUB_ENV
4548
echo "QUERY_POSTGRES_WORK_MEM=256MB" >> $GITHUB_ENV
49+
4650
- name: Set various variable for production deployment
4751
if: matrix.env == 'off-query-org'
4852
run: |
@@ -66,6 +70,27 @@ jobs:
6670
echo "POSTGRES_SHM_SIZE=2g" >> $GITHUB_ENV
6771
echo "QUERY_POSTGRES_SHARED_BUFFERS=16GB" >> $GITHUB_ENV
6872
echo "QUERY_POSTGRES_WORK_MEM=1GB" >> $GITHUB_ENV
73+
74+
- name: Set various variable for prod replica deployment
75+
if: matrix.env == 'off-query-replica-org'
76+
run: |
77+
# This is deployed on Hetzner, close to superset
78+
echo "SSH_PROXY_HOST=hetzner-02.infra.openfoodfacts.org" >> $GITHUB_ENV
79+
echo "SSH_USERNAME=off" >> $GITHUB_ENV
80+
echo "SSH_PROXY_USERNAME=off" >> $GITHUB_ENV
81+
echo "SSH_HOST=10.3.0.201" >> $GITHUB_ENV
82+
83+
# we don't need to be in common net, use a specific name
84+
echo "COMMON_NET_NAME=off-query-replica-org_default" >> $GITHUB_ENV
85+
86+
# Reduce log level on production
87+
echo "LOG_LEVEL=log" >> $GITHUB_ENV
88+
89+
# Postgres config. We use memory to gain performance on reads and writes
90+
echo "POSTGRES_SHM_SIZE=2g" >> $GITHUB_ENV
91+
echo "QUERY_POSTGRES_SHARED_BUFFERS=16GB" >> $GITHUB_ENV
92+
echo "QUERY_POSTGRES_WORK_MEM=1GB" >> $GITHUB_ENV
93+
6994
- name: Wait for container build workflow
7095
uses: tomchv/[email protected]
7196
id: wait-build
@@ -110,7 +135,7 @@ jobs:
110135
# Checkout current commit SHA
111136
git checkout -qf ${{ github.sha }}
112137
113-
- name: Set environment variables
138+
- name: Set common environment variables
114139
uses: appleboy/ssh-action@master
115140
with:
116141
host: ${{ env.SSH_HOST }}
@@ -130,6 +155,7 @@ jobs:
130155
echo "DOCKER_CLIENT_TIMEOUT=120" >> .env
131156
echo "COMPOSE_HTTP_TIMEOUT=120" >> .env
132157
echo "COMPOSE_PROJECT_NAME=off-query" >> .env
158+
echo "COMPOSE_PROFILES=app" >> .env
133159
echo "COMPOSE_PATH_SEPARATOR=," >> .env
134160
echo "RESTART_POLICY=always" >> .env
135161
echo "COMPOSE_FILE=docker-compose.yml" >> .env
@@ -140,6 +166,7 @@ jobs:
140166
echo "POSTGRES_USER=postgres" >> .env
141167
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env
142168
echo "POSTGRES_DB=query" >> .env
169+
echo "POSTGRES_CONFIG_FILE=./confs/postgresql/prod.conf" >> .env
143170
echo "COMMON_NET_NAME=${{ env.COMMON_NET_NAME }}" >> .env
144171
echo "MONGO_URI=${{ env.MONGO_URI }}" >> .env
145172
echo "REDIS_URL=${{ env.REDIS_URL }}" >> .env
@@ -153,6 +180,29 @@ jobs:
153180
echo "QUERY_DATA_DIR=dbdata" >> .env
154181
echo "LOG_LEVEL=${{ env.LOG_LEVEL }}" >> .env
155182
183+
- name: Create Postgresql config file
184+
uses: appleboy/ssh-action@master
185+
with:
186+
host: ${{ env.SSH_HOST }}
187+
username: ${{ env.SSH_USERNAME }}
188+
key: ${{ secrets.SSH_PRIVATE_KEY }}
189+
proxy_host: ${{ env.SSH_PROXY_HOST }}
190+
proxy_username: ${{ env.SSH_USERNAME }}
191+
proxy_key: ${{ secrets.SSH_PRIVATE_KEY }}
192+
script: |
193+
# start from the dev file
194+
cp confs/postgresql/dev.conf confs/postgresql/prod.conf
195+
# add replication settings
196+
echo >>confs/postgresql/prod.conf <<END_CONF
197+
# replication settings
198+
wal_level=replica
199+
max_wal_senders=3
200+
# we don't set a large wal_keep_size because we will use slots
201+
max_slot_wal_keep_size=20G
202+
# this is large (current db size 140G)
203+
max_slot_wal_keep_size=100G
204+
END_CONF
205+
156206
- name: Start services
157207
uses: appleboy/ssh-action@master
158208
with:

confs/postgresql/dev.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# this is the base config for dev
2+
# On deployment you should create a similar file adding also your configs
3+
# And point to it using POSTGRES_CONFIG_FILE in .env
4+
#
5+
# include file created by initdb
6+
include=/var/lib/postgresql/data/postgresql.conf

confs/postgresql/dev_hba.conf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# TYPE DATABASE USER ADDRESS METHOD
3+
4+
# "local" is for Unix domain socket connections only
5+
local all all trust
6+
# IPv4 local connections:
7+
host all all 127.0.0.1/32 trust
8+
# IPv6 local connections:
9+
host all all ::1/128 trust
10+
# Allow replication connections from localhost, by a user with the
11+
# replication privilege.
12+
local replication all trust
13+
host replication all 127.0.0.1/32 trust
14+
host replication all ::1/128 trust
15+
16+
host all all all md5

docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
query_postgres:
33
image: ${POSTGRES_IMAGE}
4-
command: postgres -c shared_buffers=${QUERY_POSTGRES_SHARED_BUFFERS} -c work_mem=${QUERY_POSTGRES_WORK_MEM} -c listen_addresses='*'
4+
command: postgres -c config_file=/etc/postgresql.conf -c shared_buffers=${QUERY_POSTGRES_SHARED_BUFFERS} -c work_mem=${QUERY_POSTGRES_WORK_MEM} -c listen_addresses='*'
55
restart: ${RESTART_POLICY:-always}
66
environment:
77
- POSTGRES_USER
@@ -19,6 +19,9 @@ services:
1919
- "${POSTGRES_EXPOSE:-5512}:5432"
2020
volumes:
2121
- dbdata:/var/lib/postgresql/data
22+
# mounting configuration file
23+
- ${POSTGRES_CONFIG_FILE}:/etc/postgresql.conf
24+
- ${POSTGRES_HBA_FILE}:/etc/postgresql_hba.conf
2225
networks:
2326
- common_net
2427

@@ -41,6 +44,8 @@ services:
4144
condition: service_healthy
4245
networks:
4346
- common_net
47+
profiles:
48+
- app
4449

4550
networks:
4651
common_net:

0 commit comments

Comments
 (0)