Skip to content

Commit

Permalink
Merge pull request #203 from phreaker0/cache-improvements
Browse files Browse the repository at this point in the history
cache improvements
  • Loading branch information
jimsalterjrs committed Jun 28, 2018
2 parents 8ee4175 + d0f1445 commit 22ab817
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions sanoid
Expand Up @@ -40,8 +40,10 @@ my %config = init($conf_file,$default_conf_file);

# if we call getsnaps(%config,1) it will forcibly update the cache, TTL or no TTL
my $forcecacheupdate = 0;
my $cache = '/var/cache/sanoidsnapshots.txt';
my $cacheTTL = 900; # 15 minutes
my %snaps = getsnaps( \%config, $cacheTTL, $forcecacheupdate );
my %pruned;

my %snapsbytype = getsnapsbytype( \%config, \%snaps );

Expand Down Expand Up @@ -294,12 +296,17 @@ sub prune_snapshots {
if (iszfsbusy($path)) {
print "INFO: deferring pruning of $snap - $path is currently in zfs send or receive.\n";
} else {
if (! $args{'readonly'}) { system($zfs, "destroy",$snap) == 0 or warn "could not remove $snap : $?"; }
if (! $args{'readonly'}) {
if (system($zfs, "destroy", $snap) == 0) {
$pruned{$snap} = 1;
} else {
warn "could not remove $snap : $?";
}
}
}
}
removelock('sanoid_pruning');
$forcecacheupdate = 1;
%snaps = getsnaps(%config,$cacheTTL,$forcecacheupdate);
removecachedsnapshots(0);
} else {
print "INFO: deferring snapshot pruning - valid pruning lock held by other sanoid process.\n";
}
Expand All @@ -308,7 +315,9 @@ sub prune_snapshots {
}
}


# if there were any deferred cache updates,
# do them now and wait if necessary
removecachedsnapshots(1);
} # end prune_snapshots


Expand Down Expand Up @@ -541,7 +550,6 @@ sub getsnaps {

my ($config, $cacheTTL, $forcecacheupdate) = @_;

my $cache = '/var/cache/sanoidsnapshots.txt';
my @rawsnaps;

my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($cache);
Expand Down Expand Up @@ -1181,6 +1189,55 @@ sub getchilddatasets {
return @children;
}
#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3
sub removecachedsnapshots {
my $wait = shift;
if (not %pruned) {
return;
}
my $unlocked = checklock('sanoid_cacheupdate');
if ($wait != 1 && not $unlocked) {
if ($args{'verbose'}) { print "INFO: deferring cache update (snapshot removal) - valid cache update lock held by another sanoid process.\n"; }
return;
}
# wait until we can get a lock to do our cache changes
while (not $unlocked) {
if ($args{'verbose'}) { print "INFO: waiting for cache update lock held by another sanoid process.\n"; }
sleep(10);
$unlocked = checklock('sanoid_cacheupdate');
}
writelock('sanoid_cacheupdate');
if ($args{'verbose'}) {
print "INFO: removing destroyed snapshots from cache.\n";
}
open FH, "< $cache";
my @rawsnaps = <FH>;
close FH;
open FH, "> $cache" or die 'Could not write to $cache!\n';
foreach my $snapline ( @rawsnaps ) {
my @columns = split("\t", $snapline);
my $snap = $columns[0];
print FH $snapline unless ( exists($pruned{$snap}) );
}
close FH;
removelock('sanoid_cacheupdate');
%snaps = getsnaps(\%config,$cacheTTL,$forcecacheupdate);
# clear hash
undef %pruned;
}
__END__
=head1 NAME
Expand Down

0 comments on commit 22ab817

Please sign in to comment.