Skip to content

Commit

Permalink
Feature/patroni release 1 2 3 (#121)
Browse files Browse the repository at this point in the history
* Some optimisations around restore_command = wal-e

1) wal-e wal-fetch is incredibly slow
2) When we start postgres it will call the restore_command for every
xlog segment needed to reach consistent state even if there is a valid
file in the pg_xlog dicrectory.
3) Even if the wal-e is prefetching wal segments it it is rather
expencive to move them into pg_xlog by wal-e.

All these facts are making useless streaming of xlogs with pg_basebackup,
because afterwards it will call restore_command anyway :(

In order to optimize this process we will stream xlogs not into default
pg_xlog but into separate directory and create a wrapper restore_command
script, which will:
Check if there is xlog already available on disk either in directory
created by pg_basebackup or in the pg_xlog/.wal-e/prefetch directory.
If the file is there - just move it. If it is not - call wal-e with
usual options.

* Bump up Patroni and WAL-E versions.
  • Loading branch information
Oleksii Kliukin authored and CyberDem0n committed Jan 19, 2017
1 parent 012d217 commit 1666be9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
8 changes: 4 additions & 4 deletions postgres-appliance/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& echo 'APT::Install-Suggests "0";' >> /etc/apt/apt.conf.d/01norecommend \

&& apt-get upgrade -y \
&& apt-get install -y curl ca-certificates jq pv vim gdb strace supervisor stunnel \
&& apt-get install -y curl ca-certificates jq pv vim gdb strace supervisor stunnel realpath \

## Make sure we have a en_US.UTF-8 locale available
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
Expand Down Expand Up @@ -50,8 +50,8 @@ ENV PATH=$PATH:/usr/lib/postgresql/${PGVERSION}/bin
## 1 Install tools required to build
## 2 Build
## 3 Remove tools only required for build
ENV PATRONIVERSION=1.2.2
ENV WALE_VERSION=1.0.1
ENV PATRONIVERSION=1.2.3
ENV WALE_VERSION=1.0.2
RUN export DEBIAN_FRONTEND=noninteractive \
export BUILD_PACKAGES="postgresql-server-dev-${PGVERSION} python3-pip python3-dev build-essential pgxnclient" \
export PGXN_EXTENSIONS="quantile trimmed_aggregates" \
Expand Down Expand Up @@ -103,7 +103,7 @@ ENV WALE_ENV_DIR=$PGHOME/etc/wal-e.d/env
# Set PGHOME as a login directory for the PostgreSQL user.
RUN usermod -d $PGHOME -m postgres

ADD scm-source.json configure_spilo.py launch.sh postgres_backup.sh patroni_wait.sh post_init.sh _zmon_schema.dump callback_role.py /
ADD scm-source.json configure_spilo.py launch.sh postgres_backup.sh patroni_wait.sh post_init.sh _zmon_schema.dump callback_role.py basebackup.sh wale_restore_command.sh /
ADD supervisor.d /etc/supervisor/conf.d/
ADD stunnel.d /etc/stunnel
ADD pgq_ticker.ini $PGHOME
Expand Down
46 changes: 46 additions & 0 deletions postgres-appliance/basebackup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

PARSED=$(getopt --options v --longoptions connstring:,retries:,datadir: --name "$0" -- "$@" 2> /dev/null)
eval set -- "$PARSED"

RETRIES=2

while [[ $# -gt 0 ]]; do
case $1 in
--datadir )
DATA_DIR=$2
shift
;;
--connstring )
CONNSTR=$2
shift
;;
--retries )
RETRIES=$2
shift
;;
* )
;;
esac
shift
done

[[ -z $DATA_DIR || -z $CONNSTR || ! $RETRIES =~ ^[1-9]$ ]] && exit 1

ATTEMPT=0
while [[ $((ATTEMPT++)) -le $RETRIES ]]; do
rm -fr "${DATA_DIR}"
pg_basebackup --pgdata="${DATA_DIR}" --xlog-method=stream --dbname="${CONNSTR}"
EXITCODE=$?
if [[ $EXITCODE == 0 ]]; then
XLOG_FAST=$(dirname $DATA_DIR)/xlog_fast
rm -fr $XLOG_FAST
mv ${DATA_DIR}/pg_xlog $XLOG_FAST
rm -fr $XLOG_FAST/archive_status
mkdir ${DATA_DIR}/pg_xlog
break
elif [[ $ATTEMPT -le $RETRIES ]]; then
sleep $((ATTEMPT*10))
fi
done
exit $EXITCODE
14 changes: 10 additions & 4 deletions postgres-appliance/configure_spilo.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ def deep_update(a, b):
autovacuum_max_workers: 5
autovacuum_vacuum_scale_factor: 0.05
autovacuum_analyze_scale_factor: 0.02
{{#USE_WALE}}
recovery_conf:
restore_command: envdir "{{WALE_ENV_DIR}}" wal-e --aws-instance-profile wal-fetch "%f" "%p"
{{/USE_WALE}}
initdb:
- encoding: UTF8
- locale: en_US.UTF-8
Expand Down Expand Up @@ -178,6 +174,10 @@ def deep_update(a, b):
ssl: 'on'
ssl_cert_file: {{SSL_CERTIFICATE_FILE}}
ssl_key_file: {{SSL_PRIVATE_KEY_FILE}}
{{#USE_WALE}}
recovery_conf:
restore_command: envdir "{{WALE_ENV_DIR}}" /wale_restore_command.sh "%f" "%p"
{{/USE_WALE}}
authentication:
superuser:
username: postgres
Expand All @@ -195,8 +195,11 @@ def deep_update(a, b):
create_replica_method:
{{#USE_WALE}}
- wal_e
- basebackup_fast_xlog
{{/USE_WALE}}
{{^USE_WALE}}
- basebackup
{{/USE_WALE}}
{{#USE_WALE}}
wal_e:
command: patroni_wale_restore
Expand All @@ -206,6 +209,9 @@ def deep_update(a, b):
use_iam: 1
retries: 2
no_master: 1
basebackup_fast_xlog:
command: /basebackup.sh
retries: 2
{{/USE_WALE}}
'''

Expand Down
18 changes: 18 additions & 0 deletions postgres-appliance/wale_restore_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

readonly xlog_filename=$1
readonly xlog_destination=$2

[[ -z $xlog_filename || -z $xlog_destination ]] && exit 1

readonly xlog_dir=$(dirname $xlog_destination)
readonly xlog_fast_source=$(dirname $(dirname $(realpath $xlog_dir)))/xlog_fast/$xlog_filename
readonly wale_prefetch_source=${xlog_dir}/.wal-e/prefetch/${xlog_filename}

if [[ -f $xlog_fast_source ]]; then
exec mv "${xlog_fast_source}" "${xlog_destination}"
elif [[ -f $wale_prefetch_source ]]; then
exec mv "${wale_prefetch_source}" "${xlog_destination}"
else
exec wal-e --aws-instance-profile wal-fetch "${xlog_filename}" "${xlog_destination}"
fi

0 comments on commit 1666be9

Please sign in to comment.