| 1 | package Search::Elasticsearch::Client::6_0::Async::Bulk; | ||
| 2 | $Search::Elasticsearch::Client::6_0::Async::Bulk::VERSION = '6.80'; | ||
| 3 | use Moo; | ||
| 4 | with 'Search::Elasticsearch::Client::6_0::Role::Bulk', | ||
| 5 | 'Search::Elasticsearch::Role::Is_Async'; | ||
| 6 | |||
| 7 | use Search::Elasticsearch::Util qw(parse_params throw); | ||
| 8 | use Scalar::Util qw(weaken blessed); | ||
| 9 | use Promises qw(deferred); | ||
| 10 | use Try::Tiny; | ||
| 11 | use namespace::clean; | ||
| 12 | |||
| 13 | has 'on_fatal' => ( is => 'lazy' ); | ||
| 14 | |||
| 15 | #=================================== | ||
| 16 | sub _build_on_fatal { | ||
| 17 | #=================================== | ||
| 18 | my $self = shift; | ||
| 19 | return sub { | ||
| 20 | warn("Fatal bulk error: @_"); | ||
| 21 | }; | ||
| 22 | } | ||
| 23 | |||
| 24 | #=================================== | ||
| 25 | sub add_action { | ||
| 26 | #=================================== | ||
| 27 | my $self = shift; | ||
| 28 | my $buffer = $self->_buffer; | ||
| 29 | my $max_size = $self->max_size; | ||
| 30 | my $max_count = $self->max_count; | ||
| 31 | my $max_time = $self->max_time; | ||
| 32 | |||
| 33 | my $deferred = deferred; | ||
| 34 | my @actions = @_; | ||
| 35 | |||
| 36 | my $weak_add; | ||
| 37 | my $add = sub { | ||
| 38 | while (@actions) { | ||
| 39 | my @json = try { | ||
| 40 | $self->_encode_action( splice( @actions, 0, 2 ) ); | ||
| 41 | } | ||
| 42 | catch { | ||
| 43 | $self->on_fatal->($_); | ||
| 44 | $deferred->reject($_); | ||
| 45 | (); | ||
| 46 | }; | ||
| 47 | return unless @json; | ||
| 48 | |||
| 49 | push @$buffer, @json; | ||
| 50 | |||
| 51 | my $size = $self->_buffer_size; | ||
| 52 | $size += length($_) + 1 for @json; | ||
| 53 | $self->_buffer_size($size); | ||
| 54 | |||
| 55 | my $count = $self->_buffer_count( $self->_buffer_count + 1 ); | ||
| 56 | |||
| 57 | next | ||
| 58 | unless ( $max_size and $size >= $max_size ) | ||
| 59 | || ( $max_count and $count >= $max_count ) | ||
| 60 | || ( $max_time and time >= $self->_last_flush + $max_time ); | ||
| 61 | |||
| 62 | return $self->flush->done( $weak_add, | ||
| 63 | sub { $deferred->reject(@_) } ); | ||
| 64 | } | ||
| 65 | return $deferred->resolve; | ||
| 66 | |||
| 67 | }; | ||
| 68 | |||
| 69 | weaken( $weak_add = $add ); | ||
| 70 | $add->(); | ||
| 71 | return $deferred->promise; | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | #=================================== | ||
| 76 | sub flush { | ||
| 77 | #=================================== | ||
| 78 | my $self = shift; | ||
| 79 | |||
| 80 | my $size = $self->_buffer_size; | ||
| 81 | my $count = $self->_buffer_count; | ||
| 82 | |||
| 83 | $self->_last_flush(time); | ||
| 84 | |||
| 85 | unless ($size) { | ||
| 86 | return deferred->resolve( { items => [] } )->promise; | ||
| 87 | } | ||
| 88 | |||
| 89 | my @items = ( @{ $self->_buffer } ); | ||
| 90 | $self->clear_buffer; | ||
| 91 | |||
| 92 | if ( $self->verbose ) { | ||
| 93 | local $| = 1; | ||
| 94 | print "."; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $promise | ||
| 98 | = $self->es->bulk( %{ $self->_bulk_args }, body => \@items )->catch( | ||
| 99 | sub { | ||
| 100 | my $error = shift; | ||
| 101 | if ( $error->is( 'Cxn', 'NoNodes' ) ) { | ||
| 102 | push @{ $self->_buffer }, @items; | ||
| 103 | $self->_buffer_size( $self->_buffer_size + $size ); | ||
| 104 | $self->_buffer_count( $self->_buffer_count + $count ); | ||
| 105 | } | ||
| 106 | die $error; | ||
| 107 | } | ||
| 108 | ); | ||
| 109 | $promise->then( sub { $self->_report( \@items, @_ ) }, | ||
| 110 | sub { $self->on_fatal(@_) } ); | ||
| 111 | return $promise; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | # ABSTRACT: A helper module for the Bulk API | ||
| 117 | |||
| 118 | __END__ |