blib/lib/Redis/Setlock.pm

filter: Statementstmt 32.2% 51 / 158 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 45.4% 15 / 33 Podpod 9.0% 1 / 11 totaltotal 23.5% 67 / 285 CC 34 SCAR 31.1
CC
34
Cov
22%
run
56.0
validate_redis_version
56.0
parse_options
42.0
CRAP 22.3
↑ Summary
1 package Redis::Setlock;
2 15 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Log::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Time::HiRes qw/ sleep /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Digest::SHA qw/ sha1_hex /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our $VERSION = "0.14";
16 15 our $DEFAULT_EXPIRES = 86400;
17 15 our $RETRY_INTERVAL = 0.5;
18 15 our $BLOCKING_KEY_POSTFIX = ":wait";
19 15 our $WAIT_QUEUE = 0;
20 15 our $WARN_LOCK_TIME_THRESHOLD = 0;
21  
22 15 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 15 EXIT_CODE_ERROR => 111,
24 15 };
25  
26 15 ▶use constant UNLOCK_LUA_SCRIPT => <<'END_OF_SCRIPT'
Subroutine BEGIN: called CC=1 SCAR=0.0
27 if redis.call("get",KEYS[1]) == ARGV[1]
28 then
29 return redis.call("del",KEYS[1])
30 else
31 return 0
32 end
33 END_OF_SCRIPT
34 15 ;
35  
36 15 ▶use constant BLOCKING_UNLOCK_LUA_SCRIPT_TMPL => <<'END_OF_SCRIPT'
Subroutine BEGIN: called CC=1 SCAR=0.0
37 if redis.call("get",KEYS[1]) == ARGV[1]
38 then
39 local q = KEYS[1].."%s"
40 redis.call("del", KEYS[1])
41 redis.call("expire", q, 60)
42 return redis.call("lpush", q, ARGV[1])
43 else
44 return 0
45 end
46 END_OF_SCRIPT
47 15 ;
48  
49 0 ▶sub BLOCKING_UNLOCK_LUA_SCRIPT {
Subroutine BLOCKING_UNLOCK_LUA_SCRIPT: not called CC=1 SCAR=6.9
Pod undocumented
50 0 sprintf BLOCKING_UNLOCK_LUA_SCRIPT_TMPL, $BLOCKING_KEY_POSTFIX;
51 }
52  
53 0 ▶sub parse_options {
Subroutine parse_options: not called CC=6 SCAR=37.4
Pod undocumented
54 0 my (@argv) = @_;
55  
56 0 my $p = Getopt::Long::Parser->new(
57 config => [qw/posix_default no_ignore_case auto_help bundling pass_through/]
58 );
59 0 my $opt = {
60 wait => 1,
61 exit_code => EXIT_CODE_ERROR,
62 };
63 0 ▶ $p->getoptionsfromarray(\@argv, $opt, qw/
Branch: 1 branch
BranchTrueFalseTotal
$p->getoptionsfromarray(\@argv, $opt, ("redis=s", "expires=i", "keep", "n", "N", "x", "X", "version")) or pod2usage() 0 0 0
64 redis=s
65 expires=i
66 keep
67 n
68 N
69 x
70 X
71 version
72 /) or pod2usage;
73  
74 0 ▶ if ($opt->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"version"}) 0 0 0
75 0 print STDERR "version: $VERSION\n";
76 0 exit 0;
77 }
78 0 ▶ $opt->{wait} = 0 if $opt->{n}; # no delay
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"n"} 0 0 0
79 0 ▶ $opt->{exit_code} = 0 if $opt->{x}; # exit code 0
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"x"} 0 0 0
80 0 ▶ $opt->{expires} = $DEFAULT_EXPIRES unless defined $opt->{expires};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"expires"} 0 0 0
81  
82 0 return ($opt, @argv);
83 }
84  
85 0 ▶sub lock_guard {
Subroutine lock_guard: not called CC=3 SCAR=24.8
Pod documented
86 0 my $class = shift;
87 0 my ($redis, $key, $expires, $wait) = @_;
88  
89 0 ▶ my $opt = {
Branch: 1 branch
BranchTrueFalseTotal
defined $expires ? : 0 0 0
90 wait => $wait,
91 expires => defined $expires ? $expires : $DEFAULT_EXPIRES,
92 };
93 0 my $start = [ Time::HiRes::gettimeofday ];
94 0 ▶ my $token = try_get_lock($redis, $opt, $key)
Branch: 1 branch
BranchTrueFalseTotal
my $token = try_get_lock($redis, $opt, $key) or return 0 0 0
95 or return;
96  
97 0 ▶ return Guard::guard {
Subroutine __ANON__: not called CC=4 SCAR=30.0
98 0 my $elapsed = Time::HiRes::tv_interval($start);
99 0 ▶ if ($opt->{expires} < $elapsed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->{'expires'} < $elapsed) { } 0 0 0
elsif (0 < $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD and $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD < $elapsed) { } 0 0 0
100 0 warnf "guard(key:%s token:%s) seems to be already expired. elasped %s sec", $key, $token, $elapsed;
101 ▶ } elsif (0 < $WARN_LOCK_TIME_THRESHOLD && $WARN_LOCK_TIME_THRESHOLD < $elapsed) {
Condition0%0/3
0 < $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD and $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD < $elapsed
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD and $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD < $elapsed
0 < $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD < $elapsed
Truth table0%0/3
A and B
A= 0 < $Redis::Setlock::WARN_LOCK_TIME_THRESHOLDB= $Redis::Setlock::WARN_LOCK_TIME_THRESHOLD < $elapsed
ABresult
0X0
100
111
102 0 warnf "guard(key:%s token:%s) elasped %s sec", $key, $token, $elapsed;
103 }
104 0 release_lock($redis, $opt, $key, $token);
105 0 };
106 }
107  
108 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
109 0 my $class = shift;
110  
111 0 local $Log::Minimal::PRINT = \&log_minimal_print;
112  
113 0 my ($opt, $key, @command) = parse_options(@_);
114  
115 0 ▶ pod2usage() if !defined $key || @command == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or @command == 0 0 0 0
Condition0%0/3
not defined $key or @command == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $key or @command == 0
not defined $key @command == 0
Truth table0%0/3
not(A) or B
A= not defined $keyB= @command == 0
ABresult
1X1
011
000
116  
117 0 ▶ my $redis = connect_to_redis_server($opt)
Branch: 1 branch
BranchTrueFalseTotal
my $redis = connect_to_redis_server($opt) or return 111 0 0 0
118 or return EXIT_CODE_ERROR;
119  
120 0 ▶ validate_redis_version($redis)
Branch: 1 branch
BranchTrueFalseTotal
validate_redis_version($redis) or return 111 0 0 0
121 or return EXIT_CODE_ERROR;
122  
123 0 ▶ if ( my $token = try_get_lock($redis, $opt, $key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = try_get_lock($redis, $opt, $key)) { } 0 0 0
124 0 my $code = invoke_command(@command);
125 0 release_lock($redis, $opt, $key, $token);
126 0 debugf "exit with code %d", $code;
127 0 return $code;
128 }
129 else {
130 # couldnot get lock
131 0 ▶ if ($opt->{exit_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"exit_code"}) 0 0 0
132 0 critf "unable to lock %s.", $key;
133 0 return $opt->{exit_code};
134 }
135 0 debugf "exit with code 0";
136 0 return 0; # by option x
137 }
138 }
139  
140 0 ▶sub connect_to_redis_server {
Subroutine connect_to_redis_server: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my $opt = shift;
142 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 Redis->new(
144 server => $opt->{redis},
145 0 ▶ reconnect => $opt->{wait} ? $opt->{expires} : 0,
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'wait'} ? : 0 0 0
146 every => $RETRY_INTERVAL * 1000, # to msec
147 );
148 }
149 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 my $e = $_;
151 0 my $error = (split(/\n/, $e))[0];
152 0 critf "Redis server seems down: %s", $error;
153 0 return;
154 0 };
155 }
156  
157 0 ▶sub validate_redis_version {
Subroutine validate_redis_version: not called CC=7 SCAR=40.3
Pod undocumented
158 0 my $redis = shift;
159 0 my $version = $redis->info->{redis_version};
160 0 debugf "Redis version is: %s", $version;
161 0 my ($major, $minor, $rev) = split /\./, $version;
162 0 ▶ if ( $major >= 3
Branch: 1 branch
BranchTrueFalseTotal
if ($major >= 3 or $major == 2 and $minor >= 7 or $major == 2 and $minor == 6 and $rev >= 12) 0 0 0
Condition0%0/15
$major >= 3 or $major == 2 and $minor >= 7 or $major == 2 and $minor == 6 and $rev >= 12
l!l&&r!l&&!r
000
$major >= 3 or $major == 2 and $minor >= 7
l!l&&r!l&&!r
000
$major == 2 and $minor >= 7
!ll&&!rl&&r
000
$major == 2 and $minor == 6 and $rev >= 12
!ll&&!rl&&r
000
$major == 2 and $minor == 6
!ll&&!rl&&r
000
MC/DC0%0/6
$major >= 3 or $major == 2 and $minor >= 7 or $major == 2 and $minor == 6 and $rev >= 12
$major >= 3 $major == 2 $minor >= 7 $major == 2 $minor == 6 $rev >= 12
Truth table0%0/10
A or B and C or D and E and F
A= $major >= 3B= $major == 2C= $minor >= 7D= $major == 2E= $minor == 6F= $rev >= 12
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
163 || $major == 2 && $minor >= 7
164 || $major == 2 && $minor == 6 && $rev >= 12
165 ) {
166 # ok
167 0 return 1;
168 }
169 0 critf "required Redis server version >= 2.6.12. current server version is %s", $version;
170 0 return;
171 }
172  
173 0 ▶sub try_get_lock {
Subroutine try_get_lock: not called CC=5 SCAR=34.0
Pod undocumented
174 0 my ($redis, $opt, $key) = @_;
175 0 my $got_lock;
176 0 my $token = create_token();
177 GET_LOCK:
178 0 while (1) {
179 0 my @args = ($key, $token, "EX", $opt->{expires}, "NX");
180 0 debugf "redis: SET @args";
181 0 $got_lock = $redis->set(@args);
182 0 ▶ if ($got_lock) {
Branch: 2 branches
BranchTrueFalseTotal
if ($got_lock) { } 0 0 0
elsif (not $opt->{'wait'}) { } 0 0 0
183 0 debugf "got lock: %s", $key;
184 0 last GET_LOCK;
185 }
186 elsif (!$opt->{wait}) { # no delay by option n
187 0 debugf "no delay mode. exit";
188 0 last GET_LOCK;
189 }
190 0 debugf "unable to lock. waiting for release";
191 0 ▶ if ($WAIT_QUEUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Redis::Setlock::WAIT_QUEUE) { } 0 0 0
192 0 $redis->blpop("$key${BLOCKING_KEY_POSTFIX}", $opt->{expires});
193 }
194 else {
195 0 sleep $RETRY_INTERVAL;
196 }
197 }
198 0 ▶ return $token if $got_lock;
Branch: 1 branch
BranchTrueFalseTotal
if $got_lock 0 0 0
199 }
200  
201 0 ▶sub release_lock {
Subroutine release_lock: not called CC=3 SCAR=24.8
Pod undocumented
202 0 my ($redis, $opt, $key, $token) = @_;
203 0 ▶ if ($opt->{keep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"keep"}) 0 0 0
204 0 debugf "Keep lock key %s", $key;
205 0 return;
206 }
207  
208 0 debugf "Release lock key %s", $key;
209 0 ▶ my $script = $WAIT_QUEUE ? BLOCKING_UNLOCK_LUA_SCRIPT : UNLOCK_LUA_SCRIPT;
Branch: 1 branch
BranchTrueFalseTotal
$Redis::Setlock::WAIT_QUEUE ? : 0 0 0
210 0 my $sha1 = sha1_hex($script);
211 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 $redis->evalsha($sha1, 1, $key, $token);
213 }
214 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
215 0 my $e = $_;
216 0 ▶ if ($e =~ /NOSCRIPT/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /NOSCRIPT/) { } 0 0 0
217 0 $redis->eval($script, 1, $key, $token);
218 }
219 else {
220 0 croak $e;
221 }
222 0 };
223 }
224  
225 0 ▶sub invoke_command {
Subroutine invoke_command: not called CC=4 SCAR=30.0
Pod undocumented
226 0 my @command = @_;
227 0 debugf "invoking command: @command";
228 0 ▶ if (my $pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 0 0 0
229 0 ▶ local $SIG{CHLD} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 ▶ local $SIG{TERM} = $SIG{HUP} = $SIG{INT} = $SIG{QUIT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 my $signal = shift;
232 0 warnf "Got signal %s", $signal;
233 0 kill $signal, $pid;
234 0 };
235 0 wait;
236 }
237 else {
238 0 exec @command;
239 0 die;
240 }
241 0 my $code = $?;
242 0 ▶ if ($code == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code == -1) { } 0 0 0
elsif ($code & 127) { } 0 0 0
243 0 critf "faildto execute: %s", $!;
244 0 return $code;
245 }
246 elsif ($code & 127) {
247 0 debugf "child died with signal %d", $code & 127;
248 0 return $code;
249 }
250 else {
251 0 $code = $code >> 8; # to raw exit code
252 0 debugf "child exit with code: %s", $code;
253 0 return $code;
254 }
255 }
256  
257 0 ▶sub log_minimal_print {
Subroutine log_minimal_print: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my ( $time, $type, $message, $trace) = @_;
259 0 warn "$time $$ $type $message\n";
260 }
261  
262 0 ▶sub create_token {
Subroutine create_token: not called CC=1 SCAR=6.9
Pod undocumented
263 0 Time::HiRes::time() . rand();
264 }
265  
266 15 1;
267 __END__
↑ Summary