Whamcloud - gitweb
LU-8618 tests: ha.sh improvements
[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 #
48 #
49 # ASSUMPTIONS
50 #
51 #   A Lustre file system is up and mounted on all client nodes.  This script
52 #   does not mount or unmount any Lustre targets or clients, let alone format
53 #   anything.
54 #
55 #   Each target has a failnode, so that workloads can continue after a power
56 #   failure.
57 #
58 #   CRM could be configured by 2 ways:
59 #   1.
60 #   Targets are automatically failed back when their primary node is back.  This
61 #   assumption avoids calling CRM-specific commands to trigger failbacks, making
62 #   this script more CRM-neural.
63 #   2.
64 #   Targets are not automatically failed back when their primary node is back.
65 #   CRM-specific command is executed to trigger failbacks.
66 #
67 #   A crash dump mechanism is configured to catch LBUGs, panics, etc.
68 #
69 # WORKLOADS
70 #
71 #   Each client runs set of MPI and non-MPI workloads. These
72 #   applications are run in short loops so that their exit status can be waited
73 #   for and checked within reasonable time by ha_wait_loads.
74 #   The set of MPI and non-MPI workloads are configurable by parameters:
75 #       ha_mpi_loads
76 #               default set: dd, tar, iozone
77 #       ha_nonmpi_loads
78 #               default set: ior, simul.
79 #
80 #   The number of clients run MPI loads is configured by parameter
81 #   ha_mpi_instances. Only one client runs MPI workloads by default.
82 #
83 # PROCESS STRUCTURE AND IPC
84 #
85 #   On the node where this script is run, the processes look like this:
86 #
87 #       ~ ha.sh (ha_killer)
88 #
89 #           ~ ha.sh (ha_repeat_mpi_load ior)
90 #               ~ mpirun IOR
91 #           ~ ha.sh (ha_repeat_mpi_load simul)
92 #               ~ mpirun simul
93 #           ~ ... (one for each MPI load)
94 #
95 #           ~ ha.sh (ha_repeat_nonmpi_load client2 dbench)
96 #               ~ pdsh client2 dbench
97 #           ~ ha.sh (ha_repeat_nonmpi_load client2 iozone)
98 #               ~ pdsh client2 iozone
99 #           ~ ha.sh (ha_repeat_nonmpi_load client5 iozone)
100 #               ~ pdsh client5 iozone
101 #           ~ ... (one for each non-MPI load on each client)
102 #
103 #   Each tilde represents a process.  Indentations imply parent-children
104 #   relation.
105 #
106 #   IPC is done by files in the temporary directory.
107 #
108
109 #set -x
110
111 SIMUL=${SIMUL:-$(which simul 2> /dev/null || true)}
112 IOR=${IOR:-$(which IOR 2> /dev/null || true)}
113
114 ior_blockSize=${ior_blockSize:-6g}
115 mpi_threads_per_client=${mpi_threads_per_client:-2}
116
117 iozone_SIZE=${iozone_SIZE:-262144} # 256m
118
119 mpirun=${MPIRUN:-$(which mpirun)}
120 LFS=${LFS:-$(which lfs)}
121
122 ha_check_env()
123 {
124         for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
125                 local tag=${ha_mpi_load_tags[$load]}
126                 local bin=$(echo $tag | tr '[:lower:]' '[:upper:]')
127                 if [ x${!bin} = x ]; then
128                         ha_error ha_mpi_loads: ${ha_mpi_loads}, $bin is not set
129                         exit 1
130                 fi
131         done
132 }
133
134 ha_info()
135 {
136         echo "$0: $(date +%H:%M:%S' '%s):" "$@"
137 }
138
139 ha_log()
140 {
141         local nodes=${1// /,}
142         shift
143         ha_on $nodes "lctl mark $*"
144 }
145
146 ha_error()
147 {
148     ha_info "$@" >&2
149 }
150
151 ha_trap_err()
152 {
153     local i
154
155     ha_error "Trap ERR triggered by:"
156     ha_error "    $BASH_COMMAND"
157     ha_error "Call trace:"
158     for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
159         ha_error "    ${FUNCNAME[$i]} [${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}]"
160     done
161 }
162
163 trap ha_trap_err ERR
164 set -eE
165
166 declare     ha_tmp_dir=/tmp/$(basename $0)-$$
167 declare     ha_stop_file=$ha_tmp_dir/stop
168 declare     ha_fail_file=$ha_tmp_dir/fail
169 declare     ha_status_file_prefix=$ha_tmp_dir/status
170 declare -a  ha_status_files
171 declare     ha_machine_file=$ha_tmp_dir/machine_file
172 declare     ha_power_down_cmd=${POWER_DOWN:-"pm -0"}
173 declare     ha_power_up_cmd=${POWER_UP:-"pm -1"}
174 declare     ha_failback_delay=${DELAY:-5}
175 declare     ha_failback_cmd=${FAILBACK:-""}
176 declare     ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
177 declare -a  ha_clients
178 declare -a  ha_servers
179 declare -a  ha_victims
180 declare     ha_test_dir=/mnt/lustre/$(basename $0)-$$
181 declare     ha_start_time=$(date +%s)
182 declare     ha_expected_duration=$((60 * 60 * 24))
183 declare     ha_max_failover_period=10
184 declare     ha_nr_loops=0
185 declare     ha_stop_signals="SIGINT SIGTERM SIGHUP"
186 declare     ha_load_timeout=$((60 * 10))
187 declare     ha_workloads_only=false
188 declare     ha_workloads_dry_run=false
189
190 declare     ha_mpi_instances=${ha_mpi_instances:-1}
191
192 declare     ha_mpi_loads=${ha_mpi_loads="ior simul"}
193 declare -a  ha_mpi_load_tags=($ha_mpi_loads)
194
195 declare     ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
196 declare     ha_simul_params=${SIMULP:-'" -n 10"'}
197 declare     ha_mpirun_options=${MPIRUN_OPTIONS:-""}
198
199 eval ha_params_ior=($ha_ior_params)
200 eval ha_params_simul=($ha_simul_params)
201
202 declare ha_nparams_ior=${#ha_params_ior[@]}
203 declare ha_nparams_simul=${#ha_params_simul[@]}
204
205 declare -A  ha_mpi_load_cmds=(
206     [ior]="$IOR -o {}/f.ior {params}"
207     [simul]="$SIMUL {params} -d {}"
208 )
209
210 declare     ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
211 declare -a  ha_nonmpi_load_tags=($ha_nonmpi_loads)
212 declare -a  ha_nonmpi_load_cmds=(
213         "dd if=/dev/zero of={}/f.dd bs=1M count=256"
214         "tar cf - /etc | tar xf - -C {}"
215         "iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
216 )
217
218 ha_usage()
219 {
220     ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]"                         \
221             "-v HOST[,...] [-d DIRECTORY] [-u SECONDS]"
222 }
223
224 ha_process_arguments()
225 {
226     local opt
227
228     while getopts hc:s:v:d:p:u:wr opt; do
229         case $opt in
230         h)
231             ha_usage
232             exit 0
233             ;;
234         c)
235             ha_clients=(${OPTARG//,/ })
236             ;;
237         s)
238             ha_servers=(${OPTARG//,/ })
239             ;;
240         v)
241             ha_victims=(${OPTARG//,/ })
242             ;;
243         d)
244             ha_test_dir=$OPTARG/$(basename $0)-$$
245             ;;
246         u)
247             ha_expected_duration=$OPTARG
248             ;;
249         p)
250                 ha_max_failover_period=$OPTARG
251                 ;;
252         w)
253                 ha_workloads_only=true
254                 ;;
255         r)
256                 ha_workloads_dry_run=true
257                 ;;
258         \?)
259             ha_usage
260             exit 1
261             ;;
262         esac
263     done
264
265         if [ -z "${ha_clients[*]}" ]; then
266                 ha_error "-c is mandatory"
267                 ha_usage
268                 exit 1
269         fi
270         if ! ($ha_workloads_dry_run ||
271                         $ha_workloads_only) &&
272                         ([ -z "${ha_servers[*]}" ] ||
273                         [ -z "${ha_victims[*]}" ]); then
274                 ha_error "-s, and -v are all mandatory"
275                 ha_usage
276                 exit 1
277         fi
278 }
279
280 ha_on()
281 {
282         local nodes=$1
283         local rc=0
284
285         shift
286
287         #
288         # -S is to be used here to track the
289         # remote command return values
290         #
291         pdsh -S -w $nodes PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
292 /bin:/usr/sbin:/usr/bin "$@" ||
293                 rc=$?
294         return $rc
295 }
296
297 ha_trap_exit()
298 {
299         touch "$ha_stop_file"
300         trap 0
301         if [ -e "$ha_fail_file" ]; then
302                 ha_info "Test directory $ha_test_dir not removed"
303                 ha_info "Temporary directory $ha_tmp_dir not removed"
304         else
305                 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
306                 ha_info "Please find the results in the directory $ha_tmp_dir"
307         fi
308 }
309
310 ha_trap_stop_signals()
311 {
312     ha_info "${ha_stop_signals// /,} received"
313     touch "$ha_stop_file"
314 }
315
316 ha_sleep()
317 {
318     local n=$1
319
320     ha_info "Sleeping for ${n}s"
321     #
322     # sleep(1) could interrupted.
323     #
324     sleep $n || true
325 }
326
327 ha_lock()
328 {
329     local lock=$1
330
331     until mkdir "$lock" >/dev/null 2>&1; do
332         ha_sleep 1 >/dev/null
333     done
334 }
335
336 ha_unlock()
337 {
338     local lock=$1
339
340     rm -r "$lock"
341 }
342
343 ha_dump_logs()
344 {
345     local nodes=${1// /,}
346     local file=/tmp/$(basename $0)-$$-$(date +%s).dk
347     local lock=$ha_tmp_dir/lock-dump-logs
348
349     ha_lock "$lock"
350     ha_info "Dumping lctl log to $file"
351
352         #
353         # some nodes could crash, so
354         # do not exit with error if not all logs are dumped
355         #
356         ha_on $nodes "lctl dk >$file" ||
357                 ha_error "not all logs are dumped! Some nodes are unreachable."
358         ha_unlock "$lock"
359 }
360
361 ha_repeat_mpi_load()
362 {
363         local client=$1
364         local load=$2
365         local status=$3
366         local parameter=$4
367         local tag=${ha_mpi_load_tags[$load]}
368         local cmd=${ha_mpi_load_cmds[$tag]}
369         local dir=$ha_test_dir/$client-$tag
370         local log=$ha_tmp_dir/$client-$tag
371         local rc=0
372         local nr_loops=0
373         local start_time=$(date +%s)
374
375         cmd=${cmd//"{}"/$dir}
376         cmd=${cmd//"{params}"/$parameter}
377
378         ha_info "Starting $tag"
379
380         local machines="-machinefile $ha_machine_file"
381         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
382                 {
383                 ha_on $client mkdir -p "$dir" &&
384                 ha_on $client chmod a+xwr $dir &&
385                 ha_on $client "su mpiuser sh -c \" $mpirun $ha_mpirun_options \
386                         -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
387                         $machines $cmd \" " &&
388                         ha_on $client rm -rf "$dir";
389                 } >>"$log" 2>&1 || rc=$?
390
391                 ha_info rc=$rc
392
393                 if ((rc != 0)); then
394                         touch "$ha_fail_file"
395                         touch "$ha_stop_file"
396                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
397                 fi
398                 echo $rc >"$status"
399
400                 nr_loops=$((nr_loops + 1))
401         done
402
403         avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
404
405         ha_info "$tag stopped: rc $rc avg loop time $avg_loop_time"
406 }
407
408 ha_start_mpi_loads()
409 {
410         local client
411         local load
412         local tag
413         local status
414         local n
415         local nparam
416
417         for client in ${ha_clients[@]}; do
418                 ha_info ha_machine_file=$ha_machine_file
419                 echo $client >> $ha_machine_file
420         done
421         local dirname=$(dirname $ha_machine_file)
422         for client in ${ha_clients[@]}; do
423                 ha_on $client mkdir -p $dirname
424                 scp $ha_machine_file $client:$ha_machine_file
425         done
426
427         # ha_mpi_instances defines the number of
428         # clients start mpi loads; should be <= ${#ha_clients[@]}
429         local inst=$ha_mpi_instances
430         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
431
432         for ((n = 0; n < $inst; n++)); do
433                 client=${ha_clients[n]}
434                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
435                         tag=${ha_mpi_load_tags[$load]}
436                         status=$ha_status_file_prefix-$tag-$client
437                         # ha_nparams_ior
438                         # ha_nparams_simul
439                         local num=ha_nparams_$tag
440                         nparam=$((n % num))
441                         local aref=ha_params_$tag[nparam]
442                         local parameter=${!aref}
443                         ha_repeat_mpi_load $client $load $status "$parameter" &
444                                 ha_status_files+=("$status")
445                 done
446         done
447 }
448
449 ha_repeat_nonmpi_load()
450 {
451     local client=$1
452     local load=$2
453     local status=$3
454     local tag=${ha_nonmpi_load_tags[$load]}
455     local cmd=${ha_nonmpi_load_cmds[$load]}
456     local dir=$ha_test_dir/$client-$tag
457     local log=$ha_tmp_dir/$client-$tag
458     local rc=0
459     local nr_loops=0
460     local start_time=$(date +%s)
461
462     cmd=${cmd//"{}"/$dir}
463
464     ha_info "Starting $tag on $client"
465
466         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
467                 ha_on $client "mkdir -p $dir &&                              \
468                         $cmd &&                                              \
469                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
470
471                 if ((rc != 0)); then
472                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
473                         touch "$ha_fail_file"
474                         touch "$ha_stop_file"
475                 fi
476                 echo $rc >"$status"
477
478                 nr_loops=$((nr_loops + 1))
479         done
480
481     avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
482
483     ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
484 }
485
486 ha_start_nonmpi_loads()
487 {
488     local client
489     local load
490     local tag
491     local status
492
493     for client in ${ha_clients[@]}; do
494         for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
495             tag=${ha_nonmpi_load_tags[$load]}
496             status=$ha_status_file_prefix-$tag-$client
497             ha_repeat_nonmpi_load $client $load $status &
498             ha_status_files+=("$status")
499         done
500     done
501 }
502
503 ha_start_loads()
504 {
505     trap ha_trap_stop_signals $ha_stop_signals
506     ha_start_nonmpi_loads
507     ha_start_mpi_loads
508 }
509
510 ha_stop_loads()
511 {
512     touch $ha_stop_file
513     trap - $ha_stop_signals
514     ha_info "Waiting for workloads to stop"
515     wait
516 }
517
518 ha_wait_loads()
519 {
520     local file
521     local end=$(($(date +%s) + ha_load_timeout))
522
523     ha_info "Waiting for workload status"
524     rm -f "${ha_status_files[@]}"
525
526         #
527         # return immediately if ha_stop_file exists,
528         # all status_files not needed to be checked
529         #
530         for file in "${ha_status_files[@]}"; do
531                 if [ -e "$ha_stop_file" ]; then
532                         ha_info "$ha_stop_file found! Stop."
533                         break
534                 fi
535                 #
536                 # Wait status file created during ha_load_timeout.
537                 # Existing file guarantees that some application
538                 # is completed. If no status file was created
539                 # this function guarantees that we allow
540                 # applications to continue after/before
541                 # failover/failback during ha_load_timeout time.
542                 #
543                 until [ -e "$file" ] || (($(date +%s) >= end)); do
544                         #
545                         # check ha_stop_file again, it could appear
546                         # during ha_load_timeout
547                         #
548                         if [ -e "$ha_stop_file" ]; then
549                                 ha_info "$ha_stop_file found! Stop."
550                                 break
551                         fi
552                         ha_sleep 1 >/dev/null
553                 done
554         done
555 }
556
557 ha_power_down()
558 {
559     local node=$1
560
561     ha_info "Powering down $node"
562     $ha_power_down_cmd $node
563 }
564
565 ha_power_up()
566 {
567     local node=$1
568
569     ha_info "Powering up $node"
570     $ha_power_up_cmd $node
571 }
572
573 #
574 # rand MAX
575 #
576 # Print a random integer within [0, MAX).
577 #
578 ha_rand()
579 {
580     local max=$1
581
582     #
583     # See "5.2 Bash Variables" from "info bash".
584     #
585     echo -n $((RANDOM * max / 32768))
586 }
587
588 ha_aim()
589 {
590     local i=$(ha_rand ${#ha_victims[@]})
591
592     echo -n ${ha_victims[$i]}
593 }
594
595 ha_wait_node()
596 {
597         local node=$1
598         local end=$(($(date +%s) + 10 * 60))
599
600         ha_info "Waiting for $node to boot up"
601         until ha_on $node hostname >/dev/null 2>&1 ||
602                 [ -e "$ha_stop_file" ] ||
603                         (($(date +%s) >= end)); do
604                 ha_sleep 1 >/dev/null
605         done
606 }
607
608 ha_failback()
609 {
610         local node=$1
611         ha_info "Failback resources on $node in $ha_failback_delay sec"
612
613         ha_sleep $ha_failback_delay
614         [ "$ha_failback_cmd" ] ||
615         {
616                 ha_info "No failback command set, skiping"
617                 return 0
618         }
619
620         $ha_failback_cmd $node
621 }
622
623 ha_summarize()
624 {
625     ha_info "---------------8<---------------"
626     ha_info "Summary:"
627     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
628     ha_info "    Loops: $ha_nr_loops"
629 }
630
631 ha_killer()
632 {
633         local node
634
635         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
636                         [ ! -e "$ha_stop_file" ]; do
637                 ha_info "---------------8<---------------"
638
639                 $ha_workloads_only || node=$(ha_aim)
640
641                 ha_info "Failing $node"
642                 $ha_workloads_only && ha_info "    is skipped: workload only..."
643
644                 ha_sleep $(ha_rand $ha_max_failover_period)
645                 $ha_workloads_only || ha_power_down $node
646                 ha_sleep 10
647                 ha_wait_loads || return
648
649                 if [ -e $ha_stop_file ]; then
650                         $ha_workloads_only || ha_power_up $node
651                         break
652                 fi
653
654                 ha_info "Bringing $node back"
655                 ha_sleep $(ha_rand 10)
656                 $ha_workloads_only ||
657                 {
658                         ha_power_up $node
659                         ha_wait_node $node
660                         ha_failback $node
661                 }
662
663                 #
664                 # Wait for the failback to start.
665                 #
666                 ha_sleep 60
667                 ha_wait_loads || return
668
669                 ha_sleep $(ha_rand 20)
670
671                 ha_nr_loops=$((ha_nr_loops + 1))
672                 ha_info "Loop $ha_nr_loops done"
673         done
674         ha_summarize
675 }
676
677 ha_main()
678 {
679         ha_process_arguments "$@"
680         ha_check_env
681
682         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
683                 "START: $0: $(date +%H:%M:%S' '%s)"
684         trap ha_trap_exit EXIT
685         mkdir "$ha_tmp_dir"
686         ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
687         ha_on ${ha_clients[0]} " \
688                 $LFS setstripe $ha_stripe_params $ha_test_dir"
689
690         ha_start_loads
691         ha_wait_loads
692
693         if $ha_workloads_dry_run; then
694                 ha_sleep 5
695         else
696                 ha_killer
697                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
698         fi
699
700         ha_stop_loads
701
702         if [ -e "$ha_fail_file" ]; then
703                 exit 1
704         else
705                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
706                         "END: $0: $(date +%H:%M:%S' '%s)"
707                 exit 0
708         fi
709 }
710
711 ha_main "$@"