diff options
author | Tobi Oetiker <tobi@oetiker.ch> | 2007-12-05 22:33:55 +0100 |
---|---|---|
committer | Tobi Oetiker <tobi@oetiker.ch> | 2007-12-05 22:33:55 +0100 |
commit | a4496fa65282fd3e618e15bc7619beb59b1783a1 (patch) | |
tree | e3aa3b7be88eaf9ec79c788eef746e08d220392c /lib/Smokeping | |
parent | daeb391ff2c9e47f656a8089b326625d43ffc5a3 (diff) | |
download | smokeping-a4496fa65282fd3e618e15bc7619beb59b1783a1.tar.gz smokeping-a4496fa65282fd3e618e15bc7619beb59b1783a1.tar.xz |
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.
Diffstat (limited to 'lib/Smokeping')
-rw-r--r-- | lib/Smokeping/Master.pm | 42 |
1 files changed, 23 insertions, 19 deletions
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; |