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