From 5b76b41fda50ea515afb21aa9a1de0698107612f Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sun, 15 May 2016 20:41:21 +0200 Subject: [PATCH] Use master for connect() on empty slaves pool. Internally the replication class uses this order to pick which server it should connect to: current connection, one of the slaves, master. If there is at least 1 slave, connect() will not fail even if master is undefined. If there are no slaves, connect() will pick master. If there are no connections registered for replication, connect() will fail immediatly. --- .../Aggregate/MasterSlaveReplication.php | 21 +++------ .../Aggregate/MasterSlaveReplicationTest.php | 46 +++++++------------ 2 files changed, 23 insertions(+), 44 deletions(-) diff --git a/src/Connection/Aggregate/MasterSlaveReplication.php b/src/Connection/Aggregate/MasterSlaveReplication.php index 8a8bda02..a87db892 100644 --- a/src/Connection/Aggregate/MasterSlaveReplication.php +++ b/src/Connection/Aggregate/MasterSlaveReplication.php @@ -53,16 +53,6 @@ class MasterSlaveReplication implements ReplicationInterface $this->strategy = $strategy ?: new ReplicationStrategy(); } - /** - * Checks if one master and at least one slave have been defined. - */ - protected function check() - { - if (!isset($this->master) || !$this->slaves) { - throw new \RuntimeException('Replication needs one master and at least one slave.'); - } - } - /** * Resets the connection state. */ @@ -156,8 +146,6 @@ class MasterSlaveReplication implements ReplicationInterface */ public function switchTo($connection) { - $this->check(); - if (!$connection instanceof NodeConnectionInterface) { $connection = $this->getConnectionById($connection); } @@ -242,9 +230,12 @@ class MasterSlaveReplication implements ReplicationInterface */ public function connect() { - if ($this->current === null) { - $this->check(); - $this->current = $this->pickSlave(); + if (!$this->current) { + if (!$this->current = $this->pickSlave()) { + if (!$this->current = $this->getMaster()) { + throw new ClientException("No available connection for replication"); + } + } } $this->current->connect(); diff --git a/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php b/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php index f0e8836c..ff702920 100644 --- a/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php +++ b/tests/Predis/Connection/Aggregate/MasterSlaveReplicationTest.php @@ -65,8 +65,8 @@ class MasterSlaveReplicationTest extends PredisTestCase /** * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. + * @expectedException \Predis\ClientException + * @expectedExceptionMessage No available connection for replication */ public function testThrowsExceptionOnEmptyReplication() { @@ -76,34 +76,8 @@ class MasterSlaveReplicationTest extends PredisTestCase /** * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. */ - public function testThrowsExceptionOnMissingMaster() - { - $replication = new MasterSlaveReplication(); - $replication->add($this->getMockConnection('tcp://host2?alias=slave1')); - - $replication->connect(); - } - - /** - * @group disconnected - * @expectedException \RuntimeException - * @expectedExceptionMessage Replication needs one master and at least one slave. - */ - public function testThrowsExceptionOnMissingSlave() - { - $replication = new MasterSlaveReplication(); - $replication->add($this->getMockConnection('tcp://host1?alias=master')); - - $replication->connect(); - } - - /** - * @group disconnected - */ - public function testConnectForcesConnectionToOneOfSlaves() + public function testConnectsToOneOfSlaves() { $master = $this->getMockConnection('tcp://host1?alias=master'); $master->expects($this->never())->method('connect'); @@ -118,6 +92,20 @@ class MasterSlaveReplicationTest extends PredisTestCase $replication->connect(); } + /** + * @group disconnected + */ + public function testConnectsToMasterOnMissingSlaves() + { + $master = $this->getMockConnection('tcp://host1?alias=master'); + + $replication = new MasterSlaveReplication(); + $replication->add($master); + + $replication->connect(); + $this->assertSame($master, $replication->getCurrent()); + } + /** * @group disconnected */