Whamcloud - gitweb
LU-6142 tests: Fix style issues for chownmany.c
[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 # PROCESS STRUCTURE AND IPC
86 #
87 #   On the node where this script is run, the processes look like this:
88 #
89 #       ~ ha.sh (ha_killer)
90 #
91 #           ~ ha.sh (ha_repeat_mpi_load ior)
92 #               ~ mpirun IOR
93 #           ~ ha.sh (ha_repeat_mpi_load simul)
94 #               ~ mpirun simul
95 #           ~ ha.sh (ha_repeat_mpi_load mdtest)
96 #               ~ mpirun mdtest
97 #           ~ ... (one for each MPI load)
98 #
99 #           ~ ha.sh (ha_repeat_nonmpi_load client2 dbench)
100 #               ~ pdsh client2 dbench
101 #           ~ ha.sh (ha_repeat_nonmpi_load client2 iozone)
102 #               ~ pdsh client2 iozone
103 #           ~ ha.sh (ha_repeat_nonmpi_load client5 iozone)
104 #               ~ pdsh client5 iozone
105 #           ~ ... (one for each non-MPI load on each client)
106 #
107 #   Each tilde represents a process.  Indentations imply parent-children
108 #   relation.
109 #
110 #   IPC is done by files in the temporary directory.
111 #
112
113 #set -x
114
115 SIMUL=${SIMUL:-$(which simul 2> /dev/null || true)}
116 IOR=${IOR:-$(which IOR 2> /dev/null || true)}
117 MDTEST=${MDTEST:-$(which mdtest 2> /dev/null || true)}
118
119 ior_blockSize=${ior_blockSize:-6g}
120 mpi_threads_per_client=${mpi_threads_per_client:-2}
121
122 iozone_SIZE=${iozone_SIZE:-262144} # 256m
123
124 mpirun=${MPIRUN:-$(which mpirun)}
125 LFS=${LFS:-$(which lfs)}
126
127 ha_check_env()
128 {
129         for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
130                 local tag=${ha_mpi_load_tags[$load]}
131                 local bin=$(echo $tag | tr '[:lower:]' '[:upper:]')
132                 if [ x${!bin} = x ]; then
133                         ha_error ha_mpi_loads: ${ha_mpi_loads}, $bin is not set
134                         exit 1
135                 fi
136         done
137 }
138
139 ha_info()
140 {
141         echo "$0: $(date +%H:%M:%S' '%s):" "$@"
142 }
143
144 ha_log()
145 {
146         local nodes=${1// /,}
147         shift
148         ha_on $nodes "lctl mark $*"
149 }
150
151 ha_error()
152 {
153     ha_info "$@" >&2
154 }
155
156 ha_trap_err()
157 {
158     local i
159
160     ha_error "Trap ERR triggered by:"
161     ha_error "    $BASH_COMMAND"
162     ha_error "Call trace:"
163     for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
164         ha_error "    ${FUNCNAME[$i]} [${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}]"
165     done
166 }
167
168 trap ha_trap_err ERR
169 set -eE
170
171 declare     ha_tmp_dir=/tmp/$(basename $0)-$$
172 declare     ha_stop_file=$ha_tmp_dir/stop
173 declare     ha_fail_file=$ha_tmp_dir/fail
174 declare     ha_status_file_prefix=$ha_tmp_dir/status
175 declare -a  ha_status_files
176 declare     ha_machine_file=$ha_tmp_dir/machine_file
177 declare     ha_lfsck_log=$ha_tmp_dir/lfsck.log
178 declare     ha_lfsck_lock=$ha_tmp_dir/lfsck.lock
179 declare     ha_lfsck_stop=$ha_tmp_dir/lfsck.stop
180 declare     ha_lfsck_bg=${LFSCK_BG:-false}
181 declare     ha_lfsck_after=${LFSCK_AFTER:-false}
182 declare     ha_lfsck_node=${LFSCK_NODE:-""}
183 declare     ha_lfsck_device=${LFSCK_DEV:-""}
184 declare     ha_lfsck_types=${LFSCK_TYPES:-"namespace layout"}
185 declare     ha_lfsck_custom_params=${LFSCK_CUSTOM_PARAMS:-""}
186 declare     ha_lfsck_wait=${LFSCK_WAIT:-1200}
187 declare     ha_lfsck_fail_on_repaired=${LFSCK_FAIL_ON_REPAIRED:-false}
188 declare     ha_power_down_cmd=${POWER_DOWN:-"pm -0"}
189 declare     ha_power_up_cmd=${POWER_UP:-"pm -1"}
190 declare     ha_power_delay=${POWER_DELAY:-60}
191 declare     ha_failback_delay=${DELAY:-5}
192 declare     ha_failback_cmd=${FAILBACK:-""}
193 declare     ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
194 declare     ha_dir_stripe_count=${DSTRIPECOUNT:-"1"}
195 declare     ha_mdt_index=${MDTINDEX:-"0"}
196 declare     ha_mdt_index_random=${MDTINDEXRAND:-false}
197 declare -a  ha_clients
198 declare -a  ha_servers
199 declare -a  ha_victims
200 declare     ha_test_dir=/mnt/lustre/$(basename $0)-$$
201 declare     ha_start_time=$(date +%s)
202 declare     ha_expected_duration=$((60 * 60 * 24))
203 declare     ha_max_failover_period=10
204 declare     ha_nr_loops=0
205 declare     ha_stop_signals="SIGINT SIGTERM SIGHUP"
206 declare     ha_load_timeout=$((60 * 10))
207 declare     ha_workloads_only=false
208 declare     ha_workloads_dry_run=false
209 declare     ha_simultaneous=false
210
211 declare     ha_mpi_instances=${ha_mpi_instances:-1}
212
213 declare     ha_mpi_loads=${ha_mpi_loads="ior simul mdtest"}
214 declare -a  ha_mpi_load_tags=($ha_mpi_loads)
215
216 declare     ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
217 declare     ha_simul_params=${SIMULP:-'" -n 10"'}
218 declare     ha_mdtest_params=${MDTESTP:-'" -i 1 -n 1000"'}
219 declare     ha_mpirun_options=${MPIRUN_OPTIONS:-""}
220
221 eval ha_params_ior=($ha_ior_params)
222 eval ha_params_simul=($ha_simul_params)
223 eval ha_params_mdtest=($ha_mdtest_params)
224
225 declare ha_nparams_ior=${#ha_params_ior[@]}
226 declare ha_nparams_simul=${#ha_params_simul[@]}
227 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
228
229 declare -A  ha_mpi_load_cmds=(
230         [ior]="$IOR -o {}/f.ior {params}"
231         [simul]="$SIMUL {params} -d {}"
232         [mdtest]="$MDTEST {params} -d {}"
233 )
234
235 declare     ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
236 declare -a  ha_nonmpi_load_tags=($ha_nonmpi_loads)
237 declare -a  ha_nonmpi_load_cmds=(
238         "dd if=/dev/zero of={}/f.dd bs=1M count=256"
239         "tar cf - /etc | tar xf - -C {}"
240         "iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
241 )
242
243 ha_usage()
244 {
245     ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]"                         \
246             "-v HOST[,...] [-d DIRECTORY] [-u SECONDS]"
247 }
248
249 ha_process_arguments()
250 {
251     local opt
252
253         while getopts hc:s:v:d:p:u:wrm opt; do
254         case $opt in
255         h)
256             ha_usage
257             exit 0
258             ;;
259         c)
260             ha_clients=(${OPTARG//,/ })
261             ;;
262         s)
263             ha_servers=(${OPTARG//,/ })
264             ;;
265         v)
266             ha_victims=(${OPTARG//,/ })
267             ;;
268         d)
269             ha_test_dir=$OPTARG/$(basename $0)-$$
270             ;;
271         u)
272             ha_expected_duration=$OPTARG
273             ;;
274         p)
275                 ha_max_failover_period=$OPTARG
276                 ;;
277         w)
278                 ha_workloads_only=true
279                 ;;
280         r)
281                 ha_workloads_dry_run=true
282                 ;;
283         m)
284                 ha_simultaneous=true
285                 ;;
286         \?)
287             ha_usage
288             exit 1
289             ;;
290         esac
291     done
292
293         if [ -z "${ha_clients[*]}" ]; then
294                 ha_error "-c is mandatory"
295                 ha_usage
296                 exit 1
297         fi
298         if ! ($ha_workloads_dry_run ||
299                         $ha_workloads_only) &&
300                         ([ -z "${ha_servers[*]}" ] ||
301                         [ -z "${ha_victims[*]}" ]); then
302                 ha_error "-s, and -v are all mandatory"
303                 ha_usage
304                 exit 1
305         fi
306 }
307
308 ha_on()
309 {
310         local nodes=$1
311         local rc=0
312
313         shift
314
315         #
316         # -S is to be used here to track the
317         # remote command return values
318         #
319         pdsh -S -w $nodes PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
320 /bin:/usr/sbin:/usr/bin "$@" ||
321                 rc=$?
322         return $rc
323 }
324
325 ha_trap_exit()
326 {
327         touch "$ha_stop_file"
328         trap 0
329         if [ -e "$ha_fail_file" ]; then
330                 ha_info "Test directory $ha_test_dir not removed"
331                 ha_info "Temporary directory $ha_tmp_dir not removed"
332         else
333                 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
334                 ha_info "Please find the results in the directory $ha_tmp_dir"
335         fi
336 }
337
338 ha_trap_stop_signals()
339 {
340     ha_info "${ha_stop_signals// /,} received"
341     touch "$ha_stop_file"
342 }
343
344 ha_sleep()
345 {
346     local n=$1
347
348     ha_info "Sleeping for ${n}s"
349     #
350     # sleep(1) could interrupted.
351     #
352     sleep $n || true
353 }
354
355 ha_wait_unlock()
356 {
357         local lock=$1
358
359         while [ -e $lock ]; do
360                 sleep 1
361         done
362 }
363
364 ha_lock()
365 {
366     local lock=$1
367
368     until mkdir "$lock" >/dev/null 2>&1; do
369         ha_sleep 1 >/dev/null
370     done
371 }
372
373 ha_unlock()
374 {
375     local lock=$1
376
377     rm -r "$lock"
378 }
379
380 ha_dump_logs()
381 {
382         local nodes=${1// /,}
383         local file=/tmp/$(basename $0)-$$-$(date +%s).dk
384         local lock=$ha_tmp_dir/lock-dump-logs
385         local rc=0
386
387         ha_lock "$lock"
388         ha_info "Dumping lctl log to $file"
389
390         #
391         # some nodes could crash, so
392         # do not exit with error if not all logs are dumped
393         #
394         ha_on $nodes "lctl dk >>$file" || rc=$?
395
396         [ $rc -eq 0 ] ||
397                 ha_error "not all logs are dumped! Some nodes are unreachable."
398         ha_unlock "$lock"
399 }
400
401 ha_repeat_mpi_load()
402 {
403         local client=$1
404         local load=$2
405         local status=$3
406         local parameter=$4
407         local tag=${ha_mpi_load_tags[$load]}
408         local cmd=${ha_mpi_load_cmds[$tag]}
409         local dir=$ha_test_dir/$client-$tag
410         local log=$ha_tmp_dir/$client-$tag
411         local rc=0
412         local nr_loops=0
413         local avg_loop_time=0
414         local start_time=$(date +%s)
415
416         cmd=${cmd//"{}"/$dir}
417         cmd=${cmd//"{params}"/$parameter}
418
419         ha_info "Starting $tag"
420
421         local machines="-machinefile $ha_machine_file"
422         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
423                 {
424                 local mdt_index
425                 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
426                         mdt_index=$(ha_rand $ha_mdt_index)
427                 else
428                         mdt_index=$ha_mdt_index
429                 fi
430                 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
431                 ha_on $client $LFS getdirstripe "$dir" &&
432                 ha_on $client chmod a+xwr $dir &&
433                 ha_on $client "su mpiuser sh -c \" $mpirun $ha_mpirun_options \
434                         -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
435                         $machines $cmd \" " &&
436                         ha_on $client rm -rf "$dir";
437                 } >>"$log" 2>&1 || rc=$?
438
439                 ha_info rc=$rc
440
441                 if ((rc != 0)); then
442                         touch "$ha_fail_file"
443                         touch "$ha_stop_file"
444                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
445                 fi
446                 echo $rc >"$status"
447
448                 nr_loops=$((nr_loops + 1))
449         done
450
451         [ $nr_loops -ne 0 ] &&
452                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
453
454         ha_info "$tag stopped: rc $rc avg loop time $avg_loop_time"
455 }
456
457 ha_start_mpi_loads()
458 {
459         local client
460         local load
461         local tag
462         local status
463         local n
464         local nparam
465
466         for client in ${ha_clients[@]}; do
467                 ha_info ha_machine_file=$ha_machine_file
468                 echo $client >> $ha_machine_file
469         done
470         local dirname=$(dirname $ha_machine_file)
471         for client in ${ha_clients[@]}; do
472                 ha_on $client mkdir -p $dirname
473                 scp $ha_machine_file $client:$ha_machine_file
474         done
475
476         # ha_mpi_instances defines the number of
477         # clients start mpi loads; should be <= ${#ha_clients[@]}
478         local inst=$ha_mpi_instances
479         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
480
481         for ((n = 0; n < $inst; n++)); do
482                 client=${ha_clients[n]}
483                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
484                         tag=${ha_mpi_load_tags[$load]}
485                         status=$ha_status_file_prefix-$tag-$client
486                         # ha_nparams_ior
487                         # ha_nparams_simul
488                         local num=ha_nparams_$tag
489                         nparam=$((n % num))
490                         local aref=ha_params_$tag[nparam]
491                         local parameter=${!aref}
492                         ha_repeat_mpi_load $client $load $status "$parameter" &
493                                 ha_status_files+=("$status")
494                 done
495         done
496 }
497
498 ha_repeat_nonmpi_load()
499 {
500         local client=$1
501         local load=$2
502         local status=$3
503         local tag=${ha_nonmpi_load_tags[$load]}
504         local cmd=${ha_nonmpi_load_cmds[$load]}
505         local dir=$ha_test_dir/$client-$tag
506         local log=$ha_tmp_dir/$client-$tag
507         local rc=0
508         local nr_loops=0
509         local avg_loop_time=0
510         local start_time=$(date +%s)
511
512     cmd=${cmd//"{}"/$dir}
513
514     ha_info "Starting $tag on $client"
515
516         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
517                 ha_on $client "mkdir -p $dir &&                              \
518                         $cmd &&                                              \
519                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
520
521                 if ((rc != 0)); then
522                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
523                         touch "$ha_fail_file"
524                         touch "$ha_stop_file"
525                 fi
526                 echo $rc >"$status"
527
528                 nr_loops=$((nr_loops + 1))
529         done
530
531         [ $nr_loops -ne 0 ] &&
532                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
533
534         ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
535 }
536
537 ha_start_nonmpi_loads()
538 {
539     local client
540     local load
541     local tag
542     local status
543
544     for client in ${ha_clients[@]}; do
545         for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
546             tag=${ha_nonmpi_load_tags[$load]}
547             status=$ha_status_file_prefix-$tag-$client
548             ha_repeat_nonmpi_load $client $load $status &
549             ha_status_files+=("$status")
550         done
551     done
552 }
553
554 ha_lfsck_bg () {
555         rm -f $ha_lfsck_log
556         rm -f $ha_lfsck_stop
557
558         ha_info "LFSCK BG"
559         while [ true ]; do
560                 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
561                 [ -f $ha_stop_file ] &&
562                         ha_info "$ha_stop_file found! LFSCK not started" &&
563                         break
564                 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
565                 sleep 1
566         done &
567         LFSCK_BG_PID=$!
568         ha_info LFSCK BG PID: $LFSCK_BG_PID
569 }
570
571 ha_wait_lfsck_completed () {
572         local -a status
573         local -a types=($ha_lfsck_types)
574         local type
575         local s
576
577         local nodes="${ha_servers[@]}"
578         nodes=${nodes// /,}
579
580         # -A start LFSCK on all nodes
581         # -t default all
582         [ ${#types[@]} -eq 0 ] && types=(namespace layout)
583         ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
584         for type in ${types[@]}; do
585                 eval var_$type=0
586                 for (( i=0; i<=ha_lfsck_wait; i++)); do
587                         status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
588                                 awk '/status/ { print $3 }'))
589                         for (( s=0; s<${#status[@]}; s++ )); do
590                                 # "partial" is expected after HARD failover
591                                 [[ "${status[s]}" = "completed" ]] ||
592                                 [[ "${status[s]}" = "partial" ]] ||  break
593                         done
594                         [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
595                         sleep 1
596                 done
597                 ha_info "LFSCK $type status in $i sec:"
598                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
599
600         done
601
602         for type in ${types[@]}; do
603                 local var=var_$type
604                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
605                 [[ ${!var} -eq 1 ]] ||
606                         { ha_info "lfsck not completed in $ha_lfsck_wait sec";
607                         return 1; }
608         done
609         return 0
610 }
611
612 ha_start_lfsck()
613 {
614         local -a types=($ha_lfsck_types)
615         local rc=0
616
617         # -A: start LFSCK on all nodes via the specified MDT device
618         # (see "-M" option) by single LFSCK command
619         local params=" -A -r $ha_lfsck_custom_params"
620
621         # use specified device if set
622         [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
623
624         # -t: check type(s) to be performed (default all)
625         # check only specified types if set
626         if [ ${#types[@]} -ne 0 ]; then
627                 local type="${types[@]}"
628                 params="$params -t ${type// /,}"
629         fi
630
631         ha_info "LFSCK start $params"
632         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
633         if [ $rc -ne 0 ]; then
634                 if [ -e $ha_lfsck_lock ]; then
635                         rc=0
636                         ha_wait_unlock $ha_lfsck_lock
637                         ha_sleep 120
638                         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
639                 fi
640         fi
641
642         [ $rc -eq 0 ] ||
643                 { touch "$ha_fail_file"; touch "$ha_stop_file";
644                 touch $ha_lfsck_stop; return 1; }
645
646         ha_wait_lfsck_completed ||
647                 { touch "$ha_fail_file"; touch "$ha_stop_file";
648                 touch $ha_lfsck_stop; return 1; }
649
650         return 0
651 }
652
653 ha_lfsck_repaired()
654 {
655         local n=0
656
657         n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
658                 awk '{sum += $1} END { print sum }')
659         [ $n -eq 0] ||
660                 { ha_info "Total repaired: $n";
661                 touch "$ha_fail_file"; return 1; }
662         return 0
663 }
664
665 ha_start_loads()
666 {
667     trap ha_trap_stop_signals $ha_stop_signals
668     ha_start_nonmpi_loads
669     ha_start_mpi_loads
670 }
671
672 ha_stop_loads()
673 {
674     touch $ha_stop_file
675     trap - $ha_stop_signals
676     ha_info "Waiting for workloads to stop"
677     wait
678 }
679
680 ha_wait_loads()
681 {
682     local file
683     local end=$(($(date +%s) + ha_load_timeout))
684
685     ha_info "Waiting for workload status"
686     rm -f "${ha_status_files[@]}"
687
688         #
689         # return immediately if ha_stop_file exists,
690         # all status_files not needed to be checked
691         #
692         for file in "${ha_status_files[@]}"; do
693                 if [ -e "$ha_stop_file" ]; then
694                         ha_info "$ha_stop_file found! Stop."
695                         break
696                 fi
697                 #
698                 # Wait status file created during ha_load_timeout.
699                 # Existing file guarantees that some application
700                 # is completed. If no status file was created
701                 # this function guarantees that we allow
702                 # applications to continue after/before
703                 # failover/failback during ha_load_timeout time.
704                 #
705                 until [ -e "$file" ] || (($(date +%s) >= end)); do
706                         #
707                         # check ha_stop_file again, it could appear
708                         # during ha_load_timeout
709                         #
710                         if [ -e "$ha_stop_file" ]; then
711                                 ha_info "$ha_stop_file found! Stop."
712                                 break
713                         fi
714                         ha_sleep 1 >/dev/null
715                 done
716         done
717 }
718
719 ha_power_down()
720 {
721         local nodes=$1
722         local rc=1
723         local i
724
725         if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
726                 ha_info "$ha_lfsck_node down, delay start LFSCK"
727                 ha_lock $ha_lfsck_lock
728         fi
729
730         ha_info "Powering down $nodes"
731         for i in $(seq 1 5); do
732                 $ha_power_down_cmd $nodes && rc=0 && break
733                 sleep $ha_power_delay
734         done
735
736         [ $rc -eq 0 ] || ha_info "Failed Powering down in $i attempts"
737 }
738
739 ha_power_up()
740 {
741         local nodes=$1
742         local rc=1
743         local i
744
745         ha_info "Powering up $nodes"
746         for i in $(seq 1 5); do
747                 $ha_power_up_cmd $nodes && rc=0 && break
748                 sleep $ha_power_delay
749         done
750
751         [ $rc -eq 0 ] || ha_info "Failed Powering up in $i attempts"
752 }
753
754 #
755 # rand MAX
756 #
757 # Print a random integer within [0, MAX).
758 #
759 ha_rand()
760 {
761     local max=$1
762
763     #
764     # See "5.2 Bash Variables" from "info bash".
765     #
766     echo -n $((RANDOM * max / 32768))
767 }
768
769 ha_aim()
770 {
771         local i
772         local nodes
773
774         if $ha_simultaneous ; then
775                 nodes=$(echo ${ha_victims[@]})
776                 nodes=${nodes// /,}
777         else
778                 i=$(ha_rand ${#ha_victims[@]})
779                 nodes=${ha_victims[$i]}
780         fi
781
782         echo -n $nodes
783 }
784
785 ha_wait_nodes()
786 {
787         local nodes=$1
788         local end=$(($(date +%s) + 10 * 60))
789
790         ha_info "Waiting for $nodes to boot up"
791         until ha_on $nodes hostname >/dev/null 2>&1 ||
792                 [ -e "$ha_stop_file" ] ||
793                         (($(date +%s) >= end)); do
794                 ha_sleep 1 >/dev/null
795         done
796 }
797
798 ha_failback()
799 {
800         local nodes=$1
801         ha_info "Failback resources on $nodes in $ha_failback_delay sec"
802
803         ha_sleep $ha_failback_delay
804         [ "$ha_failback_cmd" ] ||
805         {
806                 ha_info "No failback command set, skiping"
807                 return 0
808         }
809
810         $ha_failback_cmd $nodes
811         [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
812 }
813
814 ha_summarize()
815 {
816     ha_info "---------------8<---------------"
817     ha_info "Summary:"
818     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
819     ha_info "    Loops: $ha_nr_loops"
820 }
821
822 ha_killer()
823 {
824         local nodes
825
826         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
827                         [ ! -e "$ha_stop_file" ]; do
828                 ha_info "---------------8<---------------"
829
830                 $ha_workloads_only || nodes=$(ha_aim)
831
832                 ha_info "Failing $nodes"
833                 $ha_workloads_only && ha_info "    is skipped: workload only..."
834
835                 ha_sleep $(ha_rand $ha_max_failover_period)
836                 $ha_workloads_only || ha_power_down $nodes
837                 ha_sleep 10
838                 ha_wait_loads || return
839
840                 if [ -e $ha_stop_file ]; then
841                         $ha_workloads_only || ha_power_up $nodes
842                         break
843                 fi
844
845                 ha_info "Bringing $nodes back"
846                 ha_sleep $(ha_rand 10)
847                 $ha_workloads_only ||
848                 {
849                         ha_power_up $nodes
850                         ha_wait_nodes $nodes
851                         ha_failback $nodes
852                 }
853
854                 #
855                 # Wait for the failback to start.
856                 #
857                 ha_sleep 60
858                 ha_wait_loads || return
859
860                 ha_sleep $(ha_rand 20)
861
862                 ha_nr_loops=$((ha_nr_loops + 1))
863                 ha_info "Loop $ha_nr_loops done"
864         done
865         ha_summarize
866 }
867
868 ha_main()
869 {
870         ha_process_arguments "$@"
871         ha_check_env
872
873         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
874                 "START: $0: $(date +%H:%M:%S' '%s)"
875         trap ha_trap_exit EXIT
876         mkdir "$ha_tmp_dir"
877         ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
878         ha_on ${ha_clients[0]} " \
879                 $LFS setstripe $ha_stripe_params $ha_test_dir"
880
881         $ha_lfsck_bg && ha_lfsck_bg
882
883         ha_start_loads
884         ha_wait_loads
885
886         if $ha_workloads_dry_run; then
887                 ha_sleep 5
888         else
889                 ha_killer
890                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
891         fi
892
893         ha_stop_loads
894
895         $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
896
897         $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
898
899         # true because of lfsck_bg could be stopped already
900         $ha_lfsck_bg && wait $LFSCK_BG_PID || true
901
902         if [ -e "$ha_fail_file" ]; then
903                 exit 1
904         else
905                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
906                         "END: $0: $(date +%H:%M:%S' '%s)"
907                 exit 0
908         fi
909 }
910
911 ha_main "$@"