-
Notifications
You must be signed in to change notification settings - Fork 92
feat(clp-s)!: Deduplicate cached search results using compound MongoDB IDs. #2509
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
Open
sitaowang1998
wants to merge
29
commits into
y-scope:main
Choose a base branch
from
sitaowang1998:result-cache-dedup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
767feb1
Insert compound id in mongodb
sitaowang1998 3b4ad65
Use query complete time for gargabe collection
sitaowang1998 2b5deba
Rename ix to idx
sitaowang1998 d22e01a
Use _id column
sitaowang1998 cc8303a
Add legacy read support
sitaowang1998 7912f44
Use start_time instead of create_time in completion time calcuation
sitaowang1998 500bb2c
Style improvement
sitaowang1998 3143118
Merge branch 'main' into result-cache-dedup
sitaowang1998 4d0b9fb
Address coderabbit comment
sitaowang1998 49fe7e2
Fix exception catch
sitaowang1998 e7734fb
Fix style
sitaowang1998 19d477b
Fix error parsing
sitaowang1998 96538e2
Merge branch 'main' into result-cache-dedup
sitaowang1998 3b38b90
Remove unused function and const
sitaowang1998 2b6d0e1
Update mongodb in clo
sitaowang1998 b39bf3a
Remove backward compatibility
sitaowang1998 0740535
Bug fix
sitaowang1998 84e1074
Guard against null timestamp
sitaowang1998 3165049
Merge branch 'main' into result-cache-dedup
sitaowang1998 ee8c1aa
Fix docstring
sitaowang1998 6f214b4
Remove unnecessary copy
sitaowang1998 b450ab9
Restore f-string fix
sitaowang1998 09ad0bc
Fix line break
sitaowang1998 ee064c8
Use empty instead of iterator compare
sitaowang1998 fd7e107
Make option reusable
sitaowang1998 35b8a10
Fix clang-tidy
sitaowang1998 34342c6
Fix move
sitaowang1998 bcad695
Fix docstring
sitaowang1998 084c94f
Fix transport error
sitaowang1998 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
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
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,106 @@ | ||
| #include "MongoDBUtils.hpp" | ||
|
|
||
| #include <algorithm> | ||
|
sitaowang1998 marked this conversation as resolved.
Outdated
|
||
|
|
||
| #include <bsoncxx/types.hpp> | ||
| #include <mongocxx/exception/bulk_write_exception.hpp> | ||
|
|
||
| namespace clp_s { | ||
| namespace { | ||
| constexpr int32_t cDuplicateKeyErrorCode{11'000}; | ||
|
|
||
| /** | ||
| * Checks whether an aggregated bulk-write reply contains any command errors. | ||
| * @param reply The raw MongoDB bulk-write reply. | ||
| * @return true if the reply contains a command error, false otherwise. | ||
| */ | ||
| [[nodiscard]] auto has_command_errors(bsoncxx::document::view const& reply) -> bool; | ||
|
|
||
| /** | ||
| * Checks whether a bulk-write reply contains any write-concern errors. | ||
| * @param reply The raw MongoDB bulk-write reply. | ||
| * @return true if the reply contains a write-concern error, false otherwise. | ||
| */ | ||
| [[nodiscard]] auto has_write_concern_errors(bsoncxx::document::view const& reply) -> bool; | ||
|
|
||
| /** | ||
| * Checks whether an entry from a bulk-write reply's `writeErrors` array is a duplicate-key error. | ||
| * @param write_error The write-error entry to inspect. | ||
| * @return true if the entry has MongoDB's duplicate-key error code, false otherwise. | ||
| */ | ||
| [[nodiscard]] auto is_duplicate_key_write_error(bsoncxx::array::element const& write_error) -> bool; | ||
|
|
||
| [[nodiscard]] auto has_command_errors(bsoncxx::document::view const& reply) -> bool { | ||
| auto const errors_element = reply["errorReplies"]; | ||
| if (false == static_cast<bool>(errors_element)) { | ||
| return false; | ||
| } | ||
| if (bsoncxx::type::k_array != errors_element.type()) { | ||
| return true; | ||
| } | ||
| auto const errors = errors_element.get_array().value; | ||
| return errors.begin() != errors.end(); | ||
|
sitaowang1998 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| [[nodiscard]] auto has_write_concern_errors(bsoncxx::document::view const& reply) -> bool { | ||
| if (static_cast<bool>(reply["writeConcernError"])) { | ||
| return true; | ||
| } | ||
|
|
||
| auto const errors_element = reply["writeConcernErrors"]; | ||
| if (false == static_cast<bool>(errors_element)) { | ||
| return false; | ||
| } | ||
| if (bsoncxx::type::k_array != errors_element.type()) { | ||
| return true; | ||
| } | ||
| auto const errors = errors_element.get_array().value; | ||
| return errors.begin() != errors.end(); | ||
| } | ||
|
|
||
| [[nodiscard]] auto is_duplicate_key_write_error(bsoncxx::array::element const& write_error) | ||
| -> bool { | ||
| if (bsoncxx::type::k_document != write_error.type()) { | ||
| return false; | ||
| } | ||
|
|
||
| auto const code = write_error.get_document().value["code"]; | ||
| if (false == static_cast<bool>(code)) { | ||
| return false; | ||
| } | ||
| if (bsoncxx::type::k_int32 == code.type()) { | ||
| return cDuplicateKeyErrorCode == code.get_int32().value; | ||
| } | ||
| if (bsoncxx::type::k_int64 == code.type()) { | ||
| return cDuplicateKeyErrorCode == code.get_int64().value; | ||
| } | ||
| return false; | ||
| } | ||
| } // namespace | ||
|
|
||
| auto contains_only_duplicate_key_write_errors(mongocxx::bulk_write_exception const& exception) | ||
| -> bool { | ||
| auto const& raw_server_error = exception.raw_server_error(); | ||
| if (false == raw_server_error.has_value()) { | ||
| return false; | ||
| } | ||
|
|
||
| auto const reply = raw_server_error->view(); | ||
| if (has_command_errors(reply) || has_write_concern_errors(reply)) { | ||
| return false; | ||
| } | ||
|
|
||
| auto const write_errors_element = reply["writeErrors"]; | ||
| if (false == static_cast<bool>(write_errors_element) | ||
| || bsoncxx::type::k_array != write_errors_element.type()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| auto const write_errors = write_errors_element.get_array().value; | ||
| if (write_errors.begin() == write_errors.end()) { | ||
| return false; | ||
| } | ||
| return std::all_of(write_errors.begin(), write_errors.end(), is_duplicate_key_write_error); | ||
| } | ||
| } // namespace clp_s | ||
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,21 @@ | ||
| #ifndef CLP_S_MONGODBUTILS_HPP | ||
| #define CLP_S_MONGODBUTILS_HPP | ||
|
|
||
| #include <mongocxx/exception/bulk_write_exception.hpp> | ||
|
|
||
| namespace clp_s { | ||
| /** | ||
| * Returns whether the bulk write failed only because some documents already exist. | ||
| * | ||
| * Command and write-concern errors are rejected since they mean MongoDB did not confirm the | ||
| * outcome of the entire batch. At least one write error must be present, and every write error | ||
| * must be a duplicate-key error. | ||
|
sitaowang1998 marked this conversation as resolved.
Outdated
|
||
| * @param exception The exception containing the raw MongoDB bulk-write reply. | ||
| * @return true if the reply contains only duplicate-key write errors, false otherwise. | ||
|
sitaowang1998 marked this conversation as resolved.
Outdated
|
||
| */ | ||
| [[nodiscard]] auto contains_only_duplicate_key_write_errors( | ||
| mongocxx::bulk_write_exception const& exception | ||
| ) -> bool; | ||
|
sitaowang1998 marked this conversation as resolved.
|
||
| } // namespace clp_s | ||
|
|
||
| #endif // CLP_S_MONGODBUTILS_HPP | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.