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