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