| 1 | # OpenSearch::Client is an unofficial client for OpenSearch. | ||||||||||||
| 2 | # It is derived from Search::Elasticsearch version 7.714 | ||||||||||||
| 3 | # License details from the original work are contained in the | ||||||||||||
| 4 | # NOTICE file distributed with this work. | ||||||||||||
| 5 | # | ||||||||||||
| 6 | #----------------------------------------------------------------------- | ||||||||||||
| 7 | # OpenSearch::Client | ||||||||||||
| 8 | #----------------------------------------------------------------------- | ||||||||||||
| 9 | # Copyright 2026 Mark Dootson | ||||||||||||
| 10 | # | ||||||||||||
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||
| 12 | # you may not use this file except in compliance with the License. | ||||||||||||
| 13 | # You may obtain a copy of the License at | ||||||||||||
| 14 | # | ||||||||||||
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| 16 | # | ||||||||||||
| 17 | # Unless required by applicable law or agreed to in writing, software | ||||||||||||
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| 20 | # See the License for the specific language governing permissions and | ||||||||||||
| 21 | # limitations under the License. | ||||||||||||
| 22 | |||||||||||||
| 23 | package OpenSearch::Client::Role::CxnPool; | ||||||||||||
| 24 | 56 | $OpenSearch::Client::Role::CxnPool::VERSION = '3.007011'; | |||||||||||
| 25 | 56 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 56 | ▶ | use OpenSearch::Client::Util qw(parse_params); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 56 | ▶ | use List::Util qw(shuffle); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 56 | ▶ | use IO::Select(); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 56 | ▶ | use Time::HiRes qw(time sleep); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | 56 | ▶ | use OpenSearch::Client::Util qw(to_list); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | 56 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | |||||||||||||
| 33 | 56 | requires qw(next_cxn schedule_check); | |||||||||||
| 34 | |||||||||||||
| 35 | 56 | has 'cxn_factory' => ( is => 'ro', required => 1 ); | |||||||||||
| 36 | 56 | has 'logger' => ( is => 'ro', required => 1 ); | |||||||||||
| 37 | 56 | has 'serializer' => ( is => 'ro', required => 1 ); | |||||||||||
| 38 | 56 | has 'current_cxn_num' => ( is => 'rwp', default => 0 ); | |||||||||||
| 39 | 100 | ▶ | has 'cxns' => ( is => 'rwp', default => sub { [] } ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 56 | has 'seed_nodes' => ( is => 'ro', required => 1 ); | |||||||||||
| 41 | 56 | has 'retries' => ( is => 'rw', default => 0 ); | |||||||||||
| 42 | 56 | has 'randomize_cxns' => ( is => 'ro', default => 1 ); | |||||||||||
| 43 | |||||||||||||
| 44 | #=================================== | ||||||||||||
| 45 | 100 | ▶ | around BUILDARGS => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 46 | #=================================== | ||||||||||||
| 47 | 100 | my $orig = shift; | |||||||||||
| 48 | 100 | my $params = $orig->(@_); | |||||||||||
| 49 | 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'}
| |||||||||||||
| 50 | |||||||||||||
| 51 | @seed = $params->{cxn_factory}->default_host | ||||||||||||
| 52 | 100 | ▶ | unless @seed; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 100 | $params->{seed_nodes} = \@seed; | |||||||||||
| 54 | 100 | return $params; | |||||||||||
| 55 | 56 | }; | |||||||||||
| 56 | |||||||||||||
| 57 | #=================================== | ||||||||||||
| 58 | 200 | ▶ | sub next_cxn_num { | ||||||||||
|
Subroutine
next_cxn_num: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 59 | #=================================== | ||||||||||||
| 60 | 200 | my $self = shift; | |||||||||||
| 61 | 200 | my $cxns = $self->cxns; | |||||||||||
| 62 | 200 | ▶ | return unless @$cxns; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 200 | my $current = $self->current_cxn_num; | |||||||||||
| 64 | 200 | $self->_set_current_cxn_num( ( $current + 1 ) % @$cxns ); | |||||||||||
| 65 | 200 | return $current; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | #=================================== | ||||||||||||
| 69 | 112 | ▶ | sub set_cxns { | ||||||||||
|
Subroutine
set_cxns: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 70 | #=================================== | ||||||||||||
| 71 | 112 | my $self = shift; | |||||||||||
| 72 | 112 | my $factory = $self->cxn_factory; | |||||||||||
| 73 | 112 | my @cxns = map { $factory->new_cxn($_) } @_; | |||||||||||
| 74 | 112 | ▶ | @cxns = shuffle @cxns if $self->randomize_cxns; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 112 | $self->_set_cxns( \@cxns ); | |||||||||||
| 76 | 112 | $self->_set_current_cxn_num(0); | |||||||||||
| 77 | |||||||||||||
| 78 | $self->logger->infof( "Current cxns: %s", | ||||||||||||
| 79 | 112 | [ map { $_->stringify } @cxns ] ); | |||||||||||
| 80 | |||||||||||||
| 81 | 112 | return; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | #=================================== | ||||||||||||
| 85 | 114 | ▶ | sub request_ok { | ||||||||||
|
Subroutine
request_ok: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 86 | #=================================== | ||||||||||||
| 87 | 114 | my ( $self, $cxn ) = @_; | |||||||||||
| 88 | 114 | $cxn->mark_live; | |||||||||||
| 89 | 114 | $self->reset_retries; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | #=================================== | ||||||||||||
| 93 | 46 | ▶ | sub request_failed { | ||||||||||
|
Subroutine
request_failed: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 94 | #=================================== | ||||||||||||
| 95 | 46 | my ( $self, $cxn, $error ) = @_; | |||||||||||
| 96 | |||||||||||||
| 97 | 46 | ▶ | if ( $error->is( 'Cxn', 'Timeout' ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 33 | ▶ | $cxn->mark_dead if $self->should_mark_dead($error); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 33 | $self->schedule_check; | |||||||||||
| 100 | |||||||||||||
| 101 | 33 | ▶ | if ( $self->should_retry($error) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 25 | my $retries = $self->retries( $self->retries + 1 ); | |||||||||||
| 103 | 25 | ▶ | return 1 if $retries < $self->_max_retries; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | else { | ||||||||||||
| 107 | 13 | ▶ | $cxn->mark_live if $cxn; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | } | ||||||||||||
| 109 | 27 | $self->reset_retries; | |||||||||||
| 110 | 27 | return 0; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | #=================================== | ||||||||||||
| 114 | 33 | ▶ | sub should_retry { | ||||||||||
|
Subroutine
should_retry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 115 | #=================================== | ||||||||||||
| 116 | 33 | my ( $self, $error ) = @_; | |||||||||||
| 117 | 33 | return $error->is('Cxn'); | |||||||||||
| 118 | } | ||||||||||||
| 119 | |||||||||||||
| 120 | #=================================== | ||||||||||||
| 121 | 21 | ▶ | sub should_mark_dead { | ||||||||||
|
Subroutine
should_mark_dead: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 122 | #=================================== | ||||||||||||
| 123 | 21 | my ( $self, $error ) = @_; | |||||||||||
| 124 | 21 | return $error->is('Cxn'); | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | #=================================== | ||||||||||||
| 128 | 7 | ▶ | sub cxns_str { | ||||||||||
|
Subroutine
cxns_str: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 129 | #=================================== | ||||||||||||
| 130 | 7 | my $self = shift; | |||||||||||
| 131 | 7 | join ", ", map { $_->stringify } @{ $self->cxns }; | |||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | #=================================== | ||||||||||||
| 135 | 5 | ▶ | sub cxns_seeds_str { | ||||||||||
|
Subroutine
cxns_seeds_str: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 136 | #=================================== | ||||||||||||
| 137 | 5 | my $self = shift; | |||||||||||
| 138 | 4 | join ", ", ( map { $_->stringify } @{ $self->cxns } ), | |||||||||||
| 139 | 5 | @{ $self->seed_nodes }; | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | #=================================== | ||||||||||||
| 143 | 141 | ▶ | sub reset_retries { shift->retries(0) } | ||||||||||
|
Subroutine
reset_retries: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 144 | 14 | ▶ | sub _max_retries {2} | ||||||||||
|
Subroutine
_max_retries: called CC=1 SCAR=0.0
| |||||||||||||
| 145 | #=================================== | ||||||||||||
| 146 | |||||||||||||
| 147 | 56 | 1; | |||||||||||
| 148 | |||||||||||||
| 149 | __END__ | ||||||||||||