forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrunit
More file actions
executable file
·422 lines (361 loc) · 12.4 KB
/
runit
File metadata and controls
executable file
·422 lines (361 loc) · 12.4 KB
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
417
418
419
420
421
422
#!/usr/bin/env bash
bash -n "$0" | exit 1
. ${TESTSROOTDIR}/tools/runit_common.sh
export stopfile=./stopfile.txt
export COPYCOMDB2_EXE=${BUILDDIR}/db/copycomdb2
export DESTDB=${TESTCASE}dest${TESTID}
export REPLOG=$TESTDIR/logs/$DESTDB.db
export COPY_DBDIR=${DBDIR}/$DESTDB
export DEST_DBDIR=${COPY_DBDIR}/$DBNAME
export PHYSREP_PID=-1
function failexit
{
touch $stopfile
echo "Failed: $1"
exit -1
}
function override_physrep_sp()
{
local mnode=`getmaster`
# Override the register_replicant stored procedure
${CDB2SQL_EXE} $CDB2_OPTIONS $DBNAME --host $mnode "create procedure 'sys.physrep.register_replicant' version '1' { `cat ./register_replicant.lua` }"
}
function setup_physical_replicant
{
mkdir -p $DEST_DBDIR
cl="-y @$(echo $CLUSTER | tr ' ' ',')"
if [[ "$CLUSTER" =~ .*$myhost.* ]]; then
rmt=""
else
clarray=($CLUSTER)
rmt="${clarray[0]}:"
fi
${COPYCOMDB2_EXE} -x ${COMDB2_EXE} -H $DESTDB $cl $rmt${DBDIR}/${DBNAME}.lrl $DEST_DBDIR $DEST_DBDIR
if [ $? -ne 0 ]; then
failexit "copycomdb2 failed"
fi
df $DBDIR | awk '{print $1}' | grep "tmpfs\|nfs" && echo "setattr directio 0" >> $DEST_DBDIR/${DESTDB}.lrl
echo "verbose_fills on" >> $DEST_DBDIR/${DESTDB}.lrl
if [ -n "$PMUXPORT" ] ; then
echo "portmux_port $PMUXPORT" >> $DEST_DBDIR/${DESTDB}.lrl
echo "portmux_bind_path $pmux_socket" >> $DEST_DBDIR/${DESTDB}.lrl
fi
sed -i "s/qdump const1 const1.txt 0/qdump const1 rconst1.txt 0/g" $DEST_DBDIR/${DESTDB}.lrl
sed -i "s/qdump const2 const2.txt 0/qdump const2 rconst2.txt 0/g" $DEST_DBDIR/${DESTDB}.lrl
sed -i "s/qdump const3 const3.txt 1/qdump const3 rconst3.txt 1/g" $DEST_DBDIR/${DESTDB}.lrl
(cd $DBDIR; timeout --kill-after=5s $TEST_TIMEOUT $COMDB2_EXE $DESTDB --lrl $DEST_DBDIR/${DESTDB}.lrl > $REPLOG 2>&1 &) &
PHYSREP_PID=$!
out=
retries=0
while [[ "$out" != "1" ]]; do
out=$(${CDB2SQL_EXE} --tabs $DESTDB --host localhost 'select 1' 2>/dev/null)
sleep 1
let retries=retries+1
if [ $retries -eq 10 ]; then
failexit "Timeout waiting for local replicant to come up"
fi
done
}
function create_tables
{
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create table t1(a int, b blob, c longlong autoincrement)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create index t1a on t1(a)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create index t1c on t1(c)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create table xxx(a int, b blob, c longlong autoincrement)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create index xxxa on xxx(a)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "create index xxxc on xxx(c)"
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default - <<EOF
create procedure const1 version 'v1' {$(cat consumer.lua)}\$\$
create lua consumer const1 on (table xxx for insert and update and delete)
create procedure const2 version 'v1' {$(cat consumer.lua)}\$\$
create lua consumer const2 on (table alltypes for insert and update and delete)
create procedure const3 version 'v1' {$(cat consumer.lua)}\$\$
create lua consumer const3 on (table alltypes for insert and update and delete)
create procedure noprint version 'v1' {$(cat consumer.lua)}\$\$
create lua consumer noprint on (table alltypes for insert and update and delete)
EOF
}
# Create a blob
function makebl
{
typeset sz=$1
typeset val=$2
for i in $(seq 1 $sz); do
echo -n $(echo "obase=16; $(($val % 16))" | bc);
done
echo
return 0
}
function insert_alltypes
{
s=$1
e=$2
typeset i
for i in $(seq $s $e); do
bl=$(makebl 32 $i)
echo "insert into alltypes (alltypes_short, alltypes_ushort, alltypes_int, alltypes_uint, alltypes_longlong, alltypes_float, alltypes_double, alltypes_byte, alltypes_cstring, alltypes_datetime, alltypes_datetimeus, alltypes_intervalym, alltypes_intervalds, alltypes_intervaldsus, alltypes_decimal32, alltypes_decimal64, alltypes_decimal128) values ($i, $i, $i, $i, $i, '$i.$i', $i.$i, x'$bl', 'abcdefghij$i', cast(\"2015-02-23T080000.$i\" as datetime), cast(\"2015-02-23T080000.$i\" as datetimeus), cast($i as years), cast($i as days), cast($i as days), '$(($i % 1000)).$(($i % 1000))e$(($i % 92))', '$(($i % 1000)).$(($i % 1000))e$(($i % 381))', '$(($i % 1000)).$(($i % 1000))e$(($i % 6141))')"
done | $CDB2SQL_EXE -s ${CDB2_OPTIONS} $DBNAME default - >/dev/null
}
function insert_records
{
j=0
while [[ $j -lt 10 ]]; do
bl=$(makebl 16 $j)
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "insert into t1(a, b) select *, x'$bl' from generate_series(1, 10)"
bl=$(makebl 16 $j)
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "insert into xxx(a, b) select *, x'$bl' from generate_series(1, 10)"
insert_alltypes 1 10
let j=j+1
done
}
function insert_big
{
j=0
while [[ $j -lt 5 ]]; do
bl=$(makebl 16384 $j)
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "insert into xxx(a, b) select *, x'$bl' from generate_series(1, 1)"
let j=j+1
done
}
function update_records
{
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from t1")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "update t1 set a=a+1 where c = $j"
let j=j+1
done
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from xxx")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "update xxx set a=a+1 where c = $j"
let j=j+1
done
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from alltypes")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "update alltypes set alltypes_short = alltypes_short, alltypes_ushort = alltypes_ushort, alltypes_int = alltypes_int, alltypes_uint = alltypes_uint, alltypes_longlong = alltypes_longlong, alltypes_float = alltypes_float, alltypes_double = alltypes_double, alltypes_byte = alltypes_byte, alltypes_cstring = alltypes_cstring, alltypes_datetime = alltypes_datetime, alltypes_datetimeus = alltypes_datetimeus, alltypes_intervalym = alltypes_intervalym, alltypes_intervalds = alltypes_intervalds, alltypes_intervaldsus = alltypes_intervaldsus, alltypes_decimal32 = alltypes_decimal32, alltypes_decimal64 = alltypes_decimal64, alltypes_decimal128 = alltypes_decimal128 where j = $j"
let j=j+1
done
}
function delete_records
{
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from t1")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "delete from t1 where c = $j"
let j=j+1
done
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from xxx")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "delete from xxx where c = $j"
let j=j+1
done
j=1
m=$($CDB2SQL_EXE --tabs $CDB2_OPTIONS $DBNAME default "select max(c) from alltypes")
while [[ $j -le $m ]]; do
$CDB2SQL_EXE $CDB2_OPTIONS $DBNAME default "delete from alltypes where c = $j"
let j=j+1
done
}
function find_log_trigger_trace_node
{
typeset node=$1
typeset trace=$2
log=$TESTDIR/logs/logtrigger${TESTID}.${node}.db
egrep log_trigger_callback $log | egrep $trace
}
function find_log_trigger_trace_physrep
{
log=$TESTDIR/logs/$DESTDB.db
}
function find_queue_trigger_trace_node
{
typeset node=$1
typeset trace=$2
log=$TESTDIR/logs/logtrigger${TESTID}.${node}.db
egrep "Processing record for" $log | egrep $trace
}
function find_trigger_trace_physrep
{
x=$(egrep log_trigger_callback $REPLOG | egrep xxx)
if [[ -z $x ]]; then
failexit "Couldn't find trigger trace in physrep-log"
fi
echo "$x"
}
function find_trigger_trace
{
found=0
for node in $CLUSTER ; do
x=$(find_log_trigger_trace_node $node xxx)
if [[ -n $x ]]; then
found=1
fi
echo "$x"
done
if [[ "$found" == 0 ]]; then
failexit "Couldn't find trigger trace in log"
fi
}
function check_trigger_trace
{
x=$(find_trigger_trace)
if [[ -z $x ]]; then
failexit "Trigger did not fire"
fi
x=$(find_trigger_trace_physrep)
if [[ -z $x ]]; then
failexit "Trigger did not fire in physrep"
fi
echo "Found trigger trace"
echo "$x"
}
function check_no_noprint
{
found=0
for node in $CLUSTER ; do
log=$TESTDIR/logs/logtrigger${TESTID}.${node}.db
egrep "log_trigger" $log | egrep "noprint"
if [[ $? -eq 0 ]]; then
found=1
fi
done
if [[ "$found" == 1 ]]; then
failexit "Found noprint extraneous trace in log"
fi
}
function check_dropped_trace
{
found=0
for node in $CLUSTER ; do
log=$TESTDIR/logs/logtrigger${TESTID}.${node}.db
egrep "__qconst3 dropped" $log
if [[ $? -eq 0 ]]; then
found=1
fi
done
if [[ "$found" == 0 ]]; then
failexit "Couldn't find dropped trace in log"
fi
egrep "__qconst3 dropped" $REPLOG
if [[ $? -ne 0 ]]; then
failexit "Couldn't find dropped trace in physrep"
fi
}
function check_empty_dump_output
{
typeset fl=$1
typeset empty=1
log=$TESTDIR/logtrigger${TESTID}/$fl.txt
for node in $CLUSTER ; do
scp $node:$log $log.${node}
if [[ -s $log.${node} ]]; then
empty=0
fi
done
prlog=$TESTDIR/r${fl}.txt
if [[ -s $prlog ]]; then
echo "Non-empty physrep $prlog was found"
empty=0
fi
if [[ "$empty" == 0 ]]; then
failexit "non-empty $log was found"
fi
}
function check_queue_dump_output
{
typeset fl=$1
typeset found=0
typeset target_size=0
typeset output_size=0
log=$TESTDIR/logtrigger${TESTID}/$fl.txt
gunzip $fl.expected.gz
for node in $CLUSTER ; do
scp $node:$log $log.${node}
if [[ -s $log.${node} ]]; then
found=1
egrep -vi "genid|epoch|Commit-lsn" $log.${node} > $log.${node}.clean
target_size=$(wc -c < $fl.expected)
output_size=$(wc -c < $log.${node}.clean)
while [[ $output_size -lt $target_size ]]; do
echo "Output size $output_size is less than expected size $target_size. Waiting for physrep to catch up..."
sleep 1
egrep -vi "genid|epoch|Commit-lsn" $log.${node} > $log.${node}.clean
output_size=$(wc -c < $log.${node}.clean)
done
diff $log.${node}.clean $fl.expected
r=$?
if [[ $r -ne 0 ]]; then
echo "Failed: $log.${node}.clean does not match $(pwd)/$fl.expected"
exit 1
fi
fi
done
if [[ "$found" == 0 ]]; then
failexit "non-empty $log was not found"
fi
# physrep name is prepended by r
prlog=$TESTDIR/$DBNAME/r${fl}.txt
egrep -vi "genid|epoch|Commit-lsn" $prlog > ${prlog}.clean
target_size=$(wc -c < $fl.expected)
output_size=$(wc -c < ${prlog}.clean)
while [[ "$output_size" -lt "$target_size" ]]; do
echo "Physrep output size $output_size is less than expected size $target_size. Waiting for physrep to catch up..."
sleep 1
egrep -vi "genid|epoch|Commit-lsn" $prlog > ${prlog}.clean
output_size=$(wc -c < ${prlog}.clean)
done
diff ${prlog}.clean $fl.expected
r=$?
if [[ $r -ne 0 ]]; then
echo "Failed: physrep ${prlog}.clean does not match $(pwd)/$fl.expected"
exit 1
fi
}
function setup
{
override_physrep_sp
setup_physical_replicant
}
function run_test
{
rm $stopfile 2>/dev/null
create_tables
insert_records
insert_big
update_records
delete_records
# Sleep a bit: make sure physrep is caught up
echo "Sleeping for 10"
sleep 10
check_trigger_trace
check_queue_dump_output const1
check_queue_dump_output const2
check_empty_dump_output const3
check_dropped_trace
check_no_noprint
}
if [[ -z $CLUSTER ]]; then
failexit "This test requires a cluster"
fi
trap - INT EXIT
setup
run_test
# Tear down physrep
r=0
while [[ "$r" == 0 ]]; do
echo "Exiting physrep"
${CDB2SQL_EXE} $DESTDB --host localhost "exec procedure sys.cmd.send('exit')"
r=$?
kill -9 $PHYSREP_PID
sleep 1
done
if [[ -f "$stopfile" ]]; then
echo "Testcase failed"
exit 1
else
echo "Success"
exit 0
fi