Whamcloud - gitweb
LU-9771 flr: lfs mirror create and extend commands
[fs/lustre-release.git] / lustre / tests / sanity-flr.sh
index 2318044..a88b3b0 100644 (file)
@@ -2,8 +2,6 @@
 #
 # Run select tests by setting ONLY, or as arguments to the script.
 # Skip specific tests by setting EXCEPT.
-#
-# Run test by setting NOSETUP=true when ltest has setup env for us
 set -e
 set +o posix
 
@@ -12,38 +10,29 @@ export PATH=$PWD/$SRCDIR:$SRCDIR:$PWD/$SRCDIR/../utils:$PATH:/sbin
 
 ONLY=${ONLY:-"$*"}
 # Bug number for skipped test:
-ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT"
+ALWAYS_EXCEPT="$SANITY_FLR_EXCEPT 201"
 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
 
-[ "$ALWAYS_EXCEPT$EXCEPT" ] &&
-       echo "Skipping tests: $ALWAYS_EXCEPT $EXCEPT"
-
-TMP=${TMP:-/tmp}
-CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
-LFS=${LFS:-lfs}
-LCTL=${LCTL:-lctl}
-MULTIOP=${MULTIOP:-multiop}
-
 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
 . $LUSTRE/tests/test-framework.sh
 init_test_env $@
 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
 init_logging
 
-check_and_setup_lustre
-DIR=${DIR:-$MOUNT}
-assert_DIR
-
-if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ]]; then
-       skip_env "Need MDS version at least 2.7.64" && exit
+if [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.53) ]]; then
+       skip_env "Need MDS version at least 2.10.53" && exit
 fi
 
-build_test_filter
-
 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
        error "\$RUNAS_ID set to 0, but \$UID is also 0!"
 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
 
+check_and_setup_lustre
+DIR=${DIR:-$MOUNT}
+assert_DIR
+
+build_test_filter
+
 # global array to store mirror IDs
 declare -a mirror_array
 get_mirror_ids() {
@@ -94,24 +83,25 @@ start_osts() {
 test_1() {
        local tf=$DIR/$tfile
        local mirror_count=16 # LUSTRE_MIRROR_COUNT_MAX
-
-       $LFS setstripe -E EOF -c -1 $tf
-
+       local mirror_create_cmd="$LFS mirror create"
        local stripes[0]=$OSTCOUNT
 
+       mirror_create_cmd+=" -N -c ${stripes[0]}"
        for ((i = 1; i < $mirror_count; i++)); do
                # add mirrors with different stripes to the file
                stripes[$i]=$((RANDOM % OSTCOUNT))
                [ ${stripes[$i]} -eq 0 ] && stripes[$i]=1
 
-               $LFS setstripe --component-add --mirror -c ${stripes[$i]} $tf
+               mirror_create_cmd+=" -N -c ${stripes[$i]}"
        done
 
+       eval $mirror_create_cmd $tf || error "creating mirrored file $tf failed"
+
        [ $(get_mirror_ids $tf) -ne $mirror_count ] &&
                error "mirror count error"
 
        # can't create mirrors exceeding LUSTRE_MIRROR_COUNT_MAX
-       $LFS setstripe --component-add --mirror $tf &&
+       $LFS mirror extend -N $tf &&
                error "Creating the $((mirror_count+1))th mirror succeeded"
 
        local ids=($($LFS getstripe $tf | awk '/lcme_id/{print $2}' |
@@ -148,11 +138,11 @@ test_2() {
 
        local layout=$($LFS getstripe $tf2 | grep -A 4 lmm_objects)
 
-       $LFS setstripe --component-add --mirror=$tf2 $tf
+       $LFS mirror extend -N -f $tf2 $tf ||
+               error "merging $tf2 into $tf failed"
 
        [ $(get_mirror_ids $tf) -ne 2 ] && error "mirror count should be 2"
-       $LFS getstripe $tf2 | grep -q 'no stripe info' ||
-               error "$tf2 still has stripe info"
+       [[ ! -e $tf2 ]] || error "$tf2 was not unlinked"
 }
 run_test 2 "create components from existing files"
 
@@ -164,7 +154,7 @@ test_3() {
                $LFS setstripe -E -1 $DIR/$tdir-$i/$tfile
        done
 
-       $LFS setstripe --component-add --mirror=$DIR/$tdir-1/$tfile \
+       $LFS mirror extend -N -f $DIR/$tdir-1/$tfile \
                $DIR/$tdir-0/$tfile || error "creating mirrors"
 
        # mdt doesn't support to cancel layout lock for remote objects, do
@@ -198,7 +188,8 @@ test_21() {
        local blocks=$(du -kc $tf $tf2 | awk '/total/{print $1}')
 
        # add component
-       $LFS setstripe --component-add --mirror=$tf2 $tf
+       $LFS mirror extend -N -f $tf2 $tf ||
+               error "merging $tf2 into $tf failed"
 
        # cancel layout lock
        cancel_lru_locks mdc
@@ -230,7 +221,8 @@ test_22() {
        dd if=/dev/zero of=$tf bs=1M count=$((RANDOM % 20 + 1))
 
        # add component, two mirrors located on the same OST ;-)
-       $LFS setstripe --component-add --mirror -o 0 $tf
+       $LFS mirror extend -N -o 0 $tf ||
+               error "extending mirrored file $tf failed"
 
        size_blocks=$(stat --format="%b %s" $tf)
 
@@ -252,8 +244,8 @@ run_test 22 "no glimpse to OSTs for READ_ONLY files"
 test_31() {
        local tf=$DIR/$tfile
 
-       $LFS setstripe -E EOF -o 0 $tf
-       $LFS setstripe --component-add --mirror -o 1 $tf
+       $LFS mirror create -N -o 0 -N -o 1 $tf ||
+               error "creating mirrored file $tf failed"
 
        #define OBD_FAIL_GLIMPSE_IMMUTABLE 0x1A00
        $LCTL set_param fail_loc=0x1A00
@@ -298,7 +290,8 @@ test_32() {
        local cksum=$(md5sum $DIR/$tfile)
 
        # create a new mirror in sync mode
-       $LFS setstripe --component-add --mirror -o 1 $DIR/$tfile
+       $LFS mirror extend -N -o 1 $DIR/$tfile ||
+               error "extending mirrored file $DIR/$tfile failed"
 
        # make sure the mirrored file was created successfully
        [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
@@ -342,13 +335,18 @@ test_33() {
        done
 
        # create a mirrored file
-       $LFS setstripe --component-add --mirror=$DIR/$tfile-2 $DIR/$tfile
-
-       # make sure that $tfile has two mirrors and $tfile-2 has no stripe
+       $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile &&
+               error "merging $DIR/$tfile-2 into $DIR/$tfile" \
+                     "with verification should fail"
+       $LFS mirror extend --no-verify -N -f $DIR/$tfile-2 $DIR/$tfile ||
+               error "merging $DIR/$tfile-2 into $DIR/$tfile" \
+                     "without verification failed"
+
+       # make sure that $tfile has two mirrors and $tfile-2 does not exist
        [ $(get_mirror_ids $DIR/$tfile) -eq 2 ] ||
                { $LFS getstripe $DIR/$tfile; error "expected count 2"; }
-       $LFS getstripe $DIR/$tfile-2 | grep -q "no stripe info" ||
-               { $LFS getstripe $DIR/$tfile; error "expected no stripe"; }
+
+       [[ ! -e $DIR/$tfile-2 ]] || error "$DIR/$tfile-2 was not unlinked"
 
        # execpted file size
        local fsize=$((5 * max_count))
@@ -412,7 +410,8 @@ test_34a() {
                error "mirrored file size is not 3M"
 
        # merge a mirrored file
-       $LFS setstripe --component-add --mirror=$DIR/$tfile-2 $DIR/$tfile
+       $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
+               error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
 
        cancel_lru_locks osc
 
@@ -450,7 +449,8 @@ test_34b() {
                error "mirrored file size is not 3M"
 
        # merge a mirrored file
-       $LFS setstripe --component-add --mirror=$DIR/$tfile-2 $DIR/$tfile
+       $LFS mirror extend -N -f $DIR/$tfile-2 $DIR/$tfile ||
+               error "merging $DIR/$tfile-2 into $DIR/$tfile failed"
 
        cancel_lru_locks osc
 
@@ -475,7 +475,8 @@ test_35() {
        $LFS setstripe -E eof $tf
 
        # add an out-of-sync mirror to the file
-       $LFS setstripe --component-add --mirror -c 2 $tf
+       $LFS mirror extend -N -c 2 $tf ||
+               error "extending mirrored file $tf failed"
 
        $MULTIOP $tf oO_WRONLY:c ||
                error "write open a mirrored file failed"
@@ -504,8 +505,8 @@ create_file_36() {
                $LFS setstripe -E 1M -E 2M -E 4M -E eof -c -1 $tf
                $LFS setstripe -E 3M -E 6M -E eof -c -1 $tf-tmp
 
-               $LFS setstripe --component-add --mirror=$tf-tmp $tf
-               rm -f $tf-tmp
+               $LFS mirror extend -N -f $tf-tmp $tf ||
+                       error "merging $tf-tmp into $tf failed"
        done
 }
 
@@ -564,6 +565,357 @@ test_36() {
 }
 run_test 36 "write to mirrored files"
 
+create_files_37() {
+       local tf
+       local fsize=$1
+
+       echo "create test files with size $fsize .."
+
+       shift
+       for tf in "$@"; do
+               $LFS setstripe -E 1M -c 1 -E eof -c -1 $tf
+
+               dd if=/dev/urandom of=$tf bs=1M count=16 &> /dev/null
+               $TRUNCATE $tf $fsize
+       done
+}
+
+test_37()
+{
+       local tf=$DIR/$tfile
+       local tf2=$DIR/$tfile-2
+       local tf3=$DIR/$tfile-3
+
+       create_files_37 $((RANDOM + 15 * 1048576)) $tf $tf2 $tf3
+
+       # assume the mirror id will be 1, 2, and 3
+       declare -A checksums
+       checksums[1]=$(md5sum $tf | cut -f 1 -d' ')
+       checksums[2]=$(md5sum $tf2 | cut -f 1 -d' ')
+       checksums[3]=$(md5sum $tf3 | cut -f 1 -d' ')
+
+       printf '%s\n' "${checksums[@]}"
+
+       # merge these files into a mirrored file
+       $LFS mirror extend --no-verify -N -f $tf2 $tf ||
+               error "merging $tf2 into $tf failed"
+       $LFS mirror extend --no-verify -N -f $tf3 $tf ||
+               error "merging $tf3 into $tf failed"
+
+       get_mirror_ids $tf
+
+       # verify mirror read, checksums should equal to the original files'
+       echo "Verifying mirror read .."
+
+       local sum
+       for i in ${mirror_array[@]}; do
+               sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
+               [ "$sum" = "${checksums[$i]}" ] ||
+                       error "$i: mismatch: \'${checksums[$i]}\' vs. \'$sum\'"
+       done
+
+       # verify mirror copy, write to this mirrored file will invalidate
+       # the other two mirrors
+       echo "Verifying mirror copy .."
+
+       local osts=$(comma_list $(osts_nodes))
+
+       # define OBD_FAIL_OST_SKIP_LV_CHECK     0x241
+       do_nodes $osts lctl set_param fail_loc=0x241
+
+       mirror_io copy -i ${mirror_array[0]} \
+               -t $(echo ${mirror_array[@]:1} | tr ' ' ',') $tf ||
+                       error "mirror copy error"
+
+       do_nodes $osts lctl set_param fail_loc=0
+
+       # verify copying is successful by checking checksums
+       remount_client $MOUNT
+       for i in ${mirror_array[@]}; do
+               sum=$(mirror_io dump -i $i $tf | md5sum | cut -f 1 -d' ')
+               [ "$sum" = "${checksums[1]}" ] ||
+                       error "$i: mismatch checksum after copy"
+       done
+
+       rm -f $tf
+}
+run_test 37 "mirror I/O API verification"
+
+verify_flr_state()
+{
+       local tf=$1
+       local expected_state=$2
+       local state_strings=("not_flr" "read_only" "write_pending" \
+               "sync_pending")
+
+       local state=$($LFS getstripe -v $tf | awk '/lcm_flags/{ print $2 }')
+       [ $expected_state = ${state_strings[$state]} ] ||
+               error "expected: $expected_state, " \
+                       "actual ${state_strings[$state]}($state)"
+}
+
+test_38() {
+       local tf=$DIR/$tfile
+       local ref=$DIR/${tfile}-ref
+
+       $LFS setstripe -E 1M -c 1 -E 4M -c 2 -E eof -c -1 $tf
+       $LFS setstripe -E 2M -c 1 -E 6M -c 2 -E 8M -c -1 -E eof -c -1 $tf-2
+       $LFS setstripe -E 4M -c 1 -E 8M -c 2 -E eof -c -1 $tf-3
+
+       # instantiate all components
+       $LFS mirror extend -N -f $tf-2 $tf ||
+               error "merging $tf-2 into $tf failed"
+       $LFS mirror extend -N -f $tf-3 $tf ||
+               error "merging $tf-3 into $tf failed"
+       $LFS mirror extend -N -c 1 $tf ||
+               error "extending mirrored file $tf failed"
+
+       verify_flr_state $tf "read_only"
+
+       dd if=/dev/urandom of=$ref  bs=1M count=16 &> /dev/null
+
+       local fsize=$((RANDOM << 8 + 1048576))
+       $TRUNCATE $ref $fsize
+
+       local ref_cksum=$(md5sum $ref | cut -f 1 -d' ')
+
+       # case 1: verify write to mirrored file & resync work
+       cp $ref $tf || error "copy from $ref to $f error"
+       verify_flr_state $tf "write_pending"
+
+       local file_cksum=$(md5sum $tf | cut -f 1 -d' ')
+       [ "$file_cksum" = "$ref_cksum" ] || error "write failed, cksum mismatch"
+
+       get_mirror_ids $tf
+       echo "mirror IDs: ${mirror_array[@]}"
+
+       local valid_mirror stale_mirror id mirror_cksum
+       for id in "${mirror_array[@]}"; do
+               mirror_cksum=$(mirror_io dump -i $id $tf |
+                               md5sum | cut -f 1 -d' ')
+               [ "$ref_cksum" == "$mirror_cksum" ] &&
+                       { valid_mirror=$id; continue; }
+
+               stale_mirror=$id
+       done
+
+       [ -z "$stale_mirror" ] && error "stale mirror doesn't exist"
+       [ -z "$valid_mirror" ] && error "valid mirror doesn't exist"
+
+       mirror_io resync $tf || error "resync failed"
+       verify_flr_state $tf "read_only"
+
+       mirror_cksum=$(mirror_io dump -i $stale_mirror $tf |
+                       md5sum | cut -f 1 -d' ')
+       [ "$file_cksum" = "$ref_cksum" ] || error "resync failed"
+
+       # case 2: inject an error to make mirror_io exit after changing
+       # the file state to sync_pending so that we can start a concurrent
+       # write.
+       $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
+       verify_flr_state $tf "write_pending"
+
+       mirror_io resync -e resync_start $tf && error "resync succeeded"
+       verify_flr_state $tf "sync_pending"
+
+       # from sync_pending to write_pending
+       $MULTIOP $tf oO_WRONLY:w$((RANDOM % 1048576 + 1024))c
+       verify_flr_state $tf "write_pending"
+
+       mirror_io resync -e resync_start $tf && error "resync succeeded"
+       verify_flr_state $tf "sync_pending"
+
+       # from sync_pending to read_only
+       mirror_io resync $tf || error "resync failed"
+       verify_flr_state $tf "read_only"
+}
+run_test 38 "resync"
+
+ctrl_file=$(mktemp /tmp/CTRL.XXXXXX)
+lock_file=$(mktemp /var/lock/FLR.XXXXXX)
+
+write_file_200() {
+       local tf=$1
+
+       local fsize=$(stat --printf=%s $tf)
+
+       while [ -f $ctrl_file ]; do
+               local off=$((RANDOM << 8))
+               local len=$((RANDOM << 5 + 131072))
+
+               [ $((off + len)) -gt $fsize ] && {
+                       fsize=$((off + len))
+                       echo "Extending file size to $fsize .."
+               }
+
+               flock -s $lock_file -c \
+                       "$MULTIOP $tf oO_WRONLY:z${off}w${len}c" ||
+                               { rm -f $ctrl_file;
+                                 error "failed writing to $off:$len"; }
+               sleep 0.$((RANDOM % 2 + 1))
+       done
+}
+
+read_file_200() {
+       local tf=$1
+
+       while [ -f $ctrl_file ]; do
+               flock -s $lock_file -c "cat $tf &> /dev/null" ||
+                       { rm -f $ctrl_file; error "read failed"; }
+               sleep 0.$((RANDOM % 2 + 1))
+       done
+}
+
+resync_file_200() {
+       local tf=$1
+
+       options=("" "-e resync_start" "-e delay_before_copy -d 1" "" "")
+
+       exec 200<>$lock_file
+       while [ -f $ctrl_file ]; do
+               local index=$((RANDOM % ${#options[@]}))
+               local lock_taken=false
+
+               [ $((RANDOM % 4)) -eq 0 ] && {
+                       index=0
+                       lock_taken=true
+                       echo -n "lock to "
+               }
+
+               echo -n "resync file $tf with '${options[$index]}' .."
+
+               $lock_taken && flock -x 200
+               mirror_io resync ${options[$index]} $tf &> /dev/null &&
+                       echo "done" || echo "failed"
+
+               $lock_taken && flock -u 200
+
+               sleep 0.$((RANDOM % 8 + 1))
+       done
+}
+
+test_200() {
+       local tf=$DIR/$tfile
+       local tf2=$DIR2/$tfile
+       local tf3=$DIR3/$tfile
+
+       $LFS setstripe -E 1M -E 2M -c 2 -E 4M -E 16M -E eof $tf
+       $LFS setstripe -E 2M -E 6M -c 2 -E 8M -E 32M -E eof $tf-2
+       $LFS setstripe -E 4M -c 2 -E 8M -E 64M -E eof $tf-3
+
+       $LFS mirror extend -N -f $tf-2 $tf ||
+               error "merging $tf-2 into $tf failed"
+       $LFS mirror extend -N -f $tf-3 $tf ||
+               error "merging $tf-3 into $tf failed"
+
+       mkdir -p $MOUNT2 && mount_client $MOUNT2
+
+       mkdir -p $MOUNT3 && mount_client $MOUNT3
+
+       verify_flr_state $tf3 "read_only"
+
+       #define OBD_FAIL_FLR_RANDOM_PICK_MIRROR 0x1A03
+       $LCTL set_param fail_loc=0x1A03
+
+       local mds_idx=mds$(($($LFS getstripe -M $tf) + 1))
+       do_facet $mds_idx $LCTL set_param fail_loc=0x1A03
+
+       declare -a pids
+
+       write_file_200 $tf &
+       pids+=($!)
+
+       read_file_200 $tf &
+       pids+=($!)
+
+       write_file_200 $tf2 &
+       pids+=($!)
+
+       read_file_200 $tf2 &
+       pids+=($!)
+
+       resync_file_200 $tf3 &
+       pids+=($!)
+
+       local sleep_time=60
+       [ "$SLOW" = "yes" ] && sleep_time=360
+       while [ $sleep_time -gt 0 -a -f $ctrl_file ]; do
+               sleep 1
+               ((--sleep_time))
+       done
+
+       rm -f $ctrl_file
+
+       echo "Waiting ${pids[@]}"
+       wait ${pids[@]}
+
+       umount_client $MOUNT2
+       umount_client $MOUNT3
+
+       rm -f $lock_file
+
+       # resync and verify mirrors
+       mirror_io resync $tf
+       get_mirror_ids $tf
+
+       local csum=$(mirror_io dump -i ${mirror_array[0]} $tf | md5sum)
+       for id in ${mirror_array[@]:1}; do
+               [ "$(mirror_io dump -i $id $tf | md5sum)" = "$csum" ] ||
+                       error "checksum error for mirror $id"
+       done
+
+       true
+}
+run_test 200 "stress test"
+
+cleanup_test_201() {
+       trap 0
+       do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
+
+       umount_client $MOUNT2
+}
+
+test_201() {
+       local delay=${RESYNC_DELAY:-5}
+
+       MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
+              awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
+
+       trap cleanup_test_201 EXIT
+
+       CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
+                       changelog_register -n)
+
+       mkdir -p $MOUNT2 && mount_client $MOUNT2
+
+       local index=0
+       while :; do
+               local log=$($LFS changelog $MDT0 $index | grep FLRW)
+               [ -z "$log" ] && { sleep 1; continue; }
+
+               index=$(echo $log | awk '{print $1}')
+               local ts=$(date -d "$(echo $log | awk '{print $3}')" "+%s" -u)
+               local fid=$(echo $log | awk '{print $6}' | sed -e 's/t=//')
+               local file=$($LFS fid2path $MOUNT2 $fid 2> /dev/null)
+
+               ((++index))
+               [ -z "$file" ] && continue
+
+               local now=$(date +%s)
+
+               echo "file: $file $fid was modified at $ts, now: $now, " \
+                    "will be resynced at $((ts+delay))"
+
+               [ $now -lt $((ts + delay)) ] && sleep $((ts + delay - now))
+
+               mirror_io resync $file
+               echo "$file resync done"
+       done
+
+       cleanup_test_201
+}
+run_test 201 "FLR data mover"
+
 complete $SECONDS
 check_and_cleanup_lustre
 exit_status