Compare commits

...

2 Commits

Author SHA1 Message Date
svetasmirnova
29888b67b4 PT-1336 pt-stalk removes user files from the destination directory
* Added check that files to be removed were created by pt-stalk

* Added option -maxdepth 1 to find command, so the tool does not search for files in the nested directories

* Added unit tests

* This should fix PT-1812 also
2021-11-23 21:57:08 +03:00
svetasmirnova
f839612ddc PT-1336 Added file name checks and -maxdepth 1 parameter 2021-11-22 20:49:52 +03:00
3 changed files with 147 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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.
# #############################################################################