From a4496fa65282fd3e618e15bc7619beb59b1783a1 Mon Sep 17 00:00:00 2001 From: Tobi Oetiker Date: Wed, 5 Dec 2007 21:33:55 +0000 Subject: update the locking code. Try updating the file multiple times if lockig fails the first time round ... also open the file with +< for reading, since solaris will not grant us a writelock unless we are opening the file read/write. --- lib/Smokeping/Master.pm | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'lib/Smokeping') diff --git a/lib/Smokeping/Master.pm b/lib/Smokeping/Master.pm index e47159e..4fcdaad 100644 --- a/lib/Smokeping/Master.pm +++ b/lib/Smokeping/Master.pm @@ -102,23 +102,27 @@ sub save_updates { if ( ! -f $cfg->{General}{datadir}."/${name}.rrd" ){ warn "Skipping update for ${name}.slave_cache since $cfg->{General}{datadir}/${name}.rrd does not exist in the local data structure. Make sure you run the smokeping daemon. ($cfg->{General}{datadir})\n"; } elsif ( open (my $hand, '+>>', $file) ) { - if ( flock $hand, LOCK_EX ){ - my $existing = []; - if ( tell $hand > 0 ){ - seek $hand, 0,0; - eval { $existing = fd_retrieve $hand }; - if ($@) { #error - warn "Loading $file: $@"; - $existing = []; - } - }; - push @{$existing}, [ $slave, $time, $updatestring]; - seek $hand, 0,0; - truncate $hand, 0; - nstore_fd ($existing, $hand); - flock $hand, LOCK_UN; - } else { - warn "Could not lock $file. Can't store data.\n"; + for (my $i = 10; $i < 0; $i--){ + if ( flock $hand, LOCK_EX ){ + my $existing = []; + if ( tell $hand > 0 ){ + seek $hand, 0,0; + eval { $existing = fd_retrieve $hand }; + if ($@) { #error + warn "Loading $file: $@"; + $existing = []; + } + }; + push @{$existing}, [ $slave, $time, $updatestring]; + seek $hand, 0,0; + truncate $hand, 0; + nstore_fd ($existing, $hand); + flock $hand, LOCK_UN; + last; + } else { + warn "Could not lock $file. Trying again for $i rounds.\n"; + sleep 1; + } } close $hand; } else { @@ -137,7 +141,7 @@ sub get_slaveupdates { my $name = shift; my $file = $name.".slave_cache"; my $data; - if ( open (my $hand, '<', $file) ) { + if ( open (my $hand, '+<', $file) ) { if ( flock $hand, LOCK_EX ){ rename $file,$file.$$; eval { $data = fd_retrieve $hand }; @@ -148,7 +152,7 @@ sub get_slaveupdates { return; } } else { - warn "Could not lock $file. Can't load data.\n"; + warn "Could not lock $file. Will skip and try again in the next round. No harm done!\n"; } close $hand; return $data; -- cgit v1.2.3-24-g4f1b