-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-451: ChangeStream::rewind() should never execute getMore #636
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/* | ||
* Copyright 2019 MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB\Model; | ||
|
||
use MongoDB\Driver\Cursor; | ||
use IteratorIterator; | ||
|
||
/** | ||
* Iterator for tailable cursors. | ||
* | ||
* This iterator may be used to wrap a tailable cursor. By indicating whether | ||
* the cursor's first batch of results is empty, this iterator can NOP initial | ||
* calls to rewind() and prevent it from executing a getMore command. | ||
* | ||
* @internal | ||
*/ | ||
class TailableCursorIterator extends IteratorIterator | ||
{ | ||
private $isRewindNop; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @internal | ||
* @param Cursor $cursor | ||
* @param boolean $isFirstBatchEmpty | ||
*/ | ||
public function __construct(Cursor $cursor, $isFirstBatchEmpty) | ||
{ | ||
parent::__construct($cursor); | ||
$this->isRewindNop = $isFirstBatchEmpty; | ||
} | ||
|
||
/** | ||
* @see https://php.net/iteratoriterator.rewind | ||
* @return void | ||
*/ | ||
public function next() | ||
{ | ||
try { | ||
parent::next(); | ||
} finally { | ||
/* If the cursor ever advances to a valid position, do not prevent | ||
* future attempts to rewind the cursor. This will allow the driver | ||
* to throw a LogicException if the cursor has been advanced past | ||
* its first element. */ | ||
if ($this->valid()) { | ||
$this->isRewindNop = false; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @see https://php.net/iteratoriterator.rewind | ||
* @return void | ||
*/ | ||
public function rewind() | ||
{ | ||
if ($this->isRewindNop) { | ||
return; | ||
} | ||
|
||
parent::rewind(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests\Model; | ||
|
||
use MongoDB\Collection; | ||
use MongoDB\Driver\Exception\LogicException; | ||
use MongoDB\Model\TailableCursorIterator; | ||
use MongoDB\Operation\Find; | ||
use MongoDB\Operation\CreateCollection; | ||
use MongoDB\Operation\DropCollection; | ||
use MongoDB\Tests\CommandObserver; | ||
use MongoDB\Tests\FunctionalTestCase; | ||
|
||
class TailableCursorIteratorTest extends FunctionalTestCase | ||
{ | ||
private $collection; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$operation = new DropCollection($this->getDatabaseName(), $this->getCollectionName()); | ||
$operation->execute($this->getPrimaryServer()); | ||
|
||
$operation = new CreateCollection($this->getDatabaseName(), $this->getCollectionName(), ['capped' => true, 'size' => 8192]); | ||
$operation->execute($this->getPrimaryServer()); | ||
|
||
$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName()); | ||
} | ||
|
||
public function testFirstBatchIsEmpty() | ||
{ | ||
$this->collection->insertOne(['x' => 1]); | ||
|
||
$cursor = $this->collection->find(['x' => ['$gt' => 1]], ['cursorType' => Find::TAILABLE]); | ||
$iterator = new TailableCursorIterator($cursor, true); | ||
|
||
$this->assertNoCommandExecuted(function() use ($iterator) { $iterator->rewind(); }); | ||
$this->assertFalse($iterator->valid()); | ||
|
||
$this->collection->insertOne(['x' => 2]); | ||
|
||
$iterator->next(); | ||
$this->assertTrue($iterator->valid()); | ||
$this->assertMatchesDocument(['x' => 2], $iterator->current()); | ||
|
||
$this->expectException(LogicException::class); | ||
$iterator->rewind(); | ||
} | ||
|
||
public function testFirstBatchIsNotEmpty() | ||
{ | ||
$this->collection->insertOne(['x' => 1]); | ||
|
||
$cursor = $this->collection->find([], ['cursorType' => Find::TAILABLE]); | ||
$iterator = new TailableCursorIterator($cursor, false); | ||
|
||
$this->assertNoCommandExecuted(function() use ($iterator) { $iterator->rewind(); }); | ||
$this->assertTrue($iterator->valid()); | ||
$this->assertMatchesDocument(['x' => 1], $iterator->current()); | ||
|
||
$this->collection->insertOne(['x' => 2]); | ||
|
||
$iterator->next(); | ||
$this->assertTrue($iterator->valid()); | ||
$this->assertMatchesDocument(['x' => 2], $iterator->current()); | ||
|
||
$this->expectException(LogicException::class); | ||
$iterator->rewind(); | ||
} | ||
|
||
private function assertNoCommandExecuted(callable $callable) | ||
{ | ||
$commands = []; | ||
|
||
(new CommandObserver)->observe( | ||
$callable, | ||
function(array $event) use (&$commands) { | ||
$this->fail(sprintf('"%s" command was executed', $event['started']->getCommandName())); | ||
} | ||
); | ||
|
||
$this->assertEmpty($commands); | ||
} | ||
} |
Oops, something went wrong.
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.
Why was this removed?
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.
The original logic was actually a bit backwards, but it worked in practice.
The server has reported operation times for as long as change streams have been supported (since 3.6); however, the
startAtOperationTime
option has only been supported since 4.0. Previously,createResumeCallable
would decide to usestartAtOperationTime
purely by whether$this->operationTime
was set, as we would only ever set that if this condition was true and we ran theaggregate
command through APM.With this PR, we need to always check whether
aggregate
'sfirstBatch
is empty and therefore always run through APM. That means that$this->operationTime
was always captured and could result in the driver attempting to usestartAtOperationTime
to resume against a 3.6 server (in the absence of a resume token).To address this, I moved the
server_supports_feature
check tocreateResumeCallable
before we decide to use that option. That fixes the issue with resuming against a 3.6 server when no changes have been returned. This also means that the APM callback can continue collecting$this->operationTime
all the time. Alternatively, I suppose we could have added aserver_supports_feature
within the APM callback and decide whether to ignore the operation time, butcreateResumeCallable
seems more straightforward since that's when we actually attempt to specify the option.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.
That makes sense, thanks!