3 * Object caching using a SQL database.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Class to store objects in the database
29 class SqlBagOStuff
extends BagOStuff
{
39 var $lastExpireAll = 0;
40 var $purgePeriod = 100;
42 var $tableName = 'objectcache';
44 protected $connFailureTimes = array(); // UNIX timestamps
45 protected $connFailureErrors = array(); // exceptions
48 * Constructor. Parameters are:
49 * - server: A server info structure in the format required by each
50 * element in $wgDBServers.
52 * - servers: An array of server info structures describing a set of
53 * database servers to distribute keys to. If this is
54 * specified, the "server" option will be ignored.
56 * - purgePeriod: The average number of object cache requests in between
57 * garbage collection operations, where expired entries
58 * are removed from the database. Or in other words, the
59 * reciprocal of the probability of purging on any given
60 * request. If this is set to zero, purging will never be
63 * - tableName: The table name to use, default is "objectcache".
65 * - shards: The number of tables to use for data storage on each server.
66 * If this is more than 1, table names will be formed in the style
67 * objectcacheNNN where NNN is the shard index, between 0 and
68 * shards-1. The number of digits will be the minimum number
69 * required to hold the largest shard index. Data will be
70 * distributed across all tables by key hash. This is for
71 * MySQL bugs 61735 and 61736.
73 * @param $params array
75 public function __construct( $params ) {
76 if ( isset( $params['servers'] ) ) {
77 $this->serverInfos
= $params['servers'];
78 $this->numServers
= count( $this->serverInfos
);
79 $this->serverNames
= array();
80 foreach ( $this->serverInfos
as $i => $info ) {
81 $this->serverNames
[$i] = isset( $info['host'] ) ?
$info['host'] : "#$i";
83 } elseif ( isset( $params['server'] ) ) {
84 $this->serverInfos
= array( $params['server'] );
85 $this->numServers
= count( $this->serverInfos
);
87 $this->serverInfos
= false;
88 $this->numServers
= 1;
90 if ( isset( $params['purgePeriod'] ) ) {
91 $this->purgePeriod
= intval( $params['purgePeriod'] );
93 if ( isset( $params['tableName'] ) ) {
94 $this->tableName
= $params['tableName'];
96 if ( isset( $params['shards'] ) ) {
97 $this->shards
= intval( $params['shards'] );
102 * Get a connection to the specified database
104 * @param $serverIndex integer
105 * @return DatabaseBase
107 protected function getDB( $serverIndex ) {
108 global $wgDebugDBTransactions;
110 if ( !isset( $this->conns
[$serverIndex] ) ) {
111 if ( $serverIndex >= $this->numServers
) {
112 throw new MWException( __METHOD__
. ": Invalid server index \"$serverIndex\"" );
115 # Don't keep timing out trying to connect for each call if the DB is down
116 if ( isset( $this->connFailureErrors
[$serverIndex] )
117 && ( time() - $this->connFailureTimes
[$serverIndex] ) < 60 )
119 throw $this->connFailureErrors
[$serverIndex];
122 # If server connection info was given, use that
123 if ( $this->serverInfos
) {
124 if ( $wgDebugDBTransactions ) {
125 wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
127 $info = $this->serverInfos
[$serverIndex];
128 $type = isset( $info['type'] ) ?
$info['type'] : 'mysql';
129 $host = isset( $info['host'] ) ?
$info['host'] : '[unknown]';
130 wfDebug( __CLASS__
. ": connecting to $host\n" );
131 $db = DatabaseBase
::factory( $type, $info );
132 $db->clearFlag( DBO_TRX
);
135 * We must keep a separate connection to MySQL in order to avoid deadlocks
136 * However, SQLite has an opposite behavior. And PostgreSQL needs to know
137 * if we are in transaction or no
139 if ( wfGetDB( DB_MASTER
)->getType() == 'mysql' ) {
140 $this->lb
= wfGetLBFactory()->newMainLB();
141 $db = $this->lb
->getConnection( DB_MASTER
);
142 $db->clearFlag( DBO_TRX
); // auto-commit mode
144 $db = wfGetDB( DB_MASTER
);
147 if ( $wgDebugDBTransactions ) {
148 wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
150 $this->conns
[$serverIndex] = $db;
153 return $this->conns
[$serverIndex];
157 * Get the server index and table name for a given key
159 * @return Array: server index and table name
161 protected function getTableByKey( $key ) {
162 if ( $this->shards
> 1 ) {
163 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
164 $tableIndex = $hash %
$this->shards
;
168 if ( $this->numServers
> 1 ) {
169 $sortedServers = $this->serverNames
;
170 ArrayUtils
::consistentHashSort( $sortedServers, $key );
171 reset( $sortedServers );
172 $serverIndex = key( $sortedServers );
176 return array( $serverIndex, $this->getTableNameByShard( $tableIndex ) );
180 * Get the table name for a given shard index
184 protected function getTableNameByShard( $index ) {
185 if ( $this->shards
> 1 ) {
186 $decimals = strlen( $this->shards
- 1 );
187 return $this->tableName
.
188 sprintf( "%0{$decimals}d", $index );
190 return $this->tableName
;
196 * @param $casToken[optional] mixed
199 public function get( $key, &$casToken = null ) {
200 $values = $this->getMulti( array( $key ) );
201 if ( array_key_exists( $key, $values ) ) {
202 $casToken = $values[$key];
203 return $values[$key];
212 public function getMulti( array $keys ) {
213 $values = array(); // array of (key => value)
215 $keysByTable = array();
216 foreach ( $keys as $key ) {
217 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
218 $keysByTable[$serverIndex][$tableName][] = $key;
221 $this->garbageCollect(); // expire old entries if any
224 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
226 $db = $this->getDB( $serverIndex );
227 foreach ( $serverKeys as $tableName => $tableKeys ) {
228 $res = $db->select( $tableName,
229 array( 'keyname', 'value', 'exptime' ),
230 array( 'keyname' => $tableKeys ),
232 foreach ( $res as $row ) {
233 $row->serverIndex
= $serverIndex;
234 $row->tableName
= $tableName;
235 $dataRows[$row->keyname
] = $row;
238 } catch ( DBError
$e ) {
239 $this->handleReadError( $e, $serverIndex );
243 foreach ( $keys as $key ) {
244 if ( isset( $dataRows[$key] ) ) { // HIT?
245 $row = $dataRows[$key];
246 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
248 $db = $this->getDB( $row->serverIndex
);
249 if ( $this->isExpired( $db, $row->exptime
) ) { // MISS
250 $this->debug( "get: key has expired, deleting" );
251 $db->begin( __METHOD__
);
252 # Put the expiry time in the WHERE condition to avoid deleting a
253 # newly-inserted value
254 $db->delete( $row->tableName
,
255 array( 'keyname' => $key, 'exptime' => $row->exptime
),
257 $db->commit( __METHOD__
);
258 $values[$key] = false;
260 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value
) );
262 } catch ( DBQueryError
$e ) {
263 $this->handleWriteError( $e, $row->serverIndex
);
266 $values[$key] = false;
267 $this->debug( 'get: no matching rows' );
276 * @param $value mixed
277 * @param $exptime int
280 public function set( $key, $value, $exptime = 0 ) {
281 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
283 $db = $this->getDB( $serverIndex );
284 $exptime = intval( $exptime );
286 if ( $exptime < 0 ) {
290 if ( $exptime == 0 ) {
291 $encExpiry = $this->getMaxDateTime( $db );
293 if ( $exptime < 3.16e8
) { # ~10 years
297 $encExpiry = $db->timestamp( $exptime );
299 $db->begin( __METHOD__
);
300 // (bug 24425) use a replace if the db supports it instead of
301 // delete/insert to avoid clashes with conflicting keynames
307 'value' => $db->encodeBlob( $this->serialize( $value ) ),
308 'exptime' => $encExpiry
310 $db->commit( __METHOD__
);
311 } catch ( DBError
$e ) {
312 $this->handleWriteError( $e, $serverIndex );
320 * @param $casToken mixed
322 * @param $value mixed
323 * @param $exptime int
326 public function cas( $casToken, $key, $value, $exptime = 0 ) {
327 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
329 $db = $this->getDB( $serverIndex );
330 $exptime = intval( $exptime );
332 if ( $exptime < 0 ) {
336 if ( $exptime == 0 ) {
337 $encExpiry = $this->getMaxDateTime( $db );
339 if ( $exptime < 3.16e8
) { # ~10 years
342 $encExpiry = $db->timestamp( $exptime );
344 $db->begin( __METHOD__
);
345 // (bug 24425) use a replace if the db supports it instead of
346 // delete/insert to avoid clashes with conflicting keynames
351 'value' => $db->encodeBlob( $this->serialize( $value ) ),
352 'exptime' => $encExpiry
356 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
360 $db->commit( __METHOD__
);
361 } catch ( DBQueryError
$e ) {
362 $this->handleWriteError( $e, $serverIndex );
367 return (bool) $db->affectedRows();
375 public function delete( $key, $time = 0 ) {
376 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
378 $db = $this->getDB( $serverIndex );
379 $db->begin( __METHOD__
);
382 array( 'keyname' => $key ),
384 $db->commit( __METHOD__
);
385 } catch ( DBError
$e ) {
386 $this->handleWriteError( $e, $serverIndex );
398 public function incr( $key, $step = 1 ) {
399 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
401 $db = $this->getDB( $serverIndex );
402 $step = intval( $step );
403 $db->begin( __METHOD__
);
404 $row = $db->selectRow(
406 array( 'value', 'exptime' ),
407 array( 'keyname' => $key ),
409 array( 'FOR UPDATE' ) );
410 if ( $row === false ) {
412 $db->commit( __METHOD__
);
416 $db->delete( $tableName, array( 'keyname' => $key ), __METHOD__
);
417 if ( $this->isExpired( $db, $row->exptime
) ) {
418 // Expired, do not reinsert
419 $db->commit( __METHOD__
);
424 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
425 $newValue = $oldValue +
$step;
426 $db->insert( $tableName,
429 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
430 'exptime' => $row->exptime
431 ), __METHOD__
, 'IGNORE' );
433 if ( $db->affectedRows() == 0 ) {
434 // Race condition. See bug 28611
437 $db->commit( __METHOD__
);
438 } catch ( DBError
$e ) {
439 $this->handleWriteError( $e, $serverIndex );
447 * @param $exptime string
450 protected function isExpired( $db, $exptime ) {
451 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX
, $exptime ) < time();
457 protected function getMaxDateTime( $db ) {
458 if ( time() > 0x7fffffff ) {
459 return $db->timestamp( 1 << 62 );
461 return $db->timestamp( 0x7fffffff );
465 protected function garbageCollect() {
466 if ( !$this->purgePeriod
) {
470 // Only purge on one in every $this->purgePeriod requests.
471 if ( $this->purgePeriod
!== 1 && mt_rand( 0, $this->purgePeriod
- 1 ) ) {
475 // Avoid repeating the delete within a few seconds
476 if ( $now > ( $this->lastExpireAll +
1 ) ) {
477 $this->lastExpireAll
= $now;
482 public function expireAll() {
483 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
487 * Delete objects from the database which expire before a certain date.
488 * @param $timestamp string
489 * @param $progressCallback bool|callback
492 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
493 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
495 $db = $this->getDB( $serverIndex );
496 $dbTimestamp = $db->timestamp( $timestamp );
497 $totalSeconds = false;
498 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
499 for ( $i = 0; $i < $this->shards
; $i++
) {
503 if ( $maxExpTime !== false ) {
504 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
507 $this->getTableNameByShard( $i ),
508 array( 'keyname', 'exptime' ),
511 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
512 if ( !$rows->numRows() ) {
516 $row = $rows->current();
517 $minExpTime = $row->exptime
;
518 if ( $totalSeconds === false ) {
519 $totalSeconds = wfTimestamp( TS_UNIX
, $timestamp )
520 - wfTimestamp( TS_UNIX
, $minExpTime );
522 foreach ( $rows as $row ) {
523 $keys[] = $row->keyname
;
524 $maxExpTime = $row->exptime
;
527 $db->begin( __METHOD__
);
529 $this->getTableNameByShard( $i ),
531 'exptime >= ' . $db->addQuotes( $minExpTime ),
532 'exptime < ' . $db->addQuotes( $dbTimestamp ),
536 $db->commit( __METHOD__
);
538 if ( $progressCallback ) {
539 if ( intval( $totalSeconds ) === 0 ) {
542 $remainingSeconds = wfTimestamp( TS_UNIX
, $timestamp )
543 - wfTimestamp( TS_UNIX
, $maxExpTime );
544 if ( $remainingSeconds > $totalSeconds ) {
545 $totalSeconds = $remainingSeconds;
547 $percent = ( $i +
$remainingSeconds / $totalSeconds )
548 / $this->shards
* 100;
550 $percent = ( $percent / $this->numServers
)
551 +
( $serverIndex / $this->numServers
* 100 );
552 call_user_func( $progressCallback, $percent );
556 } catch ( DBError
$e ) {
557 $this->handleWriteError( $e, $serverIndex );
564 public function deleteAll() {
565 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
567 $db = $this->getDB( $serverIndex );
568 for ( $i = 0; $i < $this->shards
; $i++
) {
569 $db->begin( __METHOD__
);
570 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__
);
571 $db->commit( __METHOD__
);
573 } catch ( DBError
$e ) {
574 $this->handleWriteError( $e, $serverIndex );
582 * Serialize an object and, if possible, compress the representation.
583 * On typical message and page data, this can provide a 3X decrease
584 * in storage requirements.
589 protected function serialize( &$data ) {
590 $serial = serialize( $data );
592 if ( function_exists( 'gzdeflate' ) ) {
593 return gzdeflate( $serial );
600 * Unserialize and, if necessary, decompress an object.
601 * @param $serial string
604 protected function unserialize( $serial ) {
605 if ( function_exists( 'gzinflate' ) ) {
606 wfSuppressWarnings();
607 $decomp = gzinflate( $serial );
610 if ( false !== $decomp ) {
615 $ret = unserialize( $serial );
621 * Handle a DBError which occurred during a read operation.
623 protected function handleReadError( DBError
$exception, $serverIndex ) {
624 if ( $exception instanceof DBConnectionError
) {
625 $this->markServerDown( $exception, $serverIndex );
627 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
628 if ( $exception instanceof DBConnectionError
) {
629 wfDebug( __METHOD__
. ": ignoring connection error\n" );
631 wfDebug( __METHOD__
. ": ignoring query error\n" );
636 * Handle a DBQueryError which occurred during a write operation.
638 protected function handleWriteError( DBError
$exception, $serverIndex ) {
639 if ( $exception instanceof DBConnectionError
) {
640 $this->markServerDown( $exception, $serverIndex );
642 if ( $exception->db
&& $exception->db
->wasReadOnlyError() ) {
644 $exception->db
->rollback( __METHOD__
);
645 } catch ( DBError
$e ) {}
647 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
648 if ( $exception instanceof DBConnectionError
) {
649 wfDebug( __METHOD__
. ": ignoring connection error\n" );
651 wfDebug( __METHOD__
. ": ignoring query error\n" );
656 * Mark a server down due to a DBConnectionError exception
658 protected function markServerDown( $exception, $serverIndex ) {
659 if ( isset( $this->connFailureTimes
[$serverIndex] ) ) {
660 if ( time() - $this->connFailureTimes
[$serverIndex] >= 60 ) {
661 unset( $this->connFailureTimes
[$serverIndex] );
662 unset( $this->connFailureErrors
[$serverIndex] );
664 wfDebug( __METHOD__
. ": Server #$serverIndex already down\n" );
669 wfDebug( __METHOD__
. ": Server #$serverIndex down until " . ( $now +
60 ) . "\n" );
670 $this->connFailureTimes
[$serverIndex] = $now;
671 $this->connFailureErrors
[$serverIndex] = $exception;
675 * Create shard tables. For use from eval.php.
677 public function createTables() {
678 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
679 $db = $this->getDB( $serverIndex );
680 if ( $db->getType() !== 'mysql'
681 ||
version_compare( $db->getServerVersion(), '4.1.0', '<' ) )
683 throw new MWException( __METHOD__
. ' is not supported on this DB server' );
686 for ( $i = 0; $i < $this->shards
; $i++
) {
687 $db->begin( __METHOD__
);
689 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
690 ' LIKE ' . $db->tableName( 'objectcache' ),
692 $db->commit( __METHOD__
);
699 * Backwards compatibility alias
701 class MediaWikiBagOStuff
extends SqlBagOStuff
{ }