| 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::Role::CxnPool::Sniff; | ||||||||||||||||||||||||||||
| 24 | 11 | $OpenSearch::Client::Role::CxnPool::Sniff::VERSION = '3.007011'; | |||||||||||||||||||||||||||
| 25 | 11 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 11 | with 'OpenSearch::Client::Role::CxnPool'; | |||||||||||||||||||||||||||
| 27 | 11 | requires 'next_cxn', 'sniff'; | |||||||||||||||||||||||||||
| 28 | 11 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 11 | ▶ | use OpenSearch::Client::Util qw(parse_params); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | use List::Util qw(min); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 11 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | has 'sniff_interval' => ( is => 'ro', default => 300 ); | |||||||||||||||||||||||||||
| 35 | 11 | has 'next_sniff' => ( is => 'rw', default => 0 ); | |||||||||||||||||||||||||||
| 36 | 11 | has 'sniff_max_content_length' => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | #=================================== | ||||||||||||||||||||||||||||
| 39 | 12 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | #=================================== | ||||||||||||||||||||||||||||
| 41 | 12 | my ( $class, $params ) = parse_params(@_); | |||||||||||||||||||||||||||
| 42 | $params->{sniff_max_content_length} = !$params->{max_content_length} | ||||||||||||||||||||||||||||
| 43 | 12 | ▶ | unless defined $params->{sniff_max_content_length}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 12 | return $params; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #=================================== | ||||||||||||||||||||||||||||
| 48 | 14 | ▶ | sub schedule_check { | ||||||||||||||||||||||||||
|
Subroutine
schedule_check: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | #=================================== | ||||||||||||||||||||||||||||
| 50 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 14 | $self->logger->info("Require sniff before next request"); | |||||||||||||||||||||||||||
| 52 | 14 | $self->next_sniff(-1); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #=================================== | ||||||||||||||||||||||||||||
| 56 | 38 | ▶ | sub parse_sniff { | ||||||||||||||||||||||||||
|
Subroutine
parse_sniff: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | #=================================== | ||||||||||||||||||||||||||||
| 58 | 38 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 38 | ▶ | my $nodes = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 25 | my @live_nodes; | |||||||||||||||||||||||||||
| 61 | 25 | my $max = 0; | |||||||||||||||||||||||||||
| 62 | 25 | my $sniff_max = $self->sniff_max_content_length; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 25 | for my $node_id ( keys %$nodes ) { | |||||||||||||||||||||||||||
| 65 | 47 | my $data = $nodes->{$node_id}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 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'}
| |||||||||||||||||||||||||||||
| 68 | 47 | ▶ | my $host = $self->_extract_host($addr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | or next; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 46 | ▶ | $host = $self->should_accept_node( $host, $node_id, $data ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | or next; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 46 | push @live_nodes, $host; | |||||||||||||||||||||||||||
| 75 | 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"}
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 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'}
| |||||||||||||||||||||||||||||
| 78 | 43 | ▶ | $max | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | = $node_max == 0 ? $max | ||||||||||||||||||||||||||||
| 80 | : $max == 0 ? $node_max | ||||||||||||||||||||||||||||
| 81 | : min( $node_max, $max ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 25 | ▶ | return unless @live_nodes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 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
| |||||||||||||||||||||||||||||
| 87 | if $sniff_max and $max; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 24 | $self->set_cxns(@live_nodes); | |||||||||||||||||||||||||||
| 90 | 24 | my $next = $self->next_sniff( time() + $self->sniff_interval ); | |||||||||||||||||||||||||||
| 91 | 24 | $self->logger->infof( "Next sniff at: %s", scalar localtime($next) ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 24 | return 1; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | #=================================== | ||||||||||||||||||||||||||||
| 97 | 53 | ▶ | sub _extract_host { | ||||||||||||||||||||||||||
|
Subroutine
_extract_host: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | #=================================== | ||||||||||||||||||||||||||||
| 99 | 53 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 53 | ▶ | my $host = shift || return; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 101 | 51 | $host =~ s{^inet\[(.+)\]$}{$1}; | |||||||||||||||||||||||||||
| 102 | 51 | $host =~ s{^[^/]*/}{}; | |||||||||||||||||||||||||||
| 103 | 51 | return $host; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #=================================== | ||||||||||||||||||||||||||||
| 107 | 46 | ▶ | sub should_accept_node { return $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
should_accept_node: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | #=================================== | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 11 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||