From 6c15f78cac4a0b94907352c4184d6d7d5ca87164 Mon Sep 17 00:00:00 2001 From: Tobi Oetiker Date: Wed, 5 Mar 2008 23:13:24 +0000 Subject: longer timeout for slaves and separate cache files per slave for improved scaling -- Phillip Moore --- lib/Smokeping/Master.pm | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'lib/Smokeping/Master.pm') diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm index 8560de3..d31c95b 100644 --- a/lib/Smokeping/Master.pm +++ b/lib/Smokeping/Master.pm @@ -97,16 +97,18 @@ sub save_updates { my $updates = shift; # name\ttime\tupdatestring # name\ttime\tupdatestring + for my $update (split /\n/, $updates){ my ($name, $time, $updatestring) = split /\t/, $update; - my $file = $cfg->{General}{datadir}."/${name}.slave_cache"; + my $file = $cfg->{General}{datadir}."/${name}.${slave}.slave_cache"; if ( ${name} =~ m{(^|/)\.\.($|/)} ){ - warn "Skipping update for ${name}.slave_cache since ". + warn "Skipping update for ${name}.${slave}.slave_cache since ". "you seem to try todo some directory magic here. Don't!"; } elsif ( open (my $lock, '>>' , "$file.lock") ) { - for (my $i = 10; $i > 0; $i--){ - if ( flock $lock, LOCK_EX ){ + + for (my $i = 3; $i > 0; $i--){ + if ( flock($lock, LOCK_EX) ){ my $existing = []; if ( -r $file ){ my $in = eval { retrieve $file }; @@ -116,12 +118,12 @@ sub save_updates { $existing = $in; }; }; - push @{$existing}, [ $slave, $time, $updatestring]; + push @{$existing}, [ $slave, $time, $updatestring ]; nstore($existing, $file); last; } else { - warn "Could not lock $file. Trying again for $i rounds.\n"; - sleep rand(3); + warn "Could not lock $file ($!). Trying again for $i rounds.\n"; + sleep rand(2); } } close $lock; @@ -133,13 +135,15 @@ sub save_updates { =head3 get_slaveupdates -Read in all updates provided by slaves and return an array reference. +Read in all updates provided by the selected slave and return an array reference. =cut sub get_slaveupdates { my $name = shift; - my $file = $name.".slave_cache"; + my $slave = shift; + my $file = $name . "." . $slave. ".slave_cache"; + my @empty = (); my $data; if ( -r $file and open (my $lock, '>>', "$file.lock") ) { if ( flock $lock, LOCK_EX ){ @@ -147,7 +151,7 @@ sub get_slaveupdates { unlink $file; if ($@) { #error warn "Loading $file: $@"; - return undef; + return @empty; } } else { warn "Could not lock $file. Will skip and try again in the next round. No harm done!\n"; @@ -155,7 +159,7 @@ sub get_slaveupdates { close $lock; return $data; } - return; + return @empty; } -- cgit v1.2.3-24-g4f1b