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
|
#!/bin/sh
# This script controls the Percona Toolkit test environment. The basic
# environment is a master on port 12345 in /tmp/12345 and a slave on port
# 12346 in /tmp/12346. This script attempts to ensure that all environment
# vars like PERCONA_TOOLKIT_BRANCH and PERCONA_TOOLKIT_SANDBOX are correct.
# Exist 0 on success/no errors, or 1 on any warnings or errors.
err() {
echo
for msg; do
echo "$msg"
done
}
usage() {
err "Usage: test-env start|stop|restart|status|checkconfig|reset|kill" \
"" \
" start Start test servers in /tmp/PORT" \
" stop Stop test servers and remvoe all /tmp/PORT" \
" kill Kill test servers (use if stop fails)" \
" restart Stop and start test servers" \
" status Print status of test servers" \
" checkconfig Check test env and test servers" \
" reset Reset test server binary logs" \
" version Print MySQL version of running test servers" \
""
}
mysql_basedir_ok() {
local basedir=$1
if [ ! -d "$basedir" ] || [ ! -d "$basedir/bin" ]; then
return 0
fi
if [ ! -x "$basedir/bin/mysqld_safe" ]; then
return 0
fi
return 1 # basedir is ok
}
set_mysql_basedir() {
# Auto-detect basedir if its not explicitly set.
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
BASEDIR_AUTO_DETECTED=1
mysqld=`which mysqld 2>/dev/null`
if [ -n "$mysqld" ]; then
PERCONA_TOOLKIT_SANDBOX=`$mysqld --verbose --help 2>/dev/null | grep 'basedir ' | awk '{print $2}'`
if [ -z "$PERCONA_TOOLKIT_SANDBOX" ]; then
return 0
fi
else
return 0
fi
else
if [ -x "$PERCONA_TOOLKIT_SANDBOX/bin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/bin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/sbin/mysqld"
elif [ -x "$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld" ]; then
mysqld="$PERCONA_TOOLKIT_SANDBOX/libexec/mysqld"
else
err "Cannot find executable mysqld in $PERCONA_TOOLKIT_SANDBOX/bin, $PERCONA_TOOLKIT_SANDBOX/sbin or $PERCONA_TOOLKIT_SANDBOX/libexec."
return 0
fi
fi
mysql_basedir_ok $PERCONA_TOOLKIT_SANDBOX
local basedir_ok=$?
if [ $basedir_ok -eq 1 ]; then
export PERCONA_TOOLKIT_SANDBOX=$PERCONA_TOOLKIT_SANDBOX
fi
return $basedir_ok
}
checkconfig() {
local print_conf=$1
local stat=""
conf_err=0
if [ -z "$PERCONA_TOOLKIT_BRANCH" ] || [ ! -d "$PERCONA_TOOLKIT_BRANCH" ]; then
conf_err=1
stat="INVALID"
else
stat="ok"
fi
if [ $print_conf ]; then
echo "PERCONA_TOOLKIT_BRANCH=$PERCONA_TOOLKIT_BRANCH - $stat"
fi
set_mysql_basedir
if [ $? -ne 1 ]; then
conf_err=1
stat="INVALID"
else
stat="ok"
fi
if [ $print_conf ]; then
echo -n "PERCONA_TOOLKIT_SANDBOX=$PERCONA_TOOLKIT_SANDBOX - $stat"
if [ -n "$BASEDIR_AUTO_DETECTED" ]; then
echo " (auto-detected)"
else
echo
fi
fi
return $conf_err
}
sandbox_status() {
local type=$1
local port=$2
local master_port=$3
local status=0 # sandbox is ok, no problems
echo "MySQL $type test server on port $port:"
echo -n " PID file exists - "
if [ -f "/tmp/$port/data/mysql_sandbox$port.pid" ]; then
echo "yes"
echo -n " PID file has a PID - "
local pid=`cat /tmp/$port/data/mysql_sandbox$port.pid 2>/dev/null`
if [ -n "$pid" ]; then
echo "yes"
echo -n " process $pid is alive - "
kill -0 $pid >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
else
echo "NO"
status=1
fi
else
echo "NO"
status=1
fi
else
echo "NO"
status=1
fi
echo -n " MySQL is alive - "
$PERCONA_TOOLKIT_SANDBOX/bin/mysqladmin --defaults-file="/tmp/$port/my.sandbox.cnf" ping >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
if [ "$version" != "4.0" ] && [ "$version" != "4.1" ]; then
echo -n " sakila db is loaded - "
/tmp/$port/use -e 'show databases like "sakila"' 2>/dev/null | grep sakila >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "yes"
else
echo "NO"
status=1
fi
fi
if [ "$type" = "slave" ]; then
echo -n " slave is running - "
# Slave status should show:
# Slave_IO_Running: Yes
# Slave_SQL_Running: Yes
local slave_running=`/tmp/$port/use -e 'show slave status\G' 2>/dev/null | grep Running | grep -c Yes`
if [ $slave_running -eq 2 ]; then
echo "yes"
else
echo "NO"
status=1
fi
if [ -n "$master_port" ]; then
echo -n " slave to master $master_port - "
local mp=`/tmp/$port/use -e 'show slave status\G' 2>/dev/null | grep Master_Port | awk '{print $2}'`
if [ "$mp" = "$master_port" ]; then
echo "yes"
else
echo "NO"
status=1
fi
fi
fi
else
echo "NO"
status=1
fi
return $status
}
sandbox_is_running() {
local p=$1
ps axw | grep mysqld | grep -v grep | grep /tmp/$p >/dev/null
}
kill_sandbox() {
local p=$1
local rmdir=1
# See if the sandbox server is running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
# Try to kill it with mysqladmin shutdown. We try different
# user/pass because sometimes a test can bork acct privs.
mysqladmin -h127.1 -P$p -umsandbox -pmsandbox shutdown >/dev/null 2>&1
mysqladmin -h127.1 -P$p -uroot -pmsandbox shutdown >/dev/null 2>&1
mysqladmin -h127.1 -P$p -uroot shutdown >/dev/null 2>&1
# See if the sandbox server is still running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
# Kill both mysqld_safe and mysqld.
pid1=`ps axw | grep mysqld_safe | grep /tmp/$p | awk '{print $1}'`
pid2=`ps axw | grep mysqld | grep -v mysqld_safe | grep /tmp/$p | awk '{print $1}'`
kill -9 $pid1 # Die, damn you, die!
kill -9 $pid2
sleep 1
# Third and finaly check if the sandbox server is running.
sandbox_is_running $p
if [ $? -eq 0 ]; then
err "Failed to kill MySQL test server on port $p (PID $pid1, $pid2)"
rmdir=0
else
echo "Killed MySQL test server on port $p (PID $pid1, $pid2)"
fi
else
echo "Killed MySQL test server on port $p"
fi
fi
if [ $rmdir -eq 1 ] && [ -d "/tmp/$p" ]; then
rm -rf /tmp/$p
echo "Removed /tmp/$p"
fi
return
}
MYSQL_VERSION=""
set_mysql_version() {
if [ -d /tmp/12345 ] && [ -f /tmp/12345/use ]; then
MYSQL_VERSION=$(/tmp/12345/use -N -e "select version()" | head -n 2 | tail -n 1 | cut -d'.' -f1,2)
fi
}
# ###########################################################################
# Sanity check the cmd line options.
# ###########################################################################
if [ $# -lt 1 ]; then
usage
exit 1
fi
opt=$1
# ###########################################################################
# Process the option.
# ###########################################################################
exit_status=0
if [ $opt = 'checkconfig' ]; then
checkconfig 1
echo -n "Percona Toolkit test environment config is "
if [ $conf_err -eq 0 ]; then
echo "ok!"
exit 0
else
echo "invalid."
exit 1
fi
else
checkconfig
if [ $conf_err -eq 1 ]; then
err "The Percona Toolkit test environment config is invalid." \
"Run '$0 checkconfig' to see the current configuration."
exit 1
fi
fi
# Determine the MySQL version.
# bin/mysqld -V should print something like:
# /usr/bin/mysqld Ver 5.1.34 for linux-gnu on x86_64 (MySQL Community Server)
version=`$mysqld -V | awk '{print $3}' | cut -d. -f 1,2`;
if [ ! -d "$PERCONA_TOOLKIT_BRANCH/sandbox/servers/$version" ]; then
err "Percona Toolkit sandbox server version $version does not exists."
exit 1
fi
case $opt in
start)
cd $PERCONA_TOOLKIT_BRANCH/sandbox
./start-sandbox master 12345
exit_status=$?
if [ $exit_status -eq 0 ]; then
./start-sandbox slave 12346 12345
./start-sandbox slave 12347 12346
exit_status=$?
if [ "$version" != "4.0" ] && [ "$version" != "4.1" ]; then
if [ $? -eq 0 ]; then
echo -n "Loading sakila database... "
./load-sakila-db 12345
exit_status=$?
if [ $? -ne 0 ]; then
echo "FAILED"
else
echo "OK"
fi
fi
fi
fi
if [ $exit_status -eq 0 ]; then
set_mysql_version
echo "Percona Toolkit test environment started with MySQL v$MYSQL_VERSION."
else
err "There was an error starting the Percona Toolkit test environment."
fi
;;
stop)
cd $PERCONA_TOOLKIT_BRANCH/sandbox
./stop-sandbox 12349 12348 12347 12346 12345
exit_status=$((exit_status | $?))
./stop-sandbox 2903 2902 2901 2900
exit_status=$((exit_status | $?))
if [ $exit_status -eq 0 ]; then
echo "Percona Toolkit test environment stopped."
else
err "Error stopping the Percona Toolkit test environment."
fi
;;
kill)
# This is a blunt approach for killing the entire mk test env
# when a polite stop fails. It uses kill -9 as a last resort.
for port in 12345 12346 12347 12348 12349 2900 2901 2902 2903; do
kill_sandbox $port
done
;;
restart)
$0 stop
$0 start
;;
status)
sandbox_status 'master' '12345'
master_status=$?
sandbox_status 'slave' '12346' '12345'
slave_status=$?
echo -n "Percona Test test environment is "
if [ $master_status -eq 0 ] && [ $slave_status -eq 0 ]; then
echo "ok!"
else
echo "invalid."
exit_status=1
fi
;;
reset)
# Several tests reset the bin logs so that queries from prior tests
# don't replicate to new sandbox servers. This makes creating new
# sandbox servers a lot faster. There's no check if this works or
# not, so... yeah.
/tmp/12347/use -e "STOP SLAVE"
/tmp/12346/use -e "STOP SLAVE"
/tmp/12346/use -e "RESET MASTER"
/tmp/12345/use -e "RESET MASTER"
/tmp/12347/use -e "change master to master_host='127.0.0.1', master_user='msandbox', master_password='msandbox', master_port=12346, master_log_file='mysql-bin.000001', master_log_pos=0"
/tmp/12346/use -e "change master to master_host='127.0.0.1', master_user='msandbox', master_password='msandbox', master_port=12345, master_log_file='mysql-bin.000001', master_log_pos=0"
/tmp/12347/use -e "START SLAVE"
/tmp/12346/use -e "START SLAVE"
exit_status=0
;;
version)
set_mysql_version
echo $MYSQL_VERSION
;;
*)
usage
exit_status=1
;;
esac
exit $exit_status
|