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