| 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::CxnPool::Async::Sniff; | ||||||||||||||||||||||||||||
| 6 | 11 | $Search::Elasticsearch::CxnPool::Async::Sniff::VERSION = '7.30_1'; # TRIAL | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | $Search::Elasticsearch::CxnPool::Async::Sniff::VERSION = '7.301'; | |||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | with 'Search::Elasticsearch::Role::CxnPool::Sniff', | |||||||||||||||||||||||||||
| 11 | 'Search::Elasticsearch::Role::Is_Async'; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Promises qw(deferred); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use Search::Elasticsearch::Util qw(new_error); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | has 'concurrent_sniff' => ( is => 'rw', default => 4 ); | |||||||||||||||||||||||||||
| 19 | 11 | has '_current_sniff' => ( is => 'rw', clearer => '_clear_sniff' ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #=================================== | ||||||||||||||||||||||||||||
| 22 | sub next_cxn { | ||||||||||||||||||||||||||||
| 23 | #=================================== | ||||||||||||||||||||||||||||
| 24 | 24 | ▶ | my ( $self, $no_sniff ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_cxn: called CC=6 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | return $self->sniff->then( sub { $self->next_cxn('no_sniff') } ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 24 | ▶ | if $self->next_sniff <= time() && !$no_sniff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->next_sniff <= time and not $no_sniff
MC/DC50%1/2
$self->next_sniff <= time and not $no_sniff
$self->next_sniff <= time
not $no_sniff
Truth table66%2/3
A and not(B)
A= $self->next_sniff <= timeB= not $no_sniff
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 12 | my $cxns = $self->cxns; | |||||||||||||||||||||||||||
| 30 | 12 | my $total = @$cxns; | |||||||||||||||||||||||||||
| 31 | 12 | my $cxn; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 12 | ▶ | while ( 0 < $total-- ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $cxn = $cxns->[ $self->next_cxn_num ]; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | last if $cxn->is_live; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | undef $cxn; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 12 | my $deferred = deferred; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 12 | ▶ | if ($cxn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $deferred->resolve($cxn); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||
| 45 | 12 | $deferred->reject( | |||||||||||||||||||||||||||
| 46 | new_error( | ||||||||||||||||||||||||||||
| 47 | "NoNodes", | ||||||||||||||||||||||||||||
| 48 | "No nodes are available: [" . $self->cxns_seeds_str . ']' | ||||||||||||||||||||||||||||
| 49 | ) | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 12 | return $deferred->promise; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #=================================== | ||||||||||||||||||||||||||||
| 56 | sub sniff { | ||||||||||||||||||||||||||||
| 57 | #=================================== | ||||||||||||||||||||||||||||
| 58 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
sniff: called CC=7 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 12 | my $promise; | |||||||||||||||||||||||||||
| 61 | 12 | ▶ | if ( $promise = $self->_current_sniff ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | return $promise; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | my $deferred = deferred; | |||||||||||||||||||||||||||
| 66 | 12 | my $cxns = $self->cxns; | |||||||||||||||||||||||||||
| 67 | 12 | my $total = @$cxns; | |||||||||||||||||||||||||||
| 68 | 12 | my $done = 0; | |||||||||||||||||||||||||||
| 69 | 12 | my $current = 0; | |||||||||||||||||||||||||||
| 70 | 12 | my $done_seeds = 0; | |||||||||||||||||||||||||||
| 71 | 12 | $promise = $self->_current_sniff( $deferred->promise ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 12 | my ( @all, @skipped ); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 12 | ▶ | while ( 0 < $total-- ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | my $cxn = $cxns->[ $self->next_cxn_num ]; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $cxn->is_dead ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | push @skipped, $cxn; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 0 | push @all, $cxn; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 12 | push @all, @skipped; | |||||||||||||||||||||||||||
| 86 | 12 | ▶ | unless (@all) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 12 | @all = $self->_seeds_as_cxns; | |||||||||||||||||||||||||||
| 88 | 12 | $done_seeds++; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 12 | my ( $weak_check_sniff, $cxn ); | |||||||||||||||||||||||||||
| 92 | my $check_sniff = sub { | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 22 | ▶ | return if $done; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=24.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 22 | my ( $cxn, $nodes ) = @_; | |||||||||||||||||||||||||||
| 96 | 22 | ▶ | if ( $nodes && $self->parse_sniff($nodes) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$nodes and $self->parse_sniff($nodes)
MC/DC0%0/2
$nodes and $self->parse_sniff($nodes)
$nodes
$self->parse_sniff($nodes)
Truth table33%1/3
A and B
A= $nodesB= $self->parse_sniff($nodes)
| |||||||||||||||||||||||||||||
| 97 | 0 | $done++; | |||||||||||||||||||||||||||
| 98 | 0 | $self->_clear_sniff; | |||||||||||||||||||||||||||
| 99 | 0 | return $deferred->resolve(); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 22 | ▶ | unless ( @all || $done_seeds++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@all or $done_seeds++
MC/DC0%0/2
@all or $done_seeds++
@all
$done_seeds++
Truth table66%2/3
A or B
A= @allB= $done_seeds++
| |||||||||||||||||||||||||||||
| 103 | 0 | $self->logger->info("No live nodes available. Trying seed nodes."); | |||||||||||||||||||||||||||
| 104 | 0 | @all = $self->_seeds_as_cxns; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 22 | ▶ | if ( my $cxn = shift @all ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 10 | return $cxn->sniff->done($weak_check_sniff); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 12 | ▶ | if ( --$current == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 12 | $self->_clear_sniff; | |||||||||||||||||||||||||||
| 112 | 12 | $deferred->resolve(); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 12 | }; | |||||||||||||||||||||||||||
| 115 | 12 | weaken( $weak_check_sniff = $check_sniff ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 12 | for ( 1 .. $self->concurrent_sniff ) { | |||||||||||||||||||||||||||
| 118 | 24 | ▶ | my $cxn = shift(@all) || last; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift @all || (last)
MC/DC100%1/1
shift @all || (last)
shift @all
Truth table100%2/2
A
A= shift @all
| |||||||||||||||||||||||||||||
| 119 | 12 | $current++; | |||||||||||||||||||||||||||
| 120 | 12 | $cxn->sniff->done($check_sniff); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 12 | return $promise; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | #=================================== | ||||||||||||||||||||||||||||
| 127 | sub _seeds_as_cxns { | ||||||||||||||||||||||||||||
| 128 | #=================================== | ||||||||||||||||||||||||||||
| 129 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_seeds_as_cxns: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 12 | my $factory = $self->cxn_factory; | |||||||||||||||||||||||||||
| 131 | 12 | return map { $factory->new_cxn($_) } @{ $self->seed_nodes }; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # ABSTRACT: An async CxnPool for connecting to a local cluster with a dynamic node list | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||