diff --git a/bin/pt-stalk b/bin/pt-stalk index 77189878..509fea3a 100755 --- a/bin/pt-stalk +++ b/bin/pt-stalk @@ -1351,13 +1351,17 @@ sleep_ok() { } purge_samples() { - local dir="$1" + local dir=`realpath -s "$1"` local retention_time="$2" local retention_count="$3" local retention_size="$4" # Delete collect files which more than --retention-time days old. - find "$dir" -type f -mtime +$retention_time -exec rm -f '{}' \; + if [ -n "$OPT_PREFIX" ]; then + find "$dir" -maxdepth 1 -type f -mtime +$retention_time -name "$OPT_PREFIX-*" -exec rm -f '{}' \; + else + find "$dir" -maxdepth 1 -type f -mtime +$retention_time -regextype posix-egrep -regex "$dir/[0-9]{4}(_[0-9]{2}){5}-.*" -exec rm -f '{}' \; + fi local oprofile_dir="/var/lib/oprofile/samples" if [ -d "$oprofile_dir" ]; then @@ -1370,21 +1374,21 @@ purge_samples() { targetCnt=$(($retention_count + 0)) if [ $targetCnt -gt 0 ]; then targetCnt=$(($retention_count + 1)) - files_to_delete=$(find $dir -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +${targetCnt}) + files_to_delete=$(find "$dir" -maxdepth 1 -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +${targetCnt}) for prefix in $files_to_delete; do - echo "deleting files ${dir}${prefix}* according to the --retention-count param" - rm -f ${dir}${prefix}* 2>/dev/null + echo "deleting files ${dir}/${prefix}* according to the --retention-count param" + rm -f ${dir}/${prefix}* 2>/dev/null done fi targetSize=$(($retention_size + 0)) if [ $targetSize -gt 0 ]; then - files_to_delete=$(find $dir -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +1) + files_to_delete=$(find "$dir" -maxdepth 1 -type f -exec basename {} \; | cut -f1 -d- | sort -r | uniq | tail -n +1) for prefix in $files_to_delete; do current_size=$(du -BM $dir | cut -f1 -d"M") if [ $current_size -gt $targetSize ]; then - echo "deleting files ${dir}${prefix}* according to the --retention-size param" - rm -f ${dir}${prefix}* 2>/dev/null + echo "deleting files ${dir}/${prefix}* according to the --retention-size param" + rm -f ${dir}/${prefix}* 2>/dev/null else break fi diff --git a/sandbox/test-env b/sandbox/test-env index eb4ab01f..a82ee78d 100755 --- a/sandbox/test-env +++ b/sandbox/test-env @@ -295,6 +295,7 @@ fi case $opt in start) cd $PERCONA_TOOLKIT_BRANCH/sandbox + echo "${2:-"master"}" 12345 ./start-sandbox "${2:-"master"}" 12345 exit_status=$((exit_status | $?)) set_mysql_version diff --git a/t/pt-stalk/pt-stalk.t b/t/pt-stalk/pt-stalk.t index 69fe6021..9074fc0a 100644 --- a/t/pt-stalk/pt-stalk.t +++ b/t/pt-stalk/pt-stalk.t @@ -422,7 +422,140 @@ like( qr/matched=yes/, "Accepts floating point values as treshold variable" ); + +# ########################################################################### +# Variable declaration for the retention tests +# ########################################################################### + +my $odate; + +# ########################################################################### +# Test if retention does not remove files that were not collected +# ########################################################################### + +cleanup(); + +system("mkdir $dest"); +$odate=`date --rfc-3339=date --date='-3 month'`; +system("touch -d '$odate' $dest/nostalk"); + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --prefix nostalk --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = `ls -l $dest`; + +like( + $output, + qr/nostalk/m, + "Retention test 1: Not-matched file not touched" +); +# ########################################################################### +# Test if files that match the prefix-, are removed by the retention option +# ########################################################################### + +cleanup(); + +system("mkdir $dest"); +$odate=`date --rfc-3339=date --date='-3 month'`; +system("touch -d '$odate' $dest/nostalk-"); +system("touch -d '$odate' $dest/nostalk-innodbstatus1"); + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --prefix nostalk --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = `ls -l $dest`; + +unlike( + $output, + qr/^nostalk-$/m, + "Retention test 2: tests, matched prefix-, are removed" +); + +unlike( + $output, + qr/^nostalk-innodbstatus1$/m, + "Retention test 2: tests, matched prefix-innodbstatus1, are removed" +); + +# ########################################################################### +# Test if retention removes old files that match auto-generated pattern +# ########################################################################### + +cleanup(); + +system("mkdir $dest"); +$odate=`date --rfc-3339=date --date='-3 month'`; + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = `ls -l $dest | wc -l`; + +system("touch -d '$odate' $dest/*"); +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = `ls -l $dest | wc -l` - $output; + +is( + $output, + 0, + "Retention test 3: tests, matched auto-generated patern, are removed" +); + +# ########################################################################### +# Test if retention by size works as expected +# ########################################################################### + +cleanup(); + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --sleep 2 --dest $dest --pid $pid_file --iterations 5 -- --defaults-file=$cnf >$log_file 2>&1"); + +$output = `du -s $dest | cut -f 1`; + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --retention-size 1 --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = $output / `du -s $dest | cut -f 1`; + +ok( + $output >= 5, + "Retention test 4: retention by size works as expected" +); + +# ########################################################################### +# Test if retention by count works as expected +# ########################################################################### + +cleanup(); + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --sleep 2 --dest $dest --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = `du -s $dest | wc -l`; + +$retval = system("$trunk/bin/pt-stalk --no-stalk --run-time 2 --dest $dest --retention-count 1 --pid $pid_file --iterations 1 -- --defaults-file=$cnf >$log_file 2>&1"); + +PerconaTest::wait_until(sub { !-f $pid_file }); + +$output = $output - `du -s $dest | wc -l`; + +is( + $output, + 0, + "Retention test 5: retention by count works as expected" +); + # ########################################################################### # Test report about performance schema transactions in MySQL 5.7+ # ########################################################################### @@ -575,6 +708,7 @@ SKIP: { ); } + # ############################################################################# # Done. # #############################################################################