-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-789: Snapshot query examples #900
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
|
||
// Start Snapshot Query Example 1 | ||
$catsCollection = $client->selectCollection('pets', 'cats'); | ||
$dogsCollection = $client->selectCollection('pets', 'dogs'); |
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.
These are purposely redefined in order to demonstrate selecting a collection from the client object within the example. For context, the docs team parses this file for code between various start/end comments and uses that to render language-specific examples in the MongoDB manual.
public function testSnapshotQueries(): void | ||
{ | ||
if (version_compare($this->getServerVersion(), '5.0.0', '<')) { | ||
$this->markTestSkipped('Snapshot queries outside of transactions are not supported'); |
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.
['session' => $session] | ||
)->toArray()[0]->adoptableDogsCount; | ||
|
||
var_dump($adoptablePetsCount); |
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.
Various examples require printing output. Since we don't want that output to interfere with the test suite, we typically use output buffering to capture and discard it.
$this->assertSame(3, $adoptablePetsCount); | ||
|
||
$catsCollection->drop(); | ||
$dogsCollection->drop(); |
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.
Since these examples use specific database and collection names, the cleanup logic in tearDown()
does not apply and we manually clean up after each test.
@@ -1575,6 +1575,103 @@ public function testCausalConsistency(): void | |||
ob_end_clean(); | |||
} | |||
|
|||
public function testSnapshotQueries(): void |
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 observed SnapshotUnavailable(246) errors in CI builds. This PR will require work-arounds for replica sets and sharded clusters, which we can adapt from RUBY-2909.
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.
Added private waitForSnapshot()
and preventStaleDbVersionError()
methods for replica sets and sharded clusters, respectively.
e1e1f78
to
775e1e5
Compare
Includes work-arounds for SnapshotUnavailable(246) errors on replica sets and sharded clusters.
@aleksandr-rudo did you have any feedback/questions before I merge this? |
@@ -1751,4 +1867,39 @@ private function assertInventoryCount($count): void | |||
{ | |||
$this->assertCollectionCount($this->getDatabaseName() . '.' . $this->getCollectionName(), $count); | |||
} | |||
|
|||
private function waitForSnapshot(string $databaseName, string $collectionName): void |
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.
At a glance it seems to me, that the idea of custom function like waitForSnapshot can be useful for interacting with snapshots and in such case it can be organized in a library functionality?
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.
Note: this function was lifted from mongodb/mongo-ruby-driver@7c4117b#diff-7b02c2b4b34a58c688750366d0230014bb453e0381af394d736aa9d0a25538a5R374 in the Ruby driver.
The function itself is not generally useful, since we're targeting a specific collection. The server-side error message is:
Unable to read from a snapshot due to pending collection catalog changes; please retry the operation.
If a user were to encounter this, they would probably want to retry their original query. For our purposes here, ensuring any snapshot query (even one with a different query filter) succeeds seems to do the trick to avoid an error in the documentation example that calls this method.
tests/DocumentationExamplesTest.php
Outdated
|
||
throw $e; | ||
} | ||
} while (microtime(true) < $startTime + 10); |
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.
May be it would be more flexible to make function input argument with definition of timeout for snapshot waiting?
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.
This is only called once, so I'm not inclined to parameterize the method; however, I do think this can be improved by removing the literal 10
here and changing the function to instead calculate the deadline up front (with a comment explaining the 10-second timeout).
https://jira.mongodb.org/browse/PHPLIB-789