Merge "Made text profiler output a bit less noisy by bumping "thresholdMs""
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 26 Jun 2015 07:43:41 +0000 (07:43 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 26 Jun 2015 07:43:41 +0000 (07:43 +0000)
includes/db/DatabaseMysql.php
includes/db/DatabaseMysqlBase.php
includes/db/DatabaseMysqli.php
includes/db/LoadBalancer.php

index 823d9b6..b69efac 100644 (file)
@@ -33,10 +33,12 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @return resource False on error
         */
        protected function doQuery( $sql ) {
+               $conn = $this->getBindingHandle();
+
                if ( $this->bufferResults() ) {
-                       $ret = mysql_query( $sql, $this->mConn );
+                       $ret = mysql_query( $sql, $conn );
                } else {
-                       $ret = mysql_unbuffered_query( $sql, $this->mConn );
+                       $ret = mysql_unbuffered_query( $sql, $conn );
                }
 
                return $ret;
@@ -48,8 +50,7 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @throws DBConnectionError
         */
        protected function mysqlConnect( $realServer ) {
-               # Fail now
-               # Otherwise we get a suppressed fatal error, which is very hard to track down
+               # Avoid a suppressed fatal error, which is very hard to track down
                if ( !extension_loaded( 'mysql' ) ) {
                        throw new DBConnectionError(
                                $this,
@@ -93,8 +94,10 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @return bool
         */
        protected function mysqlSetCharset( $charset ) {
+               $conn = $this->getBindingHandle();
+
                if ( function_exists( 'mysql_set_charset' ) ) {
-                       return mysql_set_charset( $charset, $this->mConn );
+                       return mysql_set_charset( $charset, $conn );
                } else {
                        return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
                }
@@ -104,14 +107,18 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @return bool
         */
        protected function closeConnection() {
-               return mysql_close( $this->mConn );
+               $conn = $this->getBindingHandle();
+
+               return mysql_close( $conn );
        }
 
        /**
         * @return int
         */
        function insertId() {
-               return mysql_insert_id( $this->mConn );
+               $conn = $this->getBindingHandle();
+
+               return mysql_insert_id( $conn );
        }
 
        /**
@@ -129,7 +136,9 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @return int
         */
        function affectedRows() {
-               return mysql_affected_rows( $this->mConn );
+               $conn = $this->getBindingHandle();
+
+               return mysql_affected_rows( $conn );
        }
 
        /**
@@ -137,9 +146,11 @@ class DatabaseMysql extends DatabaseMysqlBase {
         * @return bool
         */
        function selectDB( $db ) {
+               $conn = $this->getBindingHandle();
+
                $this->mDBname = $db;
 
-               return mysql_select_db( $db, $this->mConn );
+               return mysql_select_db( $db, $conn );
        }
 
        protected function mysqlFreeResult( $res ) {
@@ -183,10 +194,14 @@ class DatabaseMysql extends DatabaseMysqlBase {
        }
 
        protected function mysqlRealEscapeString( $s ) {
-               return mysql_real_escape_string( $s, $this->mConn );
+               $conn = $this->getBindingHandle();
+
+               return mysql_real_escape_string( $s, $conn );
        }
 
        protected function mysqlPing() {
-               return mysql_ping( $this->mConn );
+               $conn = $this->getBindingHandle();
+
+               return mysql_ping( $conn );
        }
 }
index a189648..4085fa9 100644 (file)
@@ -586,9 +586,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
        function ping() {
                $ping = $this->mysqlPing();
                if ( $ping ) {
+                       // Connection was good or lost but reconnected...
+                       // @note: mysqlnd (php 5.6+) does not support this (PHP bug 52561)
                        return true;
                }
 
+               // Try a full disconnect/reconnect cycle if ping() failed
                $this->closeConnection();
                $this->mOpened = false;
                $this->mConn = false;
@@ -1058,6 +1061,28 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                        ( $this->lastErrno() == 1290 && strpos( $this->lastError(), '--read-only' ) !== false );
        }
 
+       /**
+        * Get the underlying binding handle, mConn
+        *
+        * Makes sure that mConn is set (disconnects and ping() failure can unset it).
+        * This catches broken callers than catch and ignore disconnection exceptions.
+        * Unlike checking isOpen(), this is safe to call inside of open().
+        *
+        * @return resource|object
+        * @throws DBUnexpectedError
+        * @since 1.26
+        */
+       protected function getBindingHandle() {
+               if ( !$this->mConn ) {
+                       throw new DBUnexpectedError(
+                               $this,
+                               'DB connection was already closed or the connection dropped.'
+                       );
+               }
+
+               return $this->mConn;
+       }
+
        /**
         * @param string $oldName
         * @param string $newName
index d2b5ecb..8b51d81 100644 (file)
@@ -34,10 +34,12 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return resource
         */
        protected function doQuery( $sql ) {
+               $conn = $this->getBindingHandle();
+
                if ( $this->bufferResults() ) {
-                       $ret = $this->mConn->query( $sql );
+                       $ret = $conn->query( $sql );
                } else {
-                       $ret = $this->mConn->query( $sql, MYSQLI_USE_RESULT );
+                       $ret = $conn->query( $sql, MYSQLI_USE_RESULT );
                }
 
                return $ret;
@@ -50,8 +52,8 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         */
        protected function mysqlConnect( $realServer ) {
                global $wgDBmysql5;
-               # Fail now
-               # Otherwise we get a suppressed fatal error, which is very hard to track down
+
+               # Avoid suppressed fatal error, which is very hard to track down
                if ( !function_exists( 'mysqli_init' ) ) {
                        throw new DBConnectionError( $this, "MySQLi functions missing,"
                                . " have you compiled PHP with the --with-mysqli option?\n" );
@@ -116,8 +118,10 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return bool
         */
        protected function mysqlSetCharset( $charset ) {
-               if ( method_exists( $this->mConn, 'set_charset' ) ) {
-                       return $this->mConn->set_charset( $charset );
+               $conn = $this->getBindingHandle();
+
+               if ( method_exists( $conn, 'set_charset' ) ) {
+                       return $conn->set_charset( $charset );
                } else {
                        return $this->query( 'SET NAMES ' . $charset, __METHOD__ );
                }
@@ -127,14 +131,18 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return bool
         */
        protected function closeConnection() {
-               return $this->mConn->close();
+               $conn = $this->getBindingHandle();
+
+               return $conn->close();
        }
 
        /**
         * @return int
         */
        function insertId() {
-               return (int)$this->mConn->insert_id;
+               $conn = $this->getBindingHandle();
+
+               return (int)$conn->insert_id;
        }
 
        /**
@@ -152,7 +160,9 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return int
         */
        function affectedRows() {
-               return $this->mConn->affected_rows;
+               $conn = $this->getBindingHandle();
+
+               return $conn->affected_rows;
        }
 
        /**
@@ -160,9 +170,11 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return bool
         */
        function selectDB( $db ) {
+               $conn = $this->getBindingHandle();
+
                $this->mDBname = $db;
 
-               return $this->mConn->select_db( $db );
+               return $conn->select_db( $db );
        }
 
        /**
@@ -289,11 +301,15 @@ class DatabaseMysqli extends DatabaseMysqlBase {
         * @return string
         */
        protected function mysqlRealEscapeString( $s ) {
-               return $this->mConn->real_escape_string( $s );
+               $conn = $this->getBindingHandle();
+
+               return $conn->real_escape_string( $s );
        }
 
        protected function mysqlPing() {
-               return $this->mConn->ping();
+               $conn = $this->getBindingHandle();
+
+               return $conn->ping();
        }
 
        /**
index 069185b..1281187 100644 (file)
@@ -650,10 +650,7 @@ class LoadBalancer {
        public function openConnection( $i, $wiki = false ) {
                if ( $wiki !== false ) {
                        $conn = $this->openForeignConnection( $i, $wiki );
-
-                       return $conn;
-               }
-               if ( isset( $this->mConns['local'][$i][0] ) ) {
+               } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
                        $conn = $this->mConns['local'][$i][0];
                } else {
                        $server = $this->mServers[$i];
@@ -670,6 +667,15 @@ class LoadBalancer {
                        }
                }
 
+               if ( $conn && !$conn->isOpen() ) {
+                       // Connection was made but later unrecoverably lost for some reason.
+                       // Do not return a handle that will just throw exceptions on use,
+                       // but let the calling code (e.g. getReaderIndex) try another server.
+                       // See DatabaseMyslBase::ping() for how this can happen.
+                       $this->mErrorConnection = $conn;
+                       $conn = false;
+               }
+
                return $conn;
        }