2 # -*- mode: Bash; tab-width: 4; indent-tabs-mode: t; -*-
3 # vim:shiftwidth=4:softtabstop=4:tabstop=4:
7 # ha.sh - test Lustre HA (aka failover) configurations
15 # ha.sh tests Lustre HA (aka failover) configurations with a CRM.
23 # Specify client nodes.
26 # Specify server nodes.
29 # Specify victim nodes to be rebooted.
32 # Choose a parent of the test directory. "/mnt/lustre" if not specified.
35 # Define a duration for the test. 86400 seconds if not specified.
38 # Define a max failover period. 10 minutes if not set.
41 # Only run the workloads; no failure will be introduced.
42 # -v, -s are ignored in this case.
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
48 # Reboot victim nodes simultaneously.
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
57 # Each target has a failnode, so that workloads can continue after a power
60 # CRM could be configured by 2 ways:
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.
66 # Targets are not automatically failed back when their primary node is back.
67 # CRM-specific command is executed to trigger failbacks.
69 # A crash dump mechanism is configured to catch LBUGs, panics, etc.
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:
78 # default set: dd, tar, iozone
80 # default set: ior, simul, mdtest
82 # The number of clients run MPI loads is configured by parameter
83 # ha_mpi_instances. Only one client runs MPI workloads by default.
85 # PROCESS STRUCTURE AND IPC
87 # On the node where this script is run, the processes look like this:
91 # ~ ha.sh (ha_repeat_mpi_load ior)
93 # ~ ha.sh (ha_repeat_mpi_load simul)
95 # ~ ha.sh (ha_repeat_mpi_load mdtest)
97 # ~ ... (one for each MPI load)
99 # ~ ha.sh (ha_repeat_nonmpi_load client2 dbench)
100 # ~ pdsh client2 dbench
101 # ~ ha.sh (ha_repeat_nonmpi_load client2 iozone)
102 # ~ pdsh client2 iozone
103 # ~ ha.sh (ha_repeat_nonmpi_load client5 iozone)
104 # ~ pdsh client5 iozone
105 # ~ ... (one for each non-MPI load on each client)
107 # Each tilde represents a process. Indentations imply parent-children
110 # IPC is done by files in the temporary directory.
115 SIMUL=${SIMUL:-$(which simul 2> /dev/null || true)}
116 IOR=${IOR:-$(which IOR 2> /dev/null || true)}
117 MDTEST=${MDTEST:-$(which mdtest 2> /dev/null || true)}
119 ior_blockSize=${ior_blockSize:-6g}
120 mpi_threads_per_client=${mpi_threads_per_client:-2}
122 iozone_SIZE=${iozone_SIZE:-262144} # 256m
124 mpirun=${MPIRUN:-$(which mpirun)}
125 LFS=${LFS:-$(which lfs)}
129 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
130 local tag=${ha_mpi_load_tags[$load]}
131 local bin=$(echo $tag | tr '[:lower:]' '[:upper:]')
132 if [ x${!bin} = x ]; then
133 ha_error ha_mpi_loads: ${ha_mpi_loads}, $bin is not set
141 echo "$0: $(date +%H:%M:%S' '%s):" "$@"
146 local nodes=${1// /,}
148 ha_on $nodes "lctl mark $*"
160 ha_error "Trap ERR triggered by:"
161 ha_error " $BASH_COMMAND"
162 ha_error "Call trace:"
163 for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
164 ha_error " ${FUNCNAME[$i]} [${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}]"
171 declare ha_tmp_dir=/tmp/$(basename $0)-$$
172 declare ha_stop_file=$ha_tmp_dir/stop
173 declare ha_fail_file=$ha_tmp_dir/fail
174 declare ha_status_file_prefix=$ha_tmp_dir/status
175 declare -a ha_status_files
176 declare ha_machine_file=$ha_tmp_dir/machine_file
177 declare ha_lfsck_log=$ha_tmp_dir/lfsck.log
178 declare ha_lfsck_lock=$ha_tmp_dir/lfsck.lock
179 declare ha_lfsck_stop=$ha_tmp_dir/lfsck.stop
180 declare ha_lfsck_bg=${LFSCK_BG:-false}
181 declare ha_lfsck_after=${LFSCK_AFTER:-false}
182 declare ha_lfsck_node=${LFSCK_NODE:-""}
183 declare ha_lfsck_device=${LFSCK_DEV:-""}
184 declare ha_lfsck_types=${LFSCK_TYPES:-"namespace layout"}
185 declare ha_lfsck_custom_params=${LFSCK_CUSTOM_PARAMS:-""}
186 declare ha_lfsck_wait=${LFSCK_WAIT:-1200}
187 declare ha_lfsck_fail_on_repaired=${LFSCK_FAIL_ON_REPAIRED:-false}
188 declare ha_power_down_cmd=${POWER_DOWN:-"pm -0"}
189 declare ha_power_up_cmd=${POWER_UP:-"pm -1"}
190 declare ha_power_delay=${POWER_DELAY:-60}
191 declare ha_failback_delay=${DELAY:-5}
192 declare ha_failback_cmd=${FAILBACK:-""}
193 declare ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
194 declare ha_dir_stripe_count=${DSTRIPECOUNT:-"1"}
195 declare ha_mdt_index=${MDTINDEX:-"0"}
196 declare ha_mdt_index_random=${MDTINDEXRAND:-false}
197 declare -a ha_clients
198 declare -a ha_servers
199 declare -a ha_victims
200 declare ha_test_dir=/mnt/lustre/$(basename $0)-$$
201 declare ha_start_time=$(date +%s)
202 declare ha_expected_duration=$((60 * 60 * 24))
203 declare ha_max_failover_period=10
204 declare ha_nr_loops=0
205 declare ha_stop_signals="SIGINT SIGTERM SIGHUP"
206 declare ha_load_timeout=$((60 * 10))
207 declare ha_workloads_only=false
208 declare ha_workloads_dry_run=false
209 declare ha_simultaneous=false
211 declare ha_mpi_instances=${ha_mpi_instances:-1}
213 declare ha_mpi_loads=${ha_mpi_loads="ior simul mdtest"}
214 declare -a ha_mpi_load_tags=($ha_mpi_loads)
216 declare ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
217 declare ha_simul_params=${SIMULP:-'" -n 10"'}
218 declare ha_mdtest_params=${MDTESTP:-'" -i 1 -n 1000"'}
219 declare ha_mpirun_options=${MPIRUN_OPTIONS:-""}
220 declare ha_clients_stripe=${CLIENTSSTRIPE:-'"$STRIPEPARAMS"'}
221 declare ha_nclientsset=${NCLIENTSSET:-1}
223 declare ha_racer_params=${RACERP:-"MDSCOUNT=1"}
225 eval ha_params_ior=($ha_ior_params)
226 eval ha_params_simul=($ha_simul_params)
227 eval ha_params_mdtest=($ha_mdtest_params)
228 eval ha_stripe_clients=($ha_clients_stripe)
230 declare ha_nparams_ior=${#ha_params_ior[@]}
231 declare ha_nparams_simul=${#ha_params_simul[@]}
232 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
233 declare ha_nstripe_clients=${#ha_stripe_clients[@]}
235 declare -A ha_mpi_load_cmds=(
236 [ior]="$IOR -o {}/f.ior {params}"
237 [simul]="$SIMUL {params} -d {}"
238 [mdtest]="$MDTEST {params} -d {}"
241 declare racer=${RACER:-"$(dirname $0)/racer/racer.sh"}
243 declare ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
244 declare -a ha_nonmpi_load_tags=($ha_nonmpi_loads)
245 declare -A ha_nonmpi_load_cmds=(
246 [dd]="dd if=/dev/zero of={}/f.dd bs=1M count=256"
247 [tar]="tar cf - /etc | tar xf - -C {}"
248 [iozone]="iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
249 [racer]="$ha_racer_params $racer {}"
254 ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]" \
255 "-v HOST[,...] [-d DIRECTORY] [-u SECONDS]"
258 ha_process_arguments()
262 while getopts hc:s:v:d:p:u:wrm opt; do
269 ha_clients=(${OPTARG//,/ })
272 ha_servers=(${OPTARG//,/ })
275 ha_victims=(${OPTARG//,/ })
278 ha_test_dir=$OPTARG/$(basename $0)-$$
281 ha_expected_duration=$OPTARG
284 ha_max_failover_period=$OPTARG
287 ha_workloads_only=true
290 ha_workloads_dry_run=true
302 if [ -z "${ha_clients[*]}" ]; then
303 ha_error "-c is mandatory"
307 if ! ($ha_workloads_dry_run ||
308 $ha_workloads_only) &&
309 ([ -z "${ha_servers[*]}" ] ||
310 [ -z "${ha_victims[*]}" ]); then
311 ha_error "-s, and -v are all mandatory"
325 # -S is to be used here to track the
326 # remote command return values
328 pdsh -S -w $nodes PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
329 /bin:/usr/sbin:/usr/bin "$@" ||
336 touch "$ha_stop_file"
338 if [ -e "$ha_fail_file" ]; then
339 ha_info "Test directory $ha_test_dir not removed"
340 ha_info "Temporary directory $ha_tmp_dir not removed"
342 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
343 ha_info "Please find the results in the directory $ha_tmp_dir"
347 ha_trap_stop_signals()
349 ha_info "${ha_stop_signals// /,} received"
350 touch "$ha_stop_file"
357 ha_info "Sleeping for ${n}s"
359 # sleep(1) could interrupted.
368 while [ -e $lock ]; do
377 until mkdir "$lock" >/dev/null 2>&1; do
378 ha_sleep 1 >/dev/null
391 local nodes=${1// /,}
392 local file=/tmp/$(basename $0)-$$-$(date +%s).dk
393 local lock=$ha_tmp_dir/lock-dump-logs
397 ha_info "Dumping lctl log to $file"
400 # some nodes could crash, so
401 # do not exit with error if not all logs are dumped
403 ha_on $nodes "lctl dk >>$file" || rc=$?
406 ha_error "not all logs are dumped! Some nodes are unreachable."
417 local stripeparams=$6
418 local tag=${ha_mpi_load_tags[$load]}
419 local cmd=${ha_mpi_load_cmds[$tag]}
420 local dir=$ha_test_dir/$client-$tag
421 local log=$ha_tmp_dir/$client-$tag
424 local avg_loop_time=0
425 local start_time=$(date +%s)
427 cmd=${cmd//"{}"/$dir}
428 cmd=${cmd//"{params}"/$parameter}
430 ha_info "Starting $tag"
432 machines="-machinefile $machines"
433 while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
436 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
437 mdt_index=$(ha_rand $ha_mdt_index)
439 mdt_index=$ha_mdt_index
441 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
442 ha_on $client $LFS getdirstripe "$dir" &&
443 ha_on $client $LFS setstripe $stripeparams $dir &&
444 ha_on $client $LFS getstripe $dir &&
445 ha_on $client chmod a+xwr $dir &&
446 ha_on $client "su mpiuser sh -c \" $mpirun $ha_mpirun_options \
447 -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
448 $machines $cmd \" " &&
449 ha_on $client rm -rf "$dir";
450 } >>"$log" 2>&1 || rc=$?
455 touch "$ha_fail_file"
456 touch "$ha_stop_file"
457 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
461 nr_loops=$((nr_loops + 1))
464 [ $nr_loops -ne 0 ] &&
465 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
467 ha_info "$tag stopped: rc $rc avg loop time $avg_loop_time"
482 # ha_mpi_instances defines the number of
483 # clients start mpi loads; should be <= ${#ha_clients[@]}
485 # ha_mpi_instances = 0
487 # ${#ha_mpi_load_tags[@]} =0
488 local inst=$ha_mpi_instances
489 (( inst == 0 )) || (( ${#ha_mpi_load_tags[@]} == 0 )) &&
490 ha_info "no mpi load to start" &&
493 (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
495 # Define names for machinefiles for each client set
496 for (( n=0; n < $ha_nclientsset; n++ )); do
497 mach[$n]=$ha_machine_file$n
500 for ((n = 0; n < ${#ha_clients[@]}; n++)); do
501 m=$(( n % ha_nclientsset))
503 ha_info machine_file=$machines
504 echo ${ha_clients[n]} >> $machines
506 local dirname=$(dirname $ha_machine_file)
507 for client in ${ha_clients[@]}; do
508 ha_on $client mkdir -p $dirname
509 scp $ha_machine_file* $client:$dirname
512 for ((n = 0; n < $inst; n++)); do
513 client=${ha_clients[n]}
514 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
515 tag=${ha_mpi_load_tags[$load]}
516 status=$ha_status_file_prefix-$tag-$client
519 local num=ha_nparams_$tag
521 local aref=ha_params_$tag[nparam]
522 local parameter=${!aref}
523 local nstripe=$((n % ha_nstripe_clients))
524 aref=ha_stripe_clients[nstripe]
525 local stripe=${!aref}
526 local m=$(( n % ha_nclientsset))
528 ha_repeat_mpi_load $client $load $status "$parameter" $machines "$stripe" &
529 ha_status_files+=("$status")
534 ha_repeat_nonmpi_load()
539 local tag=${ha_nonmpi_load_tags[$load]}
540 local cmd=${ha_nonmpi_load_cmds[$tag]}
541 local dir=$ha_test_dir/$client-$tag
542 local log=$ha_tmp_dir/$client-$tag
545 local avg_loop_time=0
546 local start_time=$(date +%s)
548 cmd=${cmd//"{}"/$dir}
550 ha_info "Starting $tag on $client"
552 while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
553 ha_on $client "mkdir -p $dir && \
555 rm -rf $dir" >>"$log" 2>&1 || rc=$?
558 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
559 touch "$ha_fail_file"
560 touch "$ha_stop_file"
564 nr_loops=$((nr_loops + 1))
567 [ $nr_loops -ne 0 ] &&
568 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
570 ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
573 ha_start_nonmpi_loads()
580 for client in ${ha_clients[@]}; do
581 for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
582 tag=${ha_nonmpi_load_tags[$load]}
583 status=$ha_status_file_prefix-$tag-$client
584 ha_repeat_nonmpi_load $client $load $status &
585 ha_status_files+=("$status")
596 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
597 [ -f $ha_stop_file ] &&
598 ha_info "$ha_stop_file found! LFSCK not started" &&
600 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
604 ha_info LFSCK BG PID: $LFSCK_BG_PID
607 ha_wait_lfsck_completed () {
609 local -a types=($ha_lfsck_types)
613 local nodes="${ha_servers[@]}"
616 # -A start LFSCK on all nodes
618 [ ${#types[@]} -eq 0 ] && types=(namespace layout)
619 ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
620 for type in ${types[@]}; do
622 for (( i=0; i<=ha_lfsck_wait; i++)); do
623 status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
624 awk '/status/ { print $3 }'))
625 for (( s=0; s<${#status[@]}; s++ )); do
626 # "partial" is expected after HARD failover
627 [[ "${status[s]}" = "completed" ]] ||
628 [[ "${status[s]}" = "partial" ]] || break
630 [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
633 ha_info "LFSCK $type status in $i sec:"
634 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
638 for type in ${types[@]}; do
640 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
641 [[ ${!var} -eq 1 ]] ||
642 { ha_info "lfsck not completed in $ha_lfsck_wait sec";
650 local -a types=($ha_lfsck_types)
653 # -A: start LFSCK on all nodes via the specified MDT device
654 # (see "-M" option) by single LFSCK command
655 local params=" -A -r $ha_lfsck_custom_params"
657 # use specified device if set
658 [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
660 # -t: check type(s) to be performed (default all)
661 # check only specified types if set
662 if [ ${#types[@]} -ne 0 ]; then
663 local type="${types[@]}"
664 params="$params -t ${type// /,}"
667 ha_info "LFSCK start $params"
668 ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
669 if [ $rc -ne 0 ]; then
670 if [ -e $ha_lfsck_lock ]; then
672 ha_wait_unlock $ha_lfsck_lock
674 ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
679 { touch "$ha_fail_file"; touch "$ha_stop_file";
680 touch $ha_lfsck_stop; return 1; }
682 ha_wait_lfsck_completed ||
683 { touch "$ha_fail_file"; touch "$ha_stop_file";
684 touch $ha_lfsck_stop; return 1; }
693 n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
694 awk '{sum += $1} END { print sum }')
696 { ha_info "Total repaired: $n";
697 touch "$ha_fail_file"; return 1; }
703 $ha_lfsck_bg && ha_lfsck_bg
704 trap ha_trap_stop_signals $ha_stop_signals
705 ha_start_nonmpi_loads
712 # true because of lfsck_bg could be stopped already
713 $ha_lfsck_bg && wait $LFSCK_BG_PID || true
714 trap - $ha_stop_signals
715 ha_info "Waiting for workloads to stop"
722 local end=$(($(date +%s) + ha_load_timeout))
724 ha_info "Waiting for workload status"
725 rm -f "${ha_status_files[@]}"
728 # return immediately if ha_stop_file exists,
729 # all status_files not needed to be checked
731 for file in "${ha_status_files[@]}"; do
732 if [ -e "$ha_stop_file" ]; then
733 ha_info "$ha_stop_file found! Stop."
737 # Wait status file created during ha_load_timeout.
738 # Existing file guarantees that some application
739 # is completed. If no status file was created
740 # this function guarantees that we allow
741 # applications to continue after/before
742 # failover/failback during ha_load_timeout time.
744 until [ -e "$file" ] || (($(date +%s) >= end)); do
746 # check ha_stop_file again, it could appear
747 # during ha_load_timeout
749 if [ -e "$ha_stop_file" ]; then
750 ha_info "$ha_stop_file found! Stop."
753 ha_sleep 1 >/dev/null
764 if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
765 ha_info "$ha_lfsck_node down, delay start LFSCK"
766 ha_lock $ha_lfsck_lock
769 ha_info "Powering down $nodes"
770 for i in $(seq 1 5); do
771 $ha_power_down_cmd $nodes && rc=0 && break
772 sleep $ha_power_delay
775 [ $rc -eq 0 ] || ha_info "Failed Powering down in $i attempts"
784 ha_info "Powering up $nodes"
785 for i in $(seq 1 5); do
786 $ha_power_up_cmd $nodes && rc=0 && break
787 sleep $ha_power_delay
790 [ $rc -eq 0 ] || ha_info "Failed Powering up in $i attempts"
796 # Print a random integer within [0, MAX).
803 # See "5.2 Bash Variables" from "info bash".
805 echo -n $((RANDOM * max / 32768))
813 if $ha_simultaneous ; then
814 nodes=$(echo ${ha_victims[@]})
817 i=$(ha_rand ${#ha_victims[@]})
818 nodes=${ha_victims[$i]}
827 local end=$(($(date +%s) + 10 * 60))
829 ha_info "Waiting for $nodes to boot up"
830 until ha_on $nodes hostname >/dev/null 2>&1 ||
831 [ -e "$ha_stop_file" ] ||
832 (($(date +%s) >= end)); do
833 ha_sleep 1 >/dev/null
840 ha_info "Failback resources on $nodes in $ha_failback_delay sec"
842 ha_sleep $ha_failback_delay
843 [ "$ha_failback_cmd" ] ||
845 ha_info "No failback command set, skiping"
849 $ha_failback_cmd $nodes
850 [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
855 ha_info "---------------8<---------------"
857 ha_info " Duration: $(($(date +%s) - $ha_start_time))s"
858 ha_info " Loops: $ha_nr_loops"
865 while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
866 [ ! -e "$ha_stop_file" ]; do
867 ha_info "---------------8<---------------"
869 $ha_workloads_only || nodes=$(ha_aim)
871 ha_info "Failing $nodes"
872 $ha_workloads_only && ha_info " is skipped: workload only..."
874 ha_sleep $(ha_rand $ha_max_failover_period)
875 $ha_workloads_only || ha_power_down $nodes
877 ha_wait_loads || return
879 if [ -e $ha_stop_file ]; then
880 $ha_workloads_only || ha_power_up $nodes
884 ha_info "Bringing $nodes back"
885 ha_sleep $(ha_rand 10)
886 $ha_workloads_only ||
894 # Wait for the failback to start.
897 ha_wait_loads || return
899 ha_sleep $(ha_rand 20)
901 ha_nr_loops=$((ha_nr_loops + 1))
902 ha_info "Loop $ha_nr_loops done"
909 ha_process_arguments "$@"
912 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
913 "START: $0: $(date +%H:%M:%S' '%s)"
914 trap ha_trap_exit EXIT
916 ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
917 ha_on ${ha_clients[0]} " \
918 $LFS setstripe $ha_stripe_params $ha_test_dir"
923 if $ha_workloads_dry_run; then
927 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
932 $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
934 $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
936 if [ -e "$ha_fail_file" ]; then
939 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
940 "END: $0: $(date +%H:%M:%S' '%s)"