Merge "Clean up postgres connection handling"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 19 Oct 2016 01:24:07 +0000 (01:24 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 19 Oct 2016 01:24:07 +0000 (01:24 +0000)
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/database/DatabasePostgres.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php

index d22ecd0..ba63432 100644 (file)
@@ -657,14 +657,22 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                if ( $this->htmlErrors !== false ) {
                        ini_set( 'html_errors', $this->htmlErrors );
                }
+
+               return $this->getLastPHPError();
+       }
+
+       /**
+        * @return string|bool Last PHP error for this DB (typically connection errors)
+        */
+       protected function getLastPHPError() {
                if ( $this->mPHPError ) {
                        $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
                        $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
 
                        return $error;
-               } else {
-                       return false;
                }
+
+               return false;
        }
 
        /**
index 84021a0..016b9cd 100644 (file)
@@ -92,10 +92,6 @@ class DatabasePostgres extends Database {
                        );
                }
 
-               if ( !strlen( $user ) ) { # e.g. the class is being loaded
-                       return null;
-               }
-
                $this->mServer = $server;
                $this->mUser = $user;
                $this->mPassword = $password;
@@ -121,7 +117,8 @@ class DatabasePostgres extends Database {
                $this->installErrorHandler();
 
                try {
-                       $this->mConn = pg_connect( $this->connectString );
+                       // Use new connections to let LoadBalancer/LBFactory handle reuse
+                       $this->mConn = pg_connect( $this->connectString, PGSQL_CONNECT_FORCE_NEW );
                } catch ( Exception $ex ) {
                        $this->restoreErrorHandler();
                        throw $ex;
@@ -130,10 +127,11 @@ class DatabasePostgres extends Database {
                $phpError = $this->restoreErrorHandler();
 
                if ( !$this->mConn ) {
-                       $this->queryLogger->debug( "DB connection error\n" );
                        $this->queryLogger->debug(
+                               "DB connection error\n" .
                                "Server: $server, Database: $dbName, User: $user, Password: " .
-                               substr( $password, 0, 3 ) . "...\n" );
+                               substr( $password, 0, 3 ) . "...\n"
+                       );
                        $this->queryLogger->debug( $this->lastError() . "\n" );
                        throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
                }
@@ -380,9 +378,9 @@ class DatabasePostgres extends Database {
                        } else {
                                return pg_last_error();
                        }
-               } else {
-                       return 'No database connection';
                }
+
+               return $this->getLastPHPError() ?: 'No database connection';
        }
 
        function lastErrno() {
index 8d30491..a86e1de 100644 (file)
@@ -553,7 +553,7 @@ class LoadBalancer implements ILoadBalancer {
                if ( $i == self::DB_REPLICA ) {
                        $this->mLastError = 'Unknown error'; // reset error string
                        # Try the general server pool if $groups are unavailable.
-                       $i = in_array( false, $groups, true )
+                       $i = ( $groups === [ false ] )
                                ? false // don't bother with this if that is what was tried above
                                : $this->getReaderIndex( false, $domain );
                        # Couldn't find a working server in getReaderIndex()?