Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Use primary read preference when reading newly peristed GridFS files #324

Merged
merged 2 commits into from
Apr 30, 2018

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Apr 27, 2018

This bug can appear when working on a replica set and assigning GridFS documents a secondary read preference (e.g. via the @ReadPreference mapping in Doctrine MongoDB ODM). The library stores the file, then reads it from the database to fetch current metadata. If the document is using a secondary read preference, this can lead to the document not being found. Furthermore, due to insufficient error handling, this will not be a regular exception but a type error when assigning the result to the GridFSFile instance.

Last but not least, the file was read twice from the database with the result of the first read not being used, so this should also (slightly) improve performance of GridFS write operations.

@alcaeus alcaeus added the bug label Apr 27, 2018
@alcaeus alcaeus added this to the 1.6.2 milestone Apr 27, 2018
@alcaeus alcaeus self-assigned this Apr 27, 2018
@alcaeus alcaeus requested a review from jmikola April 27, 2018 06:12
@alcaeus alcaeus force-pushed the use-primary-readpreference-gridfs branch from a9250e3 to 859565b Compare April 27, 2018 06:31
Copy link

@gruberro gruberro left a comment

Choose a reason for hiding this comment

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

LGTM! TY

return $closure();
} finally {
$prevTags = ! empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : [];
$object->setReadPreference($prevReadPref['type'], $prevTags);
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is an unrelated refactoring since we can now use finally?

Copy link
Member Author

Choose a reason for hiding this comment

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

That is correct. I included it to
a) have the methods consistent
b) preserve the original exception trace which got lost due to the additional throw.

* @return mixed
*/
private function withPrimaryReadPreference(\Closure $closure)
{
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to have a NOP-guard like Query::withReadPreference()?

Copy link
Member Author

Choose a reason for hiding this comment

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

I left it out because I didn't think the overhead of changing the read preference to primary and setting it back would be bad. The alternative in this case would be to compare the original read preference and conditionally change it to master. I can change it if you think it will improve performance.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense as-is. This keeps things simpler.

@alcaeus alcaeus merged commit a26a823 into doctrine:master Apr 30, 2018
@alcaeus alcaeus deleted the use-primary-readpreference-gridfs branch April 30, 2018 18:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants