Avoid "Unable to set value to APCBagOStuff" exceptions
[lhc/web/wiklou.git] / includes / libs / objectcache / BagOStuff.php
index 95c1bc7..b9be43d 100644 (file)
@@ -42,7 +42,7 @@ use Psr\Log\NullLogger;
  *
  * @ingroup Cache
  */
-abstract class BagOStuff implements LoggerAwareInterface {
+abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
        /** @var array[] Lock tracking */
        protected $locks = array();
 
@@ -67,6 +67,8 @@ abstract class BagOStuff implements LoggerAwareInterface {
        /** Bitfield constants for get()/getMulti() */
        const READ_LATEST = 1; // use latest data for replicated stores
        const READ_VERIFIED = 2; // promise that caller can tell when keys are stale
+       /** Bitfield constants for set()/merge() */
+       const WRITE_SYNC = 1; // synchronously write to all locations for replicated stores
 
        public function __construct( array $params = array() ) {
                if ( isset( $params['logger'] ) ) {
@@ -152,12 +154,13 @@ abstract class BagOStuff implements LoggerAwareInterface {
        abstract protected function doGet( $key, $flags = 0 );
 
        /**
-        * @note: This method is only needed if cas() is not is used for merge()
+        * @note: This method is only needed if merge() uses mergeViaCas()
         *
         * @param string $key
         * @param mixed $casToken
         * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
         * @return mixed Returns false on failure and if the item does not exist
+        * @throws Exception
         */
        protected function getWithToken( $key, &$casToken, $flags = 0 ) {
                throw new Exception( __METHOD__ . ' not implemented.' );
@@ -169,9 +172,10 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @param string $key
         * @param mixed $value
         * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
+        * @param int $flags Bitfield of BagOStuff::WRITE_* constants
         * @return bool Success
         */
-       abstract public function set( $key, $value, $exptime = 0 );
+       abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
 
        /**
         * Delete an item
@@ -191,15 +195,16 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @param callable $callback Callback method to be executed
         * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
         * @param int $attempts The amount of times to attempt a merge in case of failure
+        * @param int $flags Bitfield of BagOStuff::WRITE_* constants
         * @return bool Success
         * @throws InvalidArgumentException
         */
-       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+       public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
                if ( !is_callable( $callback ) ) {
                        throw new InvalidArgumentException( "Got invalid callback." );
                }
 
-               return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
+               return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
        }
 
        /**
@@ -215,7 +220,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                do {
                        $this->clearLastError();
                        $casToken = null; // passed by reference
-                       $currentValue = $this->getWithToken( $key, $casToken, BagOStuff::READ_LATEST );
+                       $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
                        if ( $this->getLastError() ) {
                                return false; // don't spam retries (retry only on races)
                        }
@@ -262,15 +267,16 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @param callable $callback Callback method to be executed
         * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
         * @param int $attempts The amount of times to attempt a merge in case of failure
+        * @param int $flags Bitfield of BagOStuff::WRITE_* constants
         * @return bool Success
         */
-       protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10 ) {
+       protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
                if ( !$this->lock( $key, 6 ) ) {
                        return false;
                }
 
                $this->clearLastError();
-               $currentValue = $this->get( $key, BagOStuff::READ_LATEST );
+               $currentValue = $this->get( $key, self::READ_LATEST );
                if ( $this->getLastError() ) {
                        $success = false;
                } else {
@@ -279,7 +285,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                        if ( $value === false ) {
                                $success = true; // do nothing
                        } else {
-                               $success = $this->set( $key, $value, $exptime ); // set the new value
+                               $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
                        }
                }
 
@@ -313,7 +319,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
                        }
                }
 
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                $this->clearLastError();
                $timestamp = microtime( true ); // starting UNIX timestamp
@@ -322,7 +328,8 @@ abstract class BagOStuff implements LoggerAwareInterface {
                } elseif ( $this->getLastError() || $timeout <= 0 ) {
                        $locked = false; // network partition or non-blocking
                } else {
-                       $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
+                       // Estimate the RTT (us); use 1ms minimum for sanity
+                       $uRTT = max( 1e3, ceil( 1e6 * ( microtime( true ) - $timestamp ) ) );
                        $sleep = 2 * $uRTT; // rough time to do get()+set()
 
                        $attempts = 0; // failed attempts
@@ -382,7 +389,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @since 1.26
         */
        final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
-               $expiry = min( $expiry ?: INF, 86400 );
+               $expiry = min( $expiry ?: INF, self::TTL_DAY );
 
                if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
                        return null;
@@ -500,18 +507,27 @@ abstract class BagOStuff implements LoggerAwareInterface {
        /**
         * Increase stored value of $key by $value while preserving its TTL
         *
-        * This will create the key with value $init and TTL $ttl if not present
+        * This will create the key with value $init and TTL $ttl instead if not present
         *
         * @param string $key
         * @param int $ttl
         * @param int $value
         * @param int $init
-        * @return bool
+        * @return int|bool New value or false on failure
         * @since 1.24
         */
        public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
-               return $this->incr( $key, $value ) ||
-                       $this->add( $key, (int)$init, $ttl ) || $this->incr( $key, $value );
+               $newValue = $this->incr( $key, $value );
+               if ( $newValue === false ) {
+                       // No key set; initialize
+                       $newValue = $this->add( $key, (int)$init, $ttl ) ? $init : false;
+               }
+               if ( $newValue === false ) {
+                       // Raced out initializing; increment
+                       $newValue = $this->incr( $key, $value );
+               }
+
+               return $newValue;
        }
 
        /**
@@ -575,7 +591,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertExpiry( $exptime ) {
-               if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
+               if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
                        return time() + $exptime;
                } else {
                        return $exptime;
@@ -590,7 +606,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return int
         */
        protected function convertToRelative( $exptime ) {
-               if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
+               if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
                        $exptime -= time();
                        if ( $exptime <= 0 ) {
                                $exptime = 1;
@@ -620,7 +636,11 @@ abstract class BagOStuff implements LoggerAwareInterface {
         * @return string
         */
        public function makeKeyInternal( $keyspace, $args ) {
-               $key = $keyspace . ':' . implode( ':', $args );
+               $key = $keyspace;
+               foreach ( $args as $arg ) {
+                       $arg = str_replace( ':', '%3A', $arg );
+                       $key = $key . ':' . $arg;
+               }
                return strtr( $key, ' ', '_' );
        }