Elasticsearch/Role/CxnPool/Sniff.pm

filter: Statementstmt 100% 54 / 54 Branchbran 88.8% 16 / 18 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 93.4% 100 / 107 CC 16 SCAR 21.4
CC
16
Cov
95%
parse_sniff
14.1
BUILDARGS
2.0
_extract_host
2.0
CRAP 8.5
« Elasticsearch/Role/Transport.pm ↑ Summary Elasticsearch/Error.pm »
1 package Search::Elasticsearch::Role::CxnPool::Sniff;
2 11 $Search::Elasticsearch::Role::CxnPool::Sniff::VERSION = '6.80';
3 11 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 with 'Search::Elasticsearch::Role::CxnPool';
5 11 requires 'next_cxn', 'sniff';
6 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Search::Elasticsearch::Util qw(parse_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 has 'sniff_interval' => ( is => 'ro', default => 300 );
13 11 has 'next_sniff' => ( is => 'rw', default => 0 );
14 11 has 'sniff_max_content_length' => ( is => 'ro' );
15  
16 #===================================
17 sub BUILDARGS {
18 #===================================
19 12 ▶ my ( $class, $params ) = parse_params(@_);
Subroutine BUILDARGS: called CC=2 SCAR=7.1
Pod undocumented
20 $params->{sniff_max_content_length} = !$params->{max_content_length}
21 12 ▶ unless defined $params->{sniff_max_content_length};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"sniff_max_content_length"} 12 0 12
22 12 return $params;
23 }
24  
25 #===================================
26 sub schedule_check {
27 #===================================
28 14 ▶ my $self = shift;
Subroutine schedule_check: called CC=1 SCAR=0.0
Pod documented
29 14 $self->logger->info("Require sniff before next request");
30 14 $self->next_sniff(-1);
31 }
32  
33 #===================================
34 sub parse_sniff {
35 #===================================
36 38 ▶ my $self = shift;
Subroutine parse_sniff: called CC=14 SCAR=26.4
Pod documented
37 38 ▶ my $nodes = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $nodes = shift() or return 13 25 38
38 25 my @live_nodes;
39 25 my $max = 0;
40 25 my $sniff_max = $self->sniff_max_content_length;
41  
42 25 for my $node_id ( keys %$nodes ) {
43 47 my $data = $nodes->{$node_id};
44  
45 47 ▶ my $addr = $data->{http}{publish_address} || $data->{http_address};
Condition66%2/3
$data->{'http'}{'publish_address'} || $data->{'http_address'}
l!l&&r!l&&!r
0461
MC/DC50%1/2
$data->{'http'}{'publish_address'} || $data->{'http_address'}
$data->{'http'}{'publish_address'} $data->{'http_address'}
Truth table66%2/3
A || B
A= $data->{'http'}{'publish_address'}B= $data->{'http_address'}
ABresult
1X1
011
000
46 47 ▶ my $host = $self->_extract_host($addr)
Branch: 1 branch
BranchTrueFalseTotal
my $host = $self->_extract_host($addr) or next 1 46 47
47 or next;
48  
49 46 ▶ $host = $self->should_accept_node( $host, $node_id, $data )
Branch: 1 branch
BranchTrueFalseTotal
$host = $self->should_accept_node($host, $node_id, $data) or next 0 46 46
50 or next;
51  
52 46 push @live_nodes, $host;
53 46 ▶ next unless $sniff_max and $data->{http};
Branch: 1 branch
BranchTrueFalseTotal
unless $sniff_max and $data->{"http"} 3 43 46
Condition66%2/3
$sniff_max and $data->{"http"}
!ll&&!rl&&r
3043
MC/DC50%1/2
$sniff_max and $data->{"http"}
$sniff_max $data->{"http"}
Truth table66%2/3
A and B
A= $sniff_maxB= $data->{"http"}
ABresult
0X0
100
111
54  
55 43 ▶ my $node_max = $data->{http}{max_content_length_in_bytes} || 0;
Condition100%2/2
$data->{'http'}{'max_content_length_in_bytes'} || 0
l!l
241
MC/DC100%1/1
$data->{'http'}{'max_content_length_in_bytes'} || 0
$data->{'http'}{'max_content_length_in_bytes'}
Truth table100%2/2
A
A= $data->{'http'}{'max_content_length_in_bytes'}
Aresult
11
00
56 43 ▶ $max
Branch: 2 branches
BranchTrueFalseTotal
$node_max == 0 ? : 41 2 43
$max == 0 ? : 1 1 2
57 = $node_max == 0 ? $max
58 : $max == 0 ? $node_max
59 : min( $node_max, $max );
60 }
61  
62 25 ▶ return unless @live_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @live_nodes 1 24 25
63  
64 24 ▶ $self->cxn_factory->max_content_length($max)
Branch: 1 branch
BranchTrueFalseTotal
if $sniff_max and $max 1 23 24
Condition100%3/3
$sniff_max and $max
!ll&&!rl&&r
1221
MC/DC100%2/2
$sniff_max and $max
$sniff_max $max
Truth table100%3/3
A and B
A= $sniff_maxB= $max
ABresult
0X0
100
111
65 if $sniff_max and $max;
66  
67 24 $self->set_cxns(@live_nodes);
68 24 my $next = $self->next_sniff( time() + $self->sniff_interval );
69 24 $self->logger->infof( "Next sniff at: %s", scalar localtime($next) );
70  
71 24 return 1;
72 }
73  
74 #===================================
75 sub _extract_host {
76 #===================================
77 53 ▶ my $self = shift;
Subroutine _extract_host: called CC=2 SCAR=6.9
78 53 ▶ my $host = shift || return;
Condition100%2/2
shift() || (return)
l!l
512
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
79 51 $host =~ s{^inet\[(.+)\]$}{$1};
80 51 $host =~ s{^[^/]*/}{};
81 51 return $host;
82 }
83  
84 #===================================
85 46 ▶sub should_accept_node { return $_[1] }
Subroutine should_accept_node: called CC=1 SCAR=0.0
Pod documented
86 #===================================
87  
88 11 1;
89  
90 =pod
91  
92 =encoding UTF-8
93  
94 =head1 NAME
95  
96 Search::Elasticsearch::Role::CxnPool::Sniff - A CxnPool role for connecting to a local cluster with a dynamic node list
97  
98 =head1 VERSION
99  
100 version 6.80
101  
102 =head1 CONFIGURATION
103  
104 =head2 C<sniff_interval>
105  
106 How often should we perform a sniff in order to detect whether new nodes
107 have been added to the cluster. Defaults to `300` seconds.
108  
109 =head2 C<sniff_max_content_length>
110  
111 Whether we should set the
112 L<max_content_length|Search::Elasticsearch::Role::Cxn/max_content_length>
113 dynamically while sniffing. Defaults to true unless a fixed
114 C<max_content_length> was specified.
115  
116 =head1 METHODS
117  
118 =head2 C<schedule_check()>
119  
120 $cxn_pool->schedule_check
121  
122 Schedules a sniff before the next request is processed.
123  
124 =head2 C<parse_sniff()>
125  
126 $bool = $cxn_pool->parse_sniff(\%nodes);
127  
128 Parses the response from a sniff request and extracts the hostname/ip
129 of all listed nodes, filtered through L</should_accept_node()>. If any live
130 nodes are found, they are passed to L<Search::Elasticsearch::Role::CxnPool/set_cxns()>.
131 The L<max_content_length|Search::Elasticsearch::Role::Cxn/max_content_length>
132 is also detected if L</sniff_max_content_length> is true.
133  
134 =head2 C<should_accept_node()>
135  
136 $host = $cxn_pool->should_accept_node($host,$node_id,\%node_data)
137  
138 This method serves as a hook which can be overridden by the user. When
139 a sniff is performed, this method is called with the C<host>
140 (eg C<192.168.5.100:9200>), the C<node_id> (the ID assigned to the node
141 by Elasticsearch) and the C<node_data> which contains the information
142 about the node that Elasticsearch has returned, eg:
143  
144 {
145 "transport_address" => "inet[192.168.5.100/192.168.5.100:9300]",
146 "http" : {
147 "publish_address" => "inet[/192.168.5.100:9200]",
148 "max_content_length" => "100mb",
149 "bound_address" => "inet[/0:0:0:0:0:0:0:0:9200]",
150 "max_content_length_in_bytes" : 104857600
151 },
152 "version" => "0.90.4",
153 "name" => "Silver Sable",
154 "hostname" => "search1.domain.com",
155 "http_address" => "inet[/192.168.5.100:9200]"
156 }
157  
158 If the node should be I<accepted> (ie used to serve data), then it should
159 return the C<host> value to use. By default, nodes are always
160 accepted.
161  
162 =head1 AUTHOR
163  
164 Enrico Zimuel <enrico.zimuel@elastic.co>
165  
166 =head1 COPYRIGHT AND LICENSE
167  
168 This software is Copyright (c) 2020 by Elasticsearch BV.
169  
170 This is free software, licensed under:
171  
172 The Apache License, Version 2.0, January 2004
173  
174 =cut
175  
176 __END__
« Elasticsearch/Role/Transport.pm ↑ Summary Elasticsearch/Error.pm »