Minor LoadBalancer cleanups
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 6 Oct 2016 16:52:17 +0000 (09:52 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 7 Oct 2016 02:07:27 +0000 (19:07 -0700)
* Close the master connection in safeWaitForMasterPos() if created.
* Remove unnecessary reuseConnection() calls.
* Add DOMAIN_ANY constant for readability.

Change-Id: I9e64a7405e0eedc50e7b93d05d65cf00d5544a77

includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
includes/site/DBSiteStore.php

index e5ed2f1..8854479 100644 (file)
@@ -78,6 +78,9 @@ interface ILoadBalancer {
        /** @var integer Request a master DB connection */
        const DB_MASTER = -2;
 
+       /** @var string Domain specifier when no specific database needs to be selected */
+       const DOMAIN_ANY = '';
+
        /**
         * Construct a manager of IDatabase connection objects
         *
index 73aa23c..33f3561 100644 (file)
@@ -105,10 +105,9 @@ class LoadBalancer implements ILoadBalancer {
 
        /** @var integer Warn when this many connection are held */
        const CONN_HELD_WARN_THRESHOLD = 10;
+
        /** @var integer Default 'max lag' when unspecified */
        const MAX_LAG_DEFAULT = 10;
-       /** @var integer Max time to wait for a replica DB to catch up (e.g. ChronologyProtector) */
-       const POS_WAIT_TIMEOUT = 10;
        /** @var integer Seconds to cache master server read-only status */
        const TTL_CACHE_READONLY = 5;
 
@@ -130,9 +129,7 @@ class LoadBalancer implements ILoadBalancer {
                        $this->localDomainIdAlias = $this->localDomain->getDatabase();
                }
 
-               $this->mWaitTimeout = isset( $params['waitTimeout'] )
-                       ? $params['waitTimeout']
-                       : self::POS_WAIT_TIMEOUT;
+               $this->mWaitTimeout = isset( $params['waitTimeout'] ) ? $params['waitTimeout'] : 10;
 
                $this->mReadIndex = -1;
                $this->mConns = [
@@ -477,7 +474,7 @@ class LoadBalancer implements ILoadBalancer {
 
                                return false;
                        } else {
-                               $conn = $this->openConnection( $index, '' );
+                               $conn = $this->openConnection( $index, self::DOMAIN_ANY );
                                if ( !$conn ) {
                                        $this->replLogger->warning( __METHOD__ . ": failed to connect to $server" );
 
@@ -1454,10 +1451,15 @@ class LoadBalancer implements ILoadBalancer {
                }
 
                if ( !$pos ) {
-                       // Get the current master position
-                       $dbw = $this->getConnection( self::DB_MASTER );
-                       $pos = $dbw->getMasterPos();
-                       $this->reuseConnection( $dbw );
+                       // Get the current master position, opening a connection if needed
+                       $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
+                       if ( $masterConn ) {
+                               $pos = $masterConn->getMasterPos();
+                       } else {
+                               $masterConn = $this->openConnection( $this->getWriterIndex(), self::DOMAIN_ANY );
+                               $pos = $masterConn->getMasterPos();
+                               $this->closeConnection( $masterConn );
+                       }
                }
 
                if ( $pos instanceof DBMasterPos ) {
index 432d5ce..e5247f2 100644 (file)
@@ -130,8 +130,6 @@ class DBSiteStore implements SiteStore {
                                $this->sites->setSite( $site );
                        }
                }
-
-               $this->dbLoadBalancer->reuseConnection( $dbr );
        }
 
        /**
@@ -249,8 +247,6 @@ class DBSiteStore implements SiteStore {
 
                $dbw->endAtomic( __METHOD__ );
 
-               $this->dbLoadBalancer->reuseConnection( $dbw );
-
                $this->reset();
 
                return $success;
@@ -280,8 +276,6 @@ class DBSiteStore implements SiteStore {
                $ok = $dbw->delete( 'site_identifiers', '*', __METHOD__ ) && $ok;
                $dbw->endAtomic( __METHOD__ );
 
-               $this->dbLoadBalancer->reuseConnection( $dbw );
-
                $this->reset();
 
                return $ok;