rdbms: avoid incorrect warnings in getLagFromPtHeartbeat()
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseMysqlBase.php
index 286d658..0a63bdc 100644 (file)
@@ -73,6 +73,9 @@ abstract class DatabaseMysqlBase extends Database {
        // Cache getServerId() for 24 hours
        const SERVER_ID_CACHE_TTL = 86400;
 
+       /** @var float Warn if lag estimates are made for transactions older than this many seconds */
+       const LAG_STALE_WARN_THRESHOLD = 0.100;
+
        /**
         * Additional $params include:
         *   - lagDetectionMethod : set to one of (Seconds_Behind_Master,pt-heartbeat).
@@ -749,6 +752,7 @@ abstract class DatabaseMysqlBase extends Database {
        protected function getLagFromSlaveStatus() {
                $res = $this->query( 'SHOW SLAVE STATUS', __METHOD__ );
                $row = $res ? $res->fetchObject() : false;
+               // If the server is not replicating, there will be no row
                if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
                        return intval( $row->Seconds_Behind_Master );
                }
@@ -762,6 +766,20 @@ abstract class DatabaseMysqlBase extends Database {
        protected function getLagFromPtHeartbeat() {
                $options = $this->lagDetectionOptions;
 
+               $staleness = $this->trxLevel
+                       ? microtime( true ) - $this->trxTimestamp()
+                       : 0;
+               if ( $staleness > self::LAG_STALE_WARN_THRESHOLD ) {
+                       // Avoid returning higher and higher lag value due to snapshot age
+                       // given that the isolation level will typically be REPEATABLE-READ
+                       $this->queryLogger->warning(
+                               "Using cached lag value for {db_server} due to active transaction",
+                               $this->getLogContext( [ 'method' => __METHOD__ ] )
+                       );
+
+                       return $this->getTransactionLagStatus()['lag'];
+               }
+
                if ( isset( $options['conds'] ) ) {
                        // Best method for multi-DC setups: use logical channel names
                        $data = $this->getHeartbeatData( $options['conds'] );
@@ -856,7 +874,8 @@ abstract class DatabaseMysqlBase extends Database {
                        // Note: this would use "TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6))" but the
                        // percision field is not supported in MySQL <= 5.5.
                        $res = $this->query(
-                               "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1"
+                               "SELECT ts FROM heartbeat.heartbeat WHERE $whereSQL ORDER BY ts DESC LIMIT 1",
+                               __METHOD__
                        );
                        $row = $res ? $res->fetchObject() : false;
                } finally {
@@ -901,6 +920,13 @@ abstract class DatabaseMysqlBase extends Database {
                        $rpos = $this->getReplicaPos();
                        $gtidsWait = $rpos ? MySQLMasterPos::getCommonDomainGTIDs( $pos, $rpos ) : [];
                        if ( !$gtidsWait ) {
+                               $this->queryLogger->error(
+                                       "No GTIDs with the same domain between master ($pos) and replica ($rpos)",
+                                       $this->getLogContext( [
+                                               'method' => __METHOD__,
+                                       ] )
+                               );
+
                                return -1; // $pos is from the wrong cluster?
                        }
                        // Wait on the GTID set (MariaDB only)