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