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