Merge "Remove PHP detection from entry points other than index.php"
[lhc/web/wiklou.git] / includes / db / DatabaseMysqlBase.php
index 61a0565..4f93419 100644 (file)
  * @since 1.22
  * @see Database
  */
-abstract class DatabaseMysqlBase extends DatabaseBase {
+abstract class DatabaseMysqlBase extends Database {
        /** @var MysqlMasterPos */
        protected $lastKnownSlavePos;
+       /** @var string Method to detect slave lag */
+       protected $lagDetectionMethod;
 
        /** @var string|null */
        private $serverVersion = null;
 
+       /**
+        * Additional $params include:
+        *   - lagDetectionMethod : set to one of (Seconds_Behind_Master,pt-heartbeat).
+        *                          pt-heartbeat assumes the table is at heartbeat.heartbeat
+        *                          and uses UTC timestamps in the heartbeat.ts column.
+        *                          (https://www.percona.com/doc/percona-toolkit/2.2/pt-heartbeat.html)
+        * @param array $params
+        */
+       function __construct( array $params ) {
+               parent::__construct( $params );
+
+               $this->lagDetectionMethod = isset( $params['lagDetectionMethod'] )
+                       ? $params['lagDetectionMethod']
+                       : 'Seconds_Behind_Master';
+       }
+
        /**
         * @return string
         */
@@ -549,6 +567,12 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
                return $sQuoted;
        }
 
+       /**
+        * @param string $s
+        * @return mixed
+        */
+       abstract protected function mysqlRealEscapeString( $s );
+
        /**
         * MySQL uses `backticks` for identifier quoting instead of the sql standard "double quotes".
         *
@@ -604,26 +628,73 @@ abstract class DatabaseMysqlBase extends DatabaseBase {
         * @return int
         */
        function getLag() {
-               return $this->getLagFromSlaveStatus();
+               if ( $this->lagDetectionMethod === 'pt-heartbeat' ) {
+                       return $this->getLagFromPtHeartbeat();
+               } else {
+                       return $this->getLagFromSlaveStatus();
+               }
        }
 
        /**
         * @return bool|int
         */
-       function getLagFromSlaveStatus() {
+       protected function getLagFromSlaveStatus() {
                $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
-               if ( !$res ) {
-                       return false;
+               $row = $res ? $res->fetchObject() : false;
+               if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
+                       return intval( $row->Seconds_Behind_Master );
                }
-               $row = $res->fetchObject();
-               if ( !$row ) {
-                       return false;
+
+               return false;
+       }
+
+       /**
+        * @return bool|float
+        */
+       protected function getLagFromPtHeartbeat() {
+               $key = wfMemcKey( 'mysql', 'master-server-id', $this->getServer() );
+               $masterId = intval( $this->srvCache->get( $key ) );
+               if ( !$masterId ) {
+                       $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
+                       $row = $res ? $res->fetchObject() : false;
+                       if ( $row && strval( $row->Master_Server_Id ) !== '' ) {
+                               $masterId = intval( $row->Master_Server_Id );
+                               $this->srvCache->set( $key, $masterId, 30 );
+                       }
                }
-               if ( strval( $row->Seconds_Behind_Master ) === '' ) {
+
+               if ( !$masterId ) {
                        return false;
-               } else {
-                       return intval( $row->Seconds_Behind_Master );
                }
+
+               $res = $this->query(
+                       "SELECT TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6)) AS Lag " .
+                       "FROM heartbeat.heartbeat WHERE server_id = $masterId"
+               );
+               $row = $res ? $res->fetchObject() : false;
+               if ( $row ) {
+                       return max( floatval( $row->Lag ) / 1e6, 0.0 );
+               }
+
+               return false;
+       }
+
+       public function getApproximateLagStatus() {
+               if ( $this->lagDetectionMethod === 'pt-heartbeat' ) {
+                       // Disable caching since this is fast enough and we don't wan't
+                       // to be *too* pessimistic by having both the cache TTL and the
+                       // pt-heartbeat interval count as lag in getSessionLagStatus()
+                       return parent::getApproximateLagStatus();
+               }
+
+               $key = wfGlobalCacheKey( 'mysql-lag', $this->getServer() );
+               $approxLag = $this->srvCache->get( $key );
+               if ( !$approxLag ) {
+                       $approxLag = parent::getApproximateLagStatus();
+                       $this->srvCache->set( $key, $approxLag, 1 );
+               }
+
+               return $approxLag;
        }
 
        /**
@@ -1198,7 +1269,7 @@ class MySQLField implements Field {
         * @return string
         */
        function tableName() {
-               return $this->tableName;
+               return $this->tablename;
        }
 
        /**
@@ -1283,6 +1354,21 @@ class MySQLMasterPos implements DBMasterPos {
                $this->asOfTime = microtime( true );
        }
 
+       function asOfTime() {
+               return $this->asOfTime;
+       }
+
+       function hasReached( DBMasterPos $pos ) {
+               if ( !( $pos instanceof self ) ) {
+                       throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ );
+               }
+
+               $thisPos = $this->getCoordinates();
+               $thatPos = $pos->getCoordinates();
+
+               return ( $thisPos && $thatPos && $thisPos >= $thatPos );
+       }
+
        function __toString() {
                // e.g db1034-bin.000976/843431247
                return "{$this->file}/{$this->pos}";
@@ -1299,15 +1385,4 @@ class MySQLMasterPos implements DBMasterPos {
 
                return false;
        }
-
-       function hasReached( MySQLMasterPos $pos ) {
-               $thisPos = $this->getCoordinates();
-               $thatPos = $pos->getCoordinates();
-
-               return ( $thisPos && $thatPos && $thisPos >= $thatPos );
-       }
-
-       function asOfTime() {
-               return $this->asOfTime;
-       }
 }