Skip to content

Update MongoDB $or queries to avoid SERVER-13732 bug #3476

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

Conversation

NotBobTheBuilder
Copy link
Contributor

MongoDB has an unfixed bug in all supported versions 2.6-3.4 which
results in suboptimal index usage for $or queries when the query has
implicit $ands at the query root.

When adding _rperm to $or queries, Parse accidentally creates
queries which hit this bug.

The fix in this commit applies the suggested workaround of putting the
_rperm property inside all $or subdocuments, moving it from the top
level and leaving $or as the only top-level operator.

MongoDB Bug Link: https://jira.mongodb.org/browse/SERVER-13732

MongoDB has an unfixed bug in all supported versions 2.6-3.4 which
results in suboptimal index usage for `$or` queries when the query has
implicit `$and`s at the query root.

When adding `_rperm` to `$or` queries, Parse accidentally creates
queries which hit this bug.

The fix in this commit applies the suggested workaround of putting the
`_rperm` property inside all `$or` subdocuments, moving it from the top
level and leaving `$or` as the only top-level operator.

MongoDB Bug Link: https://jira.mongodb.org/browse/SERVER-13732
@NotBobTheBuilder NotBobTheBuilder changed the title MongoDB $or Queries avoid SERVER-13732 bug Update MongoDB $or queries to avoid SERVER-13732 bug Feb 4, 2017
return qobj;
});
} else {
newQuery._rperm = { "$in" : [null, "*", ...acl]};
Copy link
Contributor

Choose a reason for hiding this comment

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

The spacing between the closing } and the ] is odd here and should have a space as well as the above '$in' in the if block.

@@ -18,7 +18,14 @@ function addWriteACL(query, acl) {
function addReadACL(query, acl) {
const newQuery = _.cloneDeep(query);
//Can't be any existing '_rperm' query, we don't allow client queries on that, no need to $and
newQuery._rperm = { "$in" : [null, "*", ...acl]};
if (newQuery.hasOwnProperty('$or')) {
newQuery.$or = newQuery.$or.map(function(qobj) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be nice to add a comment here that provides a link to the MongoDB JIRA ticket and a note that we are intentionally distributing the _rperm predicate.

@TylerBrock
Copy link
Contributor

Nice catch and workaround! Thanks for the contribution.

@TylerBrock TylerBrock merged commit 96a9588 into parse-community:master Feb 4, 2017
@NotBobTheBuilder NotBobTheBuilder deleted the avoid-mongodb-server-13732 branch February 4, 2017 05:40
NotBobTheBuilder added a commit to NotBobTheBuilder/parse-server that referenced this pull request Feb 24, 2017
In a prior commit, improvements were made to the addition of `_rperm`
in the case of `$or` queries, to avoid MongoDB bug SERVER-13732.

As the vast majority of $or queries previously hit this bug due to the
presence of `_rperm` on most Parse queries), the present solution
avoids the bug and improves query performance in most cases.

However, it's still possible for clients to supply their own queries
which hit that bug, such as those with `_created_at` or `_updated_at`
filters, or their own properties from their data model.

This commit makes the logic currently present for `_rperm` available
to all top level filters that exist alongside an $or query, meaning
SERVER-13732 should be avoided in all cases where keys at the top and
inner levels do not have name clashes.

- parse-community#3476
- https://jira.mongodb.org/browse/SERVER-13732
flovilmart pushed a commit that referenced this pull request Feb 26, 2017
In a prior commit, improvements were made to the addition of `_rperm`
in the case of `$or` queries, to avoid MongoDB bug SERVER-13732.

As the vast majority of $or queries previously hit this bug due to the
presence of `_rperm` on most Parse queries), the present solution
avoids the bug and improves query performance in most cases.

However, it's still possible for clients to supply their own queries
which hit that bug, such as those with `_created_at` or `_updated_at`
filters, or their own properties from their data model.

This commit makes the logic currently present for `_rperm` available
to all top level filters that exist alongside an $or query, meaning
SERVER-13732 should be avoided in all cases where keys at the top and
inner levels do not have name clashes.

- #3476
- https://jira.mongodb.org/browse/SERVER-13732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants