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