Project

General

Profile

Bug #38135 » create_snapshot.sh

Bengen Tan, 02/01/2019 12:10 AM

 
### Generate RBD list
# RBD_POOL="test-rbd0"
# [[ -f /root/rbd-list ]] || time rbd ls ${RBD_POOL} | grep "^vpayno-sparse-" | sort -V > /root/rbd-list.txt
### Then use the following script to create snapshots
# cat testing-modify-create-random-snapshots-with-data-pB
#!/bin/bash
#
# testing-modify-create-random-snapshots-with-data-pB
#
# Version: 20181204-00
[[ -f /root/rbd-list ]] || time rbd ls ${RBD_POOL} | grep "^vpayno-sparse-" | sort -V > /root/rbd-list.txt
declare DATE
DATE="$(date "+%Y%m%d-%H%M%S")"
declare -r RBD_POOL=test-rbd0
declare -r RBD_SIZE_MAX=1 # MB
declare -r MAX_WORKERS=50
declare -i work_counter
declare -i random_int
[[ -d /mnt/rbd ]] || mkdir /mnt/rbd
rsleep()
{
local max_duration_str="${1:-}";
local -i offset="${2:-0}";
local -i max_duration="${max_duration_str//[a-z]/}";
local max_duration_units="${max_duration_str//[0-9]/}";
local -i random_time;
random_time="$(( (RANDOM % max_duration) + offset ))";
sleep "${random_time}${max_duration_units}"
}
get_rbd_name()
{
printf "vpayno-sparse-%050d" "${1}"
} # get_rbd_name()
mount_rbd_and_add_data()
{
local rbd_name="${1}" # pool/rbd
local MNTPNT_ROOT="/dev/shm/kcs_test"
local rbd_mnt_path
local home_file_name
local data_file_name
local dd_size
local date_in_seconds
if [[ $(rbd lock list ${rbd_name} | wc -l) -ne 0 ]]; then
printf "This rbd is in use, skipping ...\n"
echo rbd lock list "${rbd_name}"
time rbd lock list "${rbd_name}"
printf "\n"
return 1
fi
printf "Creating RBD lock ...\n"
echo "rbd lock add ${rbd_name} vpayno"
time if ! rbd lock add "${rbd_name}" vpayno; then
printf "Getting lock failed, skipping ...\n"
echo rbd lock list "${rbd_name}"
time rbd lock list "${rbd_name}"
return 1
fi
date_in_seconds="$(date +%s)"
#rank_id="${rbd_name##*/}"
rbd_mnt_path="${MNTPNT_ROOT}/rbdtests/${rbd_name##*/}"
home_file_name="${rbd_mnt_path}/home.img"
data_file_name="${rbd_mnt_path}/${date_in_seconds}.dd"
echo mkdir -pv "${rbd_mnt_path}"
mkdir -pv "${rbd_mnt_path}"
printf "\n"
sleep 1s
echo rbd map "${rbd_name}"
bench_rbd_dev="$(time rbd map "${rbd_name}" 2>/dev/null)" || return 1
echo "rbd dev: ${bench_rbd_dev}"
printf "\n"
sleep 1s
if ! tune2fs -l "${bench_rbd_dev}" >& /dev/null; then
echo mkfs.ext4 -L "TEST${rank_id}" -O ^has_journal "${bench_rbd_dev}"
time mkfs.ext4 -L "TEST${rank_id}" -O ^has_journal "${bench_rbd_dev}"
sleep 1s
echo tune2fs -o discard "${bench_rbd_dev}"
time tune2fs -o discard "${bench_rbd_dev}"
sleep 1s
fi
sleep 1s
echo mount -v "${bench_rbd_dev}" "${rbd_mnt_path}"
time mount -v "${bench_rbd_dev}" "${rbd_mnt_path}" || unmount_rbd "${bench_rbd_dev}" "${rbd_name}" "${rbd_mnt_path}"
printf "\n"
sleep 1s
printf "\n"
echo df -h "${rbd_mnt_path}"
time df -h "${rbd_mnt_path}"
printf "\n"
if [[ -f ${home_file_name} ]]; then
printf "Removing the random home.img file from the RBD.\n"
echo rm -fv "${home_file_name}"
time rm -fv "${home_file_name}"
else
#dd_size="$(( 1024 * 1024 ))"
dd_size="1024"
printf "Writing %s of random data to the RBD (home.img)...\n" "${dd_size}KB"
echo dd if=/dev/urandom bs=1024 count="${dd_size}" of="${home_file_name}" conv=sparse
time dd if=/dev/urandom bs=1024 count="${dd_size}" of="${home_file_name}" conv=sparse
fi
printf "\n"
dd_size="$(( 1024 * (RANDOM % RBD_SIZE_MAX) ))"
printf "Writing %s of random data to the RBD (time.dd)...\n" "${dd_size}KB"
echo rm -fv "${rbd_mnt_path}/*.dd"
time rm -fv "${rbd_mnt_path}/"*.dd
echo dd if=/dev/urandom bs=1024 count="${dd_size}" of="${data_file_name}" conv=sparse
time dd if=/dev/urandom bs=1024 count="${dd_size}" of="${data_file_name}" conv=sparse
printf "\n"
echo ls -lh "${rbd_mnt_path}/"
time ls -lh "${rbd_mnt_path}/"
echo df -h "${rbd_mnt_path}"
time df -h "${rbd_mnt_path}"
printf "\n"
echo unmount_rbd "${bench_rbd_dev}" "${rbd_name}" "${rbd_mnt_path}"
time unmount_rbd "${bench_rbd_dev}" "${rbd_name}" "${rbd_mnt_path}"
} # mount_rbd_and_add_data()
unmount_rbd()
{
local bench_rbd_dev="${1}"
local rbd_name="${2}" # pool/name
local rbd_mnt_path="${3}"
local -a rbd_lock_info
#printf "Syncing...\n"
#time sync; time sync; time sync;
#printf "\n"
echo umount -v "${rbd_mnt_path}"
time umount -v "${rbd_mnt_path}"
printf "\n"
echo rmdir -v "${rbd_mnt_path}"
rmdir -v "${rbd_mnt_path}"
printf "\n"
echo rbd unmap "${rbd_name}"
time rbd unmap "${rbd_name}"
printf "\n"
sleep 1s
echo create_snapshot "${rbd_name}"
time create_snapshot "${rbd_name}"
printf "\n"
sleep 1s
echo rbd lock list "${rbd_name}"
time rbd lock list "${rbd_name}"
printf "\n"
printf "Removing RBD lock ...\n"
rbd_lock_info=( $(rbd lock list "${rbd_name}" | awk '/^client/ { print $2" "$1 }') )
echo rbd lock remove "${rbd_name}" "${rbd_lock_info[@]}"
time rbd lock remove "${rbd_name}" "${rbd_lock_info[@]}" || { rsleep 15; printf "Removing RBD lock (try 2)...\n"; time rbd lock remove "${rbd_name}" "${rbd_lock_info[@]}"; rsleep 45; printf "Removing RBD lock (try 3)...\n"; time rbd lock remove "${rbd_name}" "${rbd_lock_info[@]}"; }
printf "\n"
echo rbd lock list "${rbd_name}"
time rbd lock list "${rbd_name}"
printf "\n"
} # unmount_rbd()
create_snapshot()
{
local rbd_name="${1}" # pool/name
local snap_name
local new_snap_time
local new_snap_name
snap_name="$(rbd snap ls "${rbd_name}" | tail -n 1 | awk '/^[0-9]/ { print $2 }')"
printf "last snapshot: %s\n" "${snap_name}"
new_snap_date="$(date +%m%d%Y)"
new_snap_time="$(date +%s)"
# daily_02092018_1518166404852
new_snap_name="daily_${new_snap_date}_${new_snap_time}"
printf " new snapshot: %s\n" "${new_snap_name}"
printf "Creating snapshot ...\n"
echo rbd snap create "${rbd_name}@${new_snap_name}"
time rbd snap create "${rbd_name}@${new_snap_name}"
printf "List snapshots ...\n"
echo rbd snap list "${rbd_name}"
time rbd snap list "${rbd_name}"
printf "\n"
} # create_snapshot()
worker()
{
local rank_id="${i}"
rbd_name=$(get_rbd_name "${1}")
printf "\n(%07d) %s\n\n" ${rank_id} "${rbd_name}"
printf "Checking to see if %s/%s exists...\n" "${RBD_POOL}" "${rbd_name}"
#if ! rbd ls ${RBD_POOL} | grep -q "^${rbd_name}$"; then
if ! grep -q "^${rbd_name}$" /root/rbd-list.txt; then
printf "RBD does not exist, skipping ...\n"
return
fi
echo mount_rbd_and_add_data "${RBD_POOL}/${rbd_name}"
time mount_rbd_and_add_data "${RBD_POOL}/${rbd_name}"
printf "\n"
echo rsleep 15
rsleep 15
} # worker()
#printf "\nGenerating list of RBDs...\n"
#[[ -f /root/rbd-list ]] || time rbd ls ${RBD_POOL} | grep -q "^vpayno-sparse-" > /root/rbd-list.txt
#wc -l /root/rbd-list.txt
#printf "\n"
time for i in {0..999999}; do
random_int="$(shuf -i 0-499999 -n 1)"
worker ${random_int} &
let work_counter++
sleep 0.5s
#break
if [[ $(( ${i} % MAX_WORKERS )) -eq 0 ]]; then
jobs
time wait
printf "\n# of worker threads that have completed: %d\n\n" "${work_counter}"
echo rsleep 60
time rsleep 60
fi
[[ -f /tmp/stop-tests ]] && break
done
jobs
time wait
printf "\n# of worker threads that have completed: %d\n\n" "${work_counter}"
(2-2/4)