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