Use master for read requests on empty slaves pool.

This is the last resort in case all of the slaves are unreachable.
This commit is contained in:
Daniele Alessandri
2016-05-15 17:20:58 +02:00
parent 00c5d7de19
commit 774b4014d9
2 changed files with 29 additions and 9 deletions
@@ -112,21 +112,22 @@ class MasterSlaveReplication implements ReplicationInterface
*/
public function getConnection(CommandInterface $command)
{
if ($this->current === null) {
$this->check();
$this->current = $this->strategy->isReadOperation($command)
? $this->pickSlave()
: $this->master;
if (!$this->current) {
if ($this->strategy->isReadOperation($command) && $slave = $this->pickSlave()) {
$this->current = $slave;
} else {
$this->current = $this->getMaster();
}
return $this->current;
}
if ($this->current === $this->master) {
return $this->current;
if ($this->current === $master = $this->getMaster()) {
return $master;
}
if (!$this->strategy->isReadOperation($command)) {
$this->current = $this->master;
if (!$this->strategy->isReadOperation($command) || !$this->slaves) {
$this->current = $master;
}
return $this->current;
@@ -254,6 +254,25 @@ class MasterSlaveReplicationTest extends PredisTestCase
$this->assertSame($master, $replication->getConnection($cmd));
}
/**
* @group disconnected
*/
public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
{
$profile = Profile\Factory::getDefault();
$master = $this->getMockConnection('tcp://host1?alias=master');
$replication = new MasterSlaveReplication();
$replication->add($master);
$cmd = $profile->createCommand('exists', array('foo'));
$this->assertSame($master, $replication->getConnection($cmd));
$cmd = $profile->createCommand('set', array('foo', 'bar'));
$this->assertSame($master, $replication->getConnection($cmd));
}
/**
* @group disconnected
*/