forked from 2ndQuadrant/bdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdr_pgbench_check.sh
175 lines (147 loc) · 5.57 KB
/
bdr_pgbench_check.sh
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
#!/usr/bin/env bash
#CONFIG
DATADIR=./tmp_check
SCALE=4
CLIENTS=4
RUNMODE="$BDR_PGBENCH_RUNMODE"
if [ ! -n "$RUNMODE" ]; then
RUNMODE="parallel"
fi
RUNTIME="$BDR_PGBENCH_RUNTIME"
if [ ! -n "$RUNTIME" ]; then
RUNTIME=100
fi
#INTERNAL
TOPBUILDDIR=@top_srcdir@
BINDIR=@bindir@
LIBDIR=@libdir@
MAKE=@MAKE@
PGCONF=bdr_pgbench.conf
HBACONF=pg_hba.conf
PRIMARY_HOST=localhost
PRIMARY_PORT=7432
PRIMARY_DB=bdr_pgbench
SLAVE_HOST=localhost
SLAVE_DB=bdr_pgbench2
SLAVE_PORT=7432
SCRIPTDIR="$( cd "$(dirname "$0")" ; pwd -P )"
convertsecs () {
((h=${1}/3600))
((m=(${1}%3600)/60))
((s=${1}%60))
printf "%02d:%02d:%02d\n" $h $m $s
}
# get full paths
mkdir -p $DATADIR
rm -r $DATADIR/*
cd $DATADIR
DATADIR=`pwd -P`
BINDIR=$DATADIR/install/$BINDIR
LIBDIR=$DATADIR/install/$LIBDIR
cd $SCRIPTDIR
cd $TOPBUILDDIR
TOPBUILDDIR=`pwd -P`
echo >$SCRIPTDIR/bdr_pgbench_check.log 2>&1
on_exit() {
$BINDIR/pg_ctl -D $DATADIR/data stop -w -mfast >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
echo "Error occured, check $SCRIPTDIR/bdr_pgbench_check.log for more info"
exit 1
}
trap 'on_exit' ERR
# install pg and contrib
echo "Installing Postgres"
cd $TOPBUILDDIR
$MAKE DESTDIR="$DATADIR/install" install >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
echo "Installing Postgres contrib modules"
cd contrib
$MAKE DESTDIR="$DATADIR/install" install >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
# setup environment
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$LIBDIR
LIBPATH=$LIBPATH:$LIBDIR
# create and start pg instance
echo "Initializing Postgres instance"
cd $SCRIPTDIR
$BINDIR/initdb -D $DATADIR/data >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
cp $PGCONF $DATADIR/data/postgresql.conf
cp $HBACONF $DATADIR/data/pg_hba.conf
$BINDIR/pg_ctl -D $DATADIR/data start -w -l $DATADIR/bdr_pgbench_check_pg.log >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
# create databases
echo "Creating databases"
$BINDIR/psql -h $PRIMARY_HOST -p $PRIMARY_PORT postgres -c "CREATE DATABASE $PRIMARY_DB" >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT postgres -c "CREATE DATABASE $SLAVE_DB" >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
$BINDIR/psql -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB > /dev/null << SQL
DO \$\$
BEGIN
FOR i IN 1..10 LOOP
PERFORM * FROM pg_replication_slots;
IF FOUND THEN
RETURN;
END IF;
PERFORM pg_sleep(1);
END LOOP;
PERFORM pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;
END;\$\$;
SQL
# initialize pgbench
echo "Setting up pgbench schema on primary db"
$BINDIR/pgbench -q -i -s $SCALE -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
$BINDIR/psql -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB -c "SELECT pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;" > /dev/null
if [ "$RUNMODE" = "parallel" ]; then
echo "Setting up pgbench schema on slave db"
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB -c "CREATE SCHEMA pgbench2; ALTER DATABASE $SLAVE_DB SET search_path=pgbench2,pg_catalog;" >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB -c "SELECT pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;" > /dev/null
$BINDIR/pgbench -q -i -s $SCALE -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB -c "SELECT pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;" > /dev/null
fi
# run pgbench
echo "Running pgbench (for $(convertsecs RUNTIME)) ..."
if [ "$RUNMODE" = "zigzag" ]; then
NUM_RUNS=10
RUNTIME=$(($RUNTIME/$NUM_RUNS))
else
NUM_RUNS=1
fi
for i in `seq 1 $NUM_RUNS`; do
if [ $(($i%2)) -eq 1 ]; then
$BINDIR/pgbench -n -T $RUNTIME -j $CLIENTS -c $CLIENTS -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1 &
PRIMARY_BENCHPID=$!
fi
if [ $(($i%2)) -eq 0 ] || [ "$RUNMODE" = "parallel" ]; then
$BINDIR/pgbench -n -T $RUNTIME -j $CLIENTS -c $CLIENTS -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1 &
SLAVE_BENCHPID=$!
fi
# wait for pgbench instance(s) to finish
while kill -0 $PRIMARY_BENCHPID 2>>/dev/null || ( [ -n "$SLAVE_BENCHPID" ] && kill -0 $SLAVE_BENCHPID 2>>/dev/null ) ; do
sleep 1
done
done
$BINDIR/psql -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB -c "SELECT pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;" > /dev/null
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB -c "SELECT pg_xlog_wait_remote_apply(pg_current_xlog_location()::text, pid) FROM pg_stat_replication;" > /dev/null
SQL=$(cat <<EOF
SET search_path=pg_catalog;
DO \$\$
DECLARE
relid oid;
cnt bigint;
hsh bigint;
BEGIN
FOR relid IN SELECT t.relid FROM pg_stat_user_tables t WHERE schemaname NOT IN ('bdr') ORDER BY schemaname, relname
LOOP
EXECUTE 'SELECT count(*), sum(hashtext((t.*)::text)) FROM ' || relid::regclass::text || ' t' INTO cnt, hsh;
RAISE NOTICE '%: %, %', relid::regclass::text, cnt, hsh;
END LOOP;
END;\$\$;
EOF
)
$BINDIR/psql -h $PRIMARY_HOST -p $PRIMARY_PORT $PRIMARY_DB -c "$SQL" >$DATADIR/primary.chksum 2>&1
$BINDIR/psql -h $SLAVE_HOST -p $SLAVE_PORT $SLAVE_DB -c "$SQL" >$DATADIR/slave.chksum 2>&1
echo "Pgbench finished, cleaning up"
# stop pg
$BINDIR/pg_ctl -D $DATADIR/data stop -w -mfast >>$SCRIPTDIR/bdr_pgbench_check.log 2>&1
cd $SCRIPTDIR
diff -u $DATADIR/primary.chksum $DATADIR/slave.chksum > $DATADIR/chksum.diff
if [ ! -n "$?" ]; then
echo "ERROR: data in databases differ, check $DATADIR/chksum.diff"
exit 1
fi