From 3e3de0ed8ad6b87df4b2409373ebb6708e55f9c3 Mon Sep 17 00:00:00 2001 From: Tobi Oetiker Date: Sat, 30 Aug 2008 08:56:38 +0000 Subject: order updates to slavecash to minimize writing --- lib/Smokeping/Master.pm | 69 +++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 36 deletions(-) (limited to 'lib/Smokeping') diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm index 296935a..1f4521d 100644 --- a/lib/Smokeping/Master.pm +++ b/lib/Smokeping/Master.pm @@ -113,51 +113,48 @@ sub save_updates { my %u; for my $update (split /\n/, $updates){ my ($name, $time, $updatestring) = split /\t/, $update; - push @{$u{$name}}, [$time,$updatestring]; - } - for my $name (keys %u){ - my $file = slavedatadir($cfg) ."/${name}.${slave}.slave_cache"; if ( ${name} =~ m{(^|/)\.\.($|/)} ){ warn "Skipping update for ${name}.${slave}.slave_cache since ". "you seem to try todo some directory magic here. Don't!"; - } - else { - - for (my $i = 2; $i >= 0; $i--){ - my $fh; - if ( open ($fh, '+>>' , $file) and flock($fh, LOCK_EX) ){ - my $existing = []; - if (! -e $file) { # the reader unlinked it from under us - flock($fh, LOCK_UN); - close $fh; - next; - } - seek $fh, 0, 0; - if ( -s _ ){ - my $in = eval { fd_retrieve $fh }; - if ($@) { #error - warn "Loading $file: $@"; - } else { - $existing = $in; - }; - }; - map { - push @{$existing}, [ $slave, $_->[0], $_->[1] ]; - } @{$u{$name}}; - - nstore_fd($existing, $fh); + } else { + push @{$u{$name}}, [$time,$updatestring]; + } + } + for my $name (sort keys %u){ + my $file = slavedatadir($cfg) ."/${name}.${slave}.slave_cache"; + for (my $i = 2; $i >= 0; $i--){ + my $fh; + if ( open ($fh, '+>>' , $file) and flock($fh, LOCK_EX) ){ + my $existing = []; + if (! -e $file) { # the reader unlinked it from under us flock($fh, LOCK_UN); close $fh; - last; - } elsif ($i > 0) { - warn "Could not lock $file ($!). Trying again $i more times.\n"; - close $fh; - sleep rand(2); next; } - warn "Could not update $file, giving up for now."; + seek $fh, 0, 0; + if ( -s _ ){ + my $in = eval { fd_retrieve $fh }; + if ($@) { #error + warn "Loading $file: $@"; + } else { + $existing = $in; + }; + }; + map { + push @{$existing}, [ $slave, $_->[0], $_->[1] ]; + } @{$u{$name}}; + + nstore_fd($existing, $fh); + flock($fh, LOCK_UN); close $fh; + last; + } elsif ($i > 0) { + warn "Could not lock $file ($!). Trying again $i more times.\n"; + sleep rand(2); + next; } + warn "Could not update $file, giving up for now."; + close $fh; } } }; -- cgit v1.2.3-24-g4f1b