Skip to content

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

Merged
merged 3 commits into from
Mar 30, 2022
Merged

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Mar 16, 2022

@jmikola jmikola requested a review from aleksandr-rudo March 16, 2022 23:05

// Start Snapshot Query Example 1
$catsCollection = $client->selectCollection('pets', 'cats');
$dogsCollection = $client->selectCollection('pets', 'dogs');
Copy link
Member Author

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');
Copy link
Member Author

@jmikola jmikola Mar 16, 2022

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);
Copy link
Member Author

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();
Copy link
Member Author

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
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 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.

Copy link
Member Author

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.

@jmikola jmikola force-pushed the phplib-789 branch 2 times, most recently from e1e1f78 to 775e1e5 Compare March 17, 2022 19:07
jmikola added 2 commits March 18, 2022 16:04
Includes work-arounds for SnapshotUnavailable(246) errors on replica sets and sharded clusters.
@jmikola
Copy link
Member Author

jmikola commented Mar 29, 2022

@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
Copy link
Contributor

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?

Copy link
Member Author

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.


throw $e;
}
} while (microtime(true) < $startTime + 10);
Copy link
Contributor

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?

Copy link
Member Author

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).

@jmikola jmikola merged commit 5ef7251 into mongodb:master Mar 30, 2022
@jmikola jmikola deleted the phplib-789 branch March 30, 2022 13:58
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.

2 participants