CxnPool/Async/Sniff.pm

filter: Statementstmt 83.3% 70 / 84 Branchbran 50.0% 13 / 26 Conditioncond 63.6% 7 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 74.2% 104 / 140 CC 19 SCAR 20.1
CC
19
Cov
74%
__ANON__
12.1
sniff
8.6
next_cxn
7.5
CRAP 7.5
« Cxn/AEHTTP.pm ↑ Summary CxnPool/Async/Static.pm »
1 # Licensed to Elasticsearch B.V under one or more agreements.
2 # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3 # See the LICENSE file in the project root for more information
4  
5 package Search::Elasticsearch::CxnPool::Async::Sniff;
6 11 $Search::Elasticsearch::CxnPool::Async::Sniff::VERSION = '7.30_1'; # TRIAL
7  
8 11 $Search::Elasticsearch::CxnPool::Async::Sniff::VERSION = '7.301';
9 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 with 'Search::Elasticsearch::Role::CxnPool::Sniff',
11 'Search::Elasticsearch::Role::Is_Async';
12  
13 11 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Search::Elasticsearch::Util qw(new_error);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 has 'concurrent_sniff' => ( is => 'rw', default => 4 );
19 11 has '_current_sniff' => ( is => 'rw', clearer => '_clear_sniff' );
20  
21 #===================================
22 sub next_cxn {
23 #===================================
24 24 ▶ my ( $self, $no_sniff ) = @_;
Subroutine next_cxn: called CC=6 SCAR=20.1
Pod documented
25  
26 12 ▶ return $self->sniff->then( sub { $self->next_cxn('no_sniff') } )
Subroutine __ANON__: called CC=1 SCAR=0.0
27 24 ▶ if $self->next_sniff <= time() && !$no_sniff;
Branch: 1 branch
BranchTrueFalseTotal
if $self->next_sniff <= time and not $no_sniff 12 12 24
Condition66%2/3
$self->next_sniff <= time and not $no_sniff
!ll&&!rl&&r
01212
MC/DC50%1/2
$self->next_sniff <= time and not $no_sniff
$self->next_sniff <= time not $no_sniff
Truth table66%2/3
A and not(B)
A= $self->next_sniff <= timeB= not $no_sniff
ABresult
0X0
100
111
28  
29 12 my $cxns = $self->cxns;
30 12 my $total = @$cxns;
31 12 my $cxn;
32  
33 12 ▶ while ( 0 < $total-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $total--) 0 12 12
34 0 $cxn = $cxns->[ $self->next_cxn_num ];
35 0 ▶ last if $cxn->is_live;
Branch: 1 branch
BranchTrueFalseTotal
if $cxn->is_live 0 0 0
36 0 undef $cxn;
37 }
38  
39 12 my $deferred = deferred;
40  
41 12 ▶ if ($cxn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cxn) { } 0 12 12
42 0 $deferred->resolve($cxn);
43 }
44 else {
45 12 $deferred->reject(
46 new_error(
47 "NoNodes",
48 "No nodes are available: [" . $self->cxns_seeds_str . ']'
49 )
50 );
51 }
52 12 return $deferred->promise;
53 }
54  
55 #===================================
56 sub sniff {
57 #===================================
58 12 ▶ my $self = shift;
Subroutine sniff: called CC=7 SCAR=21.5
Pod documented
59  
60 12 my $promise;
61 12 ▶ if ( $promise = $self->_current_sniff ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($promise = $self->_current_sniff) 0 12 12
62 0 return $promise;
63 }
64  
65 12 my $deferred = deferred;
66 12 my $cxns = $self->cxns;
67 12 my $total = @$cxns;
68 12 my $done = 0;
69 12 my $current = 0;
70 12 my $done_seeds = 0;
71 12 $promise = $self->_current_sniff( $deferred->promise );
72  
73 12 my ( @all, @skipped );
74  
75 12 ▶ while ( 0 < $total-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $total--) 0 12 12
76 0 my $cxn = $cxns->[ $self->next_cxn_num ];
77 0 ▶ if ( $cxn->is_dead ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cxn->is_dead) { } 0 0 0
78 0 push @skipped, $cxn;
79 }
80 else {
81 0 push @all, $cxn;
82 }
83 }
84  
85 12 push @all, @skipped;
86 12 ▶ unless (@all) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@all) 12 0 12
87 12 @all = $self->_seeds_as_cxns;
88 12 $done_seeds++;
89 }
90  
91 12 my ( $weak_check_sniff, $cxn );
92 my $check_sniff = sub {
93  
94 22 ▶ return if $done;
Subroutine __ANON__: called CC=8 SCAR=24.9
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 22 22
95 22 my ( $cxn, $nodes ) = @_;
96 22 ▶ if ( $nodes && $self->parse_sniff($nodes) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes and $self->parse_sniff($nodes)) 0 22 22
Condition33%1/3
$nodes and $self->parse_sniff($nodes)
!ll&&!rl&&r
2200
MC/DC0%0/2
$nodes and $self->parse_sniff($nodes)
$nodes $self->parse_sniff($nodes)
Truth table33%1/3
A and B
A= $nodesB= $self->parse_sniff($nodes)
ABresult
0X0
100
111
97 0 $done++;
98 0 $self->_clear_sniff;
99 0 return $deferred->resolve();
100 }
101  
102 22 ▶ unless ( @all || $done_seeds++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@all or $done_seeds++) 0 22 22
Condition66%2/3
@all or $done_seeds++
l!l&&r!l&&!r
10120
MC/DC0%0/2
@all or $done_seeds++
@all $done_seeds++
Truth table66%2/3
A or B
A= @allB= $done_seeds++
ABresult
1X1
011
000
103 0 $self->logger->info("No live nodes available. Trying seed nodes.");
104 0 @all = $self->_seeds_as_cxns;
105 }
106  
107 22 ▶ if ( my $cxn = shift @all ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cxn = shift @all) 10 12 22
108 10 return $cxn->sniff->done($weak_check_sniff);
109 }
110 12 ▶ if ( --$current == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$current == 0) 12 0 12
111 12 $self->_clear_sniff;
112 12 $deferred->resolve();
113 }
114 12 };
115 12 weaken( $weak_check_sniff = $check_sniff );
116  
117 12 for ( 1 .. $self->concurrent_sniff ) {
118 24 ▶ my $cxn = shift(@all) || last;
Condition100%2/2
shift @all || (last)
l!l
1212
MC/DC100%1/1
shift @all || (last)
shift @all
Truth table100%2/2
A
A= shift @all
Aresult
11
00
119 12 $current++;
120 12 $cxn->sniff->done($check_sniff);
121 }
122  
123 12 return $promise;
124 }
125  
126 #===================================
127 sub _seeds_as_cxns {
128 #===================================
129 12 ▶ my $self = shift;
Subroutine _seeds_as_cxns: called CC=1 SCAR=0.0
130 12 my $factory = $self->cxn_factory;
131 12 return map { $factory->new_cxn($_) } @{ $self->seed_nodes };
132 }
133  
134 11 1;
135  
136 # ABSTRACT: An async CxnPool for connecting to a local cluster with a dynamic node list
137  
138 __END__
« Cxn/AEHTTP.pm ↑ Summary CxnPool/Async/Static.pm »