Whamcloud - gitweb
LU-8066 lov: move stripesize to sysfs
[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 #   MPI workloads can be run from several users. The list of users to use is
86 #   configured by parameter ha_mpi_users, default is "mpiuser".
87 #
88 # PROCESS STRUCTURE AND IPC
89 #
90 #   On the node where this script is run, the processes look like this:
91 #
92 #       ~ ha.sh (ha_killer)
93 #
94 #           ~ ha.sh (ha_repeat_mpi_load ior)
95 #               ~ mpirun IOR
96 #           ~ ha.sh (ha_repeat_mpi_load simul)
97 #               ~ mpirun simul
98 #           ~ ha.sh (ha_repeat_mpi_load mdtest)
99 #               ~ mpirun mdtest
100 #           ~ ... (one for each MPI load)
101 #
102 #           ~ ha.sh (ha_repeat_nonmpi_load client2 dbench)
103 #               ~ pdsh client2 dbench
104 #           ~ ha.sh (ha_repeat_nonmpi_load client2 iozone)
105 #               ~ pdsh client2 iozone
106 #           ~ ha.sh (ha_repeat_nonmpi_load client5 iozone)
107 #               ~ pdsh client5 iozone
108 #           ~ ... (one for each non-MPI load on each client)
109 #
110 #   Each tilde represents a process.  Indentations imply parent-children
111 #   relation.
112 #
113 #   IPC is done by files in the temporary directory.
114 #
115
116 #set -x
117
118 SIMUL=${SIMUL:-$(which simul 2> /dev/null || true)}
119 IOR=${IOR:-$(which IOR 2> /dev/null || true)}
120 MDTEST=${MDTEST:-$(which mdtest 2> /dev/null || true)}
121
122 ior_blockSize=${ior_blockSize:-6g}
123 mpi_threads_per_client=${mpi_threads_per_client:-2}
124
125 iozone_SIZE=${iozone_SIZE:-262144} # 256m
126
127 mpirun=${MPIRUN:-$(which mpirun)}
128 LFS=${LFS:-$(which lfs)}
129
130 ha_check_env()
131 {
132         for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
133                 local tag=${ha_mpi_load_tags[$load]}
134                 local bin=$(echo $tag | tr '[:lower:]' '[:upper:]')
135                 if [ x${!bin} = x ]; then
136                         ha_error ha_mpi_loads: ${ha_mpi_loads}, $bin is not set
137                         exit 1
138                 fi
139         done
140 }
141
142 ha_info()
143 {
144         echo "$0: $(date +%H:%M:%S' '%s):" "$@"
145 }
146
147 ha_log()
148 {
149         local nodes=${1// /,}
150         shift
151         ha_on $nodes "lctl mark $*"
152 }
153
154 ha_error()
155 {
156     ha_info "$@" >&2
157 }
158
159 ha_trap_err()
160 {
161     local i
162
163     ha_error "Trap ERR triggered by:"
164     ha_error "    $BASH_COMMAND"
165     ha_error "Call trace:"
166     for ((i = 0; i < ${#FUNCNAME[@]}; i++)); do
167         ha_error "    ${FUNCNAME[$i]} [${BASH_SOURCE[$i]}:${BASH_LINENO[$i]}]"
168     done
169 }
170
171 trap ha_trap_err ERR
172 set -eE
173
174 declare     ha_tmp_dir=/tmp/$(basename $0)-$$
175 declare     ha_stop_file=$ha_tmp_dir/stop
176 declare     ha_fail_file=$ha_tmp_dir/fail
177 declare     ha_pm_states=$ha_tmp_dir/ha_pm_states
178 declare     ha_status_file_prefix=$ha_tmp_dir/status
179 declare -a  ha_status_files
180 declare     ha_machine_file=$ha_tmp_dir/machine_file
181 declare     ha_lfsck_log=$ha_tmp_dir/lfsck.log
182 declare     ha_lfsck_lock=$ha_tmp_dir/lfsck.lock
183 declare     ha_lfsck_stop=$ha_tmp_dir/lfsck.stop
184 declare     ha_lfsck_bg=${LFSCK_BG:-false}
185 declare     ha_lfsck_after=${LFSCK_AFTER:-false}
186 declare     ha_lfsck_node=${LFSCK_NODE:-""}
187 declare     ha_lfsck_device=${LFSCK_DEV:-""}
188 declare     ha_lfsck_types=${LFSCK_TYPES:-"namespace layout"}
189 declare     ha_lfsck_custom_params=${LFSCK_CUSTOM_PARAMS:-""}
190 declare     ha_lfsck_wait=${LFSCK_WAIT:-1200}
191 declare     ha_lfsck_fail_on_repaired=${LFSCK_FAIL_ON_REPAIRED:-false}
192 declare     ha_power_down_cmd=${POWER_DOWN:-"pm -0"}
193 declare     ha_power_up_cmd=${POWER_UP:-"pm -1"}
194 declare     ha_power_delay=${POWER_DELAY:-60}
195 declare     ha_node_up_delay=${NODE_UP_DELAY:-10}
196 declare     ha_pm_host=${PM_HOST:-$(hostname)}
197 declare     ha_failback_delay=${DELAY:-5}
198 declare     ha_failback_cmd=${FAILBACK:-""}
199 declare     ha_stripe_params=${STRIPEPARAMS:-"-c 0"}
200 declare     ha_dir_stripe_count=${DSTRIPECOUNT:-"1"}
201 declare     ha_mdt_index=${MDTINDEX:-"0"}
202 declare     ha_mdt_index_random=${MDTINDEXRAND:-false}
203 declare -a  ha_clients
204 declare -a  ha_servers
205 declare -a  ha_victims
206 declare -a  ha_victims_pair
207 declare     ha_test_dir=/mnt/lustre/$(basename $0)-$$
208 declare     ha_start_time=$(date +%s)
209 declare     ha_expected_duration=$((60 * 60 * 24))
210 declare     ha_max_failover_period=10
211 declare     ha_nr_loops=0
212 declare     ha_stop_signals="SIGINT SIGTERM SIGHUP"
213 declare     ha_load_timeout=${LOAD_TIMEOUT:-$((60 * 10))}
214 declare     ha_workloads_only=false
215 declare     ha_workloads_dry_run=false
216 declare     ha_simultaneous=false
217
218 declare     ha_mpi_instances=${ha_mpi_instances:-1}
219
220 declare     ha_mpi_loads=${ha_mpi_loads="ior simul mdtest"}
221 declare -a  ha_mpi_load_tags=($ha_mpi_loads)
222 declare -a  ha_mpiusers=(${ha_mpi_users="mpiuser"})
223 declare -a  ha_users
224 declare -A  ha_mpiopts
225
226 for ((i=0; i<${#ha_mpiusers[@]}; i++)); do
227         u=${ha_mpiusers[i]%%:*}
228         o=""
229         # user gets empty option if ha_mpi_users does not specify it explicitly
230         [[ ${ha_mpiusers[i]} =~ : ]] && o=${ha_mpiusers[i]##*:}
231         ha_users[i]=$u
232         ha_mpiopts[$u]+=" $o"
233 done
234 ha_users=(${!ha_mpiopts[@]})
235
236 declare     ha_ior_params=${IORP:-'" -b $ior_blockSize -t 2m -w -W -T 1"'}
237 declare     ha_simul_params=${SIMULP:-'" -n 10"'}
238 declare     ha_mdtest_params=${MDTESTP:-'" -i 1 -n 1000"'}
239 declare     ha_mpirun_options=${MPIRUN_OPTIONS:-""}
240 declare     ha_clients_stripe=${CLIENTSSTRIPE:-'"$STRIPEPARAMS"'}
241 declare     ha_nclientsset=${NCLIENTSSET:-1}
242 declare     ha_ninstmustfail=${NINSTMUSTFAIL:-0}
243
244 declare     ha_racer_params=${RACERP:-"MDSCOUNT=1"}
245
246 eval ha_params_ior=($ha_ior_params)
247 eval ha_params_simul=($ha_simul_params)
248 eval ha_params_mdtest=($ha_mdtest_params)
249 eval ha_stripe_clients=($ha_clients_stripe)
250
251 declare ha_nparams_ior=${#ha_params_ior[@]}
252 declare ha_nparams_simul=${#ha_params_simul[@]}
253 declare ha_nparams_mdtest=${#ha_params_mdtest[@]}
254 declare ha_nstripe_clients=${#ha_stripe_clients[@]}
255
256 declare -A  ha_mpi_load_cmds=(
257         [ior]="$IOR -o {}/f.ior {params}"
258         [simul]="$SIMUL {params} -d {}"
259         [mdtest]="$MDTEST {params} -d {}"
260 )
261
262 declare racer=${RACER:-"$(dirname $0)/racer/racer.sh"}
263
264 declare     ha_nonmpi_loads=${ha_nonmpi_loads="dd tar iozone"}
265 declare -a  ha_nonmpi_load_tags=($ha_nonmpi_loads)
266 declare -A  ha_nonmpi_load_cmds=(
267         [dd]="dd if=/dev/zero of={}/f.dd bs=1M count=256"
268         [tar]="tar cf - /etc | tar xf - -C {}"
269         [iozone]="iozone -a -e -+d -s $iozone_SIZE {}/f.iozone"
270         [racer]="$ha_racer_params $racer {}"
271 )
272
273 ha_usage()
274 {
275         ha_info "Usage: $0 -c HOST[,...] -s HOST[,...]" \
276                 "-v HOST[,...] -f HOST[,...] [-d DIRECTORY] [-u SECONDS]"
277 }
278
279 ha_process_arguments()
280 {
281     local opt
282
283         while getopts hc:s:v:d:p:u:wrmf: opt; do
284         case $opt in
285         h)
286             ha_usage
287             exit 0
288             ;;
289         c)
290             ha_clients=(${OPTARG//,/ })
291             ;;
292         s)
293             ha_servers=(${OPTARG//,/ })
294             ;;
295         v)
296             ha_victims=(${OPTARG//,/ })
297             ;;
298         d)
299             ha_test_dir=$OPTARG/$(basename $0)-$$
300             ;;
301         u)
302             ha_expected_duration=$OPTARG
303             ;;
304         p)
305                 ha_max_failover_period=$OPTARG
306                 ;;
307         w)
308                 ha_workloads_only=true
309                 ;;
310         r)
311                 ha_workloads_dry_run=true
312                 ;;
313         m)
314                 ha_simultaneous=true
315                 ;;
316         f)
317                 ha_victims_pair=(${OPTARG//,/ })
318                 ;;
319         \?)
320             ha_usage
321             exit 1
322             ;;
323         esac
324     done
325
326         if [ -z "${ha_clients[*]}" ]; then
327                 ha_error "-c is mandatory"
328                 ha_usage
329                 exit 1
330         fi
331         if ! ($ha_workloads_dry_run ||
332                         $ha_workloads_only) &&
333                         ([ -z "${ha_servers[*]}" ] ||
334                         [ -z "${ha_victims[*]}" ]); then
335                 ha_error "-s, and -v are all mandatory"
336                 ha_usage
337                 exit 1
338         fi
339 }
340
341 ha_on()
342 {
343         local nodes=$1
344         local rc=0
345
346         shift
347
348         #
349         # -S is to be used here to track the
350         # remote command return values
351         #
352         pdsh -S -w $nodes "PATH=/usr/local/sbin:/usr/local/bin:/sbin:\
353 /bin:/usr/sbin:/usr/bin; $@" ||
354                 rc=$?
355         return $rc
356 }
357
358 ha_trap_exit()
359 {
360         touch "$ha_stop_file"
361         trap 0
362         if [ -e "$ha_fail_file" ]; then
363                 ha_info "Test directory $ha_test_dir not removed"
364                 ha_info "Temporary directory $ha_tmp_dir not removed"
365         else
366                 ha_on ${ha_clients[0]} rm -rf "$ha_test_dir"
367                 ha_info "Please find the results in the directory $ha_tmp_dir"
368         fi
369 }
370
371 ha_trap_stop_signals()
372 {
373     ha_info "${ha_stop_signals// /,} received"
374     touch "$ha_stop_file"
375 }
376
377 ha_sleep()
378 {
379     local n=$1
380
381     ha_info "Sleeping for ${n}s"
382     #
383     # sleep(1) could interrupted.
384     #
385     sleep $n || true
386 }
387
388 ha_wait_unlock()
389 {
390         local lock=$1
391
392         while [ -e $lock ]; do
393                 sleep 1
394         done
395 }
396
397 ha_lock()
398 {
399     local lock=$1
400
401     until mkdir "$lock" >/dev/null 2>&1; do
402         ha_sleep 1 >/dev/null
403     done
404 }
405
406 ha_unlock()
407 {
408     local lock=$1
409
410     rm -r "$lock"
411 }
412
413 ha_dump_logs()
414 {
415         local nodes=${1// /,}
416         local file=/tmp/$(basename $0)-$$-$(date +%s).dk
417         local lock=$ha_tmp_dir/lock-dump-logs
418         local rc=0
419
420         ha_lock "$lock"
421         ha_info "Dumping lctl log to $file"
422
423         #
424         # some nodes could crash, so
425         # do not exit with error if not all logs are dumped
426         #
427         ha_on $nodes "lctl dk >>$file" || rc=$?
428
429         [ $rc -eq 0 ] ||
430                 ha_error "not all logs are dumped! Some nodes are unreachable."
431         ha_unlock "$lock"
432 }
433
434 ha_repeat_mpi_load()
435 {
436         local client=$1
437         local load=$2
438         local status=$3
439         local parameter=$4
440         local machines=$5
441         local stripeparams=$6
442         local mpiuser=$7
443         local mustpass=$8
444         local mpirunoptions=$9
445         local tag=${ha_mpi_load_tags[$load]}
446         local cmd=${ha_mpi_load_cmds[$tag]}
447         local dir=$ha_test_dir/$client-$tag
448         local log=$ha_tmp_dir/$client-$tag
449         local rc=0
450         local nr_loops=0
451         local avg_loop_time=0
452         local start_time=$(date +%s)
453
454         cmd=${cmd//"{}"/$dir}
455         cmd=${cmd//"{params}"/$parameter}
456
457         [[ -n "$ha_postcmd" ]] && ha_postcmd=${ha_postcmd//"{}"/$dir}
458         [[ -n "$ha_precmd" ]] && ha_precmd=${ha_precmd//"{}"/$dir}
459         ha_info "Starting $tag"
460
461         machines="-machinefile $machines"
462         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
463                 ha_info "$client Starts: $mpiuser: $cmd" 2>&1 |  tee -a $log
464                 {
465                 local mdt_index
466                 if $ha_mdt_index_random && [ $ha_mdt_index -ne 0 ]; then
467                         mdt_index=$(ha_rand $ha_mdt_index)
468                 else
469                         mdt_index=$ha_mdt_index
470                 fi
471                 [[ -n "$ha_precmd" ]] && ha_info "$ha_precmd" &&
472                         ha_on $client "$ha_precmd" >>"$log" 2>&1
473                 ha_on $client $LFS mkdir -i$mdt_index -c$ha_dir_stripe_count "$dir" &&
474                 ha_on $client $LFS getdirstripe "$dir" &&
475                 ha_on $client $LFS setstripe $stripeparams $dir &&
476                 ha_on $client $LFS getstripe $dir &&
477                 ha_on $client chmod a+xwr $dir &&
478                 ha_on $client "su $mpiuser sh -c \" $mpirun $mpirunoptions \
479                         -np $((${#ha_clients[@]} * mpi_threads_per_client )) \
480                         $machines $cmd \" " || rc=$?
481                 [[ -n "$ha_postcmd" ]] && ha_info "$ha_postcmd" &&
482                         ha_on $client "$ha_postcmd" >>"$log" 2>&1
483                 (( ((rc == 0)) && (( mustpass != 0 )) )) ||
484                 (( ((rc != 0)) && (( mustpass == 0 )) )) &&
485                         ha_on $client rm -rf "$dir";
486                 } >>"$log" 2>&1 || rc=$?
487
488                 ha_info $client: rc=$rc mustpass=$mustpass
489
490                 # mustpass=0 means that failure is expected
491                 if (( rc !=0 )); then
492                         if (( mustpass != 0 )); then
493                                 touch "$ha_fail_file"
494                                 touch "$ha_stop_file"
495                                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
496                         else
497                                 # Ok to fail
498                                 rc=0
499                         fi
500                 elif (( mustpass == 0 )); then
501                         touch "$ha_fail_file"
502                         touch "$ha_stop_file"
503                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
504                 fi
505                 echo rc=$rc mustpass=$mustpass >"$status"
506
507                 nr_loops=$((nr_loops + 1))
508         done
509
510         [ $nr_loops -ne 0 ] &&
511                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
512
513         ha_info "$tag stopped: rc=$rc mustpass=$mustpass \
514                 avg loop time $avg_loop_time"
515 }
516
517 ha_start_mpi_loads()
518 {
519         local client
520         local load
521         local tag
522         local status
523         local n
524         local nparam
525         local machines
526         local m
527         local -a mach
528         local mpiuser
529         local nmpi
530
531         # ha_mpi_instances defines the number of
532         # clients start mpi loads; should be <= ${#ha_clients[@]}
533         # do nothing if
534         #    ha_mpi_instances = 0
535         # or
536         #    ${#ha_mpi_load_tags[@]} =0
537         local inst=$ha_mpi_instances
538         (( inst == 0 )) || (( ${#ha_mpi_load_tags[@]} == 0 )) &&
539                 ha_info "no mpi load to start" &&
540                 return 0
541
542         (( inst <= ${#ha_clients[@]} )) || inst=${#ha_clients[@]}
543
544         # Define names for machinefiles for each client set
545         for (( n=0; n < $ha_nclientsset; n++ )); do
546                 mach[$n]=$ha_machine_file$n
547         done
548
549         for ((n = 0; n < ${#ha_clients[@]}; n++)); do
550                 m=$(( n % ha_nclientsset))
551                 machines=${mach[m]}
552                 ha_info machine_file=$machines
553                 echo ${ha_clients[n]} >> $machines
554         done
555         local dirname=$(dirname $ha_machine_file)
556         for client in ${ha_clients[@]}; do
557                 ha_on $client mkdir -p $dirname
558                 scp $ha_machine_file* $client:$dirname
559         done
560
561         for ((n = 0; n < $inst; n++)); do
562                 client=${ha_clients[n]}
563                 nmpi=$((n % ${#ha_users[@]}))
564                 mpiuser=${ha_users[nmpi]}
565                 for ((load = 0; load < ${#ha_mpi_load_tags[@]}; load++)); do
566                         tag=${ha_mpi_load_tags[$load]}
567                         status=$ha_status_file_prefix-$tag-$client
568                         # ha_nparams_ior
569                         # ha_nparams_simul
570                         local num=ha_nparams_$tag
571                         nparam=$((n % num))
572                         local aref=ha_params_$tag[nparam]
573                         local parameter=${!aref}
574                         local nstripe=$((n % ha_nstripe_clients))
575                         aref=ha_stripe_clients[nstripe]
576                         local stripe=${!aref}
577                         local m=$(( n % ha_nclientsset))
578                         machines=${mach[m]}
579                         local mustpass=1
580                         [[ $ha_ninstmustfail == 0 ]] ||
581                                 mustpass=$(( n % ha_ninstmustfail ))
582                         ha_repeat_mpi_load $client $load $status "$parameter" \
583                                 $machines "$stripe" "$mpiuser" "$mustpass" \
584                                 "${ha_mpiopts[$mpiuser]} $ha_mpirun_options" &
585                                 ha_status_files+=("$status")
586                 done
587         done
588 }
589
590 ha_repeat_nonmpi_load()
591 {
592         local client=$1
593         local load=$2
594         local status=$3
595         local tag=${ha_nonmpi_load_tags[$load]}
596         local cmd=${ha_nonmpi_load_cmds[$tag]}
597         local dir=$ha_test_dir/$client-$tag
598         local log=$ha_tmp_dir/$client-$tag
599         local rc=0
600         local nr_loops=0
601         local avg_loop_time=0
602         local start_time=$(date +%s)
603
604         cmd=${cmd//"{}"/$dir}
605
606         ha_info "Starting $tag on $client"
607
608         while [ ! -e "$ha_stop_file" ] && ((rc == 0)); do
609                 ha_info "$client Starts: $cmd" 2>&1 |  tee -a $log
610                 ha_on $client "mkdir -p $dir &&                              \
611                         $cmd &&                                              \
612                         rm -rf $dir" >>"$log" 2>&1 || rc=$?
613
614                 if ((rc != 0)); then
615                         ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
616                         touch "$ha_fail_file"
617                         touch "$ha_stop_file"
618                 fi
619                 echo $rc >"$status"
620
621                 nr_loops=$((nr_loops + 1))
622         done
623
624         [ $nr_loops -ne 0 ] &&
625                 avg_loop_time=$((($(date +%s) - start_time) / nr_loops))
626
627         ha_info "$tag on $client stopped: rc $rc avg loop time ${avg_loop_time}s"
628 }
629
630 ha_start_nonmpi_loads()
631 {
632     local client
633     local load
634     local tag
635     local status
636
637     for client in ${ha_clients[@]}; do
638         for ((load = 0; load < ${#ha_nonmpi_load_tags[@]}; load++)); do
639             tag=${ha_nonmpi_load_tags[$load]}
640             status=$ha_status_file_prefix-$tag-$client
641             ha_repeat_nonmpi_load $client $load $status &
642             ha_status_files+=("$status")
643         done
644     done
645 }
646
647 ha_lfsck_bg () {
648         rm -f $ha_lfsck_log
649         rm -f $ha_lfsck_stop
650
651         ha_info "LFSCK BG"
652         while [ true ]; do
653                 [ -f $ha_lfsck_stop ] && ha_info "LFSCK stopped" && break
654                 [ -f $ha_stop_file ] &&
655                         ha_info "$ha_stop_file found! LFSCK not started" &&
656                         break
657                 ha_start_lfsck 2>&1 | tee -a $ha_lfsck_log
658                 sleep 1
659         done &
660         LFSCK_BG_PID=$!
661         ha_info LFSCK BG PID: $LFSCK_BG_PID
662 }
663
664 ha_wait_lfsck_completed () {
665         local -a status
666         local -a types=($ha_lfsck_types)
667         local type
668         local s
669
670         local nodes="${ha_servers[@]}"
671         nodes=${nodes// /,}
672
673         # -A start LFSCK on all nodes
674         # -t default all
675         [ ${#types[@]} -eq 0 ] && types=(namespace layout)
676         ha_info "Waiting LFSCK completed in $ha_lfsck_wait sec: types ${types[@]}"
677         for type in ${types[@]}; do
678                 eval var_$type=0
679                 for (( i=0; i<=ha_lfsck_wait; i++)); do
680                         status=($(ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | \
681                                 awk '/status/ { print $3 }'))
682                         for (( s=0; s<${#status[@]}; s++ )); do
683                                 # "partial" is expected after HARD failover
684                                 [[ "${status[s]}" = "completed" ]] ||
685                                 [[ "${status[s]}" = "partial" ]] ||  break
686                         done
687                         [[ $s -eq ${#status[@]} ]] && eval var_$type=1 && break
688                         sleep 1
689                 done
690                 ha_info "LFSCK $type status in $i sec:"
691                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null | grep status
692
693         done
694
695         for type in ${types[@]}; do
696                 local var=var_$type
697                 ha_on $nodes lctl get_param -n *.*.lfsck_$type 2>/dev/null
698                 [[ ${!var} -eq 1 ]] ||
699                         { ha_info "lfsck not completed in $ha_lfsck_wait sec";
700                         return 1; }
701         done
702         return 0
703 }
704
705 ha_start_lfsck()
706 {
707         local -a types=($ha_lfsck_types)
708         local rc=0
709
710         # -A: start LFSCK on all nodes via the specified MDT device
711         # (see "-M" option) by single LFSCK command
712         local params=" -A -r $ha_lfsck_custom_params"
713
714         # use specified device if set
715         [ -n "$ha_lfsck_device" ] && params="-M $ha_lfsck_device $params"
716
717         # -t: check type(s) to be performed (default all)
718         # check only specified types if set
719         if [ ${#types[@]} -ne 0 ]; then
720                 local type="${types[@]}"
721                 params="$params -t ${type// /,}"
722         fi
723
724         ha_info "LFSCK start $params"
725         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
726         if [ $rc -ne 0 ]; then
727                 if [ -e $ha_lfsck_lock ]; then
728                         rc=0
729                         ha_wait_unlock $ha_lfsck_lock
730                         ha_sleep 120
731                         ha_on $ha_lfsck_node "lctl lfsck_start $params" || rc=1
732                 fi
733         fi
734
735         [ $rc -eq 0 ] ||
736                 { touch "$ha_fail_file"; touch "$ha_stop_file";
737                 touch $ha_lfsck_stop; return 1; }
738
739         ha_wait_lfsck_completed ||
740                 { touch "$ha_fail_file"; touch "$ha_stop_file";
741                 touch $ha_lfsck_stop; return 1; }
742
743         return 0
744 }
745
746 ha_lfsck_repaired()
747 {
748         local n=0
749
750         n=$(cat $ha_lfsck_log | awk '/repaired/ {print $3}' |\
751                 awk '{sum += $1} END { print sum }')
752         [ $n -eq 0] ||
753                 { ha_info "Total repaired: $n";
754                 touch "$ha_fail_file"; return 1; }
755         return 0
756 }
757
758 ha_start_loads()
759 {
760         $ha_lfsck_bg && ha_lfsck_bg
761         trap ha_trap_stop_signals $ha_stop_signals
762         ha_start_nonmpi_loads
763         ha_start_mpi_loads
764 }
765
766 ha_stop_loads()
767 {
768         touch $ha_stop_file
769         # true because of lfsck_bg could be stopped already
770         $ha_lfsck_bg && wait $LFSCK_BG_PID || true
771         trap - $ha_stop_signals
772         ha_info "Waiting for workloads to stop"
773         wait
774 }
775
776 ha_wait_loads()
777 {
778     local file
779     local end=$(($(date +%s) + ha_load_timeout))
780
781     ha_info "Waiting $ha_load_timeout sec for workload status..."
782     rm -f "${ha_status_files[@]}"
783
784         #
785         # return immediately if ha_stop_file exists,
786         # all status_files not needed to be checked
787         #
788         for file in "${ha_status_files[@]}"; do
789                 if [ -e "$ha_stop_file" ]; then
790                         ha_info "$ha_stop_file found! Stop."
791                         break
792                 fi
793                 #
794                 # Wait status file created during ha_load_timeout.
795                 # Existing file guarantees that some application
796                 # is completed. If no status file was created
797                 # this function guarantees that we allow
798                 # applications to continue after/before
799                 # failover/failback during ha_load_timeout time.
800                 #
801                 until [ -e "$file" ] || (($(date +%s) >= end)); do
802                         #
803                         # check ha_stop_file again, it could appear
804                         # during ha_load_timeout
805                         #
806                         if [ -e "$ha_stop_file" ]; then
807                                 ha_info "$ha_stop_file found! Stop."
808                                 break
809                         fi
810                         ha_sleep 1 >/dev/null
811                 done
812         done
813 }
814
815 ha_powermanage()
816 {
817         local nodes=$1
818         local expected_state=$2
819         local state
820         local -a states
821         local i
822         local rc=0
823
824         # store pm -x -q $nodes results in a file to have
825         # more information about nodes statuses
826         ha_on $ha_pm_host pm -x -q $nodes | awk '{print $2 $3}' > $ha_pm_states
827         rc=${PIPESTATUS[0]}
828         echo pmrc=$rc
829
830         while IFS=": " read node state; do
831                 [[ "$state" = "$expected_state" ]] && {
832                         nodes=${nodes/$node/}
833                         nodes=${nodes//,,/,}
834                         nodes=${nodes/#,}
835                         nodes=${nodes/%,}
836                 }
837         done < $ha_pm_states
838
839         if [ -n "$nodes" ]; then
840                 cat $ha_pm_states
841                 return 1
842         fi
843         return 0
844 }
845
846 ha_power_down_cmd_fn()
847 {
848         local nodes=$1
849         local cmd
850
851         case $ha_power_down_cmd in
852         # format is: POWER_DOWN=sysrqcrash
853         sysrqcrash) cmd="pdsh -S -w $nodes 'echo c > /proc/sysrq-trigger' &" ;;
854         *) cmd="$ha_power_down_cmd $nodes" ;;
855         esac
856
857         eval $cmd
858 }
859
860 ha_power_down()
861 {
862         local nodes=$1
863         local rc=1
864         local i
865         local state
866
867         case $ha_power_down_cmd in
868                 *pm*) state=off ;;
869                 sysrqcrash) state=off ;;
870                 *) state=on;;
871         esac
872
873         if $ha_lfsck_bg && [[ ${nodes//,/ /} =~ $ha_lfsck_node ]]; then
874                 ha_info "$ha_lfsck_node down, delay start LFSCK"
875                 ha_lock $ha_lfsck_lock
876         fi
877
878         ha_info "Powering down $nodes : cmd: $ha_power_down_cmd"
879         for (( i=0; i<10; i++ )) {
880                 ha_info "attempt: $i"
881                 ha_power_down_cmd_fn $nodes &&
882                         ha_powermanage $nodes $state && rc=0 && break
883                 sleep $ha_power_delay
884         }
885
886         [ $rc -eq 0 ] || {
887                 ha_info "Failed Powering down in $i attempts:" \
888                         "$ha_power_down_cmd"
889                 cat $ha_pm_states
890                 exit 1
891         }
892 }
893
894 ha_get_pair()
895 {
896         local node=$1
897         local i
898
899         for ((i=0; i<${#ha_victims[@]}; i++)) {
900                 [[ ${ha_victims[i]} == $node ]] && echo ${ha_victims_pair[i]} &&
901                         return
902         }
903         [[ $i -ne ${#ha_victims[@]} ]] ||
904                 ha_error "No pair found!"
905 }
906
907 ha_power_up_delay()
908 {
909         local nodes=$1
910         local end=$(($(date +%s) + ha_node_up_delay))
911         local rc
912
913         if [[ ${#ha_victims_pair[@]} -eq 0 ]]; then
914                 ha_sleep $ha_node_up_delay
915                 return 0
916         fi
917
918         # Check CRM status on failover pair
919         while (($(date +%s) <= end)); do
920                 rc=0
921                 for n in ${nodes//,/ }; do
922                         local pair=$(ha_get_pair $n)
923                         local status=$(ha_on $pair crm_mon -1rQ | \
924                                 grep -w $n | head -1)
925
926                         ha_info "$n pair: $pair status: $status"
927                         [[ "$status" == *OFFLINE* ]] ||
928                                 rc=$((rc + $?))
929                         ha_info "rc: $rc"
930                 done
931
932                 if [[ $rc -eq 0 ]];  then
933                         ha_info "CRM: Got all victims status OFFLINE"
934                         return 0
935                 fi
936                 sleep 60
937         done
938
939         ha_info "$nodes CRM status not OFFLINE"
940         for n in ${nodes//,/ }; do
941                 local pair=$(ha_get_pair $n)
942
943                 ha_info "CRM --- $n"
944                 ha_on $pair crm_mon -1rQ
945         done
946         ha_error "CRM: some of $nodes are not OFFLINE in $ha_node_up_delay sec"
947         exit 1
948 }
949
950 ha_power_up()
951 {
952         local nodes=$1
953         local rc=1
954         local i
955
956         ha_power_up_delay $nodes
957         ha_info "Powering up $nodes : cmd: $ha_power_up_cmd"
958         for (( i=0; i<10; i++ )) {
959                 ha_info "attempt: $i"
960                 $ha_power_up_cmd $nodes &&
961                         ha_powermanage $nodes on && rc=0 && break
962                 sleep $ha_power_delay
963         }
964
965         [ $rc -eq 0 ] || {
966                 ha_info "Failed Powering up in $i attempts: $ha_power_up_cmd"
967                 cat $ha_pm_states
968                 exit 1
969         }
970 }
971
972 #
973 # rand MAX
974 #
975 # Print a random integer within [0, MAX).
976 #
977 ha_rand()
978 {
979     local max=$1
980
981     #
982     # See "5.2 Bash Variables" from "info bash".
983     #
984     echo -n $((RANDOM * max / 32768))
985 }
986
987 ha_aim()
988 {
989         local i
990         local nodes
991
992         if $ha_simultaneous ; then
993                 nodes=$(echo ${ha_victims[@]})
994                 nodes=${nodes// /,}
995         else
996                 i=$(ha_rand ${#ha_victims[@]})
997                 nodes=${ha_victims[$i]}
998         fi
999
1000         echo -n $nodes
1001 }
1002
1003 ha_wait_nodes()
1004 {
1005         local nodes=$1
1006         local end=$(($(date +%s) + 10 * 60))
1007
1008         ha_info "Waiting for $nodes to boot up"
1009         until ha_on $nodes hostname >/dev/null 2>&1 ||
1010                 [ -e "$ha_stop_file" ] ||
1011                         (($(date +%s) >= end)); do
1012                 ha_sleep 1 >/dev/null
1013         done
1014 }
1015
1016 ha_failback()
1017 {
1018         local nodes=$1
1019         ha_info "Failback resources on $nodes in $ha_failback_delay sec"
1020
1021         ha_sleep $ha_failback_delay
1022         [ "$ha_failback_cmd" ] ||
1023         {
1024                 ha_info "No failback command set, skiping"
1025                 return 0
1026         }
1027
1028         $ha_failback_cmd $nodes
1029         [ -e $ha_lfsck_lock ] && ha_unlock $ha_lfsck_lock || true
1030 }
1031
1032 ha_summarize()
1033 {
1034     ha_info "---------------8<---------------"
1035     ha_info "Summary:"
1036     ha_info "    Duration: $(($(date +%s) - $ha_start_time))s"
1037     ha_info "    Loops: $ha_nr_loops"
1038 }
1039
1040 ha_killer()
1041 {
1042         local nodes
1043
1044         while (($(date +%s) < ha_start_time + ha_expected_duration)) &&
1045                         [ ! -e "$ha_stop_file" ]; do
1046                 ha_info "---------------8<---------------"
1047
1048                 $ha_workloads_only || nodes=$(ha_aim)
1049
1050                 ha_info "Failing $nodes"
1051                 $ha_workloads_only && ha_info "    is skipped: workload only..."
1052
1053                 ha_sleep $(ha_rand $ha_max_failover_period)
1054                 $ha_workloads_only || ha_power_down $nodes
1055                 ha_sleep 10
1056                 ha_wait_loads || return
1057
1058                 if [ -e $ha_stop_file ]; then
1059                         $ha_workloads_only || ha_power_up $nodes
1060                         break
1061                 fi
1062
1063                 ha_info "Bringing $nodes back"
1064                 ha_sleep $(ha_rand 10)
1065                 $ha_workloads_only ||
1066                 {
1067                         ha_power_up $nodes
1068                         ha_wait_nodes $nodes
1069                         ha_failback $nodes
1070                 }
1071
1072                 #
1073                 # Wait for the failback to start.
1074                 #
1075                 ha_sleep 60
1076                 ha_wait_loads || return
1077
1078                 ha_sleep $(ha_rand 20)
1079
1080                 ha_nr_loops=$((ha_nr_loops + 1))
1081                 ha_info "Loop $ha_nr_loops done"
1082         done
1083         ha_summarize
1084 }
1085
1086 ha_main()
1087 {
1088         ha_process_arguments "$@"
1089         ha_check_env
1090
1091         ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1092                 "START: $0: $(date +%H:%M:%S' '%s)"
1093         trap ha_trap_exit EXIT
1094         mkdir "$ha_tmp_dir"
1095         ha_on ${ha_clients[0]} mkdir "$ha_test_dir"
1096         ha_on ${ha_clients[0]} " \
1097                 $LFS setstripe $ha_stripe_params $ha_test_dir"
1098
1099         ha_start_loads
1100         ha_wait_loads
1101
1102         if $ha_workloads_dry_run; then
1103                 ha_sleep 5
1104         else
1105                 ha_killer
1106                 ha_dump_logs "${ha_clients[*]} ${ha_servers[*]}"
1107         fi
1108
1109         ha_stop_loads
1110
1111         $ha_lfsck_after && ha_start_lfsck | tee -a $ha_lfsck_log
1112
1113         $ha_lfsck_fail_on_repaired && ha_lfsck_repaired
1114
1115         if [ -e "$ha_fail_file" ]; then
1116                 exit 1
1117         else
1118                 ha_log "${ha_clients[*]} ${ha_servers[*]}" \
1119                         "END: $0: $(date +%H:%M:%S' '%s)"
1120                 exit 0
1121         fi
1122 }
1123
1124 ha_main "$@"