# Licensed to Elasticsearch B.V. under one or more contributor # license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright # ownership. Elasticsearch B.V. licenses this file to you under # the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. package Search::Elasticsearch::CxnPool::Sniff; $Search::Elasticsearch::CxnPool::Sniff::VERSION = '8.12'; use Moo; with 'Search::Elasticsearch::Role::CxnPool::Sniff', 'Search::Elasticsearch::Role::Is_Sync'; use Search::Elasticsearch::Util qw(throw); use namespace::clean; #=================================== sub next_cxn { #=================================== my ($self) = @_; $self->sniff if $self->next_sniff <= time(); my $cxns = $self->cxns; my $total = @$cxns; while ( 0 < $total-- ) { my $cxn = $cxns->[ $self->next_cxn_num ]; return $cxn if $cxn->is_live; } throw( "NoNodes", "No nodes are available: [" . $self->cxns_seeds_str . ']' ); } #=================================== sub sniff { #=================================== my $self = shift; my $cxns = $self->cxns; my $total = @$cxns; my @skipped; while ( 0 < $total-- ) { my $cxn = $cxns->[ $self->next_cxn_num ]; if ( $cxn->is_dead ) { push @skipped, $cxn; } else { $self->sniff_cxn($cxn) and return; $cxn->mark_dead; } } for my $cxn (@skipped) { $self->sniff_cxn($cxn) and return; } $self->logger->info("No live nodes available. Trying seed nodes."); for my $seed ( @{ $self->seed_nodes } ) { my $cxn = $self->cxn_factory->new_cxn($seed); $self->sniff_cxn($cxn) and return; } } #=================================== sub sniff_cxn { #=================================== my ( $self, $cxn ) = @_; return $self->parse_sniff( $cxn->sniff ); } 1; # ABSTRACT: A CxnPool for connecting to a local cluster with a dynamic node list __END__ =pod =encoding UTF-8 =head1 NAME Search::Elasticsearch::CxnPool::Sniff - A CxnPool for connecting to a local cluster with a dynamic node list =head1 VERSION version 8.12 =head1 SYNOPSIS $e = Search::Elasticsearch->new( cxn_pool => 'Sniff', nodes => [ 'search1:9200', 'search2:9200' ], ); =head1 DESCRIPTION The L connection pool should be used when you B have direct access to the Elasticsearch cluster, eg when your web servers and Elasticsearch servers are on the same network. The nodes that you specify are used to I the cluster, which is then I to find the current list of live nodes that the cluster knows about. This sniff process is repeated regularly, or whenever a node fails, to update the list of healthy nodes. So if you add more nodes to your cluster, they will be auto-discovered during a sniff. If all sniffed nodes fail, then it falls back to sniffing the original I nodes that you specified in C. For L, this module will also dynamically detect the C which the nodes in the cluster will accept. This class does L and L. =head1 CONFIGURATION =head2 C The list of nodes to use to discover the cluster. Can accept a single node, multiple nodes, and defaults to C if no C are specified. See L for details of the node specification. =head2 See also =over =item * L =item * L =item * L =back =head2 Inherited configuration From L =over =item * L =item * L =back From L =over =item * L =back =head1 METHODS =head2 C $cxn = $cxn_pool->next_cxn Returns the next available live node (in round robin fashion), or throws a C error if no nodes can be sniffed from the cluster. =head2 C $cxn_pool->schedule_check Forces a sniff before the next Cxn is returned, to updated the list of healthy nodes in the cluster. =head2 C $bool = $cxn_pool->sniff Sniffs the cluster and returns C if the sniff was successful. =head2 Inherited methods From L =over =item * L =item * L =item * L =back From L =over =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =back =head1 AUTHOR Enrico Zimuel =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2024 by Elasticsearch BV. This is free software, licensed under: The Apache License, Version 2.0, January 2004 =cut