Support masking the WRITE_SYNC latency from ChronologyProtector
[lhc/web/wiklou.git] / includes / db / ChronologyProtector.php
index 2539b87..1cdb49f 100644 (file)
@@ -31,8 +31,12 @@ class ChronologyProtector {
 
        /** @var string Storage key name */
        protected $key;
-       /** @var array Map of (ip: <IP>, agent: <user-agent>) */
-       protected $client;
+       /** @var string Hash of client parameters */
+       protected $clientId;
+       /** @var float|null Minimum UNIX timestamp of 1+ expected startup positions */
+       protected $waitForPosTime;
+       /** @var int Max seconds to wait on positions to appear */
+       protected $waitForPosTimeout = self::POS_WAIT_TIMEOUT;
        /** @var bool Whether to no-op all method calls */
        protected $enabled = true;
        /** @var bool Whether to check and wait on positions */
@@ -44,19 +48,25 @@ class ChronologyProtector {
        protected $startupPositions = [];
        /** @var DBMasterPos[] Map of (DB master name => position) */
        protected $shutdownPositions = [];
+       /** @var float[] Map of (DB master name => 1) */
+       protected $shutdownTouchDBs = [];
+
+       /** @var integer Seconds to store positions */
+       const POSITION_TTL = 60;
+       /** @var integer Max time to wait for positions to appear */
+       const POS_WAIT_TIMEOUT = 5;
 
        /**
         * @param BagOStuff $store
         * @param array $client Map of (ip: <IP>, agent: <user-agent>)
+        * @param float $posTime UNIX timestamp
         * @since 1.27
         */
-       public function __construct( BagOStuff $store, array $client ) {
+       public function __construct( BagOStuff $store, array $client, $posTime = null ) {
                $this->store = $store;
-               $this->client = $client;
-               $this->key = $store->makeGlobalKey(
-                       'ChronologyProtector',
-                       md5( $client['ip'] . "\n" . $client['agent'] )
-               );
+               $this->clientId = md5( $client['ip'] . "\n" . $client['agent'] );
+               $this->key = $store->makeGlobalKey( __CLASS__, $this->clientId );
+               $this->waitForPosTime = $posTime;
        }
 
        /**
@@ -79,9 +89,9 @@ class ChronologyProtector {
         * Initialise a LoadBalancer to give it appropriate chronology protection.
         *
         * If the stash has a previous master position recorded, this will try to
-        * make sure that the next query to a slave of that master will see changes up
+        * make sure that the next query to a replica DB of that master will see changes up
         * to that position by delaying execution. The delay may timeout and allow stale
-        * data if no non-lagged slaves are available.
+        * data if no non-lagged replica DBs are available.
         *
         * @param LoadBalancer $lb
         * @return void
@@ -95,10 +105,8 @@ class ChronologyProtector {
 
                $masterName = $lb->getServerName( $lb->getWriterIndex() );
                if ( !empty( $this->startupPositions[$masterName] ) ) {
-                       $info = $lb->parentInfo();
                        $pos = $this->startupPositions[$masterName];
-                       wfDebugLog( 'replication', __METHOD__ .
-                               ": LB '" . $info['id'] . "' waiting for master pos $pos\n" );
+                       wfDebugLog( 'replication', __METHOD__ . ": LB for '$masterName' set to pos $pos\n" );
                        $lb->waitFor( $pos );
                }
        }
@@ -111,35 +119,50 @@ class ChronologyProtector {
         * @return void
         */
        public function shutdownLB( LoadBalancer $lb ) {
-               if ( !$this->enabled || $lb->getServerCount() <= 1 ) {
-                       return; // non-replicated setup or disabled
+               if ( !$this->enabled ) {
+                       return; // not enabled
+               } elseif ( !$lb->hasOrMadeRecentMasterChanges( INF ) ) {
+                       // Only save the position if writes have been done on the connection
+                       return;
                }
 
-               $info = $lb->parentInfo();
                $masterName = $lb->getServerName( $lb->getWriterIndex() );
-
-               // Only save the position if writes have been done on the connection
-               $db = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
-               if ( !$db || !$db->doneWrites() ) {
-                       wfDebugLog( 'replication', __METHOD__ . ": LB {$info['id']}, no writes done\n" );
-
-                       return; // nothing to do
+               if ( $lb->getServerCount() > 1 ) {
+                       $pos = $lb->getMasterPos();
+                       wfDebugLog( 'replication', __METHOD__ . ": LB for '$masterName' has pos $pos\n" );
+                       $this->shutdownPositions[$masterName] = $pos;
+               } else {
+                       wfDebugLog( 'replication', __METHOD__ . ": DB '$masterName' touched\n" );
                }
-
-               $pos = $db->getMasterPos();
-               wfDebugLog( 'replication', __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
-               $this->shutdownPositions[$masterName] = $pos;
+               $this->shutdownTouchDBs[$masterName] = 1;
        }
 
        /**
         * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
         * May commit chronology data to persistent storage.
         *
-        * @return array Empty on success; returns the (db name => position) map on failure
+        * @param callable|null $workCallback Work to do instead of waiting on syncing positions
+        * @param string $mode One of (sync, async); whether to wait on remote datacenters
+        * @return DBMasterPos[] Empty on success; returns the (db name => position) map on failure
         */
-       public function shutdown() {
-               if ( !$this->enabled || !count( $this->shutdownPositions ) ) {
-                       return true; // nothing to save
+       public function shutdown( callable $workCallback = null, $mode = 'sync' ) {
+               if ( !$this->enabled ) {
+                       return [];
+               }
+
+               $store = $this->store;
+               // Some callers might want to know if a user recently touched a DB.
+               // These writes do not need to block on all datacenters receiving them.
+               foreach ( $this->shutdownTouchDBs as $dbName => $unused ) {
+                       $store->set(
+                               $this->getTouchedKey( $this->store, $dbName ),
+                               microtime( true ),
+                               $store::TTL_DAY
+                       );
+               }
+
+               if ( !count( $this->shutdownPositions ) ) {
+                       return []; // nothing to save
                }
 
                wfDebugLog( 'replication',
@@ -150,29 +173,60 @@ class ChronologyProtector {
                // CP-protected writes should overwhemingly go to the master datacenter, so get DC-local
                // lock to merge the values. Use a DC-local get() and a synchronous all-DC set(). This
                // makes it possible for the BagOStuff class to write in parallel to all DCs with one RTT.
-               if ( $this->store->lock( $this->key, 3 ) ) {
-                       $ok = $this->store->set(
+               if ( $store->lock( $this->key, 3 ) ) {
+                       if ( $workCallback ) {
+                               // Let the store run the work before blocking on a replication sync barrier. By the
+                               // time it's done with the work, the barrier should be fast if replication caught up.
+                               $store->addBusyCallback( $workCallback );
+                       }
+                       $ok = $store->set(
                                $this->key,
-                               self::mergePositions( $this->store->get( $this->key ), $this->shutdownPositions ),
-                               BagOStuff::TTL_MINUTE,
-                               BagOStuff::WRITE_SYNC
+                               self::mergePositions( $store->get( $this->key ), $this->shutdownPositions ),
+                               self::POSITION_TTL,
+                               ( $mode === 'sync' ) ? $store::WRITE_SYNC : 0
                        );
-                       $this->store->unlock( $this->key );
+                       $store->unlock( $this->key );
                } else {
                        $ok = false;
                }
 
                if ( !$ok ) {
+                       $bouncedPositions = $this->shutdownPositions;
                        // Raced out too many times or stash is down
                        wfDebugLog( 'replication',
                                __METHOD__ . ": failed to save master pos for " .
                                implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
                        );
-
-                       return $this->shutdownPositions;
+               } elseif ( $mode === 'sync' &&
+                       $store->getQoS( $store::ATTR_SYNCWRITES ) < $store::QOS_SYNCWRITES_BE
+               ) {
+                       // Positions may not be in all datacenters, force LBFactory to play it safe
+                       wfDebugLog( 'replication',
+                               __METHOD__ . ": store does not report ability to sync replicas. " );
+                       $bouncedPositions = $this->shutdownPositions;
+               } else {
+                       $bouncedPositions = [];
                }
 
-               return [];
+               return $bouncedPositions;
+       }
+
+       /**
+        * @param string $dbName DB master name (e.g. "db1052")
+        * @return float|bool UNIX timestamp when client last touched the DB; false if not on record
+        * @since 1.28
+        */
+       public function getTouched( $dbName ) {
+               return $this->store->get( $this->getTouchedKey( $this->store, $dbName ) );
+       }
+
+       /**
+        * @param BagOStuff $store
+        * @param string $dbName
+        * @return string
+        */
+       private function getTouchedKey( BagOStuff $store, $dbName ) {
+               return $store->makeGlobalKey( __CLASS__, 'mtime', $this->clientId, $dbName );
        }
 
        /**
@@ -185,17 +239,59 @@ class ChronologyProtector {
 
                $this->initialized = true;
                if ( $this->wait ) {
-                       $data = $this->store->get( $this->key );
-                       $this->startupPositions = $data ? $data['positions'] : [];
+                       // If there is an expectation to see master positions with a certain min
+                       // timestamp, then block until they appear, or until a timeout is reached.
+                       if ( $this->waitForPosTime ) {
+                               $data = null;
+                               $loop = new WaitConditionLoop(
+                                       function () use ( &$data ) {
+                                               $data = $this->store->get( $this->key );
+
+                                               return ( self::minPosTime( $data ) >= $this->waitForPosTime )
+                                                       ? WaitConditionLoop::CONDITION_REACHED
+                                                       : WaitConditionLoop::CONDITION_CONTINUE;
+                                       },
+                                       $this->waitForPosTimeout
+                               );
+                               $result = $loop->invoke();
+                               $waitedMs = $loop->getLastWaitTime() * 1e3;
+
+                               if ( $result == $loop::CONDITION_REACHED ) {
+                                       $msg = "expected and found pos time {$this->waitForPosTime} ({$waitedMs}ms)";
+                               } else {
+                                       $msg = "expected but missed pos time {$this->waitForPosTime} ({$waitedMs}ms)";
+                               }
+                               wfDebugLog( 'replication', $msg );
+                       } else {
+                               $data = $this->store->get( $this->key );
+                       }
 
+                       $this->startupPositions = $data ? $data['positions'] : [];
                        wfDebugLog( 'replication', __METHOD__ . ": key is {$this->key} (read)\n" );
                } else {
                        $this->startupPositions = [];
-
                        wfDebugLog( 'replication', __METHOD__ . ": key is {$this->key} (unread)\n" );
                }
        }
 
+       /**
+        * @param array|bool $data
+        * @return float|null
+        */
+       private static function minPosTime( $data ) {
+               if ( !isset( $data['positions'] ) ) {
+                       return null;
+               }
+
+               $min = null;
+               foreach ( $data['positions'] as $pos ) {
+                       /** @var DBMasterPos $pos */
+                       $min = $min ? min( $pos->asOfTime(), $min ) : $pos->asOfTime();
+               }
+
+               return $min;
+       }
+
        /**
         * @param array|bool $curValue
         * @param DBMasterPos[] $shutdownPositions