From cd09f0d0fbbbdf64a0b19e85d3a0f69606df3afb Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 31 Oct 2015 03:35:44 -0700 Subject: [PATCH] Make SqlBagOStuff respect WRITE_SYNC flag Also remove some pointless newlines in class. Change-Id: I75880c56b1eac63c9a0616ca10890904ce823a99 --- includes/objectcache/SqlBagOStuff.php | 44 ++++++++++++++++++--------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index 1f384c7d07..a6f2d377c3 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -29,37 +29,29 @@ class SqlBagOStuff extends BagOStuff { /** @var LoadBalancer */ protected $lb; - /** @var array */ protected $serverInfos; - /** @var array */ protected $serverNames; - /** @var int */ protected $numServers; - - /** @var array */ - protected $conns; - /** @var int */ protected $lastExpireAll = 0; - /** @var int */ protected $purgePeriod = 100; - /** @var int */ protected $shards = 1; - /** @var string */ protected $tableName = 'objectcache'; - /** @var bool */ protected $slaveOnly = false; + /** @var int */ + protected $syncTimeout = 3; + /** @var array */ + protected $conns; /** @var array UNIX timestamps */ protected $connFailureTimes = array(); - /** @var array Exceptions */ protected $connFailureErrors = array(); @@ -92,6 +84,7 @@ class SqlBagOStuff extends BagOStuff { * garbage collection logic of expired items. This only * makes sense if the primary DB is used and only if get() * calls will be used. This is used by ReplicatedBagOStuff. + * - syncTimeout: Max seconds to wait for slaves to catch up for WRITE_SYNC. * * @param array $params */ @@ -120,6 +113,9 @@ class SqlBagOStuff extends BagOStuff { if ( isset( $params['shards'] ) ) { $this->shards = intval( $params['shards'] ); } + if ( isset( $params['syncTimeout'] ) ) { + $this->syncTimeout = $params['syncTimeout']; + } $this->slaveOnly = !empty( $params['slaveOnly'] ); } @@ -349,7 +345,12 @@ class SqlBagOStuff extends BagOStuff { } public function set( $key, $value, $exptime = 0, $flags = 0 ) { - return $this->setMulti( array( $key => $value ), $exptime ); + $ok = $this->setMulti( array( $key => $value ), $exptime ); + if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) { + $ok = $ok && $this->waitForSlaves(); + } + + return $ok; } protected function cas( $casToken, $key, $value, $exptime = 0 ) { @@ -457,7 +458,12 @@ class SqlBagOStuff extends BagOStuff { throw new Exception( "Got invalid callback." ); } - return $this->mergeViaCas( $key, $callback, $exptime, $attempts ); + $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts ); + if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) { + $ok = $ok && $this->waitForSlaves(); + } + + return $ok; } /** @@ -729,4 +735,14 @@ class SqlBagOStuff extends BagOStuff { } } } + + protected function waitForSlaves() { + if ( !$this->serverInfos ) { + // Main LB is used; wait for any slaves to catch up + return wfWaitForSlaves( null, false, false, $this->syncTimeout ); + } else { + // Custom DB server list; probably doesn't use replication + return true; + } + } } -- 2.20.1