File Coverage

blib/lib/OpenSearch/Client/CxnPool/Sniff.pm
Criterion Covered Total %
statement 36 36 100.0
branch 12 12 100.0
condition n/a
subroutine 6 6 100.0
pod 2 3 66.6
total 56 57 98.2


line stmt bran cond sub pod time code
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::CxnPool::Sniff;
24             $OpenSearch::Client::CxnPool::Sniff::VERSION = '3.007002';
25 11     11   5412 use Moo;
  11         22  
  11         64  
26             with 'OpenSearch::Client::Role::CxnPool::Sniff',
27             'OpenSearch::Client::Role::Is_Sync';
28              
29 11     11   4436 use OpenSearch::Client::Util qw(throw);
  11         34  
  11         72  
30 11     11   2756 use namespace::clean;
  11         24  
  11         57  
31              
32             #===================================
33             sub next_cxn {
34             #===================================
35 50     50 1 145 my ($self) = @_;
36              
37 50 100       228 $self->sniff if $self->next_sniff <= time();
38              
39 50         185 my $cxns = $self->cxns;
40 50         75 my $total = @$cxns;
41              
42 50         143 while ( 0 < $total-- ) {
43 49         172 my $cxn = $cxns->[ $self->next_cxn_num ];
44 49 100       148 return $cxn if $cxn->is_live;
45             }
46              
47 5         18 throw( "NoNodes",
48             "No nodes are available: [" . $self->cxns_seeds_str . ']' );
49             }
50              
51             #===================================
52             sub sniff {
53             #===================================
54 29     29 1 69 my $self = shift;
55              
56 29         66 my $cxns = $self->cxns;
57 29         49 my $total = @$cxns;
58 29         42 my @skipped;
59              
60 29         79 while ( 0 < $total-- ) {
61 19         136 my $cxn = $cxns->[ $self->next_cxn_num ];
62 19 100       81 if ( $cxn->is_dead ) {
63 5         9 push @skipped, $cxn;
64             }
65             else {
66 14 100       25 $self->sniff_cxn($cxn) and return;
67 1         3 $cxn->mark_dead;
68             }
69             }
70              
71 16         32 for my $cxn (@skipped) {
72 4 100       7 $self->sniff_cxn($cxn) and return;
73             }
74              
75 15         344 $self->logger->info("No live nodes available. Trying seed nodes.");
76 15         19106 for my $seed ( @{ $self->seed_nodes } ) {
  15         66  
77 20         142 my $cxn = $self->cxn_factory->new_cxn($seed);
78 20 100       3660 $self->sniff_cxn($cxn) and return;
79             }
80              
81             }
82              
83             #===================================
84             sub sniff_cxn {
85             #===================================
86 38     38 0 70 my ( $self, $cxn ) = @_;
87 38         101 return $self->parse_sniff( $cxn->sniff );
88             }
89              
90             1;
91              
92             __END__