Skip to content

PHPLIB-443: Propagate non-resumable errors during ChangeStream iteration #618

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 1 commit into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/ChangeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public function next()
$this->resumeCallable = null;
}
} catch (RuntimeException $e) {
if ($this->isResumableError($e)) {
$this->resume();
}
$this->resumeOrThrow($e);
}
}

Expand All @@ -144,9 +142,7 @@ public function rewind()
$this->resumeCallable = null;
}
} catch (RuntimeException $e) {
if ($this->isResumableError($e)) {
$this->resume();
}
$this->resumeOrThrow($e);
}
}

Expand Down Expand Up @@ -227,4 +223,20 @@ private function resume()
$this->csIt = $newChangeStream->csIt;
$this->csIt->rewind();
}

/**
* Either resumes after a resumable error or re-throws the exception.
*
* @param RuntimeException $exception
* @throws RuntimeException
*/
private function resumeOrThrow(RuntimeException $exception)
{
if ($this->isResumableError($exception)) {
$this->resume();
return;
}

throw $exception;
}
}
67 changes: 67 additions & 0 deletions tests/SpecTests/ChangeStreamsProseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace MongoDB\Tests\SpecTests;

use MongoDB\Collection;
use MongoDB\Driver\Exception\ServerException;

/**
* Change Streams spec prose tests.
*
* @see https://github.com/mongodb/specifications/tree/master/source/change-streams
*/
class ChangeStreamsProseTest extends FunctionalTestCase
{
private $collection;

public function setUp()
{
parent::setUp();

$this->skipIfChangeStreamIsNotSupported();

$this->collection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName());
$this->dropCollection();
}

public function tearDown()
{
if (!$this->hasFailed()) {
$this->dropCollection();
}

parent::tearDown();
}

/**
* ChangeStream will not attempt to resume after encountering error code
* 11601 (Interrupted), 136 (CappedPositionLost), or 237 (CursorKilled)
* while executing a getMore command.
*
* @dataProvider provideNonResumableErrorCodes
*/
public function testProseTest5($errorCode)
{
$this->configureFailPoint([
'configureFailPoint' => 'failCommand',
'mode' => ['times' => 1],
'data' => ['failCommands' => ['getMore'], 'errorCode' => $errorCode],
]);

$this->createCollection();
$changeStream = $this->collection->watch();

$this->expectException(ServerException::class);
$this->expectExceptionCode($errorCode);
$changeStream->rewind();
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 the initial aggregate returns an empty first batch, this rewind() is sufficient to invoke the first getMore by way of phongo_cursor_advance_and_check_for_error(), which calls mongoc_cursor_next().

}

public function provideNonResumableErrorCodes()
{
return [
[136], // CappedPositionLost
[237], // CursorKilled
[11601], // Interrupted
];
}
}
12 changes: 10 additions & 2 deletions tests/SpecTests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ protected function checkServerRequirements(array $runOn)
* The fail point will automatically be disabled during tearDown() to avoid
* affecting a subsequent test.
*
* @param stdClass $command configureFailPoint command document
* @param array|stdClass $command configureFailPoint command document
* @throws InvalidArgumentException if $command is not a configureFailPoint command
*/
protected function configureFailPoint(stdClass $command)
protected function configureFailPoint($command)
{
if (is_array($command)) {
$command = (object) $command;
}

if ( ! $command instanceof stdClass) {
throw new InvalidArgumentException('$command is not an array or stdClass instance');
}

if (key($command) !== 'configureFailPoint') {
throw new InvalidArgumentException('$command is not a configureFailPoint command');
}
Expand Down