Elasticsearch/Role/CxnPool.pm

filter: Statementstmt 100% 85 / 85 Branchbran 93.7% 15 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 19 / 19 Podpod 100% 9 / 9 totaltotal 99.2% 131 / 132 CC 10 SCAR 8.9
CC
10
Cov
99%
request_failed
6.0
__ANON__
3.0
next_cxn_num
2.0
CRAP 2.4
« Elasticsearch/Client/6_0/Direct.pm ↑ Summary Elasticsearch/Role/Serializer/JSON.pm »
1 package Search::Elasticsearch::Role::CxnPool;
2 55 $Search::Elasticsearch::Role::CxnPool::VERSION = '6.80';
3 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use Search::Elasticsearch::Util qw(parse_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use IO::Select();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use Search::Elasticsearch::Util qw(to_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 55 requires qw(next_cxn schedule_check);
12  
13 55 has 'cxn_factory' => ( is => 'ro', required => 1 );
14 55 has 'logger' => ( is => 'ro', required => 1 );
15 55 has 'serializer' => ( is => 'ro', required => 1 );
16 55 has 'current_cxn_num' => ( is => 'rwp', default => 0 );
17 100 ▶has 'cxns' => ( is => 'rwp', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18 55 has 'seed_nodes' => ( is => 'ro', required => 1 );
19 55 has 'retries' => ( is => 'rw', default => 0 );
20 55 has 'randomize_cxns' => ( is => 'ro', default => 1 );
21  
22 #===================================
23 around BUILDARGS => sub {
24 #===================================
25 100 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
26 100 my $params = $orig->(@_);
27 100 ▶ my @seed = grep {$_} to_list( delete $params->{nodes} || ('') );
Condition100%2/2
delete $params->{'nodes'} || ''
l!l
6535
MC/DC100%1/1
delete $params->{'nodes'} || ''
delete $params->{'nodes'}
Truth table100%2/2
A
A= delete $params->{'nodes'}
Aresult
11
00
28  
29 @seed = $params->{cxn_factory}->default_host
30 100 ▶ unless @seed;
Branch: 1 branch
BranchTrueFalseTotal
unless @seed 35 65 100
31 100 $params->{seed_nodes} = \@seed;
32 100 return $params;
33 55 };
34  
35 #===================================
36 sub next_cxn_num {
37 #===================================
38 200 ▶ my $self = shift;
Subroutine next_cxn_num: called CC=2 SCAR=7.0
Pod documented
39 200 my $cxns = $self->cxns;
40 200 ▶ return unless @$cxns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cxns 0 200 200
41 200 my $current = $self->current_cxn_num;
42 200 $self->_set_current_cxn_num( ( $current + 1 ) % @$cxns );
43 200 return $current;
44 }
45  
46 #===================================
47 sub set_cxns {
48 #===================================
49 112 ▶ my $self = shift;
Subroutine set_cxns: called CC=2 SCAR=6.9
Pod documented
50 112 my $factory = $self->cxn_factory;
51 112 my @cxns = map { $factory->new_cxn($_) } @_;
52 112 ▶ @cxns = shuffle @cxns if $self->randomize_cxns;
Branch: 1 branch
BranchTrueFalseTotal
if $self->randomize_cxns 67 45 112
53 112 $self->_set_cxns( \@cxns );
54 112 $self->_set_current_cxn_num(0);
55  
56 $self->logger->infof( "Current cxns: %s",
57 112 [ map { $_->stringify } @cxns ] );
58  
59 112 return;
60 }
61  
62 #===================================
63 sub request_ok {
64 #===================================
65 114 ▶ my ( $self, $cxn ) = @_;
Subroutine request_ok: called CC=1 SCAR=0.0
Pod documented
66 114 $cxn->mark_live;
67 114 $self->reset_retries;
68 }
69  
70 #===================================
71 sub request_failed {
72 #===================================
73 46 ▶ my ( $self, $cxn, $error ) = @_;
Subroutine request_failed: called CC=6 SCAR=17.9
Pod documented
74  
75 46 ▶ if ( $error->is( 'Cxn', 'Timeout' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error->is('Cxn', 'Timeout')) { } 33 13 46
76 33 ▶ $cxn->mark_dead if $self->should_mark_dead($error);
Branch: 1 branch
BranchTrueFalseTotal
if $self->should_mark_dead($error) 26 7 33
77 33 $self->schedule_check;
78  
79 33 ▶ if ( $self->should_retry($error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->should_retry($error)) 25 8 33
80 25 my $retries = $self->retries( $self->retries + 1 );
81 25 ▶ return 1 if $retries < $self->_max_retries;
Branch: 1 branch
BranchTrueFalseTotal
if $retries < $self->_max_retries 19 6 25
82 }
83 }
84 else {
85 13 ▶ $cxn->mark_live if $cxn;
Branch: 1 branch
BranchTrueFalseTotal
if $cxn 1 12 13
86 }
87 27 $self->reset_retries;
88 27 return 0;
89 }
90  
91 #===================================
92 sub should_retry {
93 #===================================
94 33 ▶ my ( $self, $error ) = @_;
Subroutine should_retry: called CC=1 SCAR=0.0
Pod documented
95 33 return $error->is('Cxn');
96 }
97  
98 #===================================
99 sub should_mark_dead {
100 #===================================
101 21 ▶ my ( $self, $error ) = @_;
Subroutine should_mark_dead: called CC=1 SCAR=0.0
Pod documented
102 21 return $error->is('Cxn');
103 }
104  
105 #===================================
106 sub cxns_str {
107 #===================================
108 7 ▶ my $self = shift;
Subroutine cxns_str: called CC=1 SCAR=0.0
Pod documented
109 7 join ", ", map { $_->stringify } @{ $self->cxns };
110 }
111  
112 #===================================
113 sub cxns_seeds_str {
114 #===================================
115 5 ▶ my $self = shift;
Subroutine cxns_seeds_str: called CC=1 SCAR=0.0
Pod documented
116 4 join ", ", ( map { $_->stringify } @{ $self->cxns } ),
117 5 @{ $self->seed_nodes };
118 }
119  
120 #===================================
121 141 ▶sub reset_retries { shift->retries(0) }
Subroutine reset_retries: called CC=1 SCAR=0.0
Pod documented
122 14 ▶sub _max_retries {2}
Subroutine _max_retries: called CC=1 SCAR=0.0
123 #===================================
124  
125 55 1;
126  
127 =pod
128  
129 =encoding UTF-8
130  
131 =head1 NAME
132  
133 Search::Elasticsearch::Role::CxnPool - Provides common functionality to the CxnPool implementations
134  
135 =head1 VERSION
136  
137 version 6.80
138  
139 =head1 DESCRIPTION
140  
141 See the CxnPool implementations:
142  
143 =over
144  
145 =item *
146  
147 L<Search::Elasticsearch::CxnPool::Static>
148  
149 =item *
150  
151 L<Search::Elasticsearch::CxnPool::Sniff>
152  
153 =item *
154  
155 L<Search::Elasticsearch::CxnPool::Static::NoPing>
156  
157 =back
158  
159 =head1 CONFIGURATION
160  
161 These configuration options should not be set by the user but are
162 documented here for completeness.
163  
164 =head2 C<randomize_cxns>
165  
166 By default, the order of cxns passed to L</set_cxns()> is randomized
167 before they are stored. Set C<randomize_cxns> to a false value to
168 disable.
169  
170 =head1 METHODS
171  
172 =head2 C<cxn_factory()>
173  
174 $factory = $cxn_pool->cxn_factory
175  
176 Returns the L<Search::Elasticsearch::Cxn::Factory> object for creating a new
177 C<$cxn> instance.
178  
179 =head2 C<logger()>
180  
181 $logger = $cxn_pool->logger
182  
183 Returns the L<Search::Elasticsearch::Role::Logger>-based object, which
184 defaults to L<Search::Elasticsearch::Logger::LogAny>.
185  
186 =head2 C<serializer()>
187  
188 $serializer = $cxn_pool->serializer
189  
190 Returns the L<Search::Elasticsearch::Role::Serializer>-based object,
191 which defaults to L<Search::Elasticsearch::Serializer::JSON>.
192  
193 =head2 C<current_cxn_num()>
194  
195 $num = $cxn_pool->current_cxn_num
196  
197 Returns the current cxn number, which is an offset into
198 the array of cxns set by L</set_cxns()>.
199  
200 =head2 C<cxns()>
201  
202 \@cxns = $cxn_pool->cxns;
203  
204 Returns the current list of L<Search::Elasticsearch::Role::Cxn>-based
205 cxn objects as set by L</set_cxns()>.
206  
207 =head2 C<seed_nodes()>
208  
209 \@seed_nodes = $cxn_pool->seed_nodes
210  
211 Returns the list of C<nodes> originally specified when calling
212 L<Search::Elasticsearch/new()>.
213  
214 =head2 C<next_cxn_num()>
215  
216 $num = $cxn_pool->next_cxn_num;
217  
218 Returns the number of the next connection, in round-robin fashion. Updates
219 the L</current_cxn_num()>.
220  
221 =head2 C<set_cxns()>
222  
223 $cxn_pool->set_cxns(@nodes);
224  
225 Takes a list of nodes, converts them into L<Search::Elasticsearch::Role::Cxn>-based
226 objects and makes them accessible via L</cxns()>.
227  
228 =head2 C<request_ok()>
229  
230 $cxn_pool->request_ok($cxn);
231  
232 Called when a request by the specified C<$cxn> object has completed successfully.
233 Marks the C<$cxn> as live.
234  
235 =head2 C<request_failed()>
236  
237 $should_retry = $cxn_pool->request_failed($cxn,$error);
238  
239 Called when a request by the specified C<$cxn> object has failed. Returns
240 C<1> if the request should be retried or C<0> if it shouldn't.
241  
242 =head2 C<should_retry()>
243  
244 $bool = $cxn_pool->should_retry($error);
245  
246 Examines the error to decide whether the request should be retried or not.
247 By default, only L<Search::Elasticsearch::Error/Search::Elasticsearch::Error::Cxn> errors
248 are retried.
249  
250 =head2 C<should_mark_dead()>
251  
252 $bool = $cxn_pool->should_mark_dead($error);
253  
254 Examines the error to decide whether the C<$cxn> should be marked as dead or not.
255 By default, only L<Search::Elasticsearch::Error/Search::Elasticsearch::Error::Cxn> errors
256 cause a C<$cxn> to be marked as dead.
257  
258 =head2 C<cxns_str()>
259  
260 $str = $cxn_pool->cxns_str
261  
262 Returns all L</cxns()> as a string for logging purposes.
263  
264 =head2 C<cxns_seeds_str()>
265  
266 $str = $cxn_pool->cxns_seeeds_str
267  
268 Returns all L</cxns()> and L</seed_nodes()> as a string for logging purposes.
269  
270 =head2 C<retries()>
271  
272 $retries = $cxn_pool->retries
273  
274 The number of times the current request has been retried.
275  
276 =head2 C<reset_retries()>
277  
278 $cxn_pool->reset_retries;
279  
280 Called at the start of a new request to reset the retries count.
281  
282 =head1 AUTHOR
283  
284 Enrico Zimuel <enrico.zimuel@elastic.co>
285  
286 =head1 COPYRIGHT AND LICENSE
287  
288 This software is Copyright (c) 2020 by Elasticsearch BV.
289  
290 This is free software, licensed under:
291  
292 The Apache License, Version 2.0, January 2004
293  
294 =cut
295  
296 __END__
« Elasticsearch/Client/6_0/Direct.pm ↑ Summary Elasticsearch/Role/Serializer/JSON.pm »