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
{
30 /** @var LoadBalancer */
34 protected $serverInfos;
37 protected $serverNames;
40 protected $numServers;
46 protected $lastExpireAll = 0;
49 protected $purgePeriod = 100;
52 protected $shards = 1;
55 protected $tableName = 'objectcache';
58 protected $slaveOnly = false;
60 /** @var array UNIX timestamps */
61 protected $connFailureTimes = array();
63 /** @var array Exceptions */
64 protected $connFailureErrors = array();
67 * Constructor. Parameters are:
68 * - server: A server info structure in the format required by each
69 * element in $wgDBServers.
71 * - servers: An array of server info structures describing a set of
72 * database servers to distribute keys to. If this is
73 * specified, the "server" option will be ignored.
75 * - purgePeriod: The average number of object cache requests in between
76 * garbage collection operations, where expired entries
77 * are removed from the database. Or in other words, the
78 * reciprocal of the probability of purging on any given
79 * request. If this is set to zero, purging will never be
82 * - tableName: The table name to use, default is "objectcache".
84 * - shards: The number of tables to use for data storage on each server.
85 * If this is more than 1, table names will be formed in the style
86 * objectcacheNNN where NNN is the shard index, between 0 and
87 * shards-1. The number of digits will be the minimum number
88 * required to hold the largest shard index. Data will be
89 * distributed across all tables by key hash. This is for
90 * MySQL bugs 61735 and 61736.
91 * - slaveOnly: Whether to only use slave DBs and avoid triggering
92 * garbage collection logic of expired items. This only
93 * makes sense if the primary DB is used and only if get()
94 * calls will be used. This is used by ReplicatedBagOStuff.
96 * @param array $params
98 public function __construct( $params ) {
99 parent
::__construct( $params );
100 if ( isset( $params['servers'] ) ) {
101 $this->serverInfos
= $params['servers'];
102 $this->numServers
= count( $this->serverInfos
);
103 $this->serverNames
= array();
104 foreach ( $this->serverInfos
as $i => $info ) {
105 $this->serverNames
[$i] = isset( $info['host'] ) ?
$info['host'] : "#$i";
107 } elseif ( isset( $params['server'] ) ) {
108 $this->serverInfos
= array( $params['server'] );
109 $this->numServers
= count( $this->serverInfos
);
111 $this->serverInfos
= false;
112 $this->numServers
= 1;
114 if ( isset( $params['purgePeriod'] ) ) {
115 $this->purgePeriod
= intval( $params['purgePeriod'] );
117 if ( isset( $params['tableName'] ) ) {
118 $this->tableName
= $params['tableName'];
120 if ( isset( $params['shards'] ) ) {
121 $this->shards
= intval( $params['shards'] );
123 $this->slaveOnly
= !empty( $params['slaveOnly'] );
127 * Get a connection to the specified database
129 * @param int $serverIndex
130 * @return DatabaseBase
131 * @throws MWException
133 protected function getDB( $serverIndex ) {
134 if ( !isset( $this->conns
[$serverIndex] ) ) {
135 if ( $serverIndex >= $this->numServers
) {
136 throw new MWException( __METHOD__
. ": Invalid server index \"$serverIndex\"" );
139 # Don't keep timing out trying to connect for each call if the DB is down
140 if ( isset( $this->connFailureErrors
[$serverIndex] )
141 && ( time() - $this->connFailureTimes
[$serverIndex] ) < 60
143 throw $this->connFailureErrors
[$serverIndex];
146 # If server connection info was given, use that
147 if ( $this->serverInfos
) {
148 $info = $this->serverInfos
[$serverIndex];
149 $type = isset( $info['type'] ) ?
$info['type'] : 'mysql';
150 $host = isset( $info['host'] ) ?
$info['host'] : '[unknown]';
151 $this->logger
->debug( __CLASS__
. ": connecting to $host" );
152 // Use a blank trx profiler to ignore expections as this is a cache
153 $info['trxProfiler'] = new TransactionProfiler();
154 $db = DatabaseBase
::factory( $type, $info );
155 $db->clearFlag( DBO_TRX
);
158 * We must keep a separate connection to MySQL in order to avoid deadlocks
159 * However, SQLite has an opposite behavior. And PostgreSQL needs to know
160 * if we are in transaction or no
162 $index = $this->slaveOnly ? DB_SLAVE
: DB_MASTER
;
163 if ( wfGetDB( $index )->getType() == 'mysql' ) {
164 $this->lb
= wfGetLBFactory()->newMainLB();
165 $db = $this->lb
->getConnection( $index );
166 $db->clearFlag( DBO_TRX
); // auto-commit mode
168 $db = wfGetDB( $index );
171 $this->logger
->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
172 $this->conns
[$serverIndex] = $db;
175 return $this->conns
[$serverIndex];
179 * Get the server index and table name for a given key
181 * @return array Server index and table name
183 protected function getTableByKey( $key ) {
184 if ( $this->shards
> 1 ) {
185 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
186 $tableIndex = $hash %
$this->shards
;
190 if ( $this->numServers
> 1 ) {
191 $sortedServers = $this->serverNames
;
192 ArrayUtils
::consistentHashSort( $sortedServers, $key );
193 reset( $sortedServers );
194 $serverIndex = key( $sortedServers );
198 return array( $serverIndex, $this->getTableNameByShard( $tableIndex ) );
202 * Get the table name for a given shard index
206 protected function getTableNameByShard( $index ) {
207 if ( $this->shards
> 1 ) {
208 $decimals = strlen( $this->shards
- 1 );
209 return $this->tableName
.
210 sprintf( "%0{$decimals}d", $index );
212 return $this->tableName
;
216 public function get( $key, &$casToken = null, $flags = 0 ) {
217 $values = $this->getMulti( array( $key ) );
218 if ( array_key_exists( $key, $values ) ) {
219 $casToken = $values[$key];
220 return $values[$key];
225 public function getMulti( array $keys, $flags = 0 ) {
226 $values = array(); // array of (key => value)
228 $keysByTable = array();
229 foreach ( $keys as $key ) {
230 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
231 $keysByTable[$serverIndex][$tableName][] = $key;
234 $this->garbageCollect(); // expire old entries if any
237 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
239 $db = $this->getDB( $serverIndex );
240 foreach ( $serverKeys as $tableName => $tableKeys ) {
241 $res = $db->select( $tableName,
242 array( 'keyname', 'value', 'exptime' ),
243 array( 'keyname' => $tableKeys ),
245 // Approximate write-on-the-fly BagOStuff API via blocking.
246 // This approximation fails if a ROLLBACK happens (which is rare).
247 // We do not want to flush the TRX as that can break callers.
248 $db->trxLevel() ?
array( 'LOCK IN SHARE MODE' ) : array()
250 if ( $res === false ) {
253 foreach ( $res as $row ) {
254 $row->serverIndex
= $serverIndex;
255 $row->tableName
= $tableName;
256 $dataRows[$row->keyname
] = $row;
259 } catch ( DBError
$e ) {
260 $this->handleReadError( $e, $serverIndex );
264 foreach ( $keys as $key ) {
265 if ( isset( $dataRows[$key] ) ) { // HIT?
266 $row = $dataRows[$key];
267 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
269 $db = $this->getDB( $row->serverIndex
);
270 if ( $this->isExpired( $db, $row->exptime
) ) { // MISS
271 $this->debug( "get: key has expired" );
273 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value
) );
275 } catch ( DBQueryError
$e ) {
276 $this->handleWriteError( $e, $row->serverIndex
);
279 $this->debug( 'get: no matching rows' );
291 public function setMulti( array $data, $expiry = 0 ) {
292 $keysByTable = array();
293 foreach ( $data as $key => $value ) {
294 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
295 $keysByTable[$serverIndex][$tableName][] = $key;
298 $this->garbageCollect(); // expire old entries if any
301 $exptime = (int)$expiry;
302 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
304 $db = $this->getDB( $serverIndex );
305 } catch ( DBError
$e ) {
306 $this->handleWriteError( $e, $serverIndex );
311 if ( $exptime < 0 ) {
315 if ( $exptime == 0 ) {
316 $encExpiry = $this->getMaxDateTime( $db );
318 $exptime = $this->convertExpiry( $exptime );
319 $encExpiry = $db->timestamp( $exptime );
321 foreach ( $serverKeys as $tableName => $tableKeys ) {
323 foreach ( $tableKeys as $key ) {
326 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
327 'exptime' => $encExpiry,
338 } catch ( DBError
$e ) {
339 $this->handleWriteError( $e, $serverIndex );
352 * @param mixed $value
353 * @param int $exptime
356 public function set( $key, $value, $exptime = 0 ) {
357 return $this->setMulti( array( $key => $value ), $exptime );
361 * @param mixed $casToken
363 * @param mixed $value
364 * @param int $exptime
367 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
368 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
370 $db = $this->getDB( $serverIndex );
371 $exptime = intval( $exptime );
373 if ( $exptime < 0 ) {
377 if ( $exptime == 0 ) {
378 $encExpiry = $this->getMaxDateTime( $db );
380 $exptime = $this->convertExpiry( $exptime );
381 $encExpiry = $db->timestamp( $exptime );
383 // (bug 24425) use a replace if the db supports it instead of
384 // delete/insert to avoid clashes with conflicting keynames
389 'value' => $db->encodeBlob( $this->serialize( $value ) ),
390 'exptime' => $encExpiry
394 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
398 } catch ( DBQueryError
$e ) {
399 $this->handleWriteError( $e, $serverIndex );
404 return (bool)$db->affectedRows();
411 public function delete( $key ) {
412 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
414 $db = $this->getDB( $serverIndex );
417 array( 'keyname' => $key ),
419 } catch ( DBError
$e ) {
420 $this->handleWriteError( $e, $serverIndex );
432 public function incr( $key, $step = 1 ) {
433 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
435 $db = $this->getDB( $serverIndex );
436 $step = intval( $step );
437 $row = $db->selectRow(
439 array( 'value', 'exptime' ),
440 array( 'keyname' => $key ),
442 array( 'FOR UPDATE' ) );
443 if ( $row === false ) {
448 $db->delete( $tableName, array( 'keyname' => $key ), __METHOD__
);
449 if ( $this->isExpired( $db, $row->exptime
) ) {
450 // Expired, do not reinsert
455 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
456 $newValue = $oldValue +
$step;
457 $db->insert( $tableName,
460 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
461 'exptime' => $row->exptime
462 ), __METHOD__
, 'IGNORE' );
464 if ( $db->affectedRows() == 0 ) {
465 // Race condition. See bug 28611
468 } catch ( DBError
$e ) {
469 $this->handleWriteError( $e, $serverIndex );
476 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
477 if ( !is_callable( $callback ) ) {
478 throw new Exception( "Got invalid callback." );
481 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
485 * @param DatabaseBase $db
486 * @param string $exptime
489 protected function isExpired( $db, $exptime ) {
490 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX
, $exptime ) < time();
494 * @param DatabaseBase $db
497 protected function getMaxDateTime( $db ) {
498 if ( time() > 0x7fffffff ) {
499 return $db->timestamp( 1 << 62 );
501 return $db->timestamp( 0x7fffffff );
505 protected function garbageCollect() {
506 if ( !$this->purgePeriod ||
$this->slaveOnly
) {
510 // Only purge on one in every $this->purgePeriod requests.
511 if ( $this->purgePeriod
!== 1 && mt_rand( 0, $this->purgePeriod
- 1 ) ) {
515 // Avoid repeating the delete within a few seconds
516 if ( $now > ( $this->lastExpireAll +
1 ) ) {
517 $this->lastExpireAll
= $now;
522 public function expireAll() {
523 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
527 * Delete objects from the database which expire before a certain date.
528 * @param string $timestamp
529 * @param bool|callable $progressCallback
532 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
533 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
535 $db = $this->getDB( $serverIndex );
536 $dbTimestamp = $db->timestamp( $timestamp );
537 $totalSeconds = false;
538 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
539 for ( $i = 0; $i < $this->shards
; $i++
) {
543 if ( $maxExpTime !== false ) {
544 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
547 $this->getTableNameByShard( $i ),
548 array( 'keyname', 'exptime' ),
551 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
552 if ( $rows === false ||
!$rows->numRows() ) {
556 $row = $rows->current();
557 $minExpTime = $row->exptime
;
558 if ( $totalSeconds === false ) {
559 $totalSeconds = wfTimestamp( TS_UNIX
, $timestamp )
560 - wfTimestamp( TS_UNIX
, $minExpTime );
562 foreach ( $rows as $row ) {
563 $keys[] = $row->keyname
;
564 $maxExpTime = $row->exptime
;
568 $this->getTableNameByShard( $i ),
570 'exptime >= ' . $db->addQuotes( $minExpTime ),
571 'exptime < ' . $db->addQuotes( $dbTimestamp ),
576 if ( $progressCallback ) {
577 if ( intval( $totalSeconds ) === 0 ) {
580 $remainingSeconds = wfTimestamp( TS_UNIX
, $timestamp )
581 - wfTimestamp( TS_UNIX
, $maxExpTime );
582 if ( $remainingSeconds > $totalSeconds ) {
583 $totalSeconds = $remainingSeconds;
585 $processedSeconds = $totalSeconds - $remainingSeconds;
586 $percent = ( $i +
$processedSeconds / $totalSeconds )
587 / $this->shards
* 100;
589 $percent = ( $percent / $this->numServers
)
590 +
( $serverIndex / $this->numServers
* 100 );
591 call_user_func( $progressCallback, $percent );
595 } catch ( DBError
$e ) {
596 $this->handleWriteError( $e, $serverIndex );
604 * Delete content of shard tables in every server.
605 * Return true if the operation is successful, false otherwise.
608 public function deleteAll() {
609 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
611 $db = $this->getDB( $serverIndex );
612 for ( $i = 0; $i < $this->shards
; $i++
) {
613 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__
);
615 } catch ( DBError
$e ) {
616 $this->handleWriteError( $e, $serverIndex );
624 * Serialize an object and, if possible, compress the representation.
625 * On typical message and page data, this can provide a 3X decrease
626 * in storage requirements.
631 protected function serialize( &$data ) {
632 $serial = serialize( $data );
634 if ( function_exists( 'gzdeflate' ) ) {
635 return gzdeflate( $serial );
642 * Unserialize and, if necessary, decompress an object.
643 * @param string $serial
646 protected function unserialize( $serial ) {
647 if ( function_exists( 'gzinflate' ) ) {
648 MediaWiki\
suppressWarnings();
649 $decomp = gzinflate( $serial );
650 MediaWiki\restoreWarnings
();
652 if ( false !== $decomp ) {
657 $ret = unserialize( $serial );
663 * Handle a DBError which occurred during a read operation.
665 * @param DBError $exception
666 * @param int $serverIndex
668 protected function handleReadError( DBError
$exception, $serverIndex ) {
669 if ( $exception instanceof DBConnectionError
) {
670 $this->markServerDown( $exception, $serverIndex );
672 $this->logger
->error( "DBError: {$exception->getMessage()}" );
673 if ( $exception instanceof DBConnectionError
) {
674 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
675 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
677 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
678 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
683 * Handle a DBQueryError which occurred during a write operation.
685 * @param DBError $exception
686 * @param int $serverIndex
688 protected function handleWriteError( DBError
$exception, $serverIndex ) {
689 if ( $exception instanceof DBConnectionError
) {
690 $this->markServerDown( $exception, $serverIndex );
692 if ( $exception->db
&& $exception->db
->wasReadOnlyError() ) {
693 if ( $exception->db
->trxLevel() ) {
695 $exception->db
->rollback( __METHOD__
);
696 } catch ( DBError
$e ) {
701 $this->logger
->error( "DBError: {$exception->getMessage()}" );
702 if ( $exception instanceof DBConnectionError
) {
703 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
704 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
706 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
707 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
712 * Mark a server down due to a DBConnectionError exception
714 * @param DBError $exception
715 * @param int $serverIndex
717 protected function markServerDown( $exception, $serverIndex ) {
718 unset( $this->conns
[$serverIndex] ); // bug T103435
720 if ( isset( $this->connFailureTimes
[$serverIndex] ) ) {
721 if ( time() - $this->connFailureTimes
[$serverIndex] >= 60 ) {
722 unset( $this->connFailureTimes
[$serverIndex] );
723 unset( $this->connFailureErrors
[$serverIndex] );
725 $this->logger
->debug( __METHOD__
. ": Server #$serverIndex already down" );
730 $this->logger
->info( __METHOD__
. ": Server #$serverIndex down until " . ( $now +
60 ) );
731 $this->connFailureTimes
[$serverIndex] = $now;
732 $this->connFailureErrors
[$serverIndex] = $exception;
736 * Create shard tables. For use from eval.php.
738 public function createTables() {
739 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
740 $db = $this->getDB( $serverIndex );
741 if ( $db->getType() !== 'mysql' ) {
742 throw new MWException( __METHOD__
. ' is not supported on this DB server' );
745 for ( $i = 0; $i < $this->shards
; $i++
) {
747 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
748 ' LIKE ' . $db->tableName( 'objectcache' ),