-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-342: Change streams should use the same session when resuming #528
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
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,9 @@ | |
use MongoDB\Driver\Server; | ||
use MongoDB\Driver\Exception\ConnectionTimeoutException; | ||
use MongoDB\Exception\ResumeTokenException; | ||
use MongoDB\Operation\CreateCollection; | ||
use MongoDB\Operation\DatabaseCommand; | ||
use MongoDB\Operation\DropCollection; | ||
use MongoDB\Operation\InsertOne; | ||
use MongoDB\Operation\Watch; | ||
use MongoDB\Tests\CommandObserver; | ||
|
@@ -588,6 +590,90 @@ public function testResumeTokenNotFoundAdvancesKey() | |
$this->assertSame(2, $changeStream->key()); | ||
} | ||
|
||
public function testSessionPersistsAfterResume() | ||
{ | ||
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions); | ||
|
||
$changeStream = null; | ||
$originalSession = null; | ||
$sessionAfterResume = []; | ||
$commands = []; | ||
|
||
/* We want to ensure that the lsid of the initial aggregate matches the | ||
* lsid of any aggregates after the change stream resumes. After | ||
* PHPC-1152 is complete, we will ensure that the lsid of the initial | ||
* aggregate matches the lsid of any subsequent aggregates and getMores. | ||
*/ | ||
(new CommandObserver)->observe( | ||
function() use ($operation, &$changeStream) { | ||
$changeStream = $operation->execute($this->getPrimaryServer()); | ||
}, | ||
function($changeStream) use (&$originalSession) { | ||
if (isset($changeStream->aggregate)) { | ||
$originalSession = bin2hex((string) $changeStream->lsid->id); | ||
} | ||
} | ||
); | ||
|
||
$changeStream->rewind(); | ||
$this->killChangeStreamCursor($changeStream); | ||
|
||
(new CommandObserver)->observe( | ||
function() use (&$changeStream) { | ||
$changeStream->next(); | ||
}, | ||
function ($changeStream) use (&$sessionAfterResume, &$commands) { | ||
$commands[] = key((array) $changeStream); | ||
$sessionAfterResume[] = bin2hex((string) $changeStream->lsid->id); | ||
} | ||
); | ||
|
||
$expectedCommands = [ | ||
/* We expect a getMore to be issued because we are calling next(). */ | ||
'getMore', | ||
/* Since we have killed the cursor, ChangeStream will resume by | ||
* issuing a new aggregate commmand. */ | ||
'aggregate', | ||
/* When ChangeStream resumes, it overwrites its original cursor with | ||
* the new cursor resulting from the last aggregate command. This | ||
* removes the last reference to the old cursor, which causes the | ||
* driver to kill it (via mongoc_cursor_destroy()). */ | ||
'killCursors', | ||
/* Finally, ChangeStream will rewind the new cursor as the last step | ||
* of the resume process. This results in one last getMore. */ | ||
'getMore', | ||
]; | ||
|
||
$this->assertSame($expectedCommands, $commands); | ||
|
||
foreach ($sessionAfterResume as $session) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice :) |
||
$this->assertEquals($session, $originalSession); | ||
} | ||
} | ||
|
||
public function testSessionFreed() | ||
{ | ||
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
$operation->execute($this->getPrimaryServer()); | ||
|
||
$operation = new Watch($this->manager, $this->getDatabaseName(), $this->getCollectionName(), [], $this->defaultOptions); | ||
$changeStream = $operation->execute($this->getPrimaryServer()); | ||
|
||
$rc = new ReflectionClass($changeStream); | ||
$rp = $rc->getProperty('resumeCallable'); | ||
$rp->setAccessible(true); | ||
|
||
$this->assertNotNull($rp->getValue($changeStream)); | ||
|
||
// Invalidate the cursor to verify that resumeCallable is unset when the cursor is exhausted. | ||
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
$operation->execute($this->getPrimaryServer()); | ||
|
||
$changeStream->next(); | ||
|
||
$this->assertNull($rp->getValue($changeStream)); | ||
} | ||
|
||
private function insertDocument($document) | ||
{ | ||
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), $document); | ||
|
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.
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.
Please test this against a replica set with auth if possible. That should highlight whether
killChangeStreamCursor()
will need to also take the same implicit session created within the Watch class.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.
After testing with auth, it looks like
killChangeStreamCursor()
does not need to take the same implicit session created within the Watch class.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.
Excellent. That answers my question in mongodb/mongo-php-driver#803 (comment).