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
;
25 use Wikimedia\Rdbms\Database
;
26 use Wikimedia\Rdbms\IDatabase
;
27 use Wikimedia\Rdbms\DBError
;
28 use Wikimedia\Rdbms\DBQueryError
;
29 use Wikimedia\Rdbms\DBConnectionError
;
30 use Wikimedia\Rdbms\LoadBalancer
;
31 use Wikimedia\ScopedCallback
;
32 use Wikimedia\WaitConditionLoop
;
35 * Class to store objects in the database
39 class SqlBagOStuff
extends BagOStuff
{
40 /** @var array[] (server index => server config) */
41 protected $serverInfos;
42 /** @var string[] (server index => tag/host name) */
43 protected $serverTags;
45 protected $numServers;
47 protected $lastExpireAll = 0;
49 protected $purgePeriod = 100;
51 protected $shards = 1;
53 protected $tableName = 'objectcache';
55 protected $replicaOnly = false;
57 protected $syncTimeout = 3;
59 /** @var LoadBalancer|null */
60 protected $separateMainLB;
63 /** @var array UNIX timestamps */
64 protected $connFailureTimes = [];
65 /** @var array Exceptions */
66 protected $connFailureErrors = [];
69 * Constructor. Parameters are:
70 * - server: A server info structure in the format required by each
71 * element in $wgDBServers.
73 * - servers: An array of server info structures describing a set of database servers
74 * to distribute keys to. If this is specified, the "server" option will be
75 * ignored. If string keys are used, then they will be used for consistent
76 * hashing *instead* of the host name (from the server config). This is useful
77 * when a cluster is replicated to another site (with different host names)
78 * but each server has a corresponding replica in the other cluster.
80 * - purgePeriod: The average number of object cache requests in between
81 * garbage collection operations, where expired entries
82 * are removed from the database. Or in other words, the
83 * reciprocal of the probability of purging on any given
84 * request. If this is set to zero, purging will never be
87 * - tableName: The table name to use, default is "objectcache".
89 * - shards: The number of tables to use for data storage on each server.
90 * If this is more than 1, table names will be formed in the style
91 * objectcacheNNN where NNN is the shard index, between 0 and
92 * shards-1. The number of digits will be the minimum number
93 * required to hold the largest shard index. Data will be
94 * distributed across all tables by key hash. This is for
95 * MySQL bugs 61735 <https://bugs.mysql.com/bug.php?id=61735>
96 * and 61736 <https://bugs.mysql.com/bug.php?id=61736>.
98 * - slaveOnly: Whether to only use replica DBs and avoid triggering
99 * garbage collection logic of expired items. This only
100 * makes sense if the primary DB is used and only if get()
101 * calls will be used. This is used by ReplicatedBagOStuff.
102 * - syncTimeout: Max seconds to wait for replica DBs to catch up for WRITE_SYNC.
104 * @param array $params
106 public function __construct( $params ) {
107 parent
::__construct( $params );
109 $this->attrMap
[self
::ATTR_EMULATION
] = self
::QOS_EMULATION_SQL
;
110 $this->attrMap
[self
::ATTR_SYNCWRITES
] = self
::QOS_SYNCWRITES_NONE
;
112 if ( isset( $params['servers'] ) ) {
113 $this->serverInfos
= [];
114 $this->serverTags
= [];
115 $this->numServers
= count( $params['servers'] );
117 foreach ( $params['servers'] as $tag => $info ) {
118 $this->serverInfos
[$index] = $info;
119 if ( is_string( $tag ) ) {
120 $this->serverTags
[$index] = $tag;
122 $this->serverTags
[$index] = $info['host'] ??
"#$index";
126 } elseif ( isset( $params['server'] ) ) {
127 $this->serverInfos
= [ $params['server'] ];
128 $this->numServers
= count( $this->serverInfos
);
130 // Default to using the main wiki's database servers
131 $this->serverInfos
= false;
132 $this->numServers
= 1;
133 $this->attrMap
[self
::ATTR_SYNCWRITES
] = self
::QOS_SYNCWRITES_BE
;
135 if ( isset( $params['purgePeriod'] ) ) {
136 $this->purgePeriod
= intval( $params['purgePeriod'] );
138 if ( isset( $params['tableName'] ) ) {
139 $this->tableName
= $params['tableName'];
141 if ( isset( $params['shards'] ) ) {
142 $this->shards
= intval( $params['shards'] );
144 if ( isset( $params['syncTimeout'] ) ) {
145 $this->syncTimeout
= $params['syncTimeout'];
147 $this->replicaOnly
= !empty( $params['slaveOnly'] );
151 * Get a connection to the specified database
153 * @param int $serverIndex
155 * @throws MWException
157 protected function getDB( $serverIndex ) {
158 if ( !isset( $this->conns
[$serverIndex] ) ) {
159 if ( $serverIndex >= $this->numServers
) {
160 throw new MWException( __METHOD__
. ": Invalid server index \"$serverIndex\"" );
163 # Don't keep timing out trying to connect for each call if the DB is down
164 if ( isset( $this->connFailureErrors
[$serverIndex] )
165 && ( time() - $this->connFailureTimes
[$serverIndex] ) < 60
167 throw $this->connFailureErrors
[$serverIndex];
170 if ( $this->serverInfos
) {
171 // Use custom database defined by server connection info
172 $info = $this->serverInfos
[$serverIndex];
173 $type = $info['type'] ??
'mysql';
174 $host = $info['host'] ??
'[unknown]';
175 $this->logger
->debug( __CLASS__
. ": connecting to $host" );
176 $db = Database
::factory( $type, $info );
177 $db->clearFlag( DBO_TRX
); // auto-commit mode
179 // Use the main LB database
180 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
181 $index = $this->replicaOnly ? DB_REPLICA
: DB_MASTER
;
182 if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
183 // Keep a separate connection to avoid contention and deadlocks
184 $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT
);
186 // However, SQLite has the opposite behavior due to DB-level locking.
187 // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead.
188 $db = $lb->getConnection( $index );
192 $this->logger
->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
193 $this->conns
[$serverIndex] = $db;
196 return $this->conns
[$serverIndex];
200 * Get the server index and table name for a given key
202 * @return array Server index and table name
204 protected function getTableByKey( $key ) {
205 if ( $this->shards
> 1 ) {
206 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
207 $tableIndex = $hash %
$this->shards
;
211 if ( $this->numServers
> 1 ) {
212 $sortedServers = $this->serverTags
;
213 ArrayUtils
::consistentHashSort( $sortedServers, $key );
214 reset( $sortedServers );
215 $serverIndex = key( $sortedServers );
219 return [ $serverIndex, $this->getTableNameByShard( $tableIndex ) ];
223 * Get the table name for a given shard index
227 protected function getTableNameByShard( $index ) {
228 if ( $this->shards
> 1 ) {
229 $decimals = strlen( $this->shards
- 1 );
230 return $this->tableName
.
231 sprintf( "%0{$decimals}d", $index );
233 return $this->tableName
;
237 protected function doGet( $key, $flags = 0 ) {
240 return $this->getWithToken( $key, $casToken, $flags );
243 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
244 $values = $this->getMulti( [ $key ] );
245 if ( array_key_exists( $key, $values ) ) {
246 $casToken = $values[$key];
247 return $values[$key];
252 public function getMulti( array $keys, $flags = 0 ) {
253 $values = []; // array of (key => value)
256 foreach ( $keys as $key ) {
257 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
258 $keysByTable[$serverIndex][$tableName][] = $key;
261 $this->garbageCollect(); // expire old entries if any
264 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
266 $db = $this->getDB( $serverIndex );
267 foreach ( $serverKeys as $tableName => $tableKeys ) {
268 $res = $db->select( $tableName,
269 [ 'keyname', 'value', 'exptime' ],
270 [ 'keyname' => $tableKeys ],
272 // Approximate write-on-the-fly BagOStuff API via blocking.
273 // This approximation fails if a ROLLBACK happens (which is rare).
274 // We do not want to flush the TRX as that can break callers.
275 $db->trxLevel() ?
[ 'LOCK IN SHARE MODE' ] : []
277 if ( $res === false ) {
280 foreach ( $res as $row ) {
281 $row->serverIndex
= $serverIndex;
282 $row->tableName
= $tableName;
283 $dataRows[$row->keyname
] = $row;
286 } catch ( DBError
$e ) {
287 $this->handleReadError( $e, $serverIndex );
291 foreach ( $keys as $key ) {
292 if ( isset( $dataRows[$key] ) ) { // HIT?
293 $row = $dataRows[$key];
294 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
297 $db = $this->getDB( $row->serverIndex
);
298 if ( $this->isExpired( $db, $row->exptime
) ) { // MISS
299 $this->debug( "get: key has expired" );
301 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value
) );
303 } catch ( DBQueryError
$e ) {
304 $this->handleWriteError( $e, $db, $row->serverIndex
);
307 $this->debug( 'get: no matching rows' );
314 public function setMulti( array $data, $expiry = 0 ) {
316 foreach ( $data as $key => $value ) {
317 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
318 $keysByTable[$serverIndex][$tableName][] = $key;
321 $this->garbageCollect(); // expire old entries if any
324 $exptime = (int)$expiry;
325 $silenceScope = $this->silenceTransactionProfiler();
326 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
329 $db = $this->getDB( $serverIndex );
330 } catch ( DBError
$e ) {
331 $this->handleWriteError( $e, $db, $serverIndex );
336 if ( $exptime < 0 ) {
340 if ( $exptime == 0 ) {
341 $encExpiry = $this->getMaxDateTime( $db );
343 $exptime = $this->convertExpiry( $exptime );
344 $encExpiry = $db->timestamp( $exptime );
346 foreach ( $serverKeys as $tableName => $tableKeys ) {
348 foreach ( $tableKeys as $key ) {
351 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
352 'exptime' => $encExpiry,
363 } catch ( DBError
$e ) {
364 $this->handleWriteError( $e, $db, $serverIndex );
375 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
376 $ok = $this->setMulti( [ $key => $value ], $exptime );
377 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
378 $ok = $this->waitForReplication() && $ok;
384 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
385 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
387 $silenceScope = $this->silenceTransactionProfiler();
389 $db = $this->getDB( $serverIndex );
390 $exptime = intval( $exptime );
392 if ( $exptime < 0 ) {
396 if ( $exptime == 0 ) {
397 $encExpiry = $this->getMaxDateTime( $db );
399 $exptime = $this->convertExpiry( $exptime );
400 $encExpiry = $db->timestamp( $exptime );
402 // (T26425) use a replace if the db supports it instead of
403 // delete/insert to avoid clashes with conflicting keynames
408 'value' => $db->encodeBlob( $this->serialize( $value ) ),
409 'exptime' => $encExpiry
413 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
417 } catch ( DBQueryError
$e ) {
418 $this->handleWriteError( $e, $db, $serverIndex );
423 return (bool)$db->affectedRows();
426 public function delete( $key ) {
427 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
429 $silenceScope = $this->silenceTransactionProfiler();
431 $db = $this->getDB( $serverIndex );
434 [ 'keyname' => $key ],
436 } catch ( DBError
$e ) {
437 $this->handleWriteError( $e, $db, $serverIndex );
444 public function incr( $key, $step = 1 ) {
445 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
447 $silenceScope = $this->silenceTransactionProfiler();
449 $db = $this->getDB( $serverIndex );
450 $step = intval( $step );
451 $row = $db->selectRow(
453 [ 'value', 'exptime' ],
454 [ 'keyname' => $key ],
457 if ( $row === false ) {
462 $db->delete( $tableName, [ 'keyname' => $key ], __METHOD__
);
463 if ( $this->isExpired( $db, $row->exptime
) ) {
464 // Expired, do not reinsert
469 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
470 $newValue = $oldValue +
$step;
471 $db->insert( $tableName,
474 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
475 'exptime' => $row->exptime
476 ], __METHOD__
, 'IGNORE' );
478 if ( $db->affectedRows() == 0 ) {
479 // Race condition. See T30611
482 } catch ( DBError
$e ) {
483 $this->handleWriteError( $e, $db, $serverIndex );
490 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
491 $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts );
492 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
493 $ok = $this->waitForReplication() && $ok;
499 public function changeTTL( $key, $expiry = 0 ) {
500 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
502 $silenceScope = $this->silenceTransactionProfiler();
504 $db = $this->getDB( $serverIndex );
507 [ 'exptime' => $db->timestamp( $this->convertExpiry( $expiry ) ) ],
508 [ 'keyname' => $key, 'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
511 if ( $db->affectedRows() == 0 ) {
514 } catch ( DBError
$e ) {
515 $this->handleWriteError( $e, $db, $serverIndex );
523 * @param IDatabase $db
524 * @param string $exptime
527 protected function isExpired( $db, $exptime ) {
528 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX
, $exptime ) < time();
532 * @param IDatabase $db
535 protected function getMaxDateTime( $db ) {
536 if ( time() > 0x7fffffff ) {
537 return $db->timestamp( 1 << 62 );
539 return $db->timestamp( 0x7fffffff );
543 protected function garbageCollect() {
544 if ( !$this->purgePeriod ||
$this->replicaOnly
) {
548 // Only purge on one in every $this->purgePeriod requests.
549 if ( $this->purgePeriod
!== 1 && mt_rand( 0, $this->purgePeriod
- 1 ) ) {
553 // Avoid repeating the delete within a few seconds
554 if ( $now > ( $this->lastExpireAll +
1 ) ) {
555 $this->lastExpireAll
= $now;
560 public function expireAll() {
561 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
565 * Delete objects from the database which expire before a certain date.
566 * @param string $timestamp
567 * @param bool|callable $progressCallback
570 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
571 $silenceScope = $this->silenceTransactionProfiler();
572 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
575 $db = $this->getDB( $serverIndex );
576 $dbTimestamp = $db->timestamp( $timestamp );
577 $totalSeconds = false;
578 $baseConds = [ 'exptime < ' . $db->addQuotes( $dbTimestamp ) ];
579 for ( $i = 0; $i < $this->shards
; $i++
) {
583 if ( $maxExpTime !== false ) {
584 $conds[] = 'exptime >= ' . $db->addQuotes( $maxExpTime );
587 $this->getTableNameByShard( $i ),
588 [ 'keyname', 'exptime' ],
591 [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ] );
592 if ( $rows === false ||
!$rows->numRows() ) {
596 $row = $rows->current();
597 $minExpTime = $row->exptime
;
598 if ( $totalSeconds === false ) {
599 $totalSeconds = wfTimestamp( TS_UNIX
, $timestamp )
600 - wfTimestamp( TS_UNIX
, $minExpTime );
602 foreach ( $rows as $row ) {
603 $keys[] = $row->keyname
;
604 $maxExpTime = $row->exptime
;
608 $this->getTableNameByShard( $i ),
610 'exptime >= ' . $db->addQuotes( $minExpTime ),
611 'exptime < ' . $db->addQuotes( $dbTimestamp ),
616 if ( $progressCallback ) {
617 if ( intval( $totalSeconds ) === 0 ) {
620 $remainingSeconds = wfTimestamp( TS_UNIX
, $timestamp )
621 - wfTimestamp( TS_UNIX
, $maxExpTime );
622 if ( $remainingSeconds > $totalSeconds ) {
623 $totalSeconds = $remainingSeconds;
625 $processedSeconds = $totalSeconds - $remainingSeconds;
626 $percent = ( $i +
$processedSeconds / $totalSeconds )
627 / $this->shards
* 100;
629 $percent = ( $percent / $this->numServers
)
630 +
( $serverIndex / $this->numServers
* 100 );
631 call_user_func( $progressCallback, $percent );
635 } catch ( DBError
$e ) {
636 $this->handleWriteError( $e, $db, $serverIndex );
644 * Delete content of shard tables in every server.
645 * Return true if the operation is successful, false otherwise.
648 public function deleteAll() {
649 $silenceScope = $this->silenceTransactionProfiler();
650 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
653 $db = $this->getDB( $serverIndex );
654 for ( $i = 0; $i < $this->shards
; $i++
) {
655 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__
);
657 } catch ( DBError
$e ) {
658 $this->handleWriteError( $e, $db, $serverIndex );
666 * Serialize an object and, if possible, compress the representation.
667 * On typical message and page data, this can provide a 3X decrease
668 * in storage requirements.
670 * @param mixed &$data
673 protected function serialize( &$data ) {
674 $serial = serialize( $data );
676 if ( function_exists( 'gzdeflate' ) ) {
677 return gzdeflate( $serial );
684 * Unserialize and, if necessary, decompress an object.
685 * @param string $serial
688 protected function unserialize( $serial ) {
689 if ( function_exists( 'gzinflate' ) ) {
690 Wikimedia\
suppressWarnings();
691 $decomp = gzinflate( $serial );
692 Wikimedia\restoreWarnings
();
694 if ( $decomp !== false ) {
699 $ret = unserialize( $serial );
705 * Handle a DBError which occurred during a read operation.
707 * @param DBError $exception
708 * @param int $serverIndex
710 protected function handleReadError( DBError
$exception, $serverIndex ) {
711 if ( $exception instanceof DBConnectionError
) {
712 $this->markServerDown( $exception, $serverIndex );
714 $this->logger
->error( "DBError: {$exception->getMessage()}" );
715 if ( $exception instanceof DBConnectionError
) {
716 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
717 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
719 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
720 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
725 * Handle a DBQueryError which occurred during a write operation.
727 * @param DBError $exception
728 * @param IDatabase|null $db DB handle or null if connection failed
729 * @param int $serverIndex
732 protected function handleWriteError( DBError
$exception, IDatabase
$db = null, $serverIndex ) {
734 $this->markServerDown( $exception, $serverIndex );
735 } elseif ( $db->wasReadOnlyError() ) {
736 if ( $db->trxLevel() && $this->usesMainDB() ) {
737 // Errors like deadlocks and connection drops already cause rollback.
738 // For consistency, we have no choice but to throw an error and trigger
739 // complete rollback if the main DB is also being used as the cache DB.
744 $this->logger
->error( "DBError: {$exception->getMessage()}" );
745 if ( $exception instanceof DBConnectionError
) {
746 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
747 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
749 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
750 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
755 * Mark a server down due to a DBConnectionError exception
757 * @param DBError $exception
758 * @param int $serverIndex
760 protected function markServerDown( DBError
$exception, $serverIndex ) {
761 unset( $this->conns
[$serverIndex] ); // bug T103435
763 if ( isset( $this->connFailureTimes
[$serverIndex] ) ) {
764 if ( time() - $this->connFailureTimes
[$serverIndex] >= 60 ) {
765 unset( $this->connFailureTimes
[$serverIndex] );
766 unset( $this->connFailureErrors
[$serverIndex] );
768 $this->logger
->debug( __METHOD__
. ": Server #$serverIndex already down" );
773 $this->logger
->info( __METHOD__
. ": Server #$serverIndex down until " . ( $now +
60 ) );
774 $this->connFailureTimes
[$serverIndex] = $now;
775 $this->connFailureErrors
[$serverIndex] = $exception;
779 * Create shard tables. For use from eval.php.
781 public function createTables() {
782 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
783 $db = $this->getDB( $serverIndex );
784 if ( $db->getType() !== 'mysql' ) {
785 throw new MWException( __METHOD__
. ' is not supported on this DB server' );
788 for ( $i = 0; $i < $this->shards
; $i++
) {
790 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
791 ' LIKE ' . $db->tableName( 'objectcache' ),
798 * @return bool Whether the main DB is used, e.g. wfGetDB( DB_MASTER )
800 protected function usesMainDB() {
801 return !$this->serverInfos
;
804 protected function waitForReplication() {
805 if ( !$this->usesMainDB() ) {
806 // Custom DB server list; probably doesn't use replication
810 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
811 if ( $lb->getServerCount() <= 1 ) {
812 return true; // no replica DBs
815 // Main LB is used; wait for any replica DBs to catch up
816 $masterPos = $lb->getMasterPos();
818 return true; // not applicable
821 $loop = new WaitConditionLoop(
822 function () use ( $lb, $masterPos ) {
823 return $lb->waitForAll( $masterPos, 1 );
829 return ( $loop->invoke() === $loop::CONDITION_REACHED
);
833 * Returns a ScopedCallback which resets the silence flag in the transaction profiler when it is
834 * destroyed on the end of a scope, for example on return or throw
835 * @return ScopedCallback
838 protected function silenceTransactionProfiler() {
839 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
840 $oldSilenced = $trxProfiler->setSilenced( true );
841 return new ScopedCallback( function () use ( $trxProfiler, $oldSilenced ) {
842 $trxProfiler->setSilenced( $oldSilenced );