-
Notifications
You must be signed in to change notification settings - Fork 54
Fix race condition in queued record & document removal #290
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
Merged
meili-bors
merged 12 commits into
meilisearch:main
from
ellnix:fix-race-condition-in-queued-removal
Feb 12, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e617028
Add test for #destroy/MSJob race condition
ellnix cff831a
Add tests for record#ms_entries
ellnix 9e227d8
Move responsibility to separate job
ellnix 48d524b
Implement record#ms_entries method
ellnix bd3d525
Implement ms_clean_up_job
ellnix fc7fe22
Call MSCleanUpJob from MeiliSearch::Rails
ellnix cd7fb15
Fix regressions in tests
ellnix 6cbd824
Fix rubocop offenses
ellnix b61c332
Fix already destroyed callback spec
ellnix 083d1e2
Fix indexes name conflict in specs
ellnix 91437f1
Refactor CleanUpJob spec for clarity
ellnix d32db1a
Merge branch 'main' into fix-race-condition-in-queued-removal
brunoocasali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module MeiliSearch | ||
module Rails | ||
class MSCleanUpJob < ::ActiveJob::Base | ||
queue_as :meilisearch | ||
|
||
def perform(documents) | ||
documents.each do |document| | ||
index = MeiliSearch::Rails.client.index(document[:index_uid]) | ||
|
||
if document[:synchronous] | ||
index.delete_document!(document[:primary_key]) | ||
else | ||
index.delete_document(document[:primary_key]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe 'MeiliSearch::Rails::MSCleanUpJob' do | ||
include ActiveJob::TestHelper | ||
|
||
def clean_up_indexes | ||
indexes.each(&:delete_all_documents) | ||
end | ||
|
||
def create_indexed_record | ||
record | ||
|
||
indexes.each do |index| | ||
index.wait_for_task(index.tasks['results'].last['uid']) | ||
end | ||
end | ||
|
||
subject(:clean_up) { MeiliSearch::Rails::MSCleanUpJob } | ||
|
||
let(:record) do | ||
Book.create name: "Moby Dick", author: "Herman Mellville", | ||
premium: false, released: true | ||
end | ||
|
||
let(:record_entries) do | ||
record.ms_entries(true).each { |h| h[:index_uid] += '_test' } | ||
end | ||
|
||
let(:indexes) do | ||
%w[SecuredBook BookAuthor Book].map do |uid| | ||
Book.index(safe_index_uid uid) | ||
end | ||
end | ||
|
||
it 'removes record from all indexes' do | ||
clean_up_indexes | ||
|
||
create_indexed_record | ||
|
||
clean_up.perform_now(record_entries) | ||
|
||
indexes.each do |index| | ||
expect(index.search('*')['hits']).to be_empty | ||
end | ||
end | ||
|
||
context 'when record is already destroyed' do | ||
subject(:record) do | ||
Restaurant.create( | ||
name: "Los Pollos Hermanos", | ||
kind: "Mexican", | ||
description: "Mexican chicken restaurant in Albuquerque, New Mexico.") | ||
end | ||
|
||
let(:indexes) { [Restaurant.index] } | ||
|
||
it 'successfully deletes its document in the index' do | ||
clean_up_indexes | ||
|
||
create_indexed_record | ||
|
||
record.delete # does not run callbacks, unlike #destroy | ||
|
||
clean_up.perform_later(record_entries) | ||
expect { perform_enqueued_jobs }.not_to raise_error | ||
|
||
indexes.each do |index| | ||
expect(index.search('*')['hits']).to be_empty | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it helpful to do the check instead of just calling async or sync. But I changed my mind 😅. Let's let the user decide; we don't know the use cases. Maybe it is too much to wait for a call in certain apps, but indispensable for others...