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_power_down_cmd=${POWER_DOWN:-"pm -0"}
178 declare ha_power_up_cmd=${POWER_UP:-"pm -1"}
179 declare ha_power_delay=${POWER_DELAY:-60}
180 declare ha_failback_delay=${DELAY:-5}
181 declare ha_failback_cmd=${FAILBACK:-""}
182 declare ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
183 declare ha_dir_stripe_count=${DSTRIPECOUNT:-"1"}
184 declare ha_mdt_index=${MDTINDEX:-"0"}
185 declare ha_mdt_index_random=${MDTINDEXRAND:-false}
186 declare -a ha_clients
187 declare -a ha_servers
188 declare -a ha_victims
189 declare ha_test_dir=/mnt/lustre/$(basename $0)-$$
190 declare ha_start_time=$(date +%s)
191 declare ha_expected_duration=$((60 * 60 * 24))
192 declare ha_max_failover_period=10
193 declare ha_nr_loops=0
194 declare ha_stop_signals="SIGINT SIGTERM SIGHUP"
195 declare ha_load_timeout=$((60 * 10))
196 declare ha_workloads_only=false
197 declare ha_workloads_dry_run=false
198 declare ha_simultaneous=false
200 declare ha_mpi_instances=${ha_mpi_instances:-1}
202 declare ha_mpi_loads=${ha_mpi_loads="ior simul mdtest"}
203 declare -a ha_mpi_load_tags=($ha_mpi_loads)
205 declare ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
206 declare ha_simul_params=${SIMULP:-'" -n 10"'}
207 declare ha_mdtest_params=${MDTESTP:-'" -i 1 -n 1000"'}
208 declare ha_mpirun_options=${MPIRUN_OPTIONS:-""}
210 eval ha_params_ior=($ha_ior_params)
211 eval ha_params_simul=($ha_simul_params)
212 eval ha_params_mdtest=($ha_mdtest_params)
214 declare ha_nparams_ior=${#ha_params_ior[@]}
215 declare ha_nparams_simul=${#ha_params_simul[@]}
216 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
218 declare -A ha_mpi_load_cmds=(
219 [ior]="$IOR -o {}/f.ior {params}"
220 [simul]="$SIMUL {params} -d {}"
221 [mdtest]="$MDTEST {params} -d {}"
224 declare ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
225 declare -a ha_nonmpi_load_tags=($ha_nonmpi_loads)
226 declare -a ha_nonmpi_load_cmds=(
227 "dd if=/dev/zero of={}/f.dd bs=1M count=256"
228 "tar cf - /etc | tar xf - -C {}"
229 "iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
234 ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]" \
235 "-v HOST[,...] [-d DIRECTORY] [-u SECONDS]"
238 ha_process_arguments()
242 while getopts hc:s:v:d:p:u:wrm opt; do
249 ha_clients=(${OPTARG//,/ })
252 ha_servers=(${OPTARG//,/ })
255 ha_victims=(${OPTARG//,/ })
258 ha_test_dir=$OPTARG/$(basename $0)-$$
261 ha_expected_duration=$OPTARG
264 ha_max_failover_period=$OPTARG
267 ha_workloads_only=true
270 ha_workloads_dry_run=true
282 if [ -z "${ha_clients[*]}" ]; then
283 ha_error "-c is mandatory"
287 if ! ($ha_workloads_dry_run ||
288 $ha_workloads_only) &&
289 ([ -z "${ha_servers[*]}" ] ||
290 [ -z "${ha_victims[*]}" ]); then
291 ha_error "-s, and -v are all mandatory"
305 # -S is to be used here to track the
306 # remote command return values
308 pdsh -S -w $nodes PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
309 /bin:/usr/sbin:/usr/bin "$@" ||
316 touch "$ha_stop_file"
318 if [ -e "$ha_fail_file" ]; then
319 ha_info "Test directory $ha_test_dir not removed"
320 ha_info "Temporary directory $ha_tmp_dir not removed"
322 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
323 ha_info "Please find the results in the directory $ha_tmp_dir"
327 ha_trap_stop_signals()
329 ha_info "${ha_stop_signals// /,} received"
330 touch "$ha_stop_file"
337 ha_info "Sleeping for ${n}s"
339 # sleep(1) could interrupted.
348 until mkdir "$lock" >/dev/null 2>&1; do
349 ha_sleep 1 >/dev/null
362 local nodes=${1// /,}
363 local file=/tmp/$(basename $0)-$$-$(date +%s).dk
364 local lock=$ha_tmp_dir/lock-dump-logs
367 ha_info "Dumping lctl log to $file"
370 # some nodes could crash, so
371 # do not exit with error if not all logs are dumped
373 ha_on $nodes "lctl dk >$file" ||
374 ha_error "not all logs are dumped! Some nodes are unreachable."
384 local tag=${ha_mpi_load_tags[$load]}
385 local cmd=${ha_mpi_load_cmds[$tag]}
386 local dir=$ha_test_dir/$client-$tag
387 local log=$ha_tmp_dir/$client-$tag
390 local start_time=$(date +%s)
392 cmd=${cmd//"{}"/$dir}
393 cmd=${cmd//"{params}"/$parameter}
395 ha_info "Starting $tag"
397 local machines="-machinefile $ha_machine_file"
398 while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
401 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
402 mdt_index=$(ha_rand $ha_mdt_index)
404 mdt_index=$ha_mdt_index
406 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
407 ha_on $client $LFS getdirstripe "$dir" &&
408 ha_on $client chmod a+xwr $dir &&
409 ha_on $client "su mpiuser sh -c \" $mpirun $ha_mpirun_options \
410 -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
411 $machines $cmd \" " &&
412 ha_on $client rm -rf "$dir";
413 } >>"$log" 2>&1 || rc=$?
418 touch "$ha_fail_file"
419 touch "$ha_stop_file"
420 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
424 nr_loops=$((nr_loops + 1))
427 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
429 ha_info "$tag stopped: rc $rc avg loop time $avg_loop_time"
441 for client in ${ha_clients[@]}; do
442 ha_info ha_machine_file=$ha_machine_file
443 echo $client >> $ha_machine_file
445 local dirname=$(dirname $ha_machine_file)
446 for client in ${ha_clients[@]}; do
447 ha_on $client mkdir -p $dirname
448 scp $ha_machine_file $client:$ha_machine_file
451 # ha_mpi_instances defines the number of
452 # clients start mpi loads; should be <= ${#ha_clients[@]}
453 local inst=$ha_mpi_instances
454 (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
456 for ((n = 0; n < $inst; n++)); do
457 client=${ha_clients[n]}
458 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
459 tag=${ha_mpi_load_tags[$load]}
460 status=$ha_status_file_prefix-$tag-$client
463 local num=ha_nparams_$tag
465 local aref=ha_params_$tag[nparam]
466 local parameter=${!aref}
467 ha_repeat_mpi_load $client $load $status "$parameter" &
468 ha_status_files+=("$status")
473 ha_repeat_nonmpi_load()
478 local tag=${ha_nonmpi_load_tags[$load]}
479 local cmd=${ha_nonmpi_load_cmds[$load]}
480 local dir=$ha_test_dir/$client-$tag
481 local log=$ha_tmp_dir/$client-$tag
484 local start_time=$(date +%s)
486 cmd=${cmd//"{}"/$dir}
488 ha_info "Starting $tag on $client"
490 while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
491 ha_on $client "mkdir -p $dir && \
493 rm -rf $dir" >>"$log" 2>&1 || rc=$?
496 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
497 touch "$ha_fail_file"
498 touch "$ha_stop_file"
502 nr_loops=$((nr_loops + 1))
505 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
507 ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
510 ha_start_nonmpi_loads()
517 for client in ${ha_clients[@]}; do
518 for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
519 tag=${ha_nonmpi_load_tags[$load]}
520 status=$ha_status_file_prefix-$tag-$client
521 ha_repeat_nonmpi_load $client $load $status &
522 ha_status_files+=("$status")
529 trap ha_trap_stop_signals $ha_stop_signals
530 ha_start_nonmpi_loads
537 trap - $ha_stop_signals
538 ha_info "Waiting for workloads to stop"
545 local end=$(($(date +%s) + ha_load_timeout))
547 ha_info "Waiting for workload status"
548 rm -f "${ha_status_files[@]}"
551 # return immediately if ha_stop_file exists,
552 # all status_files not needed to be checked
554 for file in "${ha_status_files[@]}"; do
555 if [ -e "$ha_stop_file" ]; then
556 ha_info "$ha_stop_file found! Stop."
560 # Wait status file created during ha_load_timeout.
561 # Existing file guarantees that some application
562 # is completed. If no status file was created
563 # this function guarantees that we allow
564 # applications to continue after/before
565 # failover/failback during ha_load_timeout time.
567 until [ -e "$file" ] || (($(date +%s) >= end)); do
569 # check ha_stop_file again, it could appear
570 # during ha_load_timeout
572 if [ -e "$ha_stop_file" ]; then
573 ha_info "$ha_stop_file found! Stop."
576 ha_sleep 1 >/dev/null
587 ha_info "Powering down $nodes"
588 for i in $(seq 1 5); do
589 $ha_power_down_cmd $nodes && rc=0 && break
590 sleep $ha_power_delay
593 [ $rc -eq 0 ] || ha_info "Failed Powering down in $i attempts"
602 ha_info "Powering up $nodes"
603 for i in $(seq 1 5); do
604 $ha_power_up_cmd $nodes && rc=0 && break
605 sleep $ha_power_delay
608 [ $rc -eq 0 ] || ha_info "Failed Powering up in $i attempts"
614 # Print a random integer within [0, MAX).
621 # See "5.2 Bash Variables" from "info bash".
623 echo -n $((RANDOM * max / 32768))
631 if $ha_simultaneous ; then
632 nodes=$(echo ${ha_victims[@]})
635 i=$(ha_rand ${#ha_victims[@]})
636 nodes=${ha_victims[$i]}
645 local end=$(($(date +%s) + 10 * 60))
647 ha_info "Waiting for $nodes to boot up"
648 until ha_on $nodes hostname >/dev/null 2>&1 ||
649 [ -e "$ha_stop_file" ] ||
650 (($(date +%s) >= end)); do
651 ha_sleep 1 >/dev/null
658 ha_info "Failback resources on $nodes in $ha_failback_delay sec"
660 ha_sleep $ha_failback_delay
661 [ "$ha_failback_cmd" ] ||
663 ha_info "No failback command set, skiping"
667 $ha_failback_cmd $nodes
672 ha_info "---------------8<---------------"
674 ha_info " Duration: $(($(date +%s) - $ha_start_time))s"
675 ha_info " Loops: $ha_nr_loops"
682 while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
683 [ ! -e "$ha_stop_file" ]; do
684 ha_info "---------------8<---------------"
686 $ha_workloads_only || nodes=$(ha_aim)
688 ha_info "Failing $nodes"
689 $ha_workloads_only && ha_info " is skipped: workload only..."
691 ha_sleep $(ha_rand $ha_max_failover_period)
692 $ha_workloads_only || ha_power_down $nodes
694 ha_wait_loads || return
696 if [ -e $ha_stop_file ]; then
697 $ha_workloads_only || ha_power_up $nodes
701 ha_info "Bringing $nodes back"
702 ha_sleep $(ha_rand 10)
703 $ha_workloads_only ||
711 # Wait for the failback to start.
714 ha_wait_loads || return
716 ha_sleep $(ha_rand 20)
718 ha_nr_loops=$((ha_nr_loops + 1))
719 ha_info "Loop $ha_nr_loops done"
726 ha_process_arguments "$@"
729 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
730 "START: $0: $(date +%H:%M:%S' '%s)"
731 trap ha_trap_exit EXIT
733 ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
734 ha_on ${ha_clients[0]} " \
735 $LFS setstripe $ha_stripe_params $ha_test_dir"
740 if $ha_workloads_dry_run; then
744 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
749 if [ -e "$ha_fail_file" ]; then
752 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
753 "END: $0: $(date +%H:%M:%S' '%s)"