forked from gocardless/pgsql-cluster-manager
/
break-cluster
executable file
·178 lines (145 loc) · 6.59 KB
/
break-cluster
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
set -eu
# Automated by this script:
#
# - Add backup VIP to cluster and force it to synchronous replica
#
# - Create a table called temp (SERIAL, TEXT)
# - SELECT pg_switch_xlog
# - Put the cluster into maintenance mode
# - Insert data
#
# - kill -9 the primary Postgres instance
#
# - Copy latest WAL out to edit
# - Set one of the WAL record sizes to zero (null byte)
# - Copy that WAL into /tmp/wals/ on both replicas
#
# - Send SIGABRT to synchronous replica
#
# - Take the cluster out of maintenance
# - See what happens
# Discarded steps:
#
# - Edit restore_command to restore from /tmp/wals/ (now set in initial config)
# - Restart Postgres on both replicas to pick up recovery conf (no changes needed)
# - Turn off replication on replicas (primary_conninfo) (no need, hard killing primary)
# node-exec <container> <cmd>...
function node-exec() {
docker exec "$1" "${@:2}"
}
# write-file <container> <filepath>
function write-file() {
echo -e "[$1]\twriting file $2..."
docker exec -i "$1" bash -c "cat > $2"
}
function disable-streaming-replication() {
echo -e "[$1]\tcreating recovery.conf with no primary_conninfo"
cat <<EOF | write-file "$1" /var/lib/postgresql/9.4/main/recovery.conf
standby_mode = 'on'
restore_command = 'cp /tmp/wals/%f %p'
recovery_target_timeline = 'latest'
EOF
}
function reload-postgres() {
echo -e "[$1]\treloading postgres..."
node-exec "$1" psql -U postgres postgres -c "select pg_reload_conf();"
}
function add-backup-vip() {
echo -e "[$1]\tadding backup VIP to cluster..."
node-exec "$1" crm configure primitive PostgresqlBackupVIP ocf:heartbeat:IPaddr2 params ip=172.17.0.101 cidr_netmask=32 op monitor interval=5s
node-exec "$1" crm configure colocation backup-vip-not-with-master -inf: PostgresqlBackupVIP msPostgresql:Master
}
function force-backup-vip-to-node() {
echo -e "[$1]\tforcing backup VIP to node $1..."
# VIP might already be in the right place
node-exec "$1" crm resource migrate PostgresqlBackupVIP $1 || return 0
node-exec "$1" crm resource unmigrate PostgresqlBackupVIP || return 0
}
function kill-nine-postgres() {
local pid
echo -e "[$1]\thard killing postgres..."
pid=$(node-exec "$1" head -n1 /var/lib/postgresql/9.4/main/postmaster.pid)
node-exec "$1" kill -9 $pid
}
function turn-off-corosync() {
echo -e "[$1]\tturning off pacemaker..."
# For some reason this succeeds, but exits 1, and I don't care to figure out why
node-exec "$1" service pacemaker stop || return 0
}
function create-fake-baboon() {
echo -e "[$1]\tcreating WAL directory to simulate Barman server..."
node-exec "$1" sudo -u postgres mkdir -p /tmp/wals/
}
function get-latest-wal-name() {
local latest_file
latest_file=$(node-exec "$1" sudo -u postgres ls -tr1 /var/lib/postgresql/9.4/main/pg_xlog/ | grep -v .history | grep -v archive_status | tail -n1)
# How to "return" a string in bash
echo $latest_file
}
function extract-wal() {
echo -e "[$1]\textracting latest WAL file $2 from node $1..."
docker cp "$1":/var/lib/postgresql/9.4/main/pg_xlog/"$2" .
}
function break-wal() {
echo -e "[$1]\tbreaking WAL file $1..."
"$(dirname $0)"/break-wal "$1"
}
function inject-wal() {
local without_suffix
echo -e "[$1]\tinjecting WAL file $2 into node $1..."
without_suffix=$(basename -s .broken "$2")
docker cp $2 "$1":/tmp/wals/"$without_suffix"
}
function send-signal-postgres-worker() {
local process_pid
echo -e "[$1]\tKilling $2 process with $3 on $1..."
process_pid=$(node-exec "$1" ps -ef | grep postgres | grep $2 | awk '{ print $2}')
echo -e "[$1]\t$2 process pid is $process_pid"
node-exec "$1" kill -"$3" $process_pid
}
PRIMARY="$(node-exec pg01 bash -c "crm node list | grep -B1 LATEST | head -n1 | cut -d'(' -f1")"
SYNC="$(node-exec pg01 bash -c "crm node list | grep -B1 SYNC | head -n1 | cut -d'(' -f1")"
POTENTIAL="$(node-exec pg01 bash -c "crm node list | grep -B1 POTENTIAL | head -n1 | cut -d'(' -f1")"
echo "Primary is $PRIMARY"
echo "Sync is $SYNC"
echo "Potential is $POTENTIAL"
add-backup-vip "$SYNC"
force-backup-vip-to-node "$SYNC"
sleep 2
read -p "Press enter to break everything"
node-exec "$PRIMARY" psql -U postgres postgres -c "CREATE TABLE repro (data TEXT);"
node-exec "$PRIMARY" psql -U postgres postgres -c "SELECT pg_switch_xlog();"
# node-exec "$PRIMARY" psql -U postgres postgres -c "CREATE TABLE talks (data TEXT);"
# node-exec "$PRIMARY" psql -U postgres postgres -c "SELECT pg_switch_xlog();"
node-exec "$SYNC" bash -c "crm configure property maintenance-mode=true"
# node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO talks VALUES ('binary-logs')";
# node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO talks VALUES ('niche-git-features')";
# node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO talks VALUES ('fp-karaoke')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('A')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('AB')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABC')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABC')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCD')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDE')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEF')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFG')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFG')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFGH')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFGHI')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFGHIJ')";
node-exec "$PRIMARY" psql -U postgres postgres -c "INSERT INTO repro VALUES ('ABCDEFGHIJK')";
kill-nine-postgres "$PRIMARY"
# Not doing this for now as we're not sure it reflects what really happened
# turn-off-corosync "$PRIMARY"
LATEST_WAL=$(get-latest-wal-name "$PRIMARY")
extract-wal "$PRIMARY" "$LATEST_WAL"
break-wal "$LATEST_WAL"
inject-wal "$SYNC" "$LATEST_WAL".broken
inject-wal "$POTENTIAL" "$LATEST_WAL".broken
read -p "Press enter to kill WAL writer on sync ($SYNC) and bring cluster out of maintenance"
# Running this query means we get the "aborting due to a crash in another process" log
node-exec "$SYNC" psql -U postgres postgres -c 'SELECT pg_sleep(10)' > /dev/null 2>&1 &
sleep 2
send-signal-postgres-worker $SYNC "writer" "SIGABRT"
node-exec "$SYNC" bash -c "crm configure property maintenance-mode=false"