Whamcloud - gitweb
LU-15104 tests: create client dirs with custom stripe count
[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
303 ha_usage()
304 {
305         ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]" \
306                 "-v HOST[,...] -f HOST[,...] [-d DIRECTORY] [-u SECONDS]"
307 }
308
309 ha_process_arguments()
310 {
311     local opt
312
313         while getopts hc:s:v:d:p:u:wrmf: opt; do
314         case $opt in
315         h)
316             ha_usage
317             exit 0
318             ;;
319         c)
320             ha_clients=(${OPTARG//,/ })
321             ;;
322         s)
323             ha_servers=(${OPTARG//,/ })
324             ;;
325         v)
326             ha_victims=(${OPTARG//,/ })
327             ;;
328         d)
329             ha_test_dir=$OPTARG/$(basename $0)-$$
330             ;;
331         u)
332             ha_expected_duration=$OPTARG
333             ;;
334         p)
335                 ha_max_failover_period=$OPTARG
336                 ;;
337         w)
338                 ha_workloads_only=true
339                 ;;
340         r)
341                 ha_workloads_dry_run=true
342                 ;;
343         m)
344                 ha_simultaneous=true
345                 ;;
346         f)
347                 ha_victims_pair=(${OPTARG//,/ })
348                 ;;
349         \?)
350             ha_usage
351             exit 1
352             ;;
353         esac
354     done
355
356         if [ -z "${ha_clients[*]}" ]; then
357                 ha_error "-c is mandatory"
358                 ha_usage
359                 exit 1
360         fi
361         if ! ($ha_workloads_dry_run ||
362                         $ha_workloads_only) &&
363                         ([ -z "${ha_servers[*]}" ] ||
364                         [ -z "${ha_victims[*]}" ]); then
365                 ha_error "-s, and -v are all mandatory"
366                 ha_usage
367                 exit 1
368         fi
369 }
370
371 ha_on()
372 {
373         local nodes=$1
374         local rc=0
375
376         shift
377
378         #
379         # -S is to be used here to track the
380         # remote command return values
381         #
382         pdsh -S -w $nodes "PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
383 /bin:/usr/sbin:/usr/bin; $@" ||
384                 rc=$?
385         return $rc
386 }
387
388 ha_trap_exit()
389 {
390         ha_touch stop
391         trap 0
392         if [ -e "$ha_fail_file" ]; then
393                 ha_info "Test directories ${ha_testdirs[@]} not removed"
394                 ha_info "Temporary directory $ha_tmp_dir not removed"
395         else
396                 $ha_cleanup &&
397                         ha_on ${ha_clients[0]} rm -rf ${ha_testdirs[@]} ||
398                         ha_info "Test directories ${ha_testdirs[@]} not removed"
399                 ha_info "Please find the results in the directory $ha_tmp_dir"
400         fi
401 }
402
403 ha_trap_stop_signals()
404 {
405         ha_info "${ha_stop_signals// /,} received"
406         ha_touch stop "${ha_stop_signals// /,} received"
407 }
408
409 ha_sleep()
410 {
411     local n=$1
412
413     ha_info "Sleeping for ${n}s"
414     #
415     # sleep(1) could interrupted.
416     #
417     sleep $n || true
418 }
419
420 ha_wait_unlock()
421 {
422         local lock=$1
423
424         while [ -e $lock ]; do
425                 sleep 1
426         done
427 }
428
429 ha_lock()
430 {
431     local lock=$1
432
433     until mkdir "$lock" >/dev/null 2>&1; do
434         ha_sleep 1 >/dev/null
435     done
436 }
437
438 ha_unlock()
439 {
440     local lock=$1
441
442     rm -r "$lock"
443 }
444
445 ha_dump_logs()
446 {
447         local nodes=${1// /,}
448         local file=/tmp/$(basename $0)-$$-$(date +%s).dk
449         local lock=$ha_tmp_dir/lock-dump-logs
450         local rc=0
451
452         ha_lock "$lock"
453         ha_info "Dumping lctl log to $file"
454
455         #
456         # some nodes could crash, so
457         # do not exit with error if not all logs are dumped
458         #
459         ha_on $nodes "lctl dk >>$file" || rc=$?
460
461         [ $rc -eq 0 ] ||
462                 ha_error "not all logs are dumped! Some nodes are unreachable."
463         ha_unlock "$lock"
464 }
465
466 ha_repeat_mpi_load()
467 {
468         local client=$1
469         local load=$2
470         local status=$3
471         local parameter=$4
472         local machines=$5
473         local stripeparams=$6
474         local mpiuser=$7
475         local mustpass=$8
476         local mpirunoptions=$9
477         local test_dir=${10}
478         local tag=${ha_mpi_load_tags[$load]}
479         local cmd=${ha_mpi_load_cmds[$tag]}
480         local dir=$test_dir/$client-$tag
481         local log=$ha_tmp_dir/$client-$tag
482         local rc=0
483         local nr_loops=0
484         local avg_loop_time=0
485         local start_time=$(date +%s)
486
487         cmd=${cmd//"{}"/$dir}
488         cmd=${cmd//"{params}"/$parameter}
489
490         [[ -n "$ha_postcmd" ]] && ha_postcmd=${ha_postcmd//"{}"/$dir}
491         [[ -n "$ha_precmd" ]] && ha_precmd=${ha_precmd//"{}"/$dir}
492         ha_info "Starting $tag"
493
494         machines="-machinefile $machines"
495         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
496                 ha_info "$client Starts: $mpiuser: $cmd" 2>&1 |  tee -a $log
497                 {
498                 local mdt_index
499                 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
500                         mdt_index=$(ha_rand $((ha_mdt_index + 1)) )
501                 else
502                         mdt_index=$ha_mdt_index
503                 fi
504                 local dir_stripe_count
505                 if $ha_dir_stripe_count_random &&
506                         [ $ha_dir_stripe_count -ne 1 ]; then
507                         dir_stripe_count=$(($(ha_rand $ha_dir_stripe_count) + 1))
508                 else
509                         dir_stripe_count=$ha_dir_stripe_count
510                 fi
511                 [[ -n "$ha_precmd" ]] && ha_info "$ha_precmd" &&
512                         ha_on $client "$ha_precmd" >>"$log" 2>&1
513                 ha_info "$client Creates $dir with -i$mdt_index -c$dir_stripe_count "
514                 ha_on $client $LFS mkdir -i$mdt_index -c$dir_stripe_count "$dir" &&
515                 ha_on $client $LFS getdirstripe "$dir" &&
516                 ha_on $client $LFS setstripe $stripeparams $dir &&
517                 ha_on $client $LFS getstripe $dir &&
518                 ha_on $client chmod a+xwr $dir &&
519                 ha_on $client "su $mpiuser sh -c \" $mpirun $mpirunoptions \
520                         -np $((${#ha_clients[@]} * mpi_threads_per_client / ha_nclientsset)) \
521                         $machines $cmd \" " || rc=$?
522                 [[ -n "$ha_postcmd" ]] && ha_info "$ha_postcmd" &&
523                         ha_on $client "$ha_postcmd" >>"$log" 2>&1
524                 (( ((rc == 0)) && (( mustpass != 0 )) )) ||
525                 (( ((rc != 0)) && (( mustpass == 0 )) )) &&
526                         ha_on $client rm -rf "$dir";
527                 } >>"$log" 2>&1 || rc=$?
528
529                 ha_info $client: rc=$rc mustpass=$mustpass
530
531                 # mustpass=0 means that failure is expected
532                 if (( rc !=0 )); then
533                         if (( mustpass != 0 )); then
534                                 ha_touch stop,fail $client,$tag
535                                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
536                         else
537                                 # Ok to fail
538                                 rc=0
539                         fi
540                 elif (( mustpass == 0 )); then
541                         ha_touch stop,fail $client,$tag
542                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
543                 fi
544                 echo rc=$rc mustpass=$mustpass >"$status"
545
546                 nr_loops=$((nr_loops + 1))
547         done
548
549         [ $nr_loops -ne 0 ] &&
550                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
551
552         ha_info "$tag stopped: rc=$rc mustpass=$mustpass \
553                 avg loop time $avg_loop_time"
554 }
555
556 ha_start_mpi_loads()
557 {
558         local client
559         local load
560         local tag
561         local status
562         local n
563         local nparam
564         local machines
565         local m
566         local -a mach
567         local mpiuser
568         local nmpi
569
570         # ha_mpi_instances defines the number of
571         # clients start mpi loads; should be <= ${#ha_clients[@]}
572         # do nothing if
573         #    ha_mpi_instances = 0
574         # or
575         #    ${#ha_mpi_load_tags[@]} =0
576         local inst=$ha_mpi_instances
577         (( inst == 0 )) || (( ${#ha_mpi_load_tags[@]} == 0 )) &&
578                 ha_info "no mpi load to start" &&
579                 return 0
580
581         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
582
583         # Define names for machinefiles for each client set
584         for (( n=0; n < $ha_nclientsset; n++ )); do
585                 mach[$n]=$ha_machine_file$n
586         done
587
588         for ((n = 0; n < ${#ha_clients[@]}; n++)); do
589                 m=$(( n % ha_nclientsset))
590                 machines=${mach[m]}
591                 ha_info machine_file=$machines
592                 echo ${ha_clients[n]} >> $machines
593         done
594         local dirname=$(dirname $ha_machine_file)
595         for client in ${ha_clients[@]}; do
596                 ha_on $client mkdir -p $dirname
597                 scp $ha_machine_file* $client:$dirname
598         done
599
600         local ndir
601         for ((n = 0; n < $inst; n++)); do
602                 client=${ha_clients[n]}
603                 nmpi=$((n % ${#ha_users[@]}))
604                 mpiuser=${ha_users[nmpi]}
605                 ndir=$((n % ${#ha_testdirs[@]}))
606                 test_dir=${ha_testdirs[ndir]}
607                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
608                         tag=${ha_mpi_load_tags[$load]}
609                         status=$ha_status_file_prefix-$tag-$client
610                         # ha_nparams_ior
611                         # ha_nparams_simul
612                         local num=ha_nparams_$tag
613                         nparam=$((n % num))
614                         local aref=ha_params_$tag[nparam]
615                         local parameter=${!aref}
616                         local nstripe=$((n % ha_nstripe_clients))
617                         aref=ha_stripe_clients[nstripe]
618                         local stripe=${!aref}
619                         local m=$(( n % ha_nclientsset))
620                         machines=${mach[m]}
621                         local mustpass=1
622                         [[ $ha_ninstmustfail == 0 ]] ||
623                                 mustpass=$(( n % ha_ninstmustfail ))
624                         ha_repeat_mpi_load $client $load $status "$parameter" \
625                                 $machines "$stripe" "$mpiuser" "$mustpass" \
626                                 "${ha_mpiopts[$mpiuser]} $ha_mpirun_options" "$test_dir" &
627                                 ha_status_files+=("$status")
628                 done
629         done
630 }
631
632 ha_repeat_nonmpi_load()
633 {
634         local client=$1
635         local load=$2
636         local status=$3
637         local tag=${ha_nonmpi_load_tags[$load]}
638         local cmd=${ha_nonmpi_load_cmds[$tag]}
639         local test_dir=$4
640         local dir=$test_dir/$client-$tag
641         local log=$ha_tmp_dir/$client-$tag
642         local rc=0
643         local nr_loops=0
644         local avg_loop_time=0
645         local start_time=$(date +%s)
646
647         cmd=${cmd//"{}"/$dir}
648
649         ha_info "Starting $tag on $client on $dir"
650
651         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
652                 ha_info "$client Starts: $cmd" 2>&1 |  tee -a $log
653                 ha_on $client "mkdir -p $dir &&                              \
654                         $cmd &&                                              \
655                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
656
657                 if ((rc != 0)); then
658                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
659                         ha_touch stop,fail $client,$tag
660                 fi
661                 echo $rc >"$status"
662
663                 nr_loops=$((nr_loops + 1))
664         done
665
666         [ $nr_loops -ne 0 ] &&
667                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
668
669         ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
670 }
671
672 ha_start_nonmpi_loads()
673 {
674         local client
675         local load
676         local tag
677         local status
678         local n
679         local test_dir
680         local ndir
681
682         for (( n = 0; n < ${#ha_clients[@]}; n++)); do
683                 client=${ha_clients[n]}
684                 ndir=$((n % ${#ha_testdirs[@]}))
685                 test_dir=${ha_testdirs[ndir]}
686                 for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
687                         tag=${ha_nonmpi_load_tags[$load]}
688                         status=$ha_status_file_prefix-$tag-$client
689                         ha_repeat_nonmpi_load $client $load $status $test_dir &
690                         ha_status_files+=("$status")
691                 done
692         done
693 }
694
695 declare ha_bgcmd=${ha_bgcmd:-""}
696 declare ha_bgcmd_log=$ha_tmp_dir/bgcmdlog
697
698 ha_cmd_bg () {
699         [[ -z "$ha_bgcmd" ]] && return 0
700         for ((i=0; i<${#ha_testdirs[@]}; i++)); do
701                 ha_bgcmd=${ha_bgcmd//"{}"/${ha_testdirs[i]}}
702         done
703
704         ha_info "BG cmd: $ha_bgcmd"
705         while [ true ]; do
706                 [ -f $ha_stop_file ] &&
707                         ha_info "$ha_stop_file found! $ha_bgcmd no started" &&
708                         break
709                 eval $ha_bgcmd 2>&1 | tee -a $ha_bgcmd_log
710                 sleep 1
711         done &
712         CMD_BG_PID=$!
713         ha_info CMD BG PID: $CMD_BG_PID
714         ps aux | grep $CMD_BG_PID
715 }
716
717 ha_lfsck_bg () {
718         rm -f $ha_lfsck_log
719         rm -f $ha_lfsck_stop
720
721         ha_info "LFSCK BG"
722         while [ true ]; do
723                 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
724                 [ -f $ha_stop_file ] &&
725                         ha_info "$ha_stop_file found! LFSCK not started" &&
726                         break
727                 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
728                 sleep 1
729         done &
730         LFSCK_BG_PID=$!
731         ha_info LFSCK BG PID: $LFSCK_BG_PID
732 }
733
734 ha_wait_lfsck_completed () {
735         local -a status
736         local -a types=($ha_lfsck_types)
737         local type
738         local s
739
740         local nodes="${ha_servers[@]}"
741         nodes=${nodes// /,}
742
743         # -A start LFSCK on all nodes
744         # -t default all
745         [ ${#types[@]} -eq 0 ] && types=(namespace layout)
746         ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
747         for type in ${types[@]}; do
748                 eval var_$type=0
749                 for (( i=0; i<=ha_lfsck_wait; i++)); do
750                         status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
751                                 awk '/status/ { print $3 }'))
752                         for (( s=0; s<${#status[@]}; s++ )); do
753                                 # "partial" is expected after HARD failover
754                                 [[ "${status[s]}" = "completed" ]] ||
755                                 [[ "${status[s]}" = "partial" ]] ||  break
756                         done
757                         [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
758                         sleep 1
759                 done
760                 ha_info "LFSCK $type status in $i sec:"
761                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
762
763         done
764
765         for type in ${types[@]}; do
766                 local var=var_$type
767                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
768                 [[ ${!var} -eq 1 ]] ||
769                         { ha_info "lfsck not completed in $ha_lfsck_wait sec";
770                         return 1; }
771         done
772         return 0
773 }
774
775 ha_start_lfsck()
776 {
777         local -a types=($ha_lfsck_types)
778         local rc=0
779
780         # -A: start LFSCK on all nodes via the specified MDT device
781         # (see "-M" option) by single LFSCK command
782         local params=" -A -r $ha_lfsck_custom_params"
783
784         # use specified device if set
785         [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
786
787         # -t: check type(s) to be performed (default all)
788         # check only specified types if set
789         if [ ${#types[@]} -ne 0 ]; then
790                 local type="${types[@]}"
791                 params="$params -t ${type// /,}"
792         fi
793
794         ha_info "LFSCK start $params"
795         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
796         if [ $rc -ne 0 ]; then
797                 if [ -e $ha_lfsck_lock ]; then
798                         rc=0
799                         ha_wait_unlock $ha_lfsck_lock
800                         ha_sleep 120
801                         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
802                 fi
803         fi
804
805         [ $rc -eq 0 ] ||
806                 { ha_touch stop,fail,lfsck; return 1; }
807
808         ha_wait_lfsck_completed ||
809                 { ha_touch stop,fail,lfsck; return 1; }
810
811         return 0
812 }
813
814 ha_lfsck_repaired()
815 {
816         local n=0
817
818         n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
819                 awk '{sum += $1} END { print sum }')
820         [ $n -eq 0] ||
821                 { ha_info "Total repaired: $n";
822                 ha_touch fail; return 1; }
823         return 0
824 }
825
826 ha_start_loads()
827 {
828         ha_cmd_bg
829         $ha_lfsck_bg && ha_lfsck_bg
830         trap ha_trap_stop_signals $ha_stop_signals
831         ha_start_nonmpi_loads
832         ha_start_mpi_loads
833 }
834
835 ha_stop_loads()
836 {
837         ha_touch stop
838         [[ -n $CMD_BG_PID ]] && wait $CMD_BG_PID || true
839         # true because of lfsck_bg could be stopped already
840         $ha_lfsck_bg && wait $LFSCK_BG_PID || true
841         trap - $ha_stop_signals
842         ha_info "Waiting for workloads to stop"
843         wait
844 }
845
846 ha_wait_loads()
847 {
848     local file
849     local end=$(($(date +%s) + ha_load_timeout))
850
851     ha_info "Waiting $ha_load_timeout sec for workload status..."
852     rm -f "${ha_status_files[@]}"
853
854         #
855         # return immediately if ha_stop_file exists,
856         # all status_files not needed to be checked
857         #
858         for file in "${ha_status_files[@]}"; do
859                 if [ -e "$ha_stop_file" ]; then
860                         ha_info "$ha_stop_file found! Stop."
861                         break
862                 fi
863                 #
864                 # Wait status file created during ha_load_timeout.
865                 # Existing file guarantees that some application
866                 # is completed. If no status file was created
867                 # this function guarantees that we allow
868                 # applications to continue after/before
869                 # failover/failback during ha_load_timeout time.
870                 #
871                 until [ -e "$file" ] || (($(date +%s) >= end)); do
872                         #
873                         # check ha_stop_file again, it could appear
874                         # during ha_load_timeout
875                         #
876                         if [ -e "$ha_stop_file" ]; then
877                                 ha_info "$ha_stop_file found! Stop."
878                                 break
879                         fi
880                         ha_sleep 1 >/dev/null
881                 done
882         done
883 }
884
885 ha_powermanage()
886 {
887         local nodes=$1
888         local expected_state=$2
889         local state
890         local -a states
891         local i
892         local rc=0
893
894         # store pm -x -q $nodes results in a file to have
895         # more information about nodes statuses
896         ha_on $ha_pm_host pm -x -q $nodes | awk '{print $2 $3}' > $ha_pm_states
897         rc=${PIPESTATUS[0]}
898         echo pmrc=$rc
899
900         while IFS=": " read node state; do
901                 [[ "$state" = "$expected_state" ]] && {
902                         nodes=${nodes/$node/}
903                         nodes=${nodes//,,/,}
904                         nodes=${nodes/#,}
905                         nodes=${nodes/%,}
906                 }
907         done < $ha_pm_states
908
909         if [ -n "$nodes" ]; then
910                 cat $ha_pm_states
911                 return 1
912         fi
913         return 0
914 }
915
916 ha_power_down_cmd_fn()
917 {
918         local nodes=$1
919         local cmd
920         local pid
921         local rc=0
922
923         case $ha_power_down_cmd in
924         # format is: POWER_DOWN=sysrqcrash
925         sysrqcrash)
926                 cmd="pdsh -S -w $nodes -u 120 \"echo c > /proc/sysrq-trigger\" &"
927                 eval $cmd
928                 pid=$!
929                 ha_power_down_pids=$(echo $ha_power_down_pids $pid)
930                 ha_info "ha_power_down_pids: $ha_power_down_pids"
931                 [[ -z "$ha_power_down_pids" ]] ||
932                         ps aux | grep " ${ha_power_down_pids// / \| } " ||
933                         true
934                 ;;
935         *)
936                 cmd="$ha_power_down_cmd $nodes"
937                 eval $cmd
938                 rc=$?
939                 ;;
940         esac
941
942         return $rc
943 }
944
945 ha_power_down()
946 {
947         local nodes=$1
948         local rc=1
949         local i
950         local state
951
952         case $ha_power_down_cmd in
953                 *pm*) state=off ;;
954                 sysrqcrash) state=off ;;
955                 *) state=on;;
956         esac
957
958         if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
959                 ha_info "$ha_lfsck_node down, delay start LFSCK"
960                 ha_lock $ha_lfsck_lock
961         fi
962
963         ha_info "Powering down $nodes : cmd: $ha_power_down_cmd"
964         ha_power_down_pids=""
965         for (( i=0; i<10; i++ )) {
966                 ha_info "attempt: $i"
967                 ha_power_down_cmd_fn $nodes || rc=1
968                 ha_sleep $ha_power_delay
969                 ha_powermanage $nodes $state && rc=0 && break
970         }
971         if [[ -n "$ha_power_down_pids" ]]; then
972                 kill -9 $ha_power_down_pids ||  true
973                 wait $ha_power_down_pids || true
974         fi
975
976         [ $rc -eq 0 ] || {
977                 ha_info "Failed Powering down in $i attempts:" \
978                         "$ha_power_down_cmd"
979                 cat $ha_pm_states
980                 exit 1
981         }
982 }
983
984 ha_get_pair()
985 {
986         local node=$1
987         local i
988
989         for ((i=0; i<${#ha_victims[@]}; i++)) {
990                 [[ ${ha_victims[i]} == $node ]] && echo ${ha_victims_pair[i]} &&
991                         return
992         }
993         [[ $i -ne ${#ha_victims[@]} ]] ||
994                 ha_error "No pair found!"
995 }
996
997 ha_power_up_delay()
998 {
999         local nodes=$1
1000         local end=$(($(date +%s) + ha_node_up_delay))
1001         local rc
1002
1003         if [[ ${#ha_victims_pair[@]} -eq 0 ]]; then
1004                 ha_sleep $ha_node_up_delay
1005                 return 0
1006         fi
1007
1008         # Check CRM status on failover pair
1009         while (($(date +%s) <= end)); do
1010                 rc=0
1011                 for n in ${nodes//,/ }; do
1012                         local pair=$(ha_get_pair $n)
1013                         local status=$(ha_on $pair crm_mon -1rQ | \
1014                                 grep -w $n | head -1)
1015
1016                         ha_info "$n pair: $pair status: $status"
1017                         [[ "$status" == *OFFLINE* ]] ||
1018                                 rc=$((rc + $?))
1019                         ha_info "rc: $rc"
1020                 done
1021
1022                 if [[ $rc -eq 0 ]];  then
1023                         ha_info "CRM: Got all victims status OFFLINE"
1024                         return 0
1025                 fi
1026                 sleep 60
1027         done
1028
1029         ha_info "$nodes CRM status not OFFLINE"
1030         for n in ${nodes//,/ }; do
1031                 local pair=$(ha_get_pair $n)
1032
1033                 ha_info "CRM --- $n"
1034                 ha_on $pair crm_mon -1rQ
1035         done
1036         ha_error "CRM: some of $nodes are not OFFLINE in $ha_node_up_delay sec"
1037         exit 1
1038 }
1039
1040 ha_power_up()
1041 {
1042         local nodes=$1
1043         local rc=1
1044         local i
1045
1046         ha_power_up_delay $nodes
1047         ha_info "Powering up $nodes : cmd: $ha_power_up_cmd"
1048         for (( i=0; i<10; i++ )) {
1049                 ha_info "attempt: $i"
1050                 $ha_power_up_cmd $nodes &&
1051                         ha_powermanage $nodes on && rc=0 && break
1052                 sleep $ha_power_delay
1053         }
1054
1055         [ $rc -eq 0 ] || {
1056                 ha_info "Failed Powering up in $i attempts: $ha_power_up_cmd"
1057                 cat $ha_pm_states
1058                 exit 1
1059         }
1060 }
1061
1062 #
1063 # rand MAX
1064 #
1065 # Print a random integer within [0, MAX).
1066 #
1067 ha_rand()
1068 {
1069     local max=$1
1070
1071     #
1072     # See "5.2 Bash Variables" from "info bash".
1073     #
1074     echo -n $((RANDOM * max / 32768))
1075 }
1076
1077 ha_aim()
1078 {
1079         local i
1080         local nodes
1081
1082         if $ha_simultaneous ; then
1083                 nodes=$(echo ${ha_victims[@]})
1084                 nodes=${nodes// /,}
1085         else
1086                 i=$(ha_rand ${#ha_victims[@]})
1087                 nodes=${ha_victims[$i]}
1088         fi
1089
1090         echo -n $nodes
1091 }
1092
1093 ha_wait_nodes()
1094 {
1095         local nodes=$1
1096         local end=$(($(date +%s) + $ha_wait_nodes_up))
1097
1098         ha_info "Waiting for $nodes to boot up in $ha_wait_nodes_up"
1099         until ha_on $nodes hostname >/dev/null 2>&1 ||
1100                 [ -e "$ha_stop_file" ] ||
1101                         (($(date +%s) >= end)); do
1102                 ha_sleep 1 >/dev/null
1103         done
1104
1105         ha_info "Check where we are ..."
1106         [ -e "$ha_stop_file" ] &&
1107                 ha_info "$ha_stop_file found!"
1108
1109         local -a nodes_up
1110         nodes_up=($(ha_on $nodes hostname | awk '{ print $2 }'))
1111         ha_info "Nodes $nodes are up: ${nodes_up[@]}"
1112         local -a n=(${nodes//,/ })
1113         if [[ ${#nodes_up[@]} -ne ${#n[@]} ]]; then
1114                 ha_info "Failed boot up $nodes in $ha_wait_nodes_up sec!"
1115                 ha_touch fail,stop
1116                 return 1
1117         fi
1118         return 0
1119 }
1120
1121 ha_failback()
1122 {
1123         local nodes=$1
1124         ha_info "Failback resources on $nodes in $ha_failback_delay sec"
1125
1126         ha_sleep $ha_failback_delay
1127         [ "$ha_failback_cmd" ] ||
1128         {
1129                 ha_info "No failback command set, skiping"
1130                 return 0
1131         }
1132
1133         $ha_failback_cmd $nodes
1134         [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
1135 }
1136
1137 ha_summarize()
1138 {
1139     ha_info "---------------8<---------------"
1140     ha_info "Summary:"
1141     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
1142     ha_info "    Loops: $ha_nr_loops"
1143 }
1144
1145 ha_killer()
1146 {
1147         local nodes
1148
1149         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
1150                         [ ! -e "$ha_stop_file" ]; do
1151                 ha_info "---------------8<---------------"
1152
1153                 $ha_workloads_only || nodes=$(ha_aim)
1154
1155                 ha_info "Failing $nodes"
1156                 $ha_workloads_only && ha_info "    is skipped: workload only..."
1157
1158                 ha_sleep $(ha_rand $ha_max_failover_period)
1159                 $ha_workloads_only || ha_power_down $nodes
1160                 ha_sleep 10
1161                 ha_wait_loads || return
1162
1163                 if [ -e $ha_stop_file ]; then
1164                         $ha_workloads_only || ha_power_up $nodes
1165                         break
1166                 fi
1167
1168                 ha_info "Bringing $nodes back"
1169                 ha_sleep $(ha_rand 10)
1170                 $ha_workloads_only ||
1171                 {
1172                         ha_power_up $nodes
1173                         ha_wait_nodes $nodes
1174                         ha_failback $nodes
1175                 }
1176
1177                 #
1178                 # Wait for the failback to start.
1179                 #
1180                 ha_sleep 60
1181                 ha_wait_loads || return
1182
1183                 ha_sleep $(ha_rand 20)
1184
1185                 ha_nr_loops=$((ha_nr_loops + 1))
1186                 ha_info "Loop $ha_nr_loops done"
1187         done
1188         ha_summarize
1189 }
1190
1191 ha_main()
1192 {
1193         ha_process_arguments "$@"
1194         ha_check_env
1195
1196         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1197                 "START: $0: $(date +%H:%M:%S' '%s)"
1198         trap ha_trap_exit EXIT
1199         mkdir "$ha_tmp_dir"
1200
1201         local mdt_index
1202         if $ha_test_dir_mdt_index_random &&
1203                 [ $ha_test_dir_mdt_index -ne 0 ]; then
1204                 mdt_index=$(ha_rand $((ha_test_dir_mdt_index + 1)) )
1205         else
1206                 mdt_index=$ha_test_dir_mdt_index
1207         fi
1208
1209         local dir
1210         test_dir=${ha_testdirs[0]}
1211         ha_on ${ha_clients[0]} "$LFS mkdir -i$mdt_index \
1212                 -c$ha_test_dir_stripe_count $test_dir"
1213         for ((i=0; i<${#ha_testdirs[@]}; i++)); do
1214                 test_dir=${ha_testdirs[i]}
1215                 ha_on ${ha_clients[0]} $LFS getdirstripe $test_dir
1216                 ha_on ${ha_clients[0]} " \
1217                         $LFS setstripe $ha_stripe_params $test_dir"
1218         done
1219
1220         ha_start_loads
1221         ha_wait_loads
1222
1223         if $ha_workloads_dry_run; then
1224                 ha_sleep 5
1225         else
1226                 ha_killer
1227                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
1228         fi
1229
1230         ha_stop_loads
1231
1232         $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
1233
1234         $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
1235
1236         if [ -e "$ha_fail_file" ]; then
1237                 exit 1
1238         else
1239                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1240                         "END: $0: $(date +%H:%M:%S' '%s)"
1241                 exit 0
1242         fi
1243 }
1244
1245 ha_main "$@"