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
24 use \MediaWiki\MediaWikiServices
;
27 * Class to store objects in the database
31 class SqlBagOStuff
extends BagOStuff
{
32 /** @var array[] (server index => server config) */
33 protected $serverInfos;
34 /** @var string[] (server index => tag/host name) */
35 protected $serverTags;
37 protected $numServers;
39 protected $lastExpireAll = 0;
41 protected $purgePeriod = 100;
43 protected $shards = 1;
45 protected $tableName = 'objectcache';
47 protected $slaveOnly = false;
49 protected $syncTimeout = 3;
53 /** @var array UNIX timestamps */
54 protected $connFailureTimes = [];
55 /** @var array Exceptions */
56 protected $connFailureErrors = [];
59 * Constructor. Parameters are:
60 * - server: A server info structure in the format required by each
61 * element in $wgDBServers.
63 * - servers: An array of server info structures describing a set of database servers
64 * to distribute keys to. If this is specified, the "server" option will be
65 * ignored. If string keys are used, then they will be used for consistent
66 * hashing *instead* of the host name (from the server config). This is useful
67 * when a cluster is replicated to another site (with different host names)
68 * but each server has a corresponding replica in the other cluster.
70 * - purgePeriod: The average number of object cache requests in between
71 * garbage collection operations, where expired entries
72 * are removed from the database. Or in other words, the
73 * reciprocal of the probability of purging on any given
74 * request. If this is set to zero, purging will never be
77 * - tableName: The table name to use, default is "objectcache".
79 * - shards: The number of tables to use for data storage on each server.
80 * If this is more than 1, table names will be formed in the style
81 * objectcacheNNN where NNN is the shard index, between 0 and
82 * shards-1. The number of digits will be the minimum number
83 * required to hold the largest shard index. Data will be
84 * distributed across all tables by key hash. This is for
85 * MySQL bugs 61735 and 61736.
86 * - slaveOnly: Whether to only use slave DBs and avoid triggering
87 * garbage collection logic of expired items. This only
88 * makes sense if the primary DB is used and only if get()
89 * calls will be used. This is used by ReplicatedBagOStuff.
90 * - syncTimeout: Max seconds to wait for slaves to catch up for WRITE_SYNC.
92 * @param array $params
94 public function __construct( $params ) {
95 parent
::__construct( $params );
97 $this->attrMap
[self
::ATTR_EMULATION
] = self
::QOS_EMULATION_SQL
;
99 if ( isset( $params['servers'] ) ) {
100 $this->serverInfos
= [];
101 $this->serverTags
= [];
102 $this->numServers
= count( $params['servers'] );
104 foreach ( $params['servers'] as $tag => $info ) {
105 $this->serverInfos
[$index] = $info;
106 if ( is_string( $tag ) ) {
107 $this->serverTags
[$index] = $tag;
109 $this->serverTags
[$index] = isset( $info['host'] ) ?
$info['host'] : "#$index";
113 } elseif ( isset( $params['server'] ) ) {
114 $this->serverInfos
= [ $params['server'] ];
115 $this->numServers
= count( $this->serverInfos
);
117 $this->serverInfos
= false;
118 $this->numServers
= 1;
120 if ( isset( $params['purgePeriod'] ) ) {
121 $this->purgePeriod
= intval( $params['purgePeriod'] );
123 if ( isset( $params['tableName'] ) ) {
124 $this->tableName
= $params['tableName'];
126 if ( isset( $params['shards'] ) ) {
127 $this->shards
= intval( $params['shards'] );
129 if ( isset( $params['syncTimeout'] ) ) {
130 $this->syncTimeout
= $params['syncTimeout'];
132 $this->slaveOnly
= !empty( $params['slaveOnly'] );
136 * Get a connection to the specified database
138 * @param int $serverIndex
140 * @throws MWException
142 protected function getDB( $serverIndex ) {
143 if ( !isset( $this->conns
[$serverIndex] ) ) {
144 if ( $serverIndex >= $this->numServers
) {
145 throw new MWException( __METHOD__
. ": Invalid server index \"$serverIndex\"" );
148 # Don't keep timing out trying to connect for each call if the DB is down
149 if ( isset( $this->connFailureErrors
[$serverIndex] )
150 && ( time() - $this->connFailureTimes
[$serverIndex] ) < 60
152 throw $this->connFailureErrors
[$serverIndex];
155 # If server connection info was given, use that
156 if ( $this->serverInfos
) {
157 $info = $this->serverInfos
[$serverIndex];
158 $type = isset( $info['type'] ) ?
$info['type'] : 'mysql';
159 $host = isset( $info['host'] ) ?
$info['host'] : '[unknown]';
160 $this->logger
->debug( __CLASS__
. ": connecting to $host" );
161 // Use a blank trx profiler to ignore expections as this is a cache
162 $info['trxProfiler'] = new TransactionProfiler();
163 $db = DatabaseBase
::factory( $type, $info );
164 $db->clearFlag( DBO_TRX
);
166 // We must keep a separate connection to MySQL in order to avoid deadlocks
167 // However, SQLite has an opposite behavior. And PostgreSQL needs to know
168 // if we are in transaction or not (@TODO: find some work-around).
169 $index = $this->slaveOnly ? DB_SLAVE
: DB_MASTER
;
170 if ( wfGetDB( $index )->getType() == 'mysql' ) {
171 $lb = wfGetLBFactory()->newMainLB();
172 $db = $lb->getConnection( $index );
173 $db->clearFlag( DBO_TRX
); // auto-commit mode
175 $db = wfGetDB( $index );
178 $this->logger
->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
179 $this->conns
[$serverIndex] = $db;
182 return $this->conns
[$serverIndex];
186 * Get the server index and table name for a given key
188 * @return array Server index and table name
190 protected function getTableByKey( $key ) {
191 if ( $this->shards
> 1 ) {
192 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
193 $tableIndex = $hash %
$this->shards
;
197 if ( $this->numServers
> 1 ) {
198 $sortedServers = $this->serverTags
;
199 ArrayUtils
::consistentHashSort( $sortedServers, $key );
200 reset( $sortedServers );
201 $serverIndex = key( $sortedServers );
205 return [ $serverIndex, $this->getTableNameByShard( $tableIndex ) ];
209 * Get the table name for a given shard index
213 protected function getTableNameByShard( $index ) {
214 if ( $this->shards
> 1 ) {
215 $decimals = strlen( $this->shards
- 1 );
216 return $this->tableName
.
217 sprintf( "%0{$decimals}d", $index );
219 return $this->tableName
;
223 protected function doGet( $key, $flags = 0 ) {
226 return $this->getWithToken( $key, $casToken, $flags );
229 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
230 $values = $this->getMulti( [ $key ] );
231 if ( array_key_exists( $key, $values ) ) {
232 $casToken = $values[$key];
233 return $values[$key];
238 public function getMulti( array $keys, $flags = 0 ) {
239 $values = []; // array of (key => value)
242 foreach ( $keys as $key ) {
243 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
244 $keysByTable[$serverIndex][$tableName][] = $key;
247 $this->garbageCollect(); // expire old entries if any
250 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
252 $db = $this->getDB( $serverIndex );
253 foreach ( $serverKeys as $tableName => $tableKeys ) {
254 $res = $db->select( $tableName,
255 [ 'keyname', 'value', 'exptime' ],
256 [ 'keyname' => $tableKeys ],
258 // Approximate write-on-the-fly BagOStuff API via blocking.
259 // This approximation fails if a ROLLBACK happens (which is rare).
260 // We do not want to flush the TRX as that can break callers.
261 $db->trxLevel() ?
[ 'LOCK IN SHARE MODE' ] : []
263 if ( $res === false ) {
266 foreach ( $res as $row ) {
267 $row->serverIndex
= $serverIndex;
268 $row->tableName
= $tableName;
269 $dataRows[$row->keyname
] = $row;
272 } catch ( DBError
$e ) {
273 $this->handleReadError( $e, $serverIndex );
277 foreach ( $keys as $key ) {
278 if ( isset( $dataRows[$key] ) ) { // HIT?
279 $row = $dataRows[$key];
280 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
283 $db = $this->getDB( $row->serverIndex
);
284 if ( $this->isExpired( $db, $row->exptime
) ) { // MISS
285 $this->debug( "get: key has expired" );
287 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value
) );
289 } catch ( DBQueryError
$e ) {
290 $this->handleWriteError( $e, $db, $row->serverIndex
);
293 $this->debug( 'get: no matching rows' );
300 public function setMulti( array $data, $expiry = 0 ) {
302 foreach ( $data as $key => $value ) {
303 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
304 $keysByTable[$serverIndex][$tableName][] = $key;
307 $this->garbageCollect(); // expire old entries if any
310 $exptime = (int)$expiry;
311 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
314 $db = $this->getDB( $serverIndex );
315 } catch ( DBError
$e ) {
316 $this->handleWriteError( $e, $db, $serverIndex );
321 if ( $exptime < 0 ) {
325 if ( $exptime == 0 ) {
326 $encExpiry = $this->getMaxDateTime( $db );
328 $exptime = $this->convertExpiry( $exptime );
329 $encExpiry = $db->timestamp( $exptime );
331 foreach ( $serverKeys as $tableName => $tableKeys ) {
333 foreach ( $tableKeys as $key ) {
336 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
337 'exptime' => $encExpiry,
348 } catch ( DBError
$e ) {
349 $this->handleWriteError( $e, $db, $serverIndex );
360 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
361 $ok = $this->setMulti( [ $key => $value ], $exptime );
362 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
363 $ok = $ok && $this->waitForSlaves();
369 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
370 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
373 $db = $this->getDB( $serverIndex );
374 $exptime = intval( $exptime );
376 if ( $exptime < 0 ) {
380 if ( $exptime == 0 ) {
381 $encExpiry = $this->getMaxDateTime( $db );
383 $exptime = $this->convertExpiry( $exptime );
384 $encExpiry = $db->timestamp( $exptime );
386 // (bug 24425) use a replace if the db supports it instead of
387 // delete/insert to avoid clashes with conflicting keynames
392 'value' => $db->encodeBlob( $this->serialize( $value ) ),
393 'exptime' => $encExpiry
397 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
401 } catch ( DBQueryError
$e ) {
402 $this->handleWriteError( $e, $db, $serverIndex );
407 return (bool)$db->affectedRows();
410 public function delete( $key ) {
411 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
414 $db = $this->getDB( $serverIndex );
417 [ 'keyname' => $key ],
419 } catch ( DBError
$e ) {
420 $this->handleWriteError( $e, $db, $serverIndex );
427 public function incr( $key, $step = 1 ) {
428 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
431 $db = $this->getDB( $serverIndex );
432 $step = intval( $step );
433 $row = $db->selectRow(
435 [ 'value', 'exptime' ],
436 [ 'keyname' => $key ],
439 if ( $row === false ) {
444 $db->delete( $tableName, [ 'keyname' => $key ], __METHOD__
);
445 if ( $this->isExpired( $db, $row->exptime
) ) {
446 // Expired, do not reinsert
451 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
452 $newValue = $oldValue +
$step;
453 $db->insert( $tableName,
456 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
457 'exptime' => $row->exptime
458 ], __METHOD__
, 'IGNORE' );
460 if ( $db->affectedRows() == 0 ) {
461 // Race condition. See bug 28611
464 } catch ( DBError
$e ) {
465 $this->handleWriteError( $e, $db, $serverIndex );
472 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
473 $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts );
474 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
475 $ok = $ok && $this->waitForSlaves();
481 public function changeTTL( $key, $expiry = 0 ) {
482 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
485 $db = $this->getDB( $serverIndex );
488 [ 'exptime' => $db->timestamp( $this->convertExpiry( $expiry ) ) ],
489 [ 'keyname' => $key, 'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
492 if ( $db->affectedRows() == 0 ) {
495 } catch ( DBError
$e ) {
496 $this->handleWriteError( $e, $db, $serverIndex );
504 * @param IDatabase $db
505 * @param string $exptime
508 protected function isExpired( $db, $exptime ) {
509 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX
, $exptime ) < time();
513 * @param IDatabase $db
516 protected function getMaxDateTime( $db ) {
517 if ( time() > 0x7fffffff ) {
518 return $db->timestamp( 1 << 62 );
520 return $db->timestamp( 0x7fffffff );
524 protected function garbageCollect() {
525 if ( !$this->purgePeriod ||
$this->slaveOnly
) {
529 // Only purge on one in every $this->purgePeriod requests.
530 if ( $this->purgePeriod
!== 1 && mt_rand( 0, $this->purgePeriod
- 1 ) ) {
534 // Avoid repeating the delete within a few seconds
535 if ( $now > ( $this->lastExpireAll +
1 ) ) {
536 $this->lastExpireAll
= $now;
541 public function expireAll() {
542 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
546 * Delete objects from the database which expire before a certain date.
547 * @param string $timestamp
548 * @param bool|callable $progressCallback
551 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
552 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
555 $db = $this->getDB( $serverIndex );
556 $dbTimestamp = $db->timestamp( $timestamp );
557 $totalSeconds = false;
558 $baseConds = [ 'exptime < ' . $db->addQuotes( $dbTimestamp ) ];
559 for ( $i = 0; $i < $this->shards
; $i++
) {
563 if ( $maxExpTime !== false ) {
564 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
567 $this->getTableNameByShard( $i ),
568 [ 'keyname', 'exptime' ],
571 [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ] );
572 if ( $rows === false ||
!$rows->numRows() ) {
576 $row = $rows->current();
577 $minExpTime = $row->exptime
;
578 if ( $totalSeconds === false ) {
579 $totalSeconds = wfTimestamp( TS_UNIX
, $timestamp )
580 - wfTimestamp( TS_UNIX
, $minExpTime );
582 foreach ( $rows as $row ) {
583 $keys[] = $row->keyname
;
584 $maxExpTime = $row->exptime
;
588 $this->getTableNameByShard( $i ),
590 'exptime >= ' . $db->addQuotes( $minExpTime ),
591 'exptime < ' . $db->addQuotes( $dbTimestamp ),
596 if ( $progressCallback ) {
597 if ( intval( $totalSeconds ) === 0 ) {
600 $remainingSeconds = wfTimestamp( TS_UNIX
, $timestamp )
601 - wfTimestamp( TS_UNIX
, $maxExpTime );
602 if ( $remainingSeconds > $totalSeconds ) {
603 $totalSeconds = $remainingSeconds;
605 $processedSeconds = $totalSeconds - $remainingSeconds;
606 $percent = ( $i +
$processedSeconds / $totalSeconds )
607 / $this->shards
* 100;
609 $percent = ( $percent / $this->numServers
)
610 +
( $serverIndex / $this->numServers
* 100 );
611 call_user_func( $progressCallback, $percent );
615 } catch ( DBError
$e ) {
616 $this->handleWriteError( $e, $db, $serverIndex );
624 * Delete content of shard tables in every server.
625 * Return true if the operation is successful, false otherwise.
628 public function deleteAll() {
629 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
632 $db = $this->getDB( $serverIndex );
633 for ( $i = 0; $i < $this->shards
; $i++
) {
634 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__
);
636 } catch ( DBError
$e ) {
637 $this->handleWriteError( $e, $db, $serverIndex );
645 * Serialize an object and, if possible, compress the representation.
646 * On typical message and page data, this can provide a 3X decrease
647 * in storage requirements.
652 protected function serialize( &$data ) {
653 $serial = serialize( $data );
655 if ( function_exists( 'gzdeflate' ) ) {
656 return gzdeflate( $serial );
663 * Unserialize and, if necessary, decompress an object.
664 * @param string $serial
667 protected function unserialize( $serial ) {
668 if ( function_exists( 'gzinflate' ) ) {
669 MediaWiki\
suppressWarnings();
670 $decomp = gzinflate( $serial );
671 MediaWiki\restoreWarnings
();
673 if ( false !== $decomp ) {
678 $ret = unserialize( $serial );
684 * Handle a DBError which occurred during a read operation.
686 * @param DBError $exception
687 * @param int $serverIndex
689 protected function handleReadError( DBError
$exception, $serverIndex ) {
690 if ( $exception instanceof DBConnectionError
) {
691 $this->markServerDown( $exception, $serverIndex );
693 $this->logger
->error( "DBError: {$exception->getMessage()}" );
694 if ( $exception instanceof DBConnectionError
) {
695 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
696 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
698 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
699 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
704 * Handle a DBQueryError which occurred during a write operation.
706 * @param DBError $exception
707 * @param IDatabase|null $db DB handle or null if connection failed
708 * @param int $serverIndex
711 protected function handleWriteError( DBError
$exception, IDatabase
$db = null, $serverIndex ) {
713 $this->markServerDown( $exception, $serverIndex );
714 } elseif ( $db->wasReadOnlyError() ) {
715 if ( $db->trxLevel() && $this->usesMainDB() ) {
716 // Errors like deadlocks and connection drops already cause rollback.
717 // For consistency, we have no choice but to throw an error and trigger
718 // complete rollback if the main DB is also being used as the cache DB.
723 $this->logger
->error( "DBError: {$exception->getMessage()}" );
724 if ( $exception instanceof DBConnectionError
) {
725 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
726 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
728 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
729 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
734 * Mark a server down due to a DBConnectionError exception
736 * @param DBError $exception
737 * @param int $serverIndex
739 protected function markServerDown( DBError
$exception, $serverIndex ) {
740 unset( $this->conns
[$serverIndex] ); // bug T103435
742 if ( isset( $this->connFailureTimes
[$serverIndex] ) ) {
743 if ( time() - $this->connFailureTimes
[$serverIndex] >= 60 ) {
744 unset( $this->connFailureTimes
[$serverIndex] );
745 unset( $this->connFailureErrors
[$serverIndex] );
747 $this->logger
->debug( __METHOD__
. ": Server #$serverIndex already down" );
752 $this->logger
->info( __METHOD__
. ": Server #$serverIndex down until " . ( $now +
60 ) );
753 $this->connFailureTimes
[$serverIndex] = $now;
754 $this->connFailureErrors
[$serverIndex] = $exception;
758 * Create shard tables. For use from eval.php.
760 public function createTables() {
761 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
762 $db = $this->getDB( $serverIndex );
763 if ( $db->getType() !== 'mysql' ) {
764 throw new MWException( __METHOD__
. ' is not supported on this DB server' );
767 for ( $i = 0; $i < $this->shards
; $i++
) {
769 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
770 ' LIKE ' . $db->tableName( 'objectcache' ),
777 * @return bool Whether the main DB is used, e.g. wfGetDB( DB_MASTER )
779 protected function usesMainDB() {
780 return !$this->serverInfos
;
783 protected function waitForSlaves() {
784 if ( $this->usesMainDB() ) {
785 // Main LB is used; wait for any slaves to catch up
787 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
788 $lbFactory->waitForReplication( [ 'wiki' => wfWikiID() ] );
790 } catch ( DBReplicationWaitError
$e ) {
794 // Custom DB server list; probably doesn't use replication