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