summaryrefslogtreecommitdiff
path: root/includes/objectcache/SqlBagOStuff.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/objectcache/SqlBagOStuff.php')
-rw-r--r--includes/objectcache/SqlBagOStuff.php222
1 files changed, 154 insertions, 68 deletions
diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php
index acf27036..58720790 100644
--- a/includes/objectcache/SqlBagOStuff.php
+++ b/includes/objectcache/SqlBagOStuff.php
@@ -27,22 +27,37 @@
* @ingroup Cache
*/
class SqlBagOStuff extends BagOStuff {
- /**
- * @var LoadBalancer
- */
- var $lb;
+ /** @var LoadBalancer */
+ protected $lb;
+
+ 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 $serverInfos;
- var $serverNames;
- var $numServers;
- var $conns;
- var $lastExpireAll = 0;
- var $purgePeriod = 100;
- var $shards = 1;
- var $tableName = 'objectcache';
+ /** @var int */
+ protected $shards = 1;
- protected $connFailureTimes = array(); // UNIX timestamps
- protected $connFailureErrors = array(); // exceptions
+ /** @var string */
+ protected $tableName = 'objectcache';
+
+ /** @var array UNIX timestamps */
+ protected $connFailureTimes = array();
+
+ /** @var array Exceptions */
+ protected $connFailureErrors = array();
/**
* Constructor. Parameters are:
@@ -70,7 +85,7 @@ class SqlBagOStuff extends BagOStuff {
* distributed across all tables by key hash. This is for
* MySQL bugs 61735 and 61736.
*
- * @param $params array
+ * @param array $params
*/
public function __construct( $params ) {
if ( isset( $params['servers'] ) ) {
@@ -101,7 +116,7 @@ class SqlBagOStuff extends BagOStuff {
/**
* Get a connection to the specified database
*
- * @param $serverIndex integer
+ * @param int $serverIndex
* @return DatabaseBase
*/
protected function getDB( $serverIndex ) {
@@ -114,8 +129,8 @@ class SqlBagOStuff extends BagOStuff {
# Don't keep timing out trying to connect for each call if the DB is down
if ( isset( $this->connFailureErrors[$serverIndex] )
- && ( time() - $this->connFailureTimes[$serverIndex] ) < 60 )
- {
+ && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
+ ) {
throw $this->connFailureErrors[$serverIndex];
}
@@ -155,8 +170,8 @@ class SqlBagOStuff extends BagOStuff {
/**
* Get the server index and table name for a given key
- * @param $key string
- * @return Array: server index and table name
+ * @param string $key
+ * @return array Server index and table name
*/
protected function getTableByKey( $key ) {
if ( $this->shards > 1 ) {
@@ -178,7 +193,7 @@ class SqlBagOStuff extends BagOStuff {
/**
* Get the table name for a given shard index
- * @param $index int
+ * @param int $index
* @return string
*/
protected function getTableNameByShard( $index ) {
@@ -192,8 +207,8 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $key string
- * @param $casToken[optional] mixed
+ * @param string $key
+ * @param mixed $casToken [optional]
* @return mixed
*/
public function get( $key, &$casToken = null ) {
@@ -206,8 +221,8 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $keys array
- * @return Array
+ * @param array $keys
+ * @return array
*/
public function getMulti( array $keys ) {
$values = array(); // array of (key => value)
@@ -228,7 +243,15 @@ class SqlBagOStuff extends BagOStuff {
$res = $db->select( $tableName,
array( 'keyname', 'value', 'exptime' ),
array( 'keyname' => $tableKeys ),
- __METHOD__ );
+ __METHOD__,
+ // Approximate write-on-the-fly BagOStuff API via blocking.
+ // This approximation fails if a ROLLBACK happens (which is rare).
+ // We do not want to flush the TRX as that can break callers.
+ $db->trxLevel() ? array( 'LOCK IN SHARE MODE' ) : array()
+ );
+ if ( $res === false ) {
+ continue;
+ }
foreach ( $res as $row ) {
$row->serverIndex = $serverIndex;
$row->tableName = $tableName;
@@ -248,14 +271,11 @@ class SqlBagOStuff extends BagOStuff {
$db = $this->getDB( $row->serverIndex );
if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
$this->debug( "get: key has expired, deleting" );
- $db->begin( __METHOD__ );
# Put the expiry time in the WHERE condition to avoid deleting a
# newly-inserted value
$db->delete( $row->tableName,
array( 'keyname' => $key, 'exptime' => $row->exptime ),
__METHOD__ );
- $db->commit( __METHOD__ );
- $values[$key] = false;
} else { // HIT
$values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
}
@@ -263,7 +283,6 @@ class SqlBagOStuff extends BagOStuff {
$this->handleWriteError( $e, $row->serverIndex );
}
} else { // MISS
- $values[$key] = false;
$this->debug( 'get: no matching rows' );
}
}
@@ -272,9 +291,77 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $key string
- * @param $value mixed
- * @param $exptime int
+ * @param array $data
+ * @param int $expiry
+ * @return bool
+ */
+ public function setMulti( array $data, $expiry = 0 ) {
+ $keysByTable = array();
+ foreach ( $data as $key => $value ) {
+ list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
+ $keysByTable[$serverIndex][$tableName][] = $key;
+ }
+
+ $this->garbageCollect(); // expire old entries if any
+
+ $result = true;
+ $exptime = (int)$expiry;
+ foreach ( $keysByTable as $serverIndex => $serverKeys ) {
+ try {
+ $db = $this->getDB( $serverIndex );
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $serverIndex );
+ $result = false;
+ continue;
+ }
+
+ if ( $exptime < 0 ) {
+ $exptime = 0;
+ }
+
+ if ( $exptime == 0 ) {
+ $encExpiry = $this->getMaxDateTime( $db );
+ } else {
+ if ( $exptime < 3.16e8 ) { # ~10 years
+ $exptime += time();
+ }
+ $encExpiry = $db->timestamp( $exptime );
+ }
+ foreach ( $serverKeys as $tableName => $tableKeys ) {
+ $rows = array();
+ foreach ( $tableKeys as $key ) {
+ $rows[] = array(
+ 'keyname' => $key,
+ 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
+ 'exptime' => $encExpiry,
+ );
+ }
+
+ try {
+ $db->replace(
+ $tableName,
+ array( 'keyname' ),
+ $rows,
+ __METHOD__
+ );
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $serverIndex );
+ $result = false;
+ }
+
+ }
+
+ }
+
+ return $result;
+ }
+
+
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ * @param int $exptime
* @return bool
*/
public function set( $key, $value, $exptime = 0 ) {
@@ -296,7 +383,6 @@ class SqlBagOStuff extends BagOStuff {
$encExpiry = $db->timestamp( $exptime );
}
- $db->begin( __METHOD__ );
// (bug 24425) use a replace if the db supports it instead of
// delete/insert to avoid clashes with conflicting keynames
$db->replace(
@@ -307,7 +393,6 @@ class SqlBagOStuff extends BagOStuff {
'value' => $db->encodeBlob( $this->serialize( $value ) ),
'exptime' => $encExpiry
), __METHOD__ );
- $db->commit( __METHOD__ );
} catch ( DBError $e ) {
$this->handleWriteError( $e, $serverIndex );
return false;
@@ -317,10 +402,10 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $casToken mixed
- * @param $key string
- * @param $value mixed
- * @param $exptime int
+ * @param mixed $casToken
+ * @param string $key
+ * @param mixed $value
+ * @param int $exptime
* @return bool
*/
public function cas( $casToken, $key, $value, $exptime = 0 ) {
@@ -341,7 +426,6 @@ class SqlBagOStuff extends BagOStuff {
}
$encExpiry = $db->timestamp( $exptime );
}
- $db->begin( __METHOD__ );
// (bug 24425) use a replace if the db supports it instead of
// delete/insert to avoid clashes with conflicting keynames
$db->update(
@@ -357,7 +441,6 @@ class SqlBagOStuff extends BagOStuff {
),
__METHOD__
);
- $db->commit( __METHOD__ );
} catch ( DBQueryError $e ) {
$this->handleWriteError( $e, $serverIndex );
@@ -368,20 +451,18 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $key string
- * @param $time int
+ * @param string $key
+ * @param int $time
* @return bool
*/
public function delete( $key, $time = 0 ) {
list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
try {
$db = $this->getDB( $serverIndex );
- $db->begin( __METHOD__ );
$db->delete(
$tableName,
array( 'keyname' => $key ),
__METHOD__ );
- $db->commit( __METHOD__ );
} catch ( DBError $e ) {
$this->handleWriteError( $e, $serverIndex );
return false;
@@ -391,8 +472,8 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $key string
- * @param $step int
+ * @param string $key
+ * @param int $step
* @return int|null
*/
public function incr( $key, $step = 1 ) {
@@ -400,7 +481,6 @@ class SqlBagOStuff extends BagOStuff {
try {
$db = $this->getDB( $serverIndex );
$step = intval( $step );
- $db->begin( __METHOD__ );
$row = $db->selectRow(
$tableName,
array( 'value', 'exptime' ),
@@ -409,14 +489,12 @@ class SqlBagOStuff extends BagOStuff {
array( 'FOR UPDATE' ) );
if ( $row === false ) {
// Missing
- $db->commit( __METHOD__ );
return null;
}
$db->delete( $tableName, array( 'keyname' => $key ), __METHOD__ );
if ( $this->isExpired( $db, $row->exptime ) ) {
// Expired, do not reinsert
- $db->commit( __METHOD__ );
return null;
}
@@ -434,7 +512,6 @@ class SqlBagOStuff extends BagOStuff {
// Race condition. See bug 28611
$newValue = null;
}
- $db->commit( __METHOD__ );
} catch ( DBError $e ) {
$this->handleWriteError( $e, $serverIndex );
return null;
@@ -444,7 +521,8 @@ class SqlBagOStuff extends BagOStuff {
}
/**
- * @param $exptime string
+ * @param DatabaseBase $db
+ * @param string $exptime
* @return bool
*/
protected function isExpired( $db, $exptime ) {
@@ -452,6 +530,7 @@ class SqlBagOStuff extends BagOStuff {
}
/**
+ * @param DatabaseBase $db
* @return string
*/
protected function getMaxDateTime( $db ) {
@@ -485,8 +564,8 @@ class SqlBagOStuff extends BagOStuff {
/**
* Delete objects from the database which expire before a certain date.
- * @param $timestamp string
- * @param $progressCallback bool|callback
+ * @param string $timestamp
+ * @param bool|callable $progressCallback
* @return bool
*/
public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
@@ -509,7 +588,7 @@ class SqlBagOStuff extends BagOStuff {
$conds,
__METHOD__,
array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
- if ( !$rows->numRows() ) {
+ if ( $rows === false || !$rows->numRows() ) {
break;
}
$keys = array();
@@ -524,7 +603,6 @@ class SqlBagOStuff extends BagOStuff {
$maxExpTime = $row->exptime;
}
- $db->begin( __METHOD__ );
$db->delete(
$this->getTableNameByShard( $i ),
array(
@@ -533,7 +611,6 @@ class SqlBagOStuff extends BagOStuff {
'keyname' => $keys
),
__METHOD__ );
- $db->commit( __METHOD__ );
if ( $progressCallback ) {
if ( intval( $totalSeconds ) === 0 ) {
@@ -566,9 +643,7 @@ class SqlBagOStuff extends BagOStuff {
try {
$db = $this->getDB( $serverIndex );
for ( $i = 0; $i < $this->shards; $i++ ) {
- $db->begin( __METHOD__ );
$db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
- $db->commit( __METHOD__ );
}
} catch ( DBError $e ) {
$this->handleWriteError( $e, $serverIndex );
@@ -583,7 +658,7 @@ class SqlBagOStuff extends BagOStuff {
* On typical message and page data, this can provide a 3X decrease
* in storage requirements.
*
- * @param $data mixed
+ * @param mixed $data
* @return string
*/
protected function serialize( &$data ) {
@@ -598,7 +673,7 @@ class SqlBagOStuff extends BagOStuff {
/**
* Unserialize and, if necessary, decompress an object.
- * @param $serial string
+ * @param string $serial
* @return mixed
*/
protected function unserialize( $serial ) {
@@ -619,6 +694,9 @@ class SqlBagOStuff extends BagOStuff {
/**
* Handle a DBError which occurred during a read operation.
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function handleReadError( DBError $exception, $serverIndex ) {
if ( $exception instanceof DBConnectionError ) {
@@ -626,14 +704,19 @@ class SqlBagOStuff extends BagOStuff {
}
wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
+ $this->setLastError( BagOStuff::ERR_UNREACHABLE );
wfDebug( __METHOD__ . ": ignoring connection error\n" );
} else {
+ $this->setLastError( BagOStuff::ERR_UNEXPECTED );
wfDebug( __METHOD__ . ": ignoring query error\n" );
}
}
/**
* Handle a DBQueryError which occurred during a write operation.
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function handleWriteError( DBError $exception, $serverIndex ) {
if ( $exception instanceof DBConnectionError ) {
@@ -642,18 +725,24 @@ class SqlBagOStuff extends BagOStuff {
if ( $exception->db && $exception->db->wasReadOnlyError() ) {
try {
$exception->db->rollback( __METHOD__ );
- } catch ( DBError $e ) {}
+ } catch ( DBError $e ) {
+ }
}
wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
+ $this->setLastError( BagOStuff::ERR_UNREACHABLE );
wfDebug( __METHOD__ . ": ignoring connection error\n" );
} else {
+ $this->setLastError( BagOStuff::ERR_UNEXPECTED );
wfDebug( __METHOD__ . ": ignoring query error\n" );
}
}
/**
* Mark a server down due to a DBConnectionError exception
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function markServerDown( $exception, $serverIndex ) {
if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
@@ -677,19 +766,15 @@ class SqlBagOStuff extends BagOStuff {
public function createTables() {
for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
$db = $this->getDB( $serverIndex );
- if ( $db->getType() !== 'mysql'
- || version_compare( $db->getServerVersion(), '4.1.0', '<' ) )
- {
+ if ( $db->getType() !== 'mysql' ) {
throw new MWException( __METHOD__ . ' is not supported on this DB server' );
}
for ( $i = 0; $i < $this->shards; $i++ ) {
- $db->begin( __METHOD__ );
$db->query(
'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
' LIKE ' . $db->tableName( 'objectcache' ),
__METHOD__ );
- $db->commit( __METHOD__ );
}
}
}
@@ -698,4 +783,5 @@ class SqlBagOStuff extends BagOStuff {
/**
* Backwards compatibility alias
*/
-class MediaWikiBagOStuff extends SqlBagOStuff { }
+class MediaWikiBagOStuff extends SqlBagOStuff {
+}