| 1 | # Licensed to Elasticsearch B.V. under one or more contributor | ||
| 2 | # license agreements. See the NOTICE file distributed with | ||
| 3 | # this work for additional information regarding copyright | ||
| 4 | # ownership. Elasticsearch B.V. licenses this file to you under | ||
| 5 | # the Apache License, Version 2.0 (the "License"); you may | ||
| 6 | # not use this file except in compliance with the License. | ||
| 7 | # You may obtain a copy of the License at | ||
| 8 | # | ||
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | # | ||
| 11 | # Unless required by applicable law or agreed to in writing, | ||
| 12 | # software distributed under the License is distributed on an | ||
| 13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| 14 | # KIND, either express or implied. See the License for the | ||
| 15 | # specific language governing permissions and limitations | ||
| 16 | # under the License. | ||
| 17 | |||
| 18 | package Search::Elasticsearch::Client::8_0::Async::Bulk; | ||
| 19 | $Search::Elasticsearch::Client::8_0::Async::Bulk::VERSION = '8.00'; | ||
| 20 | use Moo; | ||
| 21 | with 'Search::Elasticsearch::Client::8_0::Role::Bulk', | ||
| 22 | 'Search::Elasticsearch::Role::Is_Async'; | ||
| 23 | |||
| 24 | use Search::Elasticsearch::Util qw(parse_params throw); | ||
| 25 | use Scalar::Util qw(weaken blessed); | ||
| 26 | use Promises qw(deferred); | ||
| 27 | use Try::Tiny; | ||
| 28 | use namespace::clean; | ||
| 29 | |||
| 30 | has 'on_fatal' => ( is => 'lazy' ); | ||
| 31 | |||
| 32 | #=================================== | ||
| 33 | sub _build_on_fatal { | ||
| 34 | #=================================== | ||
| 35 | my $self = shift; | ||
| 36 | return sub { | ||
| 37 | warn("Fatal bulk error: @_"); | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | #=================================== | ||
| 42 | sub add_action { | ||
| 43 | #=================================== | ||
| 44 | my $self = shift; | ||
| 45 | my $buffer = $self->_buffer; | ||
| 46 | my $max_size = $self->max_size; | ||
| 47 | my $max_count = $self->max_count; | ||
| 48 | my $max_time = $self->max_time; | ||
| 49 | |||
| 50 | my $deferred = deferred; | ||
| 51 | my @actions = @_; | ||
| 52 | |||
| 53 | my $weak_add; | ||
| 54 | my $add = sub { | ||
| 55 | while (@actions) { | ||
| 56 | my @json = try { | ||
| 57 | $self->_encode_action( splice( @actions, 0, 2 ) ); | ||
| 58 | } | ||
| 59 | catch { | ||
| 60 | $self->on_fatal->($_); | ||
| 61 | $deferred->reject($_); | ||
| 62 | (); | ||
| 63 | }; | ||
| 64 | return unless @json; | ||
| 65 | |||
| 66 | push @$buffer, @json; | ||
| 67 | |||
| 68 | my $size = $self->_buffer_size; | ||
| 69 | $size += length($_) + 1 for @json; | ||
| 70 | $self->_buffer_size($size); | ||
| 71 | |||
| 72 | my $count = $self->_buffer_count( $self->_buffer_count + 1 ); | ||
| 73 | |||
| 74 | next | ||
| 75 | unless ( $max_size and $size >= $max_size ) | ||
| 76 | || ( $max_count and $count >= $max_count ) | ||
| 77 | || ( $max_time and time >= $self->_last_flush + $max_time ); | ||
| 78 | |||
| 79 | return $self->flush->done( $weak_add, | ||
| 80 | sub { $deferred->reject(@_) } ); | ||
| 81 | } | ||
| 82 | return $deferred->resolve; | ||
| 83 | |||
| 84 | }; | ||
| 85 | |||
| 86 | weaken( $weak_add = $add ); | ||
| 87 | $add->(); | ||
| 88 | return $deferred->promise; | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | #=================================== | ||
| 93 | sub flush { | ||
| 94 | #=================================== | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | my $size = $self->_buffer_size; | ||
| 98 | my $count = $self->_buffer_count; | ||
| 99 | |||
| 100 | $self->_last_flush(time); | ||
| 101 | |||
| 102 | unless ($size) { | ||
| 103 | return deferred->resolve( { items => [] } )->promise; | ||
| 104 | } | ||
| 105 | |||
| 106 | my @items = ( @{ $self->_buffer } ); | ||
| 107 | $self->clear_buffer; | ||
| 108 | |||
| 109 | if ( $self->verbose ) { | ||
| 110 | local $| = 1; | ||
| 111 | print "."; | ||
| 112 | } | ||
| 113 | |||
| 114 | my $promise | ||
| 115 | = $self->es->bulk( %{ $self->_bulk_args }, body => \@items )->catch( | ||
| 116 | sub { | ||
| 117 | my $error = shift; | ||
| 118 | if ( $error->is( 'Cxn', 'NoNodes' ) ) { | ||
| 119 | push @{ $self->_buffer }, @items; | ||
| 120 | $self->_buffer_size( $self->_buffer_size + $size ); | ||
| 121 | $self->_buffer_count( $self->_buffer_count + $count ); | ||
| 122 | } | ||
| 123 | die $error; | ||
| 124 | } | ||
| 125 | ); | ||
| 126 | $promise->then( sub { $self->_report( \@items, @_ ) }, | ||
| 127 | sub { $self->on_fatal(@_) } ); | ||
| 128 | return $promise; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | # ABSTRACT: A helper module for the Bulk API | ||
| 134 | |||
| 135 | __END__ |