-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix concurrent search and index delete #42621
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
henningandersen
merged 8 commits into
elastic:master
from
henningandersen:fix_concurrent_search_index_delete
Jun 6, 2019
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5f86ea1
Fix concurrent search and index delete
henningandersen 8edda7b
Removed duplicate catch block.
henningandersen ac02fc0
Always call onFree plus test fix
henningandersen 49dfb3c
Close search context if onNewXXX fails.
henningandersen d7aafcd
Merge remote-tracking branch 'origin/master' into fix_concurrent_sear…
henningandersen 5fe9892
Removed double check for index delete.
henningandersen b7a24f4
Assert that context is inactive when freeing.
henningandersen 4a48fb8
Merge remote-tracking branch 'origin/master' into fix_concurrent_sear…
henningandersen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -548,19 +548,35 @@ final SearchContext createAndPutContext(ShardSearchRequest request) throws IOExc | |
| } | ||
|
|
||
| SearchContext context = createContext(request); | ||
| onNewContext(context); | ||
| boolean success = false; | ||
| try { | ||
| putContext(context); | ||
| if (request.scroll() != null) { | ||
| // ensure that if index is deleted concurrently, we free the context immediately, either here or in freeAllContextForIndex | ||
| indicesService.indexServiceSafe(request.shardId().getIndex()); | ||
| success = true; | ||
| return context; | ||
| } finally { | ||
| if (success == false) { | ||
| freeContext(context.id()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void onNewContext(SearchContext context) { | ||
| boolean success = false; | ||
| try { | ||
| if (context.scrollContext() != null) { | ||
| openScrollContexts.incrementAndGet(); | ||
| context.indexShard().getSearchOperationListener().onNewScrollContext(context); | ||
| } | ||
| context.indexShard().getSearchOperationListener().onNewContext(context); | ||
| success = true; | ||
| return context; | ||
| } finally { | ||
| if (!success) { | ||
| freeContext(context.id()); | ||
| // currently, the concrete listener is CompositeListener, which swallows exceptions, but here we anyway try to do the | ||
| // right thing by notifying onFreeXXX in case one of the listeners fails with an exception in the future. | ||
| if (success == false) { | ||
| onFreeContext(context); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -648,18 +664,22 @@ private void freeAllContextForIndex(Index index) { | |
| public boolean freeContext(long id) { | ||
| try (SearchContext context = removeContext(id)) { | ||
| if (context != null) { | ||
| assert context.refCount() > 0 : " refCount must be > 0: " + context.refCount(); | ||
| context.indexShard().getSearchOperationListener().onFreeContext(context); | ||
| if (context.scrollContext() != null) { | ||
| openScrollContexts.decrementAndGet(); | ||
| context.indexShard().getSearchOperationListener().onFreeScrollContext(context); | ||
| } | ||
| onFreeContext(context); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private void onFreeContext(SearchContext context) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add an assertion here that ensures that we removed the context. I think it's trappy that we have an this method and we need to make sure that we never call if if we haven't removed the context. The special case is fina |
||
| assert context.refCount() > 0 : " refCount must be > 0: " + context.refCount(); | ||
| context.indexShard().getSearchOperationListener().onFreeContext(context); | ||
| if (context.scrollContext() != null) { | ||
| openScrollContexts.decrementAndGet(); | ||
| context.indexShard().getSearchOperationListener().onFreeScrollContext(context); | ||
| } | ||
| } | ||
|
|
||
| public void freeAllScrollContexts() { | ||
| for (SearchContext searchContext : activeContexts.values()) { | ||
| if (searchContext.scrollContext() != null) { | ||
|
|
||
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
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.
I am still not sure why we do this here. Why can't we just let the search go and fail whenever?
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.
It might be overkill... The way I understand the code, the primary purpose of
afterIndexRemovedis to ensure that we free memory and file system resources as quickly as possible when an index is deleted/closed. Here we try to ensure the same happens also during a race condition. Without this, we theoretically risk holding on to memory/file system resources for a while after the index is deleted/closed. With this, we should free such resources as soon as the index is deleted (and any current search phases have run to completion).I guess there is a trade-off in that doing this double validation has a small cost and the likelihood of seeing the race condition is very small. I can certainly remove this double validation if you think?
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.
I think we should remove the double validation. But the fix you added in this PR is crucial!
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.
Thanks @s1monw , I have removed the double validation, please have another look at your convenience.