Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error in #scroll_batches when search backend returns a failure #916

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,14 @@ If you use `DatabaseCleaner` in your tests with [the `transaction` strategy](htt
Chewy.use_after_commit_callbacks = !Rails.env.test?
```

## Running specs

Make sure you're running a local Elasticsearch instance.

```
ES_PORT=9200 bundle exec rspec
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, could we remove this? I planning to backport docker-compose setup with ES, so it runs on proper port /~https://github.com/toptal/chewy/pull/917/files#diff-e45e45baeda1c1e73482975a664062aa56f20c03dd9d64a827aba57775bed0d3R1-R16

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, done!

## Contributing

1. Fork it (http://github.com/toptal/chewy/fork)
Expand Down
3 changes: 3 additions & 0 deletions lib/chewy/search/scrolling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def scroll_batches(batch_size: Request::DEFAULT_BATCH_SIZE, scroll: Request::DEF
scroll_id = nil

loop do
failures = result.dig('_shards', 'failures')
tomdev marked this conversation as resolved.
Show resolved Hide resolved
raise Chewy::Error, failures if failures.present?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Chewy::Error is not the best thing here, from your example, failures is a hash, and Chewy::Error is simply a StandardError with a string argument. Please make a specific error message and convert failure into a meaningful string? For instance

class ImportFailed < Error
def initialize(type, import_errors)
message = "Import failed for `#{type}` with:\n"
import_errors.each do |action, action_errors|
message << " #{action.to_s.humanize} errors:\n"
action_errors.each do |error, documents|
message << " `#{error}`\n"
message << " on #{documents.count} documents: #{documents}\n"
end
end
super message
end
end


hits = result.fetch('hits', {}).fetch('hits', [])
fetched += hits.size
hits = hits.first(last_batch_size) if last_batch_size != 0 && fetched >= total
Expand Down
50 changes: 50 additions & 0 deletions spec/chewy/search/scrolling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@
let(:countries) { Array.new(3) { |i| Country.create!(rating: i + 2, name: "country #{i}") } }

describe '#scroll_batches' do
describe 'with search backend returning failures' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, do you think it will be possible to provide integration spec? Where you really call ES and get real answer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a suggestion how I can set up an integration spec that returns this error state? Elasticsearch should return a total count higher than zero but then return zero hits in order to hit this code path.

before do
expect(Chewy.client).to receive(:scroll).once.and_return(
'hits' => {
'total' => {
'value' => 5
},
'hits' => []
},
'_shards' => {
'total' => 5,
'successful' => 2,
'skipped' => 0,
'failed' => 3,
'failures' => [
{
'shard' => -1,
'index' => nil,
'reason' => {
'type' => 'search_context_missing_exception',
'reason' => 'No search context found for id [34462229]'
}
},
{
'shard' => -1,
'index' => nil,
'reason' => {
'type' => 'search_context_missing_exception',
'reason' => 'No search context found for id [34462228]'
}
},
{
'shard' => -1,
'index' => nil,
'reason' => {
'type' => 'search_context_missing_exception',
'reason' => 'No search context found for id [34888662]'
}
}
]
},
'_scroll_id' => 'scroll_id'
)
end

specify do
expect { request.scroll_batches(batch_size: 2) {} }.to raise_error(Chewy::Error)
end
end

context do
before { expect(Chewy.client).to receive(:scroll).twice.and_call_original }
specify do
Expand Down
Loading