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