Whamcloud - gitweb
05a90873cee2b2f921fdf6f295ffc72570d87ecf
[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 declare     ha_clients_stripe=${CLIENTSSTRIPE:-'"$STRIPEPARAMS"'}
221 declare     ha_nclientsset=${NCLIENTSSET:-1}
222
223 declare     ha_racer_params=${RACERP:-"MDSCOUNT=1"}
224
225 eval ha_params_ior=($ha_ior_params)
226 eval ha_params_simul=($ha_simul_params)
227 eval ha_params_mdtest=($ha_mdtest_params)
228 eval ha_stripe_clients=($ha_clients_stripe)
229
230 declare ha_nparams_ior=${#ha_params_ior[@]}
231 declare ha_nparams_simul=${#ha_params_simul[@]}
232 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
233 declare ha_nstripe_clients=${#ha_stripe_clients[@]}
234
235 declare -A  ha_mpi_load_cmds=(
236         [ior]="$IOR -o {}/f.ior {params}"
237         [simul]="$SIMUL {params} -d {}"
238         [mdtest]="$MDTEST {params} -d {}"
239 )
240
241 declare racer=${RACER:-"$(dirname $0)/racer/racer.sh"}
242
243 declare     ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
244 declare -a  ha_nonmpi_load_tags=($ha_nonmpi_loads)
245 declare -A  ha_nonmpi_load_cmds=(
246         [dd]="dd if=/dev/zero of={}/f.dd bs=1M count=256"
247         [tar]="tar cf - /etc | tar xf - -C {}"
248         [iozone]="iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
249         [racer]="$ha_racer_params $racer {}"
250 )
251
252 ha_usage()
253 {
254     ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]"                         \
255             "-v HOST[,...] [-d DIRECTORY] [-u SECONDS]"
256 }
257
258 ha_process_arguments()
259 {
260     local opt
261
262         while getopts hc:s:v:d:p:u:wrm opt; do
263         case $opt in
264         h)
265             ha_usage
266             exit 0
267             ;;
268         c)
269             ha_clients=(${OPTARG//,/ })
270             ;;
271         s)
272             ha_servers=(${OPTARG//,/ })
273             ;;
274         v)
275             ha_victims=(${OPTARG//,/ })
276             ;;
277         d)
278             ha_test_dir=$OPTARG/$(basename $0)-$$
279             ;;
280         u)
281             ha_expected_duration=$OPTARG
282             ;;
283         p)
284                 ha_max_failover_period=$OPTARG
285                 ;;
286         w)
287                 ha_workloads_only=true
288                 ;;
289         r)
290                 ha_workloads_dry_run=true
291                 ;;
292         m)
293                 ha_simultaneous=true
294                 ;;
295         \?)
296             ha_usage
297             exit 1
298             ;;
299         esac
300     done
301
302         if [ -z "${ha_clients[*]}" ]; then
303                 ha_error "-c is mandatory"
304                 ha_usage
305                 exit 1
306         fi
307         if ! ($ha_workloads_dry_run ||
308                         $ha_workloads_only) &&
309                         ([ -z "${ha_servers[*]}" ] ||
310                         [ -z "${ha_victims[*]}" ]); then
311                 ha_error "-s, and -v are all mandatory"
312                 ha_usage
313                 exit 1
314         fi
315 }
316
317 ha_on()
318 {
319         local nodes=$1
320         local rc=0
321
322         shift
323
324         #
325         # -S is to be used here to track the
326         # remote command return values
327         #
328         pdsh -S -w $nodes PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
329 /bin:/usr/sbin:/usr/bin "$@" ||
330                 rc=$?
331         return $rc
332 }
333
334 ha_trap_exit()
335 {
336         touch "$ha_stop_file"
337         trap 0
338         if [ -e "$ha_fail_file" ]; then
339                 ha_info "Test directory $ha_test_dir not removed"
340                 ha_info "Temporary directory $ha_tmp_dir not removed"
341         else
342                 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
343                 ha_info "Please find the results in the directory $ha_tmp_dir"
344         fi
345 }
346
347 ha_trap_stop_signals()
348 {
349     ha_info "${ha_stop_signals// /,} received"
350     touch "$ha_stop_file"
351 }
352
353 ha_sleep()
354 {
355     local n=$1
356
357     ha_info "Sleeping for ${n}s"
358     #
359     # sleep(1) could interrupted.
360     #
361     sleep $n || true
362 }
363
364 ha_wait_unlock()
365 {
366         local lock=$1
367
368         while [ -e $lock ]; do
369                 sleep 1
370         done
371 }
372
373 ha_lock()
374 {
375     local lock=$1
376
377     until mkdir "$lock" >/dev/null 2>&1; do
378         ha_sleep 1 >/dev/null
379     done
380 }
381
382 ha_unlock()
383 {
384     local lock=$1
385
386     rm -r "$lock"
387 }
388
389 ha_dump_logs()
390 {
391         local nodes=${1// /,}
392         local file=/tmp/$(basename $0)-$$-$(date +%s).dk
393         local lock=$ha_tmp_dir/lock-dump-logs
394         local rc=0
395
396         ha_lock "$lock"
397         ha_info "Dumping lctl log to $file"
398
399         #
400         # some nodes could crash, so
401         # do not exit with error if not all logs are dumped
402         #
403         ha_on $nodes "lctl dk >>$file" || rc=$?
404
405         [ $rc -eq 0 ] ||
406                 ha_error "not all logs are dumped! Some nodes are unreachable."
407         ha_unlock "$lock"
408 }
409
410 ha_repeat_mpi_load()
411 {
412         local client=$1
413         local load=$2
414         local status=$3
415         local parameter=$4
416         local machines=$5
417         local stripeparams=$6
418         local tag=${ha_mpi_load_tags[$load]}
419         local cmd=${ha_mpi_load_cmds[$tag]}
420         local dir=$ha_test_dir/$client-$tag
421         local log=$ha_tmp_dir/$client-$tag
422         local rc=0
423         local nr_loops=0
424         local avg_loop_time=0
425         local start_time=$(date +%s)
426
427         cmd=${cmd//"{}"/$dir}
428         cmd=${cmd//"{params}"/$parameter}
429
430         ha_info "Starting $tag"
431
432         machines="-machinefile $machines"
433         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
434                 {
435                 local mdt_index
436                 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
437                         mdt_index=$(ha_rand $ha_mdt_index)
438                 else
439                         mdt_index=$ha_mdt_index
440                 fi
441                 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
442                 ha_on $client $LFS getdirstripe "$dir" &&
443                 ha_on $client $LFS setstripe $stripeparams $dir &&
444                 ha_on $client $LFS getstripe $dir &&
445                 ha_on $client chmod a+xwr $dir &&
446                 ha_on $client "su mpiuser sh -c \" $mpirun $ha_mpirun_options \
447                         -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
448                         $machines $cmd \" " &&
449                         ha_on $client rm -rf "$dir";
450                 } >>"$log" 2>&1 || rc=$?
451
452                 ha_info rc=$rc
453
454                 if ((rc != 0)); then
455                         touch "$ha_fail_file"
456                         touch "$ha_stop_file"
457                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
458                 fi
459                 echo $rc >"$status"
460
461                 nr_loops=$((nr_loops + 1))
462         done
463
464         [ $nr_loops -ne 0 ] &&
465                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
466
467         ha_info "$tag stopped: rc $rc avg loop time $avg_loop_time"
468 }
469
470 ha_start_mpi_loads()
471 {
472         local client
473         local load
474         local tag
475         local status
476         local n
477         local nparam
478         local machines
479         local m
480         local -a mach
481
482         # ha_mpi_instances defines the number of
483         # clients start mpi loads; should be <= ${#ha_clients[@]}
484         # do nothing if
485         #    ha_mpi_instances = 0
486         # or
487         #    ${#ha_mpi_load_tags[@]} =0
488         local inst=$ha_mpi_instances
489         (( inst == 0 )) || (( ${#ha_mpi_load_tags[@]} == 0 )) &&
490                 ha_info "no mpi load to start" &&
491                 return 0
492
493         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
494
495         # Define names for machinefiles for each client set
496         for (( n=0; n < $ha_nclientsset; n++ )); do
497                 mach[$n]=$ha_machine_file$n
498         done
499
500         for ((n = 0; n < ${#ha_clients[@]}; n++)); do
501                 m=$(( n % ha_nclientsset))
502                 machines=${mach[m]}
503                 ha_info machine_file=$machines
504                 echo ${ha_clients[n]} >> $machines
505         done
506         local dirname=$(dirname $ha_machine_file)
507         for client in ${ha_clients[@]}; do
508                 ha_on $client mkdir -p $dirname
509                 scp $ha_machine_file* $client:$dirname
510         done
511
512         for ((n = 0; n < $inst; n++)); do
513                 client=${ha_clients[n]}
514                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
515                         tag=${ha_mpi_load_tags[$load]}
516                         status=$ha_status_file_prefix-$tag-$client
517                         # ha_nparams_ior
518                         # ha_nparams_simul
519                         local num=ha_nparams_$tag
520                         nparam=$((n % num))
521                         local aref=ha_params_$tag[nparam]
522                         local parameter=${!aref}
523                         local nstripe=$((n % ha_nstripe_clients))
524                         aref=ha_stripe_clients[nstripe]
525                         local stripe=${!aref}
526                         local m=$(( n % ha_nclientsset))
527                         machines=${mach[m]}
528                         ha_repeat_mpi_load $client $load $status "$parameter" $machines "$stripe" &
529                                 ha_status_files+=("$status")
530                 done
531         done
532 }
533
534 ha_repeat_nonmpi_load()
535 {
536         local client=$1
537         local load=$2
538         local status=$3
539         local tag=${ha_nonmpi_load_tags[$load]}
540         local cmd=${ha_nonmpi_load_cmds[$tag]}
541         local dir=$ha_test_dir/$client-$tag
542         local log=$ha_tmp_dir/$client-$tag
543         local rc=0
544         local nr_loops=0
545         local avg_loop_time=0
546         local start_time=$(date +%s)
547
548     cmd=${cmd//"{}"/$dir}
549
550     ha_info "Starting $tag on $client"
551
552         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
553                 ha_on $client "mkdir -p $dir &&                              \
554                         $cmd &&                                              \
555                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
556
557                 if ((rc != 0)); then
558                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
559                         touch "$ha_fail_file"
560                         touch "$ha_stop_file"
561                 fi
562                 echo $rc >"$status"
563
564                 nr_loops=$((nr_loops + 1))
565         done
566
567         [ $nr_loops -ne 0 ] &&
568                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
569
570         ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
571 }
572
573 ha_start_nonmpi_loads()
574 {
575     local client
576     local load
577     local tag
578     local status
579
580     for client in ${ha_clients[@]}; do
581         for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
582             tag=${ha_nonmpi_load_tags[$load]}
583             status=$ha_status_file_prefix-$tag-$client
584             ha_repeat_nonmpi_load $client $load $status &
585             ha_status_files+=("$status")
586         done
587     done
588 }
589
590 ha_lfsck_bg () {
591         rm -f $ha_lfsck_log
592         rm -f $ha_lfsck_stop
593
594         ha_info "LFSCK BG"
595         while [ true ]; do
596                 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
597                 [ -f $ha_stop_file ] &&
598                         ha_info "$ha_stop_file found! LFSCK not started" &&
599                         break
600                 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
601                 sleep 1
602         done &
603         LFSCK_BG_PID=$!
604         ha_info LFSCK BG PID: $LFSCK_BG_PID
605 }
606
607 ha_wait_lfsck_completed () {
608         local -a status
609         local -a types=($ha_lfsck_types)
610         local type
611         local s
612
613         local nodes="${ha_servers[@]}"
614         nodes=${nodes// /,}
615
616         # -A start LFSCK on all nodes
617         # -t default all
618         [ ${#types[@]} -eq 0 ] && types=(namespace layout)
619         ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
620         for type in ${types[@]}; do
621                 eval var_$type=0
622                 for (( i=0; i<=ha_lfsck_wait; i++)); do
623                         status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
624                                 awk '/status/ { print $3 }'))
625                         for (( s=0; s<${#status[@]}; s++ )); do
626                                 # "partial" is expected after HARD failover
627                                 [[ "${status[s]}" = "completed" ]] ||
628                                 [[ "${status[s]}" = "partial" ]] ||  break
629                         done
630                         [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
631                         sleep 1
632                 done
633                 ha_info "LFSCK $type status in $i sec:"
634                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
635
636         done
637
638         for type in ${types[@]}; do
639                 local var=var_$type
640                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
641                 [[ ${!var} -eq 1 ]] ||
642                         { ha_info "lfsck not completed in $ha_lfsck_wait sec";
643                         return 1; }
644         done
645         return 0
646 }
647
648 ha_start_lfsck()
649 {
650         local -a types=($ha_lfsck_types)
651         local rc=0
652
653         # -A: start LFSCK on all nodes via the specified MDT device
654         # (see "-M" option) by single LFSCK command
655         local params=" -A -r $ha_lfsck_custom_params"
656
657         # use specified device if set
658         [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
659
660         # -t: check type(s) to be performed (default all)
661         # check only specified types if set
662         if [ ${#types[@]} -ne 0 ]; then
663                 local type="${types[@]}"
664                 params="$params -t ${type// /,}"
665         fi
666
667         ha_info "LFSCK start $params"
668         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
669         if [ $rc -ne 0 ]; then
670                 if [ -e $ha_lfsck_lock ]; then
671                         rc=0
672                         ha_wait_unlock $ha_lfsck_lock
673                         ha_sleep 120
674                         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
675                 fi
676         fi
677
678         [ $rc -eq 0 ] ||
679                 { touch "$ha_fail_file"; touch "$ha_stop_file";
680                 touch $ha_lfsck_stop; return 1; }
681
682         ha_wait_lfsck_completed ||
683                 { touch "$ha_fail_file"; touch "$ha_stop_file";
684                 touch $ha_lfsck_stop; return 1; }
685
686         return 0
687 }
688
689 ha_lfsck_repaired()
690 {
691         local n=0
692
693         n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
694                 awk '{sum += $1} END { print sum }')
695         [ $n -eq 0] ||
696                 { ha_info "Total repaired: $n";
697                 touch "$ha_fail_file"; return 1; }
698         return 0
699 }
700
701 ha_start_loads()
702 {
703     trap ha_trap_stop_signals $ha_stop_signals
704     ha_start_nonmpi_loads
705     ha_start_mpi_loads
706 }
707
708 ha_stop_loads()
709 {
710     touch $ha_stop_file
711     trap - $ha_stop_signals
712     ha_info "Waiting for workloads to stop"
713     wait
714 }
715
716 ha_wait_loads()
717 {
718     local file
719     local end=$(($(date +%s) + ha_load_timeout))
720
721     ha_info "Waiting for workload status"
722     rm -f "${ha_status_files[@]}"
723
724         #
725         # return immediately if ha_stop_file exists,
726         # all status_files not needed to be checked
727         #
728         for file in "${ha_status_files[@]}"; do
729                 if [ -e "$ha_stop_file" ]; then
730                         ha_info "$ha_stop_file found! Stop."
731                         break
732                 fi
733                 #
734                 # Wait status file created during ha_load_timeout.
735                 # Existing file guarantees that some application
736                 # is completed. If no status file was created
737                 # this function guarantees that we allow
738                 # applications to continue after/before
739                 # failover/failback during ha_load_timeout time.
740                 #
741                 until [ -e "$file" ] || (($(date +%s) >= end)); do
742                         #
743                         # check ha_stop_file again, it could appear
744                         # during ha_load_timeout
745                         #
746                         if [ -e "$ha_stop_file" ]; then
747                                 ha_info "$ha_stop_file found! Stop."
748                                 break
749                         fi
750                         ha_sleep 1 >/dev/null
751                 done
752         done
753 }
754
755 ha_power_down()
756 {
757         local nodes=$1
758         local rc=1
759         local i
760
761         if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
762                 ha_info "$ha_lfsck_node down, delay start LFSCK"
763                 ha_lock $ha_lfsck_lock
764         fi
765
766         ha_info "Powering down $nodes"
767         for i in $(seq 1 5); do
768                 $ha_power_down_cmd $nodes && rc=0 && break
769                 sleep $ha_power_delay
770         done
771
772         [ $rc -eq 0 ] || ha_info "Failed Powering down in $i attempts"
773 }
774
775 ha_power_up()
776 {
777         local nodes=$1
778         local rc=1
779         local i
780
781         ha_info "Powering up $nodes"
782         for i in $(seq 1 5); do
783                 $ha_power_up_cmd $nodes && rc=0 && break
784                 sleep $ha_power_delay
785         done
786
787         [ $rc -eq 0 ] || ha_info "Failed Powering up in $i attempts"
788 }
789
790 #
791 # rand MAX
792 #
793 # Print a random integer within [0, MAX).
794 #
795 ha_rand()
796 {
797     local max=$1
798
799     #
800     # See "5.2 Bash Variables" from "info bash".
801     #
802     echo -n $((RANDOM * max / 32768))
803 }
804
805 ha_aim()
806 {
807         local i
808         local nodes
809
810         if $ha_simultaneous ; then
811                 nodes=$(echo ${ha_victims[@]})
812                 nodes=${nodes// /,}
813         else
814                 i=$(ha_rand ${#ha_victims[@]})
815                 nodes=${ha_victims[$i]}
816         fi
817
818         echo -n $nodes
819 }
820
821 ha_wait_nodes()
822 {
823         local nodes=$1
824         local end=$(($(date +%s) + 10 * 60))
825
826         ha_info "Waiting for $nodes to boot up"
827         until ha_on $nodes hostname >/dev/null 2>&1 ||
828                 [ -e "$ha_stop_file" ] ||
829                         (($(date +%s) >= end)); do
830                 ha_sleep 1 >/dev/null
831         done
832 }
833
834 ha_failback()
835 {
836         local nodes=$1
837         ha_info "Failback resources on $nodes in $ha_failback_delay sec"
838
839         ha_sleep $ha_failback_delay
840         [ "$ha_failback_cmd" ] ||
841         {
842                 ha_info "No failback command set, skiping"
843                 return 0
844         }
845
846         $ha_failback_cmd $nodes
847         [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
848 }
849
850 ha_summarize()
851 {
852     ha_info "---------------8<---------------"
853     ha_info "Summary:"
854     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
855     ha_info "    Loops: $ha_nr_loops"
856 }
857
858 ha_killer()
859 {
860         local nodes
861
862         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
863                         [ ! -e "$ha_stop_file" ]; do
864                 ha_info "---------------8<---------------"
865
866                 $ha_workloads_only || nodes=$(ha_aim)
867
868                 ha_info "Failing $nodes"
869                 $ha_workloads_only && ha_info "    is skipped: workload only..."
870
871                 ha_sleep $(ha_rand $ha_max_failover_period)
872                 $ha_workloads_only || ha_power_down $nodes
873                 ha_sleep 10
874                 ha_wait_loads || return
875
876                 if [ -e $ha_stop_file ]; then
877                         $ha_workloads_only || ha_power_up $nodes
878                         break
879                 fi
880
881                 ha_info "Bringing $nodes back"
882                 ha_sleep $(ha_rand 10)
883                 $ha_workloads_only ||
884                 {
885                         ha_power_up $nodes
886                         ha_wait_nodes $nodes
887                         ha_failback $nodes
888                 }
889
890                 #
891                 # Wait for the failback to start.
892                 #
893                 ha_sleep 60
894                 ha_wait_loads || return
895
896                 ha_sleep $(ha_rand 20)
897
898                 ha_nr_loops=$((ha_nr_loops + 1))
899                 ha_info "Loop $ha_nr_loops done"
900         done
901         ha_summarize
902 }
903
904 ha_main()
905 {
906         ha_process_arguments "$@"
907         ha_check_env
908
909         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
910                 "START: $0: $(date +%H:%M:%S' '%s)"
911         trap ha_trap_exit EXIT
912         mkdir "$ha_tmp_dir"
913         ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
914         ha_on ${ha_clients[0]} " \
915                 $LFS setstripe $ha_stripe_params $ha_test_dir"
916
917         $ha_lfsck_bg && ha_lfsck_bg
918
919         ha_start_loads
920         ha_wait_loads
921
922         if $ha_workloads_dry_run; then
923                 ha_sleep 5
924         else
925                 ha_killer
926                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
927         fi
928
929         ha_stop_loads
930
931         $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
932
933         $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
934
935         # true because of lfsck_bg could be stopped already
936         $ha_lfsck_bg && wait $LFSCK_BG_PID || true
937
938         if [ -e "$ha_fail_file" ]; then
939                 exit 1
940         else
941                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
942                         "END: $0: $(date +%H:%M:%S' '%s)"
943                 exit 0
944         fi
945 }
946
947 ha_main "$@"