Elasticsearch/Role/CxnPool.pm

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