blib/lib/Narada/Lock.pm

filter: Statementstmt 87.7% 79 / 90 Branchbran 30.0% 12 / 40 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 95.6% 22 / 23 Podpod 100% 5 / 5 totaltotal 72.8% 121 / 166 CC 23 SCAR 22.9
CC
23
Cov
69%
child_inherit_lock
30.0
shared_lock
19.2
exclusive_lock
14.0
CRAP 9.9
« blib/script/narada-new-1 ↑ Summary blib/script/narada-setup-qmail »
1 package Narada::Lock;
2  
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION = 'v2.3.4';
8  
9 16 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Narada;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Fcntl qw( :DEFAULT :flock F_SETFD FD_CLOEXEC );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 16 ▶use constant IS_NARADA1 => eval { local $SIG{__DIE__}; Narada::detect('narada-1') } || undef;
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
eval { || undef
l!l
214
MC/DC100%1/1
eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } } || undef
eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } }
Truth table100%2/2
A
A= eval { do { local $SIG{'__DIE__'}; Narada::detect('narada-1') } }
Aresult
11
00
17 16 ▶use constant DIR => IS_NARADA1 ? 'var/' : q{};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use constant LOCKNEW => DIR.'.lock.new';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use constant LOCKFILE => DIR.'.lock';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use constant TICK => 0.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 16 my $F_lock;
23  
24  
25 sub shared_lock :Export {
26 22 ▶ my $timeout = shift;
Subroutine shared_lock: called CC=8 SCAR=29.6
Pod documented
27 22 ▶ return 1 if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 22 22
28 22 ▶ sysopen $F_lock, LOCKFILE, O_RDONLY|O_CREAT or croak "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen $F_lock, ".lock", 64 or croak("open: $!") 0 22 22
29 22 while (1) {
30 22 ▶ next if -e LOCKNEW;
Branch: 1 branch
BranchTrueFalseTotal
if -e ".lock.new" 0 22 22
31 22 ▶ last if flock $F_lock, LOCK_SH|LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if flock $F_lock, 5 22 0 22
32 0 ▶ $!{EWOULDBLOCK} or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
$!{"EWOULDBLOCK"} or croak("flock: $!") 0 0 0
33 } continue {
34 0 ▶ return if defined $timeout and (($timeout-=TICK) < TICK);
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout and ($timeout -= "0.1") < "0.1" 0 0 0
Condition0%0/3
defined $timeout and ($timeout -= "0.1") < "0.1"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $timeout and ($timeout -= "0.1") < "0.1"
defined $timeout ($timeout -= "0.1") < "0.1"
Truth table0%0/3
A and B
A= defined $timeoutB= ($timeout -= "0.1") < "0.1"
ABresult
0X0
100
111
35 0 sleep TICK;
36 }
37 22 return 1;
38 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 sub exclusive_lock :Export {
41 28 ▶ return if $ENV{NARADA_SKIP_LOCK};
Subroutine exclusive_lock: called CC=7 SCAR=26.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 28 28
42 28 ▶ sysopen $F_lock, LOCKFILE, O_WRONLY|O_CREAT or croak "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen $F_lock, ".lock", 65 or croak("open: $!") 0 28 28
43 28 while (1) {
44 28 ▶ last if flock $F_lock, LOCK_EX|LOCK_NB;
Branch: 1 branch
BranchTrueFalseTotal
if flock $F_lock, 6 28 0 28
45 0 ▶ $!{EWOULDBLOCK} or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
$!{"EWOULDBLOCK"} or croak("flock: $!") 0 0 0
46 0 ▶ system('touch', LOCKNEW) == 0 or croak "touch: $!/$?";
Branch: 1 branch
BranchTrueFalseTotal
system("touch", ".lock.new") == 0 or croak("touch: $!/$?") 0 0 0
47 0 sleep TICK;
48 }
49 28 ▶ system('touch', LOCKNEW) == 0 or croak "touch: $!/$?";
Branch: 1 branch
BranchTrueFalseTotal
system("touch", ".lock.new") == 0 or croak("touch: $!/$?") 0 28 28
50 28 return;
51 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 sub unlock_new :Export {
54 22 ▶ return if $ENV{NARADA_SKIP_LOCK};
Subroutine unlock_new: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 22 22
55 22 unlink LOCKNEW;
56 22 return;
57 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 sub unlock :Export {
60 22 ▶ return if $ENV{NARADA_SKIP_LOCK};
Subroutine unlock: called CC=4 SCAR=14.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 22 22
61 22 ▶ if ($F_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_lock) 22 0 22
62 22 ▶ flock $F_lock, LOCK_UN or croak "flock: $!";
Branch: 1 branch
BranchTrueFalseTotal
flock $F_lock, 8 or croak("flock: $!") 0 22 22
63 }
64 22 return;
65 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 sub child_inherit_lock :Export {
68 0 ▶ my ($is_inherit) = @_;
Subroutine child_inherit_lock: not called CC=5 SCAR=34.0
Pod documented
69 0 ▶ return if $ENV{NARADA_SKIP_LOCK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NARADA_SKIP_LOCK"} 0 0 0
70 0 ▶ if ($F_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($F_lock) 0 0 0
71 0 ▶ fcntl $F_lock, F_SETFD, $is_inherit ? 0 : FD_CLOEXEC or croak "fcntl: $!";
Branch: 2 branches
BranchTrueFalseTotal
fcntl $F_lock, 2, $is_inherit ? 0 : 1 or croak("fcntl: $!") 0 0 0
$is_inherit ? : 0 0 0
72 }
73 0 return;
74 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76  
77 16 1; # Magic true value required at end of module
78 __END__
79  
80 =encoding utf8
81  
82 =head1 NAME
83  
84 Narada::Lock - manage project locks
85  
86  
87 =head1 VERSION
88  
89 This document describes Narada::Lock version v2.3.4
90  
91  
92 =head1 SYNOPSIS
93  
94 use Narada::Lock qw( shared_lock unlock child_inherit_lock );
95 use Narada::Lock qw( exclusive_lock unlock_new unlock );
96  
97 shared_lock();
98 unlock();
99 shared_lock(0) or die "Can't get lock right now";
100 unlock();
101 shared_lock(5) or die "Can't get lock in 5 seconds";
102 unlock();
103  
104 shared_lock();
105 system('sleep 1');
106 child_inherit_lock(1);
107 system('sleep 10 &');
108 child_inherit_lock(0);
109 system('sleep 1');
110 unlock();
111  
112 exclusive_lock();
113 # do critical operations, reboot-safe
114 unlock_new();
115 # do non-critical operations, still in exclusive mode
116 unlock();
117  
118  
119 =head1 DESCRIPTION
120  
121 To allow safe backup/update/maintenance of project, there should be
122 possibility to guarantee consistent state of project, at some point.
123 To reach this goal, ALL operations which modify project data on disk
124 (including both project files and database) must be done under shared lock,
125 and all operations which require consistent project state must be done
126 under exclusive lock.
127  
128 This module contain helper functions to manage project locks, but all
129 operations required for these locks can be implemented using any
130 programming language, so all applications in project (including non-perl
131 applications) are able to manage project locks.
132  
133 Shared lock is set using flock(2) LOCK_SH on file C<.lock>.
134 Exclusive lock is set using flock(2) LOCK_EX on file C<.lock>.
135  
136 =head2 FREEZE NEW TASKS
137  
138 There exists scenario when it's impossible to set exclusive lock:
139 if new tasks will start and set shared lock before old tasks will drop shared
140 lock (and so shared lock will be set all of time).
141  
142 To work around this scenario another file C<.lock.new> should be used
143 as semaphore - it should be created before trying to set exclusive lock,
144 and new tasks shouldn't try to set shared lock while this file exists.
145  
146 This file should be removed after finishing critical operations - this
147 guarantee project data will not change even if system will be rebooted,
148 because after reboot existence of file C<.lock.new> will prevent from
149 starting new tasks with shared locks but not prevent from placing
150 exclusive lock again and continue these critical operations.
151  
152  
153 =head1 INTERFACE
154  
155 =over
156  
157 =item B<shared_lock>( $timeout )
158  
159 Try to get shared lock which is required to modify any project data (files or
160 database).
161  
162 If $timeout undefined - will wait forever until lock will be granted.
163 If $timeout >=1 will try to get lock every 1 second until $timeout expire.
164  
165 Use unlock() to free this lock.
166  
167 Return: true if able to get lock.
168  
169 =item B<exclusive_lock>()
170  
171 Try to get exclusive lock which is required to guarantee consistent project
172 state (needed while backup/update/maintenance operations).
173  
174 Set two locks: create file C<.lock.new> which signal other scripts to
175 not try to set shared lock while this file exists and get LOCK_EX on file
176 C<.lock> to be sure all current tasks finished and unlocked their
177 shared locks.
178  
179 Will delay until get lock.
180  
181 Use unlock_new() in combination with exit() or unlock() to free these locks.
182  
183 Return: nothing.
184  
185 =item B<unlock_new>()
186  
187 Free first lock set by exclusive_lock() (i.e. remove file C<.lock.new>).
188 This allow other tasks to get shared_lock() after this process exit or
189 call unlock().
190  
191 Return: nothing.
192  
193 =item B<unlock>()
194  
195 Free lock set by shared_lock() (or second lock set by exclusive_lock()).
196  
197 Return: nothing.
198  
199 =item B<child_inherit_lock>( $is_inherit )
200  
201 By default, child processes don't inherit our FD with lock.
202 This is acceptable only if we don't run child in background or if
203 child will get own locks on start.
204  
205 In other cases you should call child_inherit_lock() with true value in
206 $is_inherit to force child to inherit our lock (just like DJB's `setlock`
207 or Pepe's `chpst -[lL]` do).
208 Calling child_inherit_lock() with false value in $is_inherit will switch
209 back to default behaviour (new child will not inherit FD with lock).
210  
211 Examples:
212  
213 # OK: not in background
214 system("rm -rf var/something");
215  
216 # OK: in background, but this is our script,
217 # which will get lock on it's own
218 system("./another_script_of_this_project &");
219  
220 # ERROR: in background, no lock
221 system("( sleep 5; rm -rf var/something ) &");
222  
223 # OK: in background, inherit lock
224 child_inherit_lock(1); # from now all childs will inherit lock
225 system("( sleep 5; rm -rf var/something ) &");
226 child_inherit_lock(0); # next child will not inherit lock
227  
228 Return: nothing.
229  
230 =back
231  
232  
233 =head1 CONFIGURATION AND ENVIRONMENT
234  
235 Narada::Lock requires configuration files and directories provided by
236 Narada framework.
237  
238 If $ENV{NARADA_SKIP_LOCK} is set to any true value then shared_lock(),
239 exclusive_lock(), unlock_new(), unlock() and child_inherit_lock() will do
240 nothing (shared_lock() will return true).
241  
242  
243 =head1 COMPATIBILITY
244  
245 Narada 1.x project use C<var/.lock> instead of C<.lock>.
246  
247 Narada 1.x project use C<var/.lock.new> instead of C<.lock.new>.
248  
249  
250 =head1 SUPPORT
251  
252 =head2 Bugs / Feature Requests
253  
254 Please report any bugs or feature requests through the issue tracker
255 at L<https://github.com/powerman/Narada/issues>.
256 You will be notified automatically of any progress on your issue.
257  
258 =head2 Source Code
259  
260 This is open source software. The code repository is available for
261 public review and contribution under the terms of the license.
262 Feel free to fork the repository and submit pull requests.
263  
264 L<https://github.com/powerman/Narada>
265  
266 git clone https://github.com/powerman/Narada.git
267  
268 =head2 Resources
269  
270 =over
271  
272 =item * MetaCPAN Search
273  
274 L<https://metacpan.org/search?q=Narada>
275  
276 =item * CPAN Ratings
277  
278 L<http://cpanratings.perl.org/dist/Narada>
279  
280 =item * AnnoCPAN: Annotated CPAN documentation
281  
282 L<http://annocpan.org/dist/Narada>
283  
284 =item * CPAN Testers Matrix
285  
286 L<http://matrix.cpantesters.org/?dist=Narada>
287  
288 =item * CPANTS: A CPAN Testing Service (Kwalitee)
289  
290 L<http://cpants.cpanauthors.org/dist/Narada>
291  
292 =back
293  
294  
295 =head1 AUTHOR
296  
297 Alex Efros E<lt>powerman@cpan.orgE<gt>
298  
299  
300 =head1 COPYRIGHT AND LICENSE
301  
302 This software is Copyright (c) 2008-2016 by Alex Efros E<lt>powerman@cpan.orgE<gt>.
303  
304 This is free software, licensed under:
305  
306 The MIT (X11) License
307  
308  
309 =cut
« blib/script/narada-new-1 ↑ Summary blib/script/narada-setup-qmail »