Whamcloud - gitweb
LU-15028 tests: improve ha.sh to be more verbose
[fs/lustre-release.git] / lustre / tests / ha.sh
1 #!/bin/bash
2 # -*- mode: Bash; tab-width: 4; indent-tabs-mode: t; -*-
3 # vim:shiftwidth=4:softtabstop=4:tabstop=4:
4 #
5 # NAME
6 #
7 #   ha.sh - test Lustre HA (aka failover) configurations
8 #
9 # SYNOPSIS
10 #
11 #   ha.sh [OPTIONS]
12 #
13 # DESCRIPTION
14 #
15 #   ha.sh tests Lustre HA (aka failover) configurations with a CRM.
16 #
17 # OPTIONS
18 #
19 #   -h
20 #       Help.
21 #
22 #   -c HOST[,...]
23 #       Specify client nodes.
24 #
25 #   -s HOST[,...]
26 #       Specify server nodes.
27 #
28 #   -v HOST[,...]
29 #       Specify victim nodes to be rebooted.
30 #
31 #   -d DIRECTORY
32 #       Choose a parent of the test directory.  "/mnt/lustre" if not specified.
33 #
34 #   -u SECONDS
35 #       Define a duration for the test. 86400 seconds if not specified.
36 #
37 #   -p SECONDS
38 #       Define a max failover period. 10 minutes if not set.
39 #
40 #   -w
41 #       Only run the workloads; no failure will be introduced.
42 #       -v, -s are ignored in this case.
43 #   -r
44 #       Workloads dry run for several seconds; no failures will be introduced.
45 #       This option is useful to verify the loads.
46 #       -u is ignored in this case
47 #   -m
48 #       Reboot victim nodes simultaneously.
49 #
50 #
51 # ASSUMPTIONS
52 #
53 #   A Lustre file system is up and mounted on all client nodes.  This script
54 #   does not mount or unmount any Lustre targets or clients, let alone format
55 #   anything.
56 #
57 #   Each target has a failnode, so that workloads can continue after a power
58 #   failure.
59 #
60 #   CRM could be configured by 2 ways:
61 #   1.
62 #   Targets are automatically failed back when their primary node is back.  This
63 #   assumption avoids calling CRM-specific commands to trigger failbacks, making
64 #   this script more CRM-neural.
65 #   2.
66 #   Targets are not automatically failed back when their primary node is back.
67 #   CRM-specific command is executed to trigger failbacks.
68 #
69 #   A crash dump mechanism is configured to catch LBUGs, panics, etc.
70 #
71 # WORKLOADS
72 #
73 #   Each client runs set of MPI and non-MPI workloads. These
74 #   applications are run in short loops so that their exit status can be waited
75 #   for and checked within reasonable time by ha_wait_loads.
76 #   The set of MPI and non-MPI workloads are configurable by parameters:
77 #       ha_nonmpi_loads
78 #               default set: dd, tar, iozone
79 #       ha_mpi_loads
80 #               default set: ior, simul, mdtest
81 #
82 #   The number of clients run MPI loads is configured by parameter
83 #   ha_mpi_instances. Only one client runs MPI workloads by default.
84 #
85 #   MPI workloads can be run from several users. The list of users to use is
86 #   configured by parameter ha_mpi_users, default is "mpiuser".
87 #
88 # PROCESS STRUCTURE AND IPC
89 #
90 #   On the node where this script is run, the processes look like this:
91 #
92 #       ~ ha.sh (ha_killer)
93 #
94 #           ~ ha.sh (ha_repeat_mpi_load ior)
95 #               ~ mpirun IOR
96 #           ~ ha.sh (ha_repeat_mpi_load simul)
97 #               ~ mpirun simul
98 #           ~ ha.sh (ha_repeat_mpi_load mdtest)
99 #               ~ mpirun mdtest
100 #           ~ ... (one for each MPI load)
101 #
102 #           ~ ha.sh (ha_repeat_nonmpi_load client2 dbench)
103 #               ~ pdsh client2 dbench
104 #           ~ ha.sh (ha_repeat_nonmpi_load client2 iozone)
105 #               ~ pdsh client2 iozone
106 #           ~ ha.sh (ha_repeat_nonmpi_load client5 iozone)
107 #               ~ pdsh client5 iozone
108 #           ~ ... (one for each non-MPI load on each client)
109 #
110 #   Each tilde represents a process.  Indentations imply parent-children
111 #   relation.
112 #
113 #   IPC is done by files in the temporary directory.
114 #
115
116 #set -x
117
118 SIMUL=${SIMUL:-$(which simul 2> /dev/null || true)}
119 IOR=${IOR:-$(which IOR 2> /dev/null || true)}
120 MDTEST=${MDTEST:-$(which mdtest 2> /dev/null || true)}
121
122 ior_blockSize=${ior_blockSize:-6g}
123 mpi_threads_per_client=${mpi_threads_per_client:-2}
124
125 iozone_SIZE=${iozone_SIZE:-262144} # 256m
126
127 mpirun=${MPIRUN:-$(which mpirun)}
128 LFS=${LFS:-$(which lfs)}
129
130 ha_check_env()
131 {
132         for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
133                 local tag=${ha_mpi_load_tags[$load]}
134                 local bin=$(echo $tag | tr '[:lower:]' '[:upper:]')
135                 if [ x${!bin} = x ]; then
136                         ha_error ha_mpi_loads: ${ha_mpi_loads}, $bin is not set
137                         exit 1
138                 fi
139         done
140 }
141
142 ha_info()
143 {
144         echo "$0: $(date +%H:%M:%S' '%s):" "$@"
145 }
146
147 ha_touch()
148 {
149         local date=$(date +%H:%M:%S' '%s)
150
151         [[ $1 =~ stop ]] &&
152                 echo $date ${FUNCNAME[1]} $2 >> $ha_stop_file ||
153                 true
154         [[ $1 =~ fail ]] &&
155                 echo $date ${FUNCNAME[1]} $2 >> $ha_fail_file ||
156                 true
157         [[ $1 =~ lfsck ]] &&
158                 echo $date ${FUNCNAME[1]} $2 >> $ha_lfsck_stop ||
159                 true
160 }
161
162 ha_log()
163 {
164         local nodes=${1// /,}
165         shift
166         ha_on $nodes "lctl mark $*"
167 }
168
169 ha_error()
170 {
171     ha_info "$@" >&2
172 }
173
174 ha_trap_err()
175 {
176     local i
177
178     ha_error "Trap ERR triggered by:"
179     ha_error "    $BASH_COMMAND"
180     ha_error "Call trace:"
181     for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
182         ha_error "    ${FUNCNAME[$i]} [${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}]"
183     done
184 }
185
186 trap ha_trap_err ERR
187 set -eE
188
189 declare     ha_power_down_pids
190 declare     ha_tmp_dir=/tmp/$(basename $0)-$$
191 declare     ha_stop_file=$ha_tmp_dir/stop
192 declare     ha_fail_file=$ha_tmp_dir/fail
193 declare     ha_pm_states=$ha_tmp_dir/ha_pm_states
194 declare     ha_status_file_prefix=$ha_tmp_dir/status
195 declare -a  ha_status_files
196 declare     ha_machine_file=$ha_tmp_dir/machine_file
197 declare     ha_lfsck_log=$ha_tmp_dir/lfsck.log
198 declare     ha_lfsck_lock=$ha_tmp_dir/lfsck.lock
199 declare     ha_lfsck_stop=$ha_tmp_dir/lfsck.stop
200 declare     ha_lfsck_bg=${LFSCK_BG:-false}
201 declare     ha_lfsck_after=${LFSCK_AFTER:-false}
202 declare     ha_lfsck_node=${LFSCK_NODE:-""}
203 declare     ha_lfsck_device=${LFSCK_DEV:-""}
204 declare     ha_lfsck_types=${LFSCK_TYPES:-"namespace layout"}
205 declare     ha_lfsck_custom_params=${LFSCK_CUSTOM_PARAMS:-""}
206 declare     ha_lfsck_wait=${LFSCK_WAIT:-1200}
207 declare     ha_lfsck_fail_on_repaired=${LFSCK_FAIL_ON_REPAIRED:-false}
208 declare     ha_power_down_cmd=${POWER_DOWN:-"pm -0"}
209 declare     ha_power_up_cmd=${POWER_UP:-"pm -1"}
210 declare     ha_power_delay=${POWER_DELAY:-60}
211 declare     ha_node_up_delay=${NODE_UP_DELAY:-10}
212 declare     ha_wait_nodes_up=${WAIT_NODES_UP:-600}
213 declare     ha_pm_host=${PM_HOST:-$(hostname)}
214 declare     ha_failback_delay=${DELAY:-5}
215 declare     ha_failback_cmd=${FAILBACK:-""}
216 declare     ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
217 declare     ha_test_dir_stripe_count=${TDSTRIPECOUNT:-"1"}
218 declare     ha_test_dir_mdt_index=${TDMDTINDEX:-"0"}
219 declare     ha_test_dir_mdt_index_random=${TDMDTINDEXRAND:-false}
220 declare     ha_dir_stripe_count=${DSTRIPECOUNT:-"1"}
221 declare     ha_mdt_index=${MDTINDEX:-"0"}
222 declare     ha_mdt_index_random=${MDTINDEXRAND:-false}
223 declare -a  ha_clients
224 declare -a  ha_servers
225 declare -a  ha_victims
226 declare -a  ha_victims_pair
227 declare     ha_test_dir=/mnt/lustre/$(basename $0)-$$
228 declare -a  ha_testdirs=(${ha_test_dirs="$ha_test_dir"})
229
230 for ((i=0; i<${#ha_testdirs[@]}; i++)); do
231         echo I=$i ${ha_testdirs[i]}
232         ha_testdirs[i]="${ha_testdirs[i]}/$(basename $0)-$$"
233         echo i=$i ${ha_testdirs[i]}
234 done
235
236 declare     ha_cleanup=${CLEANUP:-true}
237 declare     ha_start_time=$(date +%s)
238 declare     ha_expected_duration=$((60 * 60 * 24))
239 declare     ha_max_failover_period=10
240 declare     ha_nr_loops=0
241 declare     ha_stop_signals="SIGINT SIGTERM SIGHUP"
242 declare     ha_load_timeout=${LOAD_TIMEOUT:-$((60 * 10))}
243 declare     ha_workloads_only=false
244 declare     ha_workloads_dry_run=false
245 declare     ha_simultaneous=false
246
247 declare     ha_mpi_instances=${ha_mpi_instances:-1}
248
249 declare     ha_mpi_loads=${ha_mpi_loads="ior simul mdtest"}
250 declare -a  ha_mpi_load_tags=($ha_mpi_loads)
251 declare -a  ha_mpiusers=(${ha_mpi_users="mpiuser"})
252 declare -a  ha_users
253 declare -A  ha_mpiopts
254
255 for ((i=0; i<${#ha_mpiusers[@]}; i++)); do
256         u=${ha_mpiusers[i]%%:*}
257         o=""
258         # user gets empty option if ha_mpi_users does not specify it explicitly
259         [[ ${ha_mpiusers[i]} =~ : ]] && o=${ha_mpiusers[i]##*:}
260         ha_users[i]=$u
261         ha_mpiopts[$u]+=" $o"
262 done
263 ha_users=(${!ha_mpiopts[@]})
264
265 declare     ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
266 declare     ha_simul_params=${SIMULP:-'" -n 10"'}
267 declare     ha_mdtest_params=${MDTESTP:-'" -i 1 -n 1000"'}
268 declare     ha_mpirun_options=${MPIRUN_OPTIONS:-""}
269 declare     ha_clients_stripe=${CLIENTSSTRIPE:-'"$STRIPEPARAMS"'}
270 declare     ha_nclientsset=${NCLIENTSSET:-1}
271 declare     ha_ninstmustfail=${NINSTMUSTFAIL:-0}
272
273 declare     ha_racer_params=${RACERP:-"MDSCOUNT=1"}
274
275 eval ha_params_ior=($ha_ior_params)
276 eval ha_params_simul=($ha_simul_params)
277 eval ha_params_mdtest=($ha_mdtest_params)
278 eval ha_stripe_clients=($ha_clients_stripe)
279
280 declare ha_nparams_ior=${#ha_params_ior[@]}
281 declare ha_nparams_simul=${#ha_params_simul[@]}
282 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
283 declare ha_nstripe_clients=${#ha_stripe_clients[@]}
284
285 declare -A  ha_mpi_load_cmds=(
286         [ior]="$IOR -o {}/f.ior {params}"
287         [simul]="$SIMUL {params} -d {}"
288         [mdtest]="$MDTEST {params} -d {}"
289 )
290
291 declare racer=${RACER:-"$(dirname $0)/racer/racer.sh"}
292
293 declare     ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
294 declare -a  ha_nonmpi_load_tags=($ha_nonmpi_loads)
295 declare -A  ha_nonmpi_load_cmds=(
296         [dd]="dd if=/dev/zero of={}/f.dd bs=1M count=256"
297         [tar]="tar cf - /etc | tar xf - -C {}"
298         [iozone]="iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
299         [racer]="$ha_racer_params $racer {}"
300 )
301
302 ha_usage()
303 {
304         ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]" \
305                 "-v HOST[,...] -f HOST[,...] [-d DIRECTORY] [-u SECONDS]"
306 }
307
308 ha_process_arguments()
309 {
310     local opt
311
312         while getopts hc:s:v:d:p:u:wrmf: opt; do
313         case $opt in
314         h)
315             ha_usage
316             exit 0
317             ;;
318         c)
319             ha_clients=(${OPTARG//,/ })
320             ;;
321         s)
322             ha_servers=(${OPTARG//,/ })
323             ;;
324         v)
325             ha_victims=(${OPTARG//,/ })
326             ;;
327         d)
328             ha_test_dir=$OPTARG/$(basename $0)-$$
329             ;;
330         u)
331             ha_expected_duration=$OPTARG
332             ;;
333         p)
334                 ha_max_failover_period=$OPTARG
335                 ;;
336         w)
337                 ha_workloads_only=true
338                 ;;
339         r)
340                 ha_workloads_dry_run=true
341                 ;;
342         m)
343                 ha_simultaneous=true
344                 ;;
345         f)
346                 ha_victims_pair=(${OPTARG//,/ })
347                 ;;
348         \?)
349             ha_usage
350             exit 1
351             ;;
352         esac
353     done
354
355         if [ -z "${ha_clients[*]}" ]; then
356                 ha_error "-c is mandatory"
357                 ha_usage
358                 exit 1
359         fi
360         if ! ($ha_workloads_dry_run ||
361                         $ha_workloads_only) &&
362                         ([ -z "${ha_servers[*]}" ] ||
363                         [ -z "${ha_victims[*]}" ]); then
364                 ha_error "-s, and -v are all mandatory"
365                 ha_usage
366                 exit 1
367         fi
368 }
369
370 ha_on()
371 {
372         local nodes=$1
373         local rc=0
374
375         shift
376
377         #
378         # -S is to be used here to track the
379         # remote command return values
380         #
381         pdsh -S -w $nodes "PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
382 /bin:/usr/sbin:/usr/bin; $@" ||
383                 rc=$?
384         return $rc
385 }
386
387 ha_trap_exit()
388 {
389         ha_touch stop
390         trap 0
391         if [ -e "$ha_fail_file" ]; then
392                 ha_info "Test directories ${ha_testdirs[@]} not removed"
393                 ha_info "Temporary directory $ha_tmp_dir not removed"
394         else
395                 $ha_cleanup &&
396                         ha_on ${ha_clients[0]} rm -rf ${ha_testdirs[@]} ||
397                         ha_info "Test directories ${ha_testdirs[@]} not removed"
398                 ha_info "Please find the results in the directory $ha_tmp_dir"
399         fi
400 }
401
402 ha_trap_stop_signals()
403 {
404         ha_info "${ha_stop_signals// /,} received"
405         ha_touch stop "${ha_stop_signals// /,} received"
406 }
407
408 ha_sleep()
409 {
410     local n=$1
411
412     ha_info "Sleeping for ${n}s"
413     #
414     # sleep(1) could interrupted.
415     #
416     sleep $n || true
417 }
418
419 ha_wait_unlock()
420 {
421         local lock=$1
422
423         while [ -e $lock ]; do
424                 sleep 1
425         done
426 }
427
428 ha_lock()
429 {
430     local lock=$1
431
432     until mkdir "$lock" >/dev/null 2>&1; do
433         ha_sleep 1 >/dev/null
434     done
435 }
436
437 ha_unlock()
438 {
439     local lock=$1
440
441     rm -r "$lock"
442 }
443
444 ha_dump_logs()
445 {
446         local nodes=${1// /,}
447         local file=/tmp/$(basename $0)-$$-$(date +%s).dk
448         local lock=$ha_tmp_dir/lock-dump-logs
449         local rc=0
450
451         ha_lock "$lock"
452         ha_info "Dumping lctl log to $file"
453
454         #
455         # some nodes could crash, so
456         # do not exit with error if not all logs are dumped
457         #
458         ha_on $nodes "lctl dk >>$file" || rc=$?
459
460         [ $rc -eq 0 ] ||
461                 ha_error "not all logs are dumped! Some nodes are unreachable."
462         ha_unlock "$lock"
463 }
464
465 ha_repeat_mpi_load()
466 {
467         local client=$1
468         local load=$2
469         local status=$3
470         local parameter=$4
471         local machines=$5
472         local stripeparams=$6
473         local mpiuser=$7
474         local mustpass=$8
475         local mpirunoptions=$9
476         local test_dir=${10}
477         local tag=${ha_mpi_load_tags[$load]}
478         local cmd=${ha_mpi_load_cmds[$tag]}
479         local dir=$test_dir/$client-$tag
480         local log=$ha_tmp_dir/$client-$tag
481         local rc=0
482         local nr_loops=0
483         local avg_loop_time=0
484         local start_time=$(date +%s)
485
486         cmd=${cmd//"{}"/$dir}
487         cmd=${cmd//"{params}"/$parameter}
488
489         [[ -n "$ha_postcmd" ]] && ha_postcmd=${ha_postcmd//"{}"/$dir}
490         [[ -n "$ha_precmd" ]] && ha_precmd=${ha_precmd//"{}"/$dir}
491         ha_info "Starting $tag"
492
493         machines="-machinefile $machines"
494         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
495                 ha_info "$client Starts: $mpiuser: $cmd" 2>&1 |  tee -a $log
496                 {
497                 local mdt_index
498                 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
499                         mdt_index=$(ha_rand $ha_mdt_index)
500                 else
501                         mdt_index=$ha_mdt_index
502                 fi
503                 [[ -n "$ha_precmd" ]] && ha_info "$ha_precmd" &&
504                         ha_on $client "$ha_precmd" >>"$log" 2>&1
505                 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
506                 ha_on $client $LFS getdirstripe "$dir" &&
507                 ha_on $client $LFS setstripe $stripeparams $dir &&
508                 ha_on $client $LFS getstripe $dir &&
509                 ha_on $client chmod a+xwr $dir &&
510                 ha_on $client "su $mpiuser sh -c \" $mpirun $mpirunoptions \
511                         -np $((${#ha_clients[@]} * mpi_threads_per_client / ha_nclientsset)) \
512                         $machines $cmd \" " || rc=$?
513                 [[ -n "$ha_postcmd" ]] && ha_info "$ha_postcmd" &&
514                         ha_on $client "$ha_postcmd" >>"$log" 2>&1
515                 (( ((rc == 0)) && (( mustpass != 0 )) )) ||
516                 (( ((rc != 0)) && (( mustpass == 0 )) )) &&
517                         ha_on $client rm -rf "$dir";
518                 } >>"$log" 2>&1 || rc=$?
519
520                 ha_info $client: rc=$rc mustpass=$mustpass
521
522                 # mustpass=0 means that failure is expected
523                 if (( rc !=0 )); then
524                         if (( mustpass != 0 )); then
525                                 ha_touch stop,fail $client,$tag
526                                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
527                         else
528                                 # Ok to fail
529                                 rc=0
530                         fi
531                 elif (( mustpass == 0 )); then
532                         ha_touch stop,fail $client,$tag
533                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
534                 fi
535                 echo rc=$rc mustpass=$mustpass >"$status"
536
537                 nr_loops=$((nr_loops + 1))
538         done
539
540         [ $nr_loops -ne 0 ] &&
541                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
542
543         ha_info "$tag stopped: rc=$rc mustpass=$mustpass \
544                 avg loop time $avg_loop_time"
545 }
546
547 ha_start_mpi_loads()
548 {
549         local client
550         local load
551         local tag
552         local status
553         local n
554         local nparam
555         local machines
556         local m
557         local -a mach
558         local mpiuser
559         local nmpi
560
561         # ha_mpi_instances defines the number of
562         # clients start mpi loads; should be <= ${#ha_clients[@]}
563         # do nothing if
564         #    ha_mpi_instances = 0
565         # or
566         #    ${#ha_mpi_load_tags[@]} =0
567         local inst=$ha_mpi_instances
568         (( inst == 0 )) || (( ${#ha_mpi_load_tags[@]} == 0 )) &&
569                 ha_info "no mpi load to start" &&
570                 return 0
571
572         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
573
574         # Define names for machinefiles for each client set
575         for (( n=0; n < $ha_nclientsset; n++ )); do
576                 mach[$n]=$ha_machine_file$n
577         done
578
579         for ((n = 0; n < ${#ha_clients[@]}; n++)); do
580                 m=$(( n % ha_nclientsset))
581                 machines=${mach[m]}
582                 ha_info machine_file=$machines
583                 echo ${ha_clients[n]} >> $machines
584         done
585         local dirname=$(dirname $ha_machine_file)
586         for client in ${ha_clients[@]}; do
587                 ha_on $client mkdir -p $dirname
588                 scp $ha_machine_file* $client:$dirname
589         done
590
591         local ndir
592         for ((n = 0; n < $inst; n++)); do
593                 client=${ha_clients[n]}
594                 nmpi=$((n % ${#ha_users[@]}))
595                 mpiuser=${ha_users[nmpi]}
596                 ndir=$((n % ${#ha_testdirs[@]}))
597                 test_dir=${ha_testdirs[ndir]}
598                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
599                         tag=${ha_mpi_load_tags[$load]}
600                         status=$ha_status_file_prefix-$tag-$client
601                         # ha_nparams_ior
602                         # ha_nparams_simul
603                         local num=ha_nparams_$tag
604                         nparam=$((n % num))
605                         local aref=ha_params_$tag[nparam]
606                         local parameter=${!aref}
607                         local nstripe=$((n % ha_nstripe_clients))
608                         aref=ha_stripe_clients[nstripe]
609                         local stripe=${!aref}
610                         local m=$(( n % ha_nclientsset))
611                         machines=${mach[m]}
612                         local mustpass=1
613                         [[ $ha_ninstmustfail == 0 ]] ||
614                                 mustpass=$(( n % ha_ninstmustfail ))
615                         ha_repeat_mpi_load $client $load $status "$parameter" \
616                                 $machines "$stripe" "$mpiuser" "$mustpass" \
617                                 "${ha_mpiopts[$mpiuser]} $ha_mpirun_options" "$test_dir" &
618                                 ha_status_files+=("$status")
619                 done
620         done
621 }
622
623 ha_repeat_nonmpi_load()
624 {
625         local client=$1
626         local load=$2
627         local status=$3
628         local tag=${ha_nonmpi_load_tags[$load]}
629         local cmd=${ha_nonmpi_load_cmds[$tag]}
630         local test_dir=$4
631         local dir=$test_dir/$client-$tag
632         local log=$ha_tmp_dir/$client-$tag
633         local rc=0
634         local nr_loops=0
635         local avg_loop_time=0
636         local start_time=$(date +%s)
637
638         cmd=${cmd//"{}"/$dir}
639
640         ha_info "Starting $tag on $client on $dir"
641
642         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
643                 ha_info "$client Starts: $cmd" 2>&1 |  tee -a $log
644                 ha_on $client "mkdir -p $dir &&                              \
645                         $cmd &&                                              \
646                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
647
648                 if ((rc != 0)); then
649                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
650                         ha_touch stop,fail $client,$tag
651                 fi
652                 echo $rc >"$status"
653
654                 nr_loops=$((nr_loops + 1))
655         done
656
657         [ $nr_loops -ne 0 ] &&
658                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
659
660         ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
661 }
662
663 ha_start_nonmpi_loads()
664 {
665         local client
666         local load
667         local tag
668         local status
669         local n
670         local test_dir
671         local ndir
672
673         for (( n = 0; n < ${#ha_clients[@]}; n++)); do
674                 client=${ha_clients[n]}
675                 ndir=$((n % ${#ha_testdirs[@]}))
676                 test_dir=${ha_testdirs[ndir]}
677                 for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
678                         tag=${ha_nonmpi_load_tags[$load]}
679                         status=$ha_status_file_prefix-$tag-$client
680                         ha_repeat_nonmpi_load $client $load $status $test_dir &
681                         ha_status_files+=("$status")
682                 done
683         done
684 }
685
686 declare ha_bgcmd=${ha_bgcmd:-""}
687 declare ha_bgcmd_log=$ha_tmp_dir/bgcmdlog
688
689 ha_cmd_bg () {
690         [[ -z "$ha_bgcmd" ]] && return 0
691         for ((i=0; i<${#ha_testdirs[@]}; i++)); do
692                 ha_bgcmd=${ha_bgcmd//"{}"/${ha_testdirs[i]}}
693         done
694
695         ha_info "BG cmd: $ha_bgcmd"
696         while [ true ]; do
697                 [ -f $ha_stop_file ] &&
698                         ha_info "$ha_stop_file found! $ha_bgcmd no started" &&
699                         break
700                 eval $ha_bgcmd 2>&1 | tee -a $ha_bgcmd_log
701                 sleep 1
702         done &
703         CMD_BG_PID=$!
704         ha_info CMD BG PID: $CMD_BG_PID
705         ps aux | grep $CMD_BG_PID
706 }
707
708 ha_lfsck_bg () {
709         rm -f $ha_lfsck_log
710         rm -f $ha_lfsck_stop
711
712         ha_info "LFSCK BG"
713         while [ true ]; do
714                 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
715                 [ -f $ha_stop_file ] &&
716                         ha_info "$ha_stop_file found! LFSCK not started" &&
717                         break
718                 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
719                 sleep 1
720         done &
721         LFSCK_BG_PID=$!
722         ha_info LFSCK BG PID: $LFSCK_BG_PID
723 }
724
725 ha_wait_lfsck_completed () {
726         local -a status
727         local -a types=($ha_lfsck_types)
728         local type
729         local s
730
731         local nodes="${ha_servers[@]}"
732         nodes=${nodes// /,}
733
734         # -A start LFSCK on all nodes
735         # -t default all
736         [ ${#types[@]} -eq 0 ] && types=(namespace layout)
737         ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
738         for type in ${types[@]}; do
739                 eval var_$type=0
740                 for (( i=0; i<=ha_lfsck_wait; i++)); do
741                         status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
742                                 awk '/status/ { print $3 }'))
743                         for (( s=0; s<${#status[@]}; s++ )); do
744                                 # "partial" is expected after HARD failover
745                                 [[ "${status[s]}" = "completed" ]] ||
746                                 [[ "${status[s]}" = "partial" ]] ||  break
747                         done
748                         [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
749                         sleep 1
750                 done
751                 ha_info "LFSCK $type status in $i sec:"
752                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
753
754         done
755
756         for type in ${types[@]}; do
757                 local var=var_$type
758                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
759                 [[ ${!var} -eq 1 ]] ||
760                         { ha_info "lfsck not completed in $ha_lfsck_wait sec";
761                         return 1; }
762         done
763         return 0
764 }
765
766 ha_start_lfsck()
767 {
768         local -a types=($ha_lfsck_types)
769         local rc=0
770
771         # -A: start LFSCK on all nodes via the specified MDT device
772         # (see "-M" option) by single LFSCK command
773         local params=" -A -r $ha_lfsck_custom_params"
774
775         # use specified device if set
776         [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
777
778         # -t: check type(s) to be performed (default all)
779         # check only specified types if set
780         if [ ${#types[@]} -ne 0 ]; then
781                 local type="${types[@]}"
782                 params="$params -t ${type// /,}"
783         fi
784
785         ha_info "LFSCK start $params"
786         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
787         if [ $rc -ne 0 ]; then
788                 if [ -e $ha_lfsck_lock ]; then
789                         rc=0
790                         ha_wait_unlock $ha_lfsck_lock
791                         ha_sleep 120
792                         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
793                 fi
794         fi
795
796         [ $rc -eq 0 ] ||
797                 { ha_touch stop,fail,lfsck; return 1; }
798
799         ha_wait_lfsck_completed ||
800                 { ha_touch stop,fail,lfsck; return 1; }
801
802         return 0
803 }
804
805 ha_lfsck_repaired()
806 {
807         local n=0
808
809         n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
810                 awk '{sum += $1} END { print sum }')
811         [ $n -eq 0] ||
812                 { ha_info "Total repaired: $n";
813                 ha_touch fail; return 1; }
814         return 0
815 }
816
817 ha_start_loads()
818 {
819         ha_cmd_bg
820         $ha_lfsck_bg && ha_lfsck_bg
821         trap ha_trap_stop_signals $ha_stop_signals
822         ha_start_nonmpi_loads
823         ha_start_mpi_loads
824 }
825
826 ha_stop_loads()
827 {
828         ha_touch stop
829         [[ -n $CMD_BG_PID ]] && wait $CMD_BG_PID || true
830         # true because of lfsck_bg could be stopped already
831         $ha_lfsck_bg && wait $LFSCK_BG_PID || true
832         trap - $ha_stop_signals
833         ha_info "Waiting for workloads to stop"
834         wait
835 }
836
837 ha_wait_loads()
838 {
839     local file
840     local end=$(($(date +%s) + ha_load_timeout))
841
842     ha_info "Waiting $ha_load_timeout sec for workload status..."
843     rm -f "${ha_status_files[@]}"
844
845         #
846         # return immediately if ha_stop_file exists,
847         # all status_files not needed to be checked
848         #
849         for file in "${ha_status_files[@]}"; do
850                 if [ -e "$ha_stop_file" ]; then
851                         ha_info "$ha_stop_file found! Stop."
852                         break
853                 fi
854                 #
855                 # Wait status file created during ha_load_timeout.
856                 # Existing file guarantees that some application
857                 # is completed. If no status file was created
858                 # this function guarantees that we allow
859                 # applications to continue after/before
860                 # failover/failback during ha_load_timeout time.
861                 #
862                 until [ -e "$file" ] || (($(date +%s) >= end)); do
863                         #
864                         # check ha_stop_file again, it could appear
865                         # during ha_load_timeout
866                         #
867                         if [ -e "$ha_stop_file" ]; then
868                                 ha_info "$ha_stop_file found! Stop."
869                                 break
870                         fi
871                         ha_sleep 1 >/dev/null
872                 done
873         done
874 }
875
876 ha_powermanage()
877 {
878         local nodes=$1
879         local expected_state=$2
880         local state
881         local -a states
882         local i
883         local rc=0
884
885         # store pm -x -q $nodes results in a file to have
886         # more information about nodes statuses
887         ha_on $ha_pm_host pm -x -q $nodes | awk '{print $2 $3}' > $ha_pm_states
888         rc=${PIPESTATUS[0]}
889         echo pmrc=$rc
890
891         while IFS=": " read node state; do
892                 [[ "$state" = "$expected_state" ]] && {
893                         nodes=${nodes/$node/}
894                         nodes=${nodes//,,/,}
895                         nodes=${nodes/#,}
896                         nodes=${nodes/%,}
897                 }
898         done < $ha_pm_states
899
900         if [ -n "$nodes" ]; then
901                 cat $ha_pm_states
902                 return 1
903         fi
904         return 0
905 }
906
907 ha_power_down_cmd_fn()
908 {
909         local nodes=$1
910         local cmd
911         local pid
912         local rc=0
913
914         case $ha_power_down_cmd in
915         # format is: POWER_DOWN=sysrqcrash
916         sysrqcrash)
917                 cmd="pdsh -S -w $nodes -u 120 \"echo c > /proc/sysrq-trigger\" &"
918                 eval $cmd
919                 pid=$!
920                 ha_power_down_pids=$(echo $ha_power_down_pids $pid)
921                 ha_info "ha_power_down_pids: $ha_power_down_pids"
922                 [[ -z "$ha_power_down_pids" ]] ||
923                         ps aux | grep " ${ha_power_down_pids// / \| } " ||
924                         true
925                 ;;
926         *)
927                 cmd="$ha_power_down_cmd $nodes"
928                 eval $cmd
929                 rc=$?
930                 ;;
931         esac
932
933         return $rc
934 }
935
936 ha_power_down()
937 {
938         local nodes=$1
939         local rc=1
940         local i
941         local state
942
943         case $ha_power_down_cmd in
944                 *pm*) state=off ;;
945                 sysrqcrash) state=off ;;
946                 *) state=on;;
947         esac
948
949         if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
950                 ha_info "$ha_lfsck_node down, delay start LFSCK"
951                 ha_lock $ha_lfsck_lock
952         fi
953
954         ha_info "Powering down $nodes : cmd: $ha_power_down_cmd"
955         ha_power_down_pids=""
956         for (( i=0; i<10; i++ )) {
957                 ha_info "attempt: $i"
958                 ha_power_down_cmd_fn $nodes || rc=1
959                 ha_sleep $ha_power_delay
960                 ha_powermanage $nodes $state && rc=0 && break
961         }
962         if [[ -n "$ha_power_down_pids" ]]; then
963                 kill -9 $ha_power_down_pids ||  true
964                 wait $ha_power_down_pids || true
965         fi
966
967         [ $rc -eq 0 ] || {
968                 ha_info "Failed Powering down in $i attempts:" \
969                         "$ha_power_down_cmd"
970                 cat $ha_pm_states
971                 exit 1
972         }
973 }
974
975 ha_get_pair()
976 {
977         local node=$1
978         local i
979
980         for ((i=0; i<${#ha_victims[@]}; i++)) {
981                 [[ ${ha_victims[i]} == $node ]] && echo ${ha_victims_pair[i]} &&
982                         return
983         }
984         [[ $i -ne ${#ha_victims[@]} ]] ||
985                 ha_error "No pair found!"
986 }
987
988 ha_power_up_delay()
989 {
990         local nodes=$1
991         local end=$(($(date +%s) + ha_node_up_delay))
992         local rc
993
994         if [[ ${#ha_victims_pair[@]} -eq 0 ]]; then
995                 ha_sleep $ha_node_up_delay
996                 return 0
997         fi
998
999         # Check CRM status on failover pair
1000         while (($(date +%s) <= end)); do
1001                 rc=0
1002                 for n in ${nodes//,/ }; do
1003                         local pair=$(ha_get_pair $n)
1004                         local status=$(ha_on $pair crm_mon -1rQ | \
1005                                 grep -w $n | head -1)
1006
1007                         ha_info "$n pair: $pair status: $status"
1008                         [[ "$status" == *OFFLINE* ]] ||
1009                                 rc=$((rc + $?))
1010                         ha_info "rc: $rc"
1011                 done
1012
1013                 if [[ $rc -eq 0 ]];  then
1014                         ha_info "CRM: Got all victims status OFFLINE"
1015                         return 0
1016                 fi
1017                 sleep 60
1018         done
1019
1020         ha_info "$nodes CRM status not OFFLINE"
1021         for n in ${nodes//,/ }; do
1022                 local pair=$(ha_get_pair $n)
1023
1024                 ha_info "CRM --- $n"
1025                 ha_on $pair crm_mon -1rQ
1026         done
1027         ha_error "CRM: some of $nodes are not OFFLINE in $ha_node_up_delay sec"
1028         exit 1
1029 }
1030
1031 ha_power_up()
1032 {
1033         local nodes=$1
1034         local rc=1
1035         local i
1036
1037         ha_power_up_delay $nodes
1038         ha_info "Powering up $nodes : cmd: $ha_power_up_cmd"
1039         for (( i=0; i<10; i++ )) {
1040                 ha_info "attempt: $i"
1041                 $ha_power_up_cmd $nodes &&
1042                         ha_powermanage $nodes on && rc=0 && break
1043                 sleep $ha_power_delay
1044         }
1045
1046         [ $rc -eq 0 ] || {
1047                 ha_info "Failed Powering up in $i attempts: $ha_power_up_cmd"
1048                 cat $ha_pm_states
1049                 exit 1
1050         }
1051 }
1052
1053 #
1054 # rand MAX
1055 #
1056 # Print a random integer within [0, MAX).
1057 #
1058 ha_rand()
1059 {
1060     local max=$1
1061
1062     #
1063     # See "5.2 Bash Variables" from "info bash".
1064     #
1065     echo -n $((RANDOM * max / 32768))
1066 }
1067
1068 ha_aim()
1069 {
1070         local i
1071         local nodes
1072
1073         if $ha_simultaneous ; then
1074                 nodes=$(echo ${ha_victims[@]})
1075                 nodes=${nodes// /,}
1076         else
1077                 i=$(ha_rand ${#ha_victims[@]})
1078                 nodes=${ha_victims[$i]}
1079         fi
1080
1081         echo -n $nodes
1082 }
1083
1084 ha_wait_nodes()
1085 {
1086         local nodes=$1
1087         local end=$(($(date +%s) + $ha_wait_nodes_up))
1088
1089         ha_info "Waiting for $nodes to boot up in $ha_wait_nodes_up"
1090         until ha_on $nodes hostname >/dev/null 2>&1 ||
1091                 [ -e "$ha_stop_file" ] ||
1092                         (($(date +%s) >= end)); do
1093                 ha_sleep 1 >/dev/null
1094         done
1095
1096         ha_info "Check where we are ..."
1097         [ -e "$ha_stop_file" ] &&
1098                 ha_info "$ha_stop_file found!"
1099
1100         local -a nodes_up
1101         nodes_up=($(ha_on $nodes hostname | awk '{ print $2 }'))
1102         ha_info "Nodes $nodes are up: ${nodes_up[@]}"
1103         local -a n=(${nodes//,/ })
1104         if [[ ${#nodes_up[@]} -ne ${#n[@]} ]]; then
1105                 ha_info "Failed boot up $nodes in $ha_wait_nodes_up sec!"
1106                 ha_touch fail,stop
1107                 return 1
1108         fi
1109         return 0
1110 }
1111
1112 ha_failback()
1113 {
1114         local nodes=$1
1115         ha_info "Failback resources on $nodes in $ha_failback_delay sec"
1116
1117         ha_sleep $ha_failback_delay
1118         [ "$ha_failback_cmd" ] ||
1119         {
1120                 ha_info "No failback command set, skiping"
1121                 return 0
1122         }
1123
1124         $ha_failback_cmd $nodes
1125         [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
1126 }
1127
1128 ha_summarize()
1129 {
1130     ha_info "---------------8<---------------"
1131     ha_info "Summary:"
1132     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
1133     ha_info "    Loops: $ha_nr_loops"
1134 }
1135
1136 ha_killer()
1137 {
1138         local nodes
1139
1140         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
1141                         [ ! -e "$ha_stop_file" ]; do
1142                 ha_info "---------------8<---------------"
1143
1144                 $ha_workloads_only || nodes=$(ha_aim)
1145
1146                 ha_info "Failing $nodes"
1147                 $ha_workloads_only && ha_info "    is skipped: workload only..."
1148
1149                 ha_sleep $(ha_rand $ha_max_failover_period)
1150                 $ha_workloads_only || ha_power_down $nodes
1151                 ha_sleep 10
1152                 ha_wait_loads || return
1153
1154                 if [ -e $ha_stop_file ]; then
1155                         $ha_workloads_only || ha_power_up $nodes
1156                         break
1157                 fi
1158
1159                 ha_info "Bringing $nodes back"
1160                 ha_sleep $(ha_rand 10)
1161                 $ha_workloads_only ||
1162                 {
1163                         ha_power_up $nodes
1164                         ha_wait_nodes $nodes
1165                         ha_failback $nodes
1166                 }
1167
1168                 #
1169                 # Wait for the failback to start.
1170                 #
1171                 ha_sleep 60
1172                 ha_wait_loads || return
1173
1174                 ha_sleep $(ha_rand 20)
1175
1176                 ha_nr_loops=$((ha_nr_loops + 1))
1177                 ha_info "Loop $ha_nr_loops done"
1178         done
1179         ha_summarize
1180 }
1181
1182 ha_main()
1183 {
1184         ha_process_arguments "$@"
1185         ha_check_env
1186
1187         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1188                 "START: $0: $(date +%H:%M:%S' '%s)"
1189         trap ha_trap_exit EXIT
1190         mkdir "$ha_tmp_dir"
1191
1192         local mdt_index
1193         if $ha_test_dir_mdt_index_random &&
1194                 [ $ha_test_dir_mdt_index -ne 0 ]; then
1195                 mdt_index=$(ha_rand $ha_test_dir_mdt_index)
1196         else
1197                 mdt_index=$ha_test_dir_mdt_index
1198         fi
1199
1200         local dir
1201         test_dir=${ha_testdirs[0]}
1202         ha_on ${ha_clients[0]} "$LFS mkdir -i$mdt_index \
1203                 -c$ha_test_dir_stripe_count $test_dir"
1204         for ((i=0; i<${#ha_testdirs[@]}; i++)); do
1205                 test_dir=${ha_testdirs[i]}
1206                 ha_on ${ha_clients[0]} $LFS getdirstripe $test_dir
1207                 ha_on ${ha_clients[0]} " \
1208                         $LFS setstripe $ha_stripe_params $test_dir"
1209         done
1210
1211         ha_start_loads
1212         ha_wait_loads
1213
1214         if $ha_workloads_dry_run; then
1215                 ha_sleep 5
1216         else
1217                 ha_killer
1218                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
1219         fi
1220
1221         ha_stop_loads
1222
1223         $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
1224
1225         $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
1226
1227         if [ -e "$ha_fail_file" ]; then
1228                 exit 1
1229         else
1230                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1231                         "END: $0: $(date +%H:%M:%S' '%s)"
1232                 exit 0
1233         fi
1234 }
1235
1236 ha_main "$@"