Skip to content

Commit

Permalink
Merge pull request #47 from gregorg/fix_pg_reconnections
Browse files Browse the repository at this point in the history
Fix pg reconnections
  • Loading branch information
gregorg authored May 23, 2018
2 parents 597303a + 3643c2e commit d33f8d3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
14 changes: 7 additions & 7 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function closeConnections()
abstract public function theServerAcceptMoreConnections($n);

/**
* @Given a master\/slaves connection :connectionName with :slaveCount slaves
* @Given a master-slaves connection :connectionName with :slaveCount slaves
*/
public function aMasterSlavesConnectionWithSlaves($connectionName, $slaveCount)
{
Expand All @@ -95,10 +95,10 @@ public function aMasterSlavesConnectionWithSlaves($connectionName, $slaveCount)
}

/**
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retry
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retries
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retry with username :username
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retries with username :username
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retry
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retries
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retry with username :username
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retries with username :username
*/
public function aRetryMasterSlavesConnectionWithSlavesLimitedToRetriesWithusername($connectionName, $slaveCount, $n, $username = null)
{
Expand Down Expand Up @@ -249,8 +249,8 @@ public function aRetryConnectionLimitedToRetryWithusername($connectionName, $n,
}

/**
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retry with db :db
* @Given a retry master\/slaves connection :connectionName with :slaveCount slaves limited to :n retry with db :db and username :username
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retry with db :db
* @Given a retry master-slaves connection :connectionName with :slaveCount slaves limited to :n retry with db :db and username :username
*/
public function aRetryMasterSlavesConnectionWithSlavesLimitedToRetryWithDbAndUsername($connectionName, $slaveCount, $n, $db, $username = null)
{
Expand Down
4 changes: 2 additions & 2 deletions features/master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Feature: Master / Slaves

Background:
Given a master/slaves connection "conn" with 3 slaves
Given a master-slaves connection "conn" with 3 slaves

Scenario: Read request on slave
When I query "SELECT 1" on "conn"
Expand All @@ -21,7 +21,7 @@ Feature: Master / Slaves
And "conn" is on slave

Scenario: Connect on master when there is no slaves
Given a master/slaves connection "connMaster" with no slaves
Given a master-slaves connection "connMaster" with no slaves
When I query "SELECT 1" on "connMaster"
Then the last query succeeded on "connMaster"
And "connMaster" is on master
Expand Down
33 changes: 18 additions & 15 deletions features/retry-master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
Feature: Retry Master/Slaves

Scenario: Get database
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry
Then I can get the database name on "conn"

Scenario: ACCESS_DENIED_ERROR restart on another slave
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry with username "nobody"
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry with username "nobody"
When I query "SELECT 1" on "conn"
Then the last query failed on "conn"
And the last error should be "ACCESS_DENIED" on "conn"
Expand All @@ -15,15 +15,15 @@ Feature: Retry Master/Slaves

@skip-mysql-replic
Scenario: ER_BAD_DB_ERROR restart on another slave
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry with db "unknown_db"
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry with db "unknown_db"
When I query "SELECT 1" on "conn"
Then the last query failed on "conn"
And the last error should be "DBACCESS_DENIED" on "conn"
And "conn" retry limit should be 0
And "conn" should have 1 slave
@skip-pdo-pgsql
Scenario: database has Gone Away
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry
And requests are forced on master for "conn"
And database has Gone Away on "conn"
When I query "SELECT 1" on "conn"
Expand All @@ -33,7 +33,7 @@ Feature: Retry Master/Slaves
And "conn" should have 2 slaves

Scenario: ACCESS_DENIED_ERROR does not restart on master
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry with username "nobody"
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry with username "nobody"
And requests are forced on master for "conn"
When I query "SELECT 1" on "conn"
Then the last query failed on "conn"
Expand All @@ -43,7 +43,7 @@ Feature: Retry Master/Slaves

@skip-mysql-replic
Scenario: ER_BAD_DB_ERROR does not restart on master
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry with db "unknown_db" and username "root"
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry with db "unknown_db" and username "root"
And requests are forced on master for "conn"
When I query "SELECT 1" on "conn"
Then the last query failed on "conn"
Expand All @@ -53,7 +53,7 @@ Feature: Retry Master/Slaves

@skip-travis @skip-mysqli
Scenario: Replication is stopped on slave and query restart on another slave
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
Given a retry master-slaves connection "conn" with 2 slaves limited to 1 retry
And slave replication is stopped on "conn"
When I query "SELECT 1" on "conn"
Then the last query succeeded on "conn"
Expand All @@ -62,12 +62,15 @@ Feature: Retry Master/Slaves

@skip-mysqli @skip-mysql-replic @skip-travis @skip-pdo-mysql
Scenario: Connect only once
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
When I query "SELECT 1" on "conn"
Then I query "SELECT 2" on "conn"
Then I query "SELECT 3" on "conn"
Then I query "SELECT 4" on "conn"
And "conn" retry limit should be 1
And "conn" should have 2 slaves
And there is 1 connections established on "conn"
Given a retry master-slaves connection "connce" with 2 slaves limited to 1 retry
When I query "SELECT 1" on "connce"
Then I query "SELECT 2" on "connce"
Then I query "SELECT 3" on "connce"
Then I query "SELECT 4" on "connce"
Then I query "SELECT 5" on "connce"
Then I query "SELECT 6" on "connce"
Then I query "SELECT 7" on "connce"
And "connce" retry limit should be 1
And "connce" should have 2 slaves
And there is 2 connections established on "connce"

4 changes: 3 additions & 1 deletion src/Driver/Connection/MasterSlavesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ public function connectToSlave()
if ($this->currentConnectionParams !== null // if connection exists
&& (
$this->currentConnectionParams !== $this->master // and is not a master
|| $this->currentConnectionParams === $this->currentSlave // or is current slave
|| count($this->slaves) === 0 // or there is no slave
|| ($this->currentSlave !== null && $this->currentConnectionParams === $this->slaves[$this->currentSlave]) // or is current slave
)
) {
return;
}
$this->close();
$this->currentConnectionParams = null;
$this->currentSlave = null;
$this->wrappedConnection = null;
Expand Down

0 comments on commit d33f8d3

Please sign in to comment.