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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
#!/bin/sh
#
# **********
# test_8_logship
#
# This test script creates a standalone pgbench database
# as slony_test1 and then:
#
# - initializes a primary node and starts the node daemon
# - creates two sets containing all 4 pgbench tables
# - creates a second database as slony_test2
# - adds database slony_test2 to the system
# - starts the second replication daemon
# - creates the pgbench tables (schema only)
# - subscribes the replication set from the primary node
# - creates a 3rd database node similar that replicates
# cascaded from the second node.
# - waits until the pgbench is terminated and the user
# stopped all 3 replication engines.
# - compares the 3 database.
# **********
TERMPROG=xterm
WHICHPROG=which
PATH=/usr/local/slony1/bin:$PATH
export PATH
TMPOUT=/tmp/output.$$
LOGSHIPDIR=./offline_logs
mkdir -p $LOGSHIPDIR
rm -f $LOGSHIPDIR/*
DB1=slony_test1
DB2=slony_test2
DB3=slony_test3
DB4=slony_test4
DEBUG_LEVEL=2
PGBENCH_SCALE=1
PGBENCH_CLIENTS=10
PGBENCH_TRANS=`expr 25000 / $PGBENCH_CLIENTS`
trap '
echo ""
echo "**** user abort"
if [ ! -z $pgbench_pid ] ; then
echo "**** killing pgbench"
kill -15 $pgbench_pid
fi
if [ ! -z $slon1_pid ] ; then
echo "**** killing node daemon 1"
kill -15 $slon1_pid
fi
if [ ! -z $slon2_pid ] ; then
echo "**** killing node daemon 2"
kill -15 $slon2_pid
fi
if [ ! -z $slon3_pid ] ; then
echo "**** killing node daemon 3"
kill -15 $slon3_pid
fi
slony_logshipper -T ./test_8_logshipper.conf
exit 2
' 2 15
######################################################################
# Preparations ... create a standalone pgbench database and
# have the "application" (pgbench) running.
######################################################################
#####
# Make sure the install is up to date
#####
WGM=`$WHICHPROG gmake | egrep '^/'`
if [ -z "$WGM" ] ; then
MAKE=make
CGNU=`make -v | grep GNU`
if [ -z "$CGNU" ] ; then
echo "GNU Make not found - please install GNU Make"
exit 1
fi
else
MAKE=gmake
fi
echo -n "**** running 'make install' in src directory ... "
if ! ${MAKE} -C .. install >$TMPOUT 2>&1 ; then
echo "failed"; cat $TMPOUT; rm $TMPOUT; exit -1
fi
echo "done"
rm $TMPOUT
#####
# Remove old databases, if they exist
#####
echo "**** remove old test databases"
dropdb $DB1 || echo "**** ignored"
sleep 1
dropdb $DB2 || echo "**** ignored"
sleep 1
dropdb $DB3 || echo "**** ignored"
sleep 1
dropdb $DB4 || echo "**** ignored"
sleep 1
#####
# Create the "Primary Node"
#####
echo "**** creating database for Node 1"
createdb $DB1 || exit -1
pgbench -i -s $PGBENCH_SCALE $DB1
psql $DB1 <<_EOF_
create sequence history_seq;
alter table history add column seqno int8;
alter table history alter column seqno set default nextval('history_seq');
update history set seqno = nextval('history_seq') where seqno is null;
alter table history add primary key (seqno);
_EOF_
pg_dump -s $DB1 >pgbench_schema.sql
#####
# Start pgbench in the background and give it rampup time
#####
pgbench -n -f test_8_pgbench-1.sql -s $PGBENCH_SCALE -c $PGBENCH_CLIENTS -t $PGBENCH_TRANS $DB1 &
pgbench_pid=$!
echo "**** pgbench is running in background with pid $pgbench_pid"
echo -n "**** sleeping 10 seconds to give pgbench time for rampup ... "
sleep 10
echo "done"
echo ""
echo "**********************************************************************"
echo "**** $DB1 is now a standalone database with a running pgbench"
echo "**********************************************************************"
echo ""
######################################################################
# Setup DB1 as the primary cluster T1 node, start the node daemon,
# and create a replication set containing the pgbench tables.
######################################################################
echo "**** initializing $DB1 as Primary Node for Slony-I cluster T1"
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB1';
node 3 admin conninfo = 'dbname=$DB1';
init cluster (id = 1, comment = 'Node 1');
echo 'Database $DB1 initialized as Node 1';
_EOF_
if [ $? -ne 0 ] ; then
kill $pgbench_pid;
exit -1
fi
echo "**** starting the Slony-I node daemon for $DB1"
$TERMPROG -sb -sl 3000 -title "Slon node 1" -e sh -c "slon -d$DEBUG_LEVEL T1 dbname=$DB1; echo -n 'Enter>'; read line" &
slon1_pid=$!
echo "slon[$slon1_pid] on dbname=$DB1"
echo "**** creating a replication set containing the 4 pgbench tables ... "
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
try {
create set (id = 1, origin = 1, comment = 'Set 1 - pgbench tables');
set add table (set id = 1, origin = 1,
id = 1, fully qualified name = 'public.accounts',
comment = 'Table accounts');
set add table (set id = 1, origin = 1,
id = 2, fully qualified name = 'public.branches',
comment = 'Table branches');
set add table (set id = 1, origin = 1,
id = 3, fully qualified name = 'public.tellers',
comment = 'Table tellers');
create set (id = 2, origin = 1, comment = 'Set 2 - pgbench history');
set add table (set id = 2, origin = 1,
id = 4, fully qualified name = 'public.history',
comment = 'Table history');
}
on error {
exit -1;
}
_EOF_
if [ $? -ne 0 ] ; then
echo "failed"
kill $pgbench_pid 2>/dev/null
kill $slon1_pid 2>/dev/null
cat $TMPOUT
rm $TMPOUT
exit -1
fi
echo "**** set created"
#####
# Check that pgbench is still running
#####
if ! kill -0 $pgbench_pid 2>/dev/null ; then
echo "**** pgbench terminated ???"
kill $slon1_pid 2>/dev/null
exit -1
fi
echo ""
echo "**********************************************************************"
echo "**** $DB1 is now the Slony-I origin for set 1"
echo "**********************************************************************"
echo ""
######################################################################
# Setup DB2 as a subscriber node and let it subscribe the replication
# set of the running pgbench
######################################################################
echo "**** creating database for Node 2"
if ! createdb $DB2 ; then
kill $pgbench_pid 2>/dev/null
kill $slon1_pid 2>/dev/null
exit -1
fi
echo "**** initializing $DB2 as Node 2 of Slony-I cluster T1"
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
try {
store node (id = 2, comment = 'Node 2', event node=1);
store path (server = 1, client = 2, conninfo = 'dbname=$DB1');
store path (server = 2, client = 1, conninfo = 'dbname=$DB2');
}
on error { exit -1; }
echo 'Database $DB2 added as Node 2';
_EOF_
if [ $? -ne 0 ] ; then
kill $pgbench_pid 2>/dev/null
kill $slon1_pid 2>/dev/null
exit -1
fi
echo "**** starting the Slony-I node daemon for $DB2"
$TERMPROG -sb -sl 3000 -title "Slon node 2" -e sh -c "slon -d$DEBUG_LEVEL T1 dbname=$DB2; echo -n 'Enter>'; read line" &
slon2_pid=$!
echo "slon[$slon2_pid] on dbname=$DB2"
#####
# Check that pgbench is still running
#####
if ! kill -0 $pgbench_pid 2>/dev/null ; then
echo "**** pgbench terminated ???"
kill $slon1_pid 2>/dev/null
exit -1
fi
######################################################################
# And now comes the moment where the big elephant starts to pee
# and the attendants in the first row climb on their chairs ...
######################################################################
echo "**** creating pgbench tables and subscribing Node 2 to set 1"
(
cat pgbench_schema.sql
) | psql -q $DB2
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
try {
subscribe set ( id = 1, provider = 1, receiver = 2, forward = yes );
subscribe set ( id = 2, provider = 1, receiver = 2, forward = yes );
}
on error {
exit 1;
}
echo '***** Subscribe set queued - issue SYNC';
sync (id = 1);
echo '***** Waiting for SYNC to be done on subscriber';
wait for event (origin = 1, confirmed = 2, wait on = 1);
echo '***** Subscription complete';
_EOF_
echo ""
echo "**********************************************************************"
echo "**** $DB2 should now be attempting to catch up."
echo "**********************************************************************"
echo ""
######################################################################
# Setup DB3 as a subscriber node and let it subscribe the replication
# set of the running pgbench
######################################################################
echo "**** creating database for Node 3"
if ! createdb $DB3 ; then
kill $pgbench_pid 2>/dev/null
kill $slon1_pid 2>/dev/null
kill $slon2_pid 2>/dev/null
exit -1
fi
echo "**** initializing $DB3 as Node 3 of Slony-I cluster T1"
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
store node (id = 3, comment = 'Node 3', event node=1);
store path (server = 1, client = 3, conninfo = 'dbname=$DB1');
store path (server = 2, client = 3, conninfo = 'dbname=$DB2');
store path (server = 3, client = 1, conninfo = 'dbname=$DB3');
store path (server = 3, client = 2, conninfo = 'dbname=$DB3');
echo 'Database $DB3 added as Node 3';
_EOF_
if [ $? -ne 0 ] ; then
kill $pgbench_pid 2>/dev/null
kill $slon1_pid 2>/dev/null
kill $slon2_pid 2>/dev/null
exit -1
fi
######################################################################
# Create the pgbench tables and subscribe to set 1
######################################################################
echo "**** creating pgbench tables and subscribing Node 3 to set 1"
(
cat pgbench_schema.sql
) | psql -q $DB3
######################################################################
# Create the offline database for node 4
######################################################################
echo "**** creating database for offline node 4"
if ! createdb $DB4 ; then
exit 1
fi
echo "**** loading pgbench schema into $DB4"
psql -q $DB4 <./pgbench_schema.sql
echo "**** taking offline_dump.sql from $DB3 for logshipping"
sh ../../tools/slony1_dump.sh $DB3 T1 >offline_dump.sql
echo "**** loading Slony-I offline replica schema into $DB4"
psql -q $DB4 <./offline_dump.sql
######################################################################
# Start the replication daemon for node 3
######################################################################
echo "**** starting the Slony-I node daemon for $DB3"
$TERMPROG -sb -sl 3000 -title "Slon node 3" -e sh -c "slon -d$DEBUG_LEVEL -a $LOGSHIPDIR -x 'slony_logshipper ./test_8_logshipper.conf' T1 dbname=$DB3; echo -n 'Enter>'; read line" &
slon3_pid=$!
echo "slon[$slon3_pid] on dbname=$DB3"
#####
# Check that pgbench is still running
#####
if ! kill -0 $pgbench_pid 2>/dev/null ; then
echo "**** pgbench terminated ???"
kill $slon1_pid 2>/dev/null
kill $slon2_pid 2>/dev/null
kill $slon3_pid 2>/dev/null
slony_logshipper -T ./test_8_logshipper.conf
exit -1
fi
######################################################################
# Subscribe node 3 to set 1
######################################################################
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
subscribe set ( id = 1, provider = 2, receiver = 3, forward = no );
subscribe set ( id = 2, provider = 2, receiver = 3, forward = no );
_EOF_
echo ""
echo "**********************************************************************"
echo "**** $DB3 should now be copying data and attempting to catch up."
echo "**********************************************************************"
echo ""
echo -n "**** waiting for pgbench to finish "
while kill -0 $pgbench_pid 2>/dev/null ; do
echo -n "."
sleep 10
done
echo "**** pgbench finished"
slonik <<_EOF_
cluster name = T1;
node 1 admin conninfo = 'dbname=$DB1';
node 2 admin conninfo = 'dbname=$DB2';
node 3 admin conninfo = 'dbname=$DB3';
sync (id = 1);
echo '**** waiting for node 3 to catch up';
wait for event (origin = 1, confirmed = all, wait on = 1, timeout = 0);
echo '**** done.';
_EOF_
echo "**** you might terminate the replication engines."
./compare_pgbench_dumps $DB1 $DB2
./compare_pgbench_dumps $DB1 $DB3
slony_logshipper -t ./test_8_logshipper.conf
./compare_pgbench_dumps $DB1 $DB4
|