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