-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.bash
executable file
·176 lines (164 loc) · 5.19 KB
/
script.bash
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
#!/usr/bin/env bash
my() {
MYSQL_PWD=password mysql --host mysql --port 3306 --user user --database exam
}
pg() {
PGPASSWORD=password psql --host postgres --port 5432 --user user --dbname exam
}
escape() {
sed -e 's/[]\/$*.^[]/\\&/g'
}
replace_sleep() {
cmd="$1"
sleep_time="$2"
sleep_time_before_commit="$3"
sed "s/%%SLEEP_DELAY%%/$(sleep_fn "$cmd" "$sleep_time" | escape)/g" \
| sed "s/%%SLEEP_BEFORE_COMMIT%%/$(sleep_fn "$cmd" "$sleep_time_before_commit" | escape)/g"
}
replace_user() {
cmd="$1"
user="$2"
sed "s/%%USER%%/$(printf %s "$user" | escape)/g"
}
sleep_fn() {
local cmd="$1"
local sec="$2"
local my_fn="sleep($sec)";
local pg_fn="(pg_sleep($sec)::text != '')::int";
local var="${cmd}_fn"
echo "${!var}"
}
begin_with_tx() {
local cmd="$1"
local isolation="$2"
local my_begin="SET TRANSACTION ISOLATION LEVEL $isolation; BEGIN"
local pg_begin="BEGIN; SET TRANSACTION ISOLATION LEVEL $isolation"
local var="${cmd}_begin"
echo "${!var}"
}
perform() {
local cmd="$1"
local isolation="$2"
local commit_delay="$3"
local sql="$4"
local replaced="$(replace_sleep "$cmd" "1|2" "$commit_delay|0" <<< "$sql")"
echo -e "CMD:$cmd ISOLATION:$isolation\n\n[SQL]\n$replaced\n"
"$cmd" <<< 'SELECT * FROM products;'
perform_one A 1 "$commit_delay" "$cmd" "$isolation" "$sql" &
perform_one B 2 0 "$cmd" "$isolation" "$sql" &
wait
"$cmd" <<< 'SELECT * FROM products;'
"$cmd" <<< 'UPDATE products SET remaining_amount=1,referencing_amount_A=NULL,referencing_amount_B=NULL;'
echo
}
perform_one() {
local user="$1"
local sleep_time="$2"
local commit_delay="$3"
local cmd="$4"
local isolation="$5"
local sql="$6"
exec > >(trap "" INT TERM; sed "s/^/$user: /")
exec 2> >(trap "" INT TERM; sed "s/^/$user: /" >&2)
local replaced
replaced="$(replace_sleep "$cmd" "$sleep_time" "$commit_delay" <<< "$sql")"
replaced="$(replace_user "$cmd" "$user" <<< "$replaced")"
"$cmd" <<< \
"$(begin_with_tx "$cmd" "$isolation");
$replaced
COMMIT;"
}
cmds=(
pg
my
)
isolations=(
'READ UNCOMMITTED'
'READ COMMITTED'
'REPEATABLE READ'
'SERIALIZABLE'
)
commit_delays=(
2
0
)
sqls=(
'-- NORMAL, DELAY BEFORE READ --
SELECT %%SLEEP_DELAY%%;
UPDATE products
SET referencing_amount_%%USER%%=remaining_amount, remaining_amount=remaining_amount-1
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- NORMAL, DELAY ON PRE-WRITE --
UPDATE products
SET referencing_amount_%%USER%%=(SELECT %%SLEEP_DELAY%%)+remaining_amount, remaining_amount=remaining_amount-1
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- NORMAL, DELAY ON POST-WRITE --
UPDATE products
SET referencing_amount_%%USER%%=remaining_amount, remaining_amount=remaining_amount-1+(SELECT %%SLEEP_DELAY%%)
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY WHERE, DELAY BEFORE READ --
SELECT %%SLEEP_DELAY%%;
UPDATE products
SET referencing_amount_%%USER%%=remaining_amount, remaining_amount=remaining_amount-1
WHERE EXISTS(
SELECT * FROM (
SELECT * FROM products WHERE id=1 AND remaining_amount>0
) tmp
);
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY WHERE, DELAY ON PRE-WRITE --
UPDATE products
SET referencing_amount_%%USER%%=(SELECT %%SLEEP_DELAY%%)+remaining_amount, remaining_amount=remaining_amount-1
WHERE EXISTS(
SELECT * FROM (
SELECT * FROM products WHERE id=1 AND remaining_amount>0
) tmp
);
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY WHERE, DELAY ON POST-WRITE --
UPDATE products
SET referencing_amount_%%USER%%=+remaining_amount, remaining_amount=remaining_amount-1+(SELECT %%SLEEP_DELAY%%)
WHERE EXISTS(
SELECT * FROM (
SELECT * FROM products WHERE id=1 AND remaining_amount>0
) tmp
);
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY SET, DELAY BEFORE READ --
SELECT %%SLEEP_DELAY%%;
UPDATE products
SET
referencing_amount_%%USER%%=(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp1),
remaining_amount=(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp2)-1
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY SET DELAY ON PRE-WRITE --
UPDATE products
SET
referencing_amount_%%USER%%=(SELECT %%SLEEP_DELAY%%)+(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp1),
remaining_amount=(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp2)-1
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
'-- SUBQUERY SET DELAY ON POST-WRITE --
UPDATE products
SET
referencing_amount_%%USER%%=(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp1),
remaining_amount=(SELECT remaining_amount FROM (SELECT * FROM products WHERE id=1) tmp2)-1+(SELECT %%SLEEP_DELAY%%)
WHERE id=1 AND remaining_amount>0;
SELECT %%SLEEP_BEFORE_COMMIT%%;'
)
for cmd in "${cmds[@]}"; do
for sql in "${sqls[@]}"; do
for isolation in "${isolations[@]}"; do
if [[ "$cmd" == pg ]] && [[ "$isolation" == 'READ UNCOMMITTED' ]]; then
continue
fi
for commit_delay in "${commit_delays[@]}"; do
perform "$cmd" "$isolation" "$commit_delay" "$sql"
done
done
done
done