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