File Coverage

blib/lib/Search/Elasticsearch/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             # 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::CxnPool::Sniff;
19             $Search::Elasticsearch::CxnPool::Sniff::VERSION = '8.00';
20 11     11   5172 use Moo;
  11         28  
  11         59  
21             with 'Search::Elasticsearch::Role::CxnPool::Sniff',
22             'Search::Elasticsearch::Role::Is_Sync';
23              
24 11     11   3660 use Search::Elasticsearch::Util qw(throw);
  11         38  
  11         77  
25 11     11   2911 use namespace::clean;
  11         28  
  11         68  
26              
27             #===================================
28             sub next_cxn {
29             #===================================
30 50     50 1 113 my ($self) = @_;
31              
32 50 100       255 $self->sniff if $self->next_sniff <= time();
33              
34 50         186 my $cxns = $self->cxns;
35 50         90 my $total = @$cxns;
36              
37 50         143 while ( 0 < $total-- ) {
38 49         150 my $cxn = $cxns->[ $self->next_cxn_num ];
39 49 100       186 return $cxn if $cxn->is_live;
40             }
41              
42 5         21 throw( "NoNodes",
43             "No nodes are available: [" . $self->cxns_seeds_str . ']' );
44             }
45              
46             #===================================
47             sub sniff {
48             #===================================
49 29     29 1 59 my $self = shift;
50              
51 29         260 my $cxns = $self->cxns;
52 29         60 my $total = @$cxns;
53 29         57 my @skipped;
54              
55 29         145 while ( 0 < $total-- ) {
56 19         159 my $cxn = $cxns->[ $self->next_cxn_num ];
57 19 100       66 if ( $cxn->is_dead ) {
58 5         16 push @skipped, $cxn;
59             }
60             else {
61 14 100       34 $self->sniff_cxn($cxn) and return;
62 1         5 $cxn->mark_dead;
63             }
64             }
65              
66 16         47 for my $cxn (@skipped) {
67 4 100       10 $self->sniff_cxn($cxn) and return;
68             }
69              
70 15         397 $self->logger->info("No live nodes available. Trying seed nodes.");
71 15         21191 for my $seed ( @{ $self->seed_nodes } ) {
  15         82  
72 20         122 my $cxn = $self->cxn_factory->new_cxn($seed);
73 20 100       4385 $self->sniff_cxn($cxn) and return;
74             }
75              
76             }
77              
78             #===================================
79             sub sniff_cxn {
80             #===================================
81 38     38 0 93 my ( $self, $cxn ) = @_;
82 38         128 return $self->parse_sniff( $cxn->sniff );
83             }
84              
85             1;
86              
87             # ABSTRACT: A CxnPool for connecting to a local cluster with a dynamic node list
88              
89             __END__