mirror of
https://github.com/predis/predis.git
synced 2026-11-23 20:12:22 +00:00
dc61d2d81e
* Return null when a watched key aborts a Relay EXEC When a watched key aborts EXEC, Redis replies with a null array but Relay throws "Transaction failed", and RelayConnection returns that as an error response. The strategy mapped it to null in a catch for a ServerException, which RelayConnection never throws, so a transaction aborted by WATCH failed with a ServerException instead of AbortedMultiExecException and the retry option was ignored. RelayConnection now returns null for that failure of EXEC, as the stream connection does for the null array, so every strategy handles it the same way. The dead catch in NonClusterConnectionStrategy is removed, and so is the exclusion that kept a Relay connection open when EXEC fails with an error other than EXECABORT, since a WATCH abort no longer reaches it. A Relay connection whose EXEC was rejected while MULTI stays open is now closed like any other. Client::exec() on a Relay connection returns null after a WATCH abort, as it does on other connections. * Add the Relay WATCH abort fix to the changelog
473 lines
15 KiB
PHP
473 lines
15 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) 2009-2020 Daniele Alessandri
|
|
* (c) 2021-2026 Till Krüss
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Transaction\Strategy;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Predis\Command\CommandInterface;
|
|
use Predis\Command\Redis\GET;
|
|
use Predis\Command\Redis\SET;
|
|
use Predis\Connection\ConnectionException;
|
|
use Predis\Connection\NodeConnectionInterface;
|
|
use Predis\Connection\Parameters;
|
|
use Predis\Connection\RelayConnection;
|
|
use Predis\Connection\Resource\Exception\StreamInitException;
|
|
use Predis\Response\Error;
|
|
use Predis\Response\ServerException;
|
|
use Predis\Retry\Retry;
|
|
use Predis\Retry\Strategy\ExponentialBackoff;
|
|
use Predis\TimeoutException;
|
|
use Predis\Transaction\MultiExecState;
|
|
|
|
class NodeConnectionStrategyTest extends TestCase
|
|
{
|
|
/**
|
|
* @var NodeConnectionInterface
|
|
*/
|
|
private $mockConnection;
|
|
|
|
/**
|
|
* @var CommandInterface
|
|
*/
|
|
private $mockCommand;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $connected = true;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->mockConnection = $this->getMockBuilder(NodeConnectionInterface::class)->getMock();
|
|
$this->mockConnection
|
|
->method('isConnected')
|
|
->willReturnCallback(function () {
|
|
return $this->connected;
|
|
});
|
|
$this->mockCommand = $this->getMockBuilder(CommandInterface::class)->getMock();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testExecuteCommand(): void
|
|
{
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('executeCommand')
|
|
->with($this->mockCommand)
|
|
->willReturn('OK');
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertEquals('OK', $strategy->executeCommand($this->mockCommand));
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testUnwatch(): void
|
|
{
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('executeCommand')
|
|
->with($this->callback(static function ($command) {
|
|
return $command->getId() === 'UNWATCH';
|
|
}))
|
|
->willReturn('OK');
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertEquals('OK', $strategy->unwatch());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testUnwatchWithRetries(): void
|
|
{
|
|
$parameters = new Parameters([
|
|
'retry' => new Retry(new ExponentialBackoff(1000, 10000), 3),
|
|
]);
|
|
|
|
$this->mockConnection
|
|
->expects($this->exactly(4))
|
|
->method('executeCommand')
|
|
->with($this->callback(static function ($command) {
|
|
return $command->getId() === 'UNWATCH';
|
|
}))
|
|
->willReturnOnConsecutiveCalls(
|
|
$this->throwException(new TimeoutException($this->mockConnection)),
|
|
$this->throwException(new TimeoutException($this->mockConnection)),
|
|
$this->throwException(new TimeoutException($this->mockConnection)),
|
|
'OK'
|
|
);
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn($parameters);
|
|
|
|
$this->mockConnection
|
|
->expects($this->exactly(3))
|
|
->method('disconnect');
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertEquals('OK', $strategy->unwatch());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testMultiIsRetriedUntilTheTransactionReachesTheConnection(): void
|
|
{
|
|
$failures = 0;
|
|
$this->mockConnection
|
|
->expects($this->exactly(5))
|
|
->method('executeCommand')
|
|
->willReturnCallback(function (CommandInterface $command) use (&$failures) {
|
|
if ($command->getId() === 'MULTI' && $failures++ % 2 === 0) {
|
|
throw new TimeoutException($this->mockConnection);
|
|
}
|
|
|
|
return $command->getId() === 'EXEC' ? [] : 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters(['retry' => new Retry(new ExponentialBackoff(1, 10), 3)]));
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
$this->assertSame([], $strategy->executeTransaction());
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testMultiIsRetriedAfterAFailedConnect(): void
|
|
{
|
|
$failed = false;
|
|
$this->mockConnection
|
|
->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->willReturnCallback(function () use (&$failed) {
|
|
if (!$failed) {
|
|
$failed = true;
|
|
|
|
throw new StreamInitException('Connection refused');
|
|
}
|
|
|
|
return 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters(['retry' => new Retry(new ExponentialBackoff(1, 10), 3)]));
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider transactionEnds
|
|
* @return void
|
|
*/
|
|
public function testMultiIsRetriedAgainOnceTheTransactionEnds(string $end): void
|
|
{
|
|
$failMulti = false;
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('executeCommand')
|
|
->willReturnCallback(function (CommandInterface $command) use (&$failMulti) {
|
|
if ($command->getId() === 'MULTI' && $failMulti) {
|
|
$failMulti = false;
|
|
|
|
throw new TimeoutException($this->mockConnection);
|
|
}
|
|
|
|
if ($command->getId() === 'SET') {
|
|
throw new ConnectionException($this->mockConnection, 'Connection lost');
|
|
}
|
|
|
|
return 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters(['retry' => new Retry(new ExponentialBackoff(1, 10), 3)]));
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
$strategy->initializeTransaction();
|
|
|
|
switch ($end) {
|
|
case 'DISCARD':
|
|
$strategy->discard();
|
|
break;
|
|
case 'UNWATCH':
|
|
$strategy->unwatch();
|
|
break;
|
|
}
|
|
|
|
$failMulti = true;
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
$this->assertFalse($failMulti);
|
|
}
|
|
|
|
public function transactionEnds(): array
|
|
{
|
|
return [
|
|
'DISCARD' => ['DISCARD'],
|
|
'UNWATCH' => ['UNWATCH'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testCommandsAfterWatchFailUntilUnwatchOnceTheConnectionIsLost(): void
|
|
{
|
|
$sent = [];
|
|
$this->mockConnection
|
|
->expects($this->exactly(3))
|
|
->method('executeCommand')
|
|
->willReturnCallback(function (CommandInterface $command) use (&$sent) {
|
|
$sent[] = $command->getId();
|
|
|
|
if ($sent === ['WATCH', 'GET']) {
|
|
throw new TimeoutException($this->mockConnection);
|
|
}
|
|
|
|
return 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('disconnect')
|
|
->willReturnCallback(function () {
|
|
$this->connected = false;
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters(['retry' => new Retry(new ExponentialBackoff(1, 10), 3)]));
|
|
|
|
$state = new MultiExecState();
|
|
$state->flag(MultiExecState::CAS);
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, $state);
|
|
$strategy->watch(['foo']);
|
|
|
|
try {
|
|
$strategy->executeCommand(new GET());
|
|
$this->fail('Expected the connection error to reach the caller.');
|
|
} catch (TimeoutException $exception) {
|
|
// NOOP
|
|
}
|
|
|
|
try {
|
|
$strategy->executeCommand(new GET());
|
|
$this->fail('Expected the lost connection to reach the caller.');
|
|
} catch (ConnectionException $exception) {
|
|
$this->assertSame('The connection holding the transaction was lost.', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertSame(['WATCH', 'GET'], $sent);
|
|
|
|
$this->assertEquals('OK', $strategy->unwatch());
|
|
|
|
$this->assertSame('OK', $strategy->executeCommand(new GET())->getResponse());
|
|
$this->assertSame(['WATCH', 'GET', 'GET'], $sent);
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function testDiscardEndsATransactionWhoseConnectionWasLost(): void
|
|
{
|
|
$sent = [];
|
|
$this->mockConnection
|
|
->expects($this->exactly(3))
|
|
->method('executeCommand')
|
|
->willReturnCallback(function (CommandInterface $command) use (&$sent) {
|
|
$sent[] = $command->getId();
|
|
|
|
if ($command->getId() === 'SET') {
|
|
throw new ConnectionException($this->mockConnection, 'Connection lost');
|
|
}
|
|
|
|
return 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->once())
|
|
->method('disconnect')
|
|
->willReturnCallback(function () {
|
|
$this->connected = false;
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters(['retry' => new Retry(new ExponentialBackoff(1, 10), 3)]));
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
$strategy->initializeTransaction();
|
|
|
|
try {
|
|
$strategy->executeCommand(new SET());
|
|
$this->fail('Expected the connection error to reach the caller.');
|
|
} catch (ConnectionException $exception) {
|
|
// NOOP
|
|
}
|
|
|
|
try {
|
|
$strategy->executeTransaction();
|
|
$this->fail('Expected EXEC to fail on the lost connection.');
|
|
} catch (ConnectionException $exception) {
|
|
$this->assertSame('The connection holding the transaction was lost.', $exception->getMessage());
|
|
}
|
|
|
|
$this->assertEquals('OK', $strategy->discard());
|
|
$this->assertSame(['MULTI', 'SET'], $sent);
|
|
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
$this->assertSame(['MULTI', 'SET', 'MULTI'], $sent);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider rejectedExecReplies
|
|
* @return void
|
|
*/
|
|
public function testRejectedExecClosesTheConnectionUnlessTheTransactionWasDiscarded(string $error, bool $closed): void
|
|
{
|
|
$this->mockConnection
|
|
->method('executeCommand')
|
|
->willReturnCallback(static function (CommandInterface $command) use ($error) {
|
|
return $command->getId() === 'EXEC' ? new Error($error) : 'OK';
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($closed ? $this->once() : $this->never())
|
|
->method('disconnect')
|
|
->willReturnCallback(function () {
|
|
$this->connected = false;
|
|
});
|
|
|
|
$this->mockConnection
|
|
->expects($this->any())
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
|
|
$strategy = new NodeConnectionStrategy($this->mockConnection, new MultiExecState());
|
|
$strategy->initializeTransaction();
|
|
|
|
try {
|
|
$strategy->executeTransaction();
|
|
$this->fail('Expected EXEC to fail.');
|
|
} catch (ServerException $exception) {
|
|
$this->assertSame($error, $exception->getMessage());
|
|
}
|
|
|
|
$this->assertTrue($strategy->initializeTransaction());
|
|
}
|
|
|
|
public function rejectedExecReplies(): array
|
|
{
|
|
return [
|
|
'transaction discarded' => [
|
|
'EXECABORT Transaction discarded because of previous errors.',
|
|
false,
|
|
],
|
|
'EXEC rejected while MULTI stays open' => [
|
|
'BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.',
|
|
true,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @requires extension relay
|
|
* @dataProvider relayExecReplies
|
|
* @param mixed $reply
|
|
*/
|
|
public function testRelayExecFailureIsHandledLikeOtherConnections($reply, bool $closed): void
|
|
{
|
|
$relay = $this->getMockBuilder(RelayConnection::class)->disableOriginalConstructor()->getMock();
|
|
$relay
|
|
->method('executeCommand')
|
|
->willReturnCallback(static function (CommandInterface $command) use ($reply) {
|
|
return $command->getId() === 'EXEC' ? $reply : 'OK';
|
|
});
|
|
$relay
|
|
->method('isConnected')
|
|
->willReturn(true);
|
|
$relay
|
|
->method('getParameters')
|
|
->willReturn(new Parameters());
|
|
$relay
|
|
->expects($closed ? $this->once() : $this->never())
|
|
->method('disconnect');
|
|
|
|
$state = new MultiExecState();
|
|
$state->flag(MultiExecState::CAS);
|
|
$strategy = new NodeConnectionStrategy($relay, $state);
|
|
$strategy->watch(['foo']);
|
|
$strategy->multi();
|
|
|
|
if ($reply === null) {
|
|
$this->assertNull($strategy->executeTransaction());
|
|
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$strategy->executeTransaction();
|
|
$this->fail('Expected EXEC to fail.');
|
|
} catch (ServerException $exception) {
|
|
$this->assertSame($reply->getMessage(), $exception->getMessage());
|
|
}
|
|
}
|
|
|
|
public function relayExecReplies(): array
|
|
{
|
|
return [
|
|
'aborted by WATCH' => [null, false],
|
|
'transaction discarded' => [
|
|
new Error('EXECABORT Transaction discarded because of previous errors. (RELAY_ERR_REDIS; commands.c:17075)'),
|
|
false,
|
|
],
|
|
'EXEC rejected while MULTI stays open' => [
|
|
new Error('BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE. (RELAY_ERR_REDIS; commands.c:17075)'),
|
|
true,
|
|
],
|
|
];
|
|
}
|
|
}
|