| 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::2_0::Async::Bulk; | ||
| 6 | $Search::Elasticsearch::Client::2_0::Async::Bulk::VERSION = '7.30_1'; # TRIAL | ||
| 7 | |||
| 8 | $Search::Elasticsearch::Client::2_0::Async::Bulk::VERSION = '7.301'; | ||
| 9 | use Moo; | ||
| 10 | with 'Search::Elasticsearch::Client::2_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 | #=================================== | ||
| 121 | sub reindex { | ||
| 122 | #=================================== | ||
| 123 | my ( $self, $params ) = parse_params(@_); | ||
| 124 | |||
| 125 | my $src = $params->{source} | ||
| 126 | or throw( 'Param', "Missing required param <source>" ); | ||
| 127 | |||
| 128 | my $transform = $self->_doc_transformer($params); | ||
| 129 | |||
| 130 | if ( ref $src eq 'HASH' ) { | ||
| 131 | $src = $self->_hash_to_scroll( {%$src}, $transform ); | ||
| 132 | } | ||
| 133 | elsif ( blessed($src) && $src->isa('Search::Elasticsearch::Client::2_0::Scroll') ) { | ||
| 134 | my $scroll = $src; | ||
| 135 | $src = sub { | ||
| 136 | $scroll->refill_buffer; | ||
| 137 | $scroll->drain_buffer; | ||
| 138 | }; | ||
| 139 | } | ||
| 140 | |||
| 141 | my $promise; | ||
| 142 | |||
| 143 | # async scroll | ||
| 144 | if ( blessed($src) && $src->isa('Search::Elasticsearch::Client::2_0::Async::Scroll') ) { | ||
| 145 | $promise = $src->start; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | |||
| 149 | my $deferred = deferred; | ||
| 150 | my $weak_cb; | ||
| 151 | my $cb = sub { | ||
| 152 | my @docs = grep {defined} $src->(); | ||
| 153 | return $deferred->resolve | ||
| 154 | unless @docs; | ||
| 155 | $self->index( grep {$_} map { $transform->($_) } @docs ) | ||
| 156 | ->done( $weak_cb, sub { $deferred->reject(@_) } ); | ||
| 157 | }; | ||
| 158 | weaken( $weak_cb = $cb ); | ||
| 159 | $promise = $deferred->promise; | ||
| 160 | $cb->(); | ||
| 161 | } | ||
| 162 | |||
| 163 | $promise->finally( sub { $self->flush } ); | ||
| 164 | } | ||
| 165 | |||
| 166 | #=================================== | ||
| 167 | sub _hash_to_scroll { | ||
| 168 | #=================================== | ||
| 169 | my ( $self, $src, $transform ) = @_; | ||
| 170 | |||
| 171 | my $scroll; | ||
| 172 | my $i = 1; | ||
| 173 | my $on_results = sub { | ||
| 174 | my @docs; | ||
| 175 | while (@_) { | ||
| 176 | my $doc = $transform->( shift() ) or next; | ||
| 177 | push @docs, $doc; | ||
| 178 | } | ||
| 179 | return unless @docs; | ||
| 180 | |||
| 181 | $self->index(@docs)->catch( sub { $scroll->finish } ); | ||
| 182 | }; | ||
| 183 | |||
| 184 | my $on_start; | ||
| 185 | if ( $self->verbose ) { | ||
| 186 | $on_start = sub { | ||
| 187 | $scroll = shift; | ||
| 188 | print "Reindexing " . $scroll->total . " docs\n"; | ||
| 189 | }; | ||
| 190 | } | ||
| 191 | my $es = delete $src->{es} || $self->es; | ||
| 192 | return $scroll = $es->scroll_helper( | ||
| 193 | search_type => 'scan', | ||
| 194 | size => 500, | ||
| 195 | %$src, | ||
| 196 | on_results => $on_results, | ||
| 197 | on_start => $on_start, | ||
| 198 | ); | ||
| 199 | |||
| 200 | } | ||
| 201 | |||
| 202 | 1; | ||
| 203 | |||
| 204 | # ABSTRACT: A helper module for the Bulk API and for reindexing | ||
| 205 | |||
| 206 | __END__ |