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;
46 /** @var int UNIX timestamp */
47 protected $lastGarbageCollect = 0;
49 protected $purgePeriod = 10;
51 protected $purgeLimit = 100;
53 protected $shards = 1;
55 protected $tableName = 'objectcache';
57 protected $replicaOnly = false;
59 protected $syncTimeout = 3;
61 /** @var LoadBalancer|null */
62 protected $separateMainLB;
65 /** @var array UNIX timestamps */
66 protected $connFailureTimes = [];
67 /** @var array Exceptions */
68 protected $connFailureErrors = [];
71 const GARBAGE_COLLECT_DELAY_SEC
= 1;
74 * Constructor. Parameters are:
75 * - server: A server info structure in the format required by each
76 * element in $wgDBServers.
78 * - servers: An array of server info structures describing a set of database servers
79 * to distribute keys to. If this is specified, the "server" option will be
80 * ignored. If string keys are used, then they will be used for consistent
81 * hashing *instead* of the host name (from the server config). This is useful
82 * when a cluster is replicated to another site (with different host names)
83 * but each server has a corresponding replica in the other cluster.
85 * - purgePeriod: The average number of object cache writes in between
86 * garbage collection operations, where expired entries
87 * are removed from the database. Or in other words, the
88 * reciprocal of the probability of purging on any given
89 * write. If this is set to zero, purging will never be done.
91 * - purgeLimit: Maximum number of rows to purge at once.
93 * - tableName: The table name to use, default is "objectcache".
95 * - shards: The number of tables to use for data storage on each server.
96 * If this is more than 1, table names will be formed in the style
97 * objectcacheNNN where NNN is the shard index, between 0 and
98 * shards-1. The number of digits will be the minimum number
99 * required to hold the largest shard index. Data will be
100 * distributed across all tables by key hash. This is for
101 * MySQL bugs 61735 <https://bugs.mysql.com/bug.php?id=61735>
102 * and 61736 <https://bugs.mysql.com/bug.php?id=61736>.
104 * - slaveOnly: Whether to only use replica DBs and avoid triggering
105 * garbage collection logic of expired items. This only
106 * makes sense if the primary DB is used and only if get()
107 * calls will be used. This is used by ReplicatedBagOStuff.
108 * - syncTimeout: Max seconds to wait for replica DBs to catch up for WRITE_SYNC.
110 * @param array $params
112 public function __construct( $params ) {
113 parent
::__construct( $params );
115 $this->attrMap
[self
::ATTR_EMULATION
] = self
::QOS_EMULATION_SQL
;
116 $this->attrMap
[self
::ATTR_SYNCWRITES
] = self
::QOS_SYNCWRITES_NONE
;
118 if ( isset( $params['servers'] ) ) {
119 $this->serverInfos
= [];
120 $this->serverTags
= [];
121 $this->numServers
= count( $params['servers'] );
123 foreach ( $params['servers'] as $tag => $info ) {
124 $this->serverInfos
[$index] = $info;
125 if ( is_string( $tag ) ) {
126 $this->serverTags
[$index] = $tag;
128 $this->serverTags
[$index] = $info['host'] ??
"#$index";
132 } elseif ( isset( $params['server'] ) ) {
133 $this->serverInfos
= [ $params['server'] ];
134 $this->numServers
= count( $this->serverInfos
);
136 // Default to using the main wiki's database servers
137 $this->serverInfos
= false;
138 $this->numServers
= 1;
139 $this->attrMap
[self
::ATTR_SYNCWRITES
] = self
::QOS_SYNCWRITES_BE
;
141 if ( isset( $params['purgePeriod'] ) ) {
142 $this->purgePeriod
= intval( $params['purgePeriod'] );
144 if ( isset( $params['purgeLimit'] ) ) {
145 $this->purgeLimit
= intval( $params['purgeLimit'] );
147 if ( isset( $params['tableName'] ) ) {
148 $this->tableName
= $params['tableName'];
150 if ( isset( $params['shards'] ) ) {
151 $this->shards
= intval( $params['shards'] );
153 if ( isset( $params['syncTimeout'] ) ) {
154 $this->syncTimeout
= $params['syncTimeout'];
156 $this->replicaOnly
= !empty( $params['slaveOnly'] );
160 * Get a connection to the specified database
162 * @param int $serverIndex
164 * @throws MWException
166 protected function getDB( $serverIndex ) {
167 if ( !isset( $this->conns
[$serverIndex] ) ) {
168 if ( $serverIndex >= $this->numServers
) {
169 throw new MWException( __METHOD__
. ": Invalid server index \"$serverIndex\"" );
172 # Don't keep timing out trying to connect for each call if the DB is down
173 if ( isset( $this->connFailureErrors
[$serverIndex] )
174 && ( time() - $this->connFailureTimes
[$serverIndex] ) < 60
176 throw $this->connFailureErrors
[$serverIndex];
179 if ( $this->serverInfos
) {
180 // Use custom database defined by server connection info
181 $info = $this->serverInfos
[$serverIndex];
182 $type = $info['type'] ??
'mysql';
183 $host = $info['host'] ??
'[unknown]';
184 $this->logger
->debug( __CLASS__
. ": connecting to $host" );
185 $db = Database
::factory( $type, $info );
186 $db->clearFlag( DBO_TRX
); // auto-commit mode
188 // Use the main LB database
189 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
190 $index = $this->replicaOnly ? DB_REPLICA
: DB_MASTER
;
191 if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
192 // Keep a separate connection to avoid contention and deadlocks
193 $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT
);
195 // However, SQLite has the opposite behavior due to DB-level locking.
196 // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead.
197 $db = $lb->getConnection( $index );
201 $this->logger
->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
202 $this->conns
[$serverIndex] = $db;
205 return $this->conns
[$serverIndex];
209 * Get the server index and table name for a given key
211 * @return array Server index and table name
213 protected function getTableByKey( $key ) {
214 if ( $this->shards
> 1 ) {
215 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
216 $tableIndex = $hash %
$this->shards
;
220 if ( $this->numServers
> 1 ) {
221 $sortedServers = $this->serverTags
;
222 ArrayUtils
::consistentHashSort( $sortedServers, $key );
223 reset( $sortedServers );
224 $serverIndex = key( $sortedServers );
228 return [ $serverIndex, $this->getTableNameByShard( $tableIndex ) ];
232 * Get the table name for a given shard index
236 protected function getTableNameByShard( $index ) {
237 if ( $this->shards
> 1 ) {
238 $decimals = strlen( $this->shards
- 1 );
239 return $this->tableName
.
240 sprintf( "%0{$decimals}d", $index );
242 return $this->tableName
;
246 protected function doGet( $key, $flags = 0, &$casToken = null ) {
249 $blobs = $this->fetchBlobMulti( [ $key ] );
250 if ( array_key_exists( $key, $blobs ) ) {
251 $blob = $blobs[$key];
252 $value = $this->unserialize( $blob );
254 $casToken = ( $value !== false ) ?
$blob : null;
262 protected function doGetMulti( array $keys, $flags = 0 ) {
265 $blobs = $this->fetchBlobMulti( $keys );
266 foreach ( $blobs as $key => $blob ) {
267 $values[$key] = $this->unserialize( $blob );
273 protected function fetchBlobMulti( array $keys, $flags = 0 ) {
274 $values = []; // array of (key => value)
277 foreach ( $keys as $key ) {
278 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
279 $keysByTable[$serverIndex][$tableName][] = $key;
283 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
285 $db = $this->getDB( $serverIndex );
286 foreach ( $serverKeys as $tableName => $tableKeys ) {
287 $res = $db->select( $tableName,
288 [ 'keyname', 'value', 'exptime' ],
289 [ 'keyname' => $tableKeys ],
291 // Approximate write-on-the-fly BagOStuff API via blocking.
292 // This approximation fails if a ROLLBACK happens (which is rare).
293 // We do not want to flush the TRX as that can break callers.
294 $db->trxLevel() ?
[ 'LOCK IN SHARE MODE' ] : []
296 if ( $res === false ) {
299 foreach ( $res as $row ) {
300 $row->serverIndex
= $serverIndex;
301 $row->tableName
= $tableName;
302 $dataRows[$row->keyname
] = $row;
305 } catch ( DBError
$e ) {
306 $this->handleReadError( $e, $serverIndex );
310 foreach ( $keys as $key ) {
311 if ( isset( $dataRows[$key] ) ) { // HIT?
312 $row = $dataRows[$key];
313 $this->debug( "get: retrieved data; expiry time is " . $row->exptime
);
316 $db = $this->getDB( $row->serverIndex
);
317 if ( $this->isExpired( $db, $row->exptime
) ) { // MISS
318 $this->debug( "get: key has expired" );
320 $values[$key] = $db->decodeBlob( $row->value
);
322 } catch ( DBQueryError
$e ) {
323 $this->handleWriteError( $e, $db, $row->serverIndex
);
326 $this->debug( 'get: no matching rows' );
333 public function setMulti( array $data, $expiry = 0, $flags = 0 ) {
334 return $this->insertMulti( $data, $expiry, $flags, true );
337 private function insertMulti( array $data, $expiry, $flags, $replace ) {
339 foreach ( $data as $key => $value ) {
340 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
341 $keysByTable[$serverIndex][$tableName][] = $key;
345 $exptime = (int)$expiry;
346 /** @noinspection PhpUnusedLocalVariableInspection */
347 $silenceScope = $this->silenceTransactionProfiler();
348 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
351 $db = $this->getDB( $serverIndex );
352 $this->occasionallyGarbageCollect( $db );
353 } catch ( DBError
$e ) {
354 $this->handleWriteError( $e, $db, $serverIndex );
359 if ( $exptime < 0 ) {
363 if ( $exptime == 0 ) {
364 $encExpiry = $this->getMaxDateTime( $db );
366 $exptime = $this->convertToExpiry( $exptime );
367 $encExpiry = $db->timestamp( $exptime );
369 foreach ( $serverKeys as $tableName => $tableKeys ) {
371 foreach ( $tableKeys as $key ) {
374 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
375 'exptime' => $encExpiry,
381 $db->replace( $tableName, [ 'keyname' ], $rows, __METHOD__
);
383 $db->insert( $tableName, $rows, __METHOD__
, [ 'IGNORE' ] );
384 $result = ( $db->affectedRows() > 0 && $result );
386 } catch ( DBError
$e ) {
387 $this->handleWriteError( $e, $db, $serverIndex );
394 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
395 $result = $this->waitForReplication() && $result;
401 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
402 $ok = $this->insertMulti( [ $key => $value ], $exptime, $flags, true );
407 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
408 $added = $this->insertMulti( [ $key => $value ], $exptime, $flags, false );
413 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
414 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
416 /** @noinspection PhpUnusedLocalVariableInspection */
417 $silenceScope = $this->silenceTransactionProfiler();
419 $db = $this->getDB( $serverIndex );
420 $exptime = intval( $exptime );
422 if ( $exptime < 0 ) {
426 if ( $exptime == 0 ) {
427 $encExpiry = $this->getMaxDateTime( $db );
429 $exptime = $this->convertToExpiry( $exptime );
430 $encExpiry = $db->timestamp( $exptime );
432 // (T26425) use a replace if the db supports it instead of
433 // delete/insert to avoid clashes with conflicting keynames
438 'value' => $db->encodeBlob( $this->serialize( $value ) ),
439 'exptime' => $encExpiry
443 'value' => $db->encodeBlob( $casToken )
447 } catch ( DBQueryError
$e ) {
448 $this->handleWriteError( $e, $db, $serverIndex );
453 return (bool)$db->affectedRows();
456 public function deleteMulti( array $keys, $flags = 0 ) {
457 return $this->purgeMulti( $keys, $flags );
460 public function purgeMulti( array $keys, $flags = 0 ) {
462 foreach ( $keys as $key ) {
463 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
464 $keysByTable[$serverIndex][$tableName][] = $key;
468 /** @noinspection PhpUnusedLocalVariableInspection */
469 $silenceScope = $this->silenceTransactionProfiler();
470 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
473 $db = $this->getDB( $serverIndex );
474 } catch ( DBError
$e ) {
475 $this->handleWriteError( $e, $db, $serverIndex );
480 foreach ( $serverKeys as $tableName => $tableKeys ) {
482 $db->delete( $tableName, [ 'keyname' => $tableKeys ], __METHOD__
);
483 } catch ( DBError
$e ) {
484 $this->handleWriteError( $e, $db, $serverIndex );
491 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
492 $result = $this->waitForReplication() && $result;
498 protected function doDelete( $key, $flags = 0 ) {
499 $ok = $this->purgeMulti( [ $key ], $flags );
504 public function incr( $key, $step = 1 ) {
505 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
507 /** @noinspection PhpUnusedLocalVariableInspection */
508 $silenceScope = $this->silenceTransactionProfiler();
510 $db = $this->getDB( $serverIndex );
511 $step = intval( $step );
512 $row = $db->selectRow(
514 [ 'value', 'exptime' ],
515 [ 'keyname' => $key ],
519 if ( $row === false ) {
523 $db->delete( $tableName, [ 'keyname' => $key ], __METHOD__
);
524 if ( $this->isExpired( $db, $row->exptime
) ) {
525 // Expired, do not reinsert
529 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value
) ) );
530 $newValue = $oldValue +
$step;
535 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
536 'exptime' => $row->exptime
542 if ( $db->affectedRows() == 0 ) {
543 // Race condition. See T30611
546 } catch ( DBError
$e ) {
547 $this->handleWriteError( $e, $db, $serverIndex );
554 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
555 $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts, $flags );
556 if ( ( $flags & self
::WRITE_SYNC
) == self
::WRITE_SYNC
) {
557 $ok = $this->waitForReplication() && $ok;
563 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
564 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
566 /** @noinspection PhpUnusedLocalVariableInspection */
567 $silenceScope = $this->silenceTransactionProfiler();
569 $db = $this->getDB( $serverIndex );
570 if ( $exptime == 0 ) {
571 $timestamp = $this->getMaxDateTime( $db );
573 $timestamp = $db->timestamp( $this->convertToExpiry( $exptime ) );
577 [ 'exptime' => $timestamp ],
578 [ 'keyname' => $key, 'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
581 if ( $db->affectedRows() == 0 ) {
582 $exists = (bool)$db->selectField(
585 [ 'keyname' => $key, 'exptime' => $timestamp ],
592 } catch ( DBError
$e ) {
593 $this->handleWriteError( $e, $db, $serverIndex );
601 * @param IDatabase $db
602 * @param string $exptime
605 protected function isExpired( $db, $exptime ) {
607 $exptime != $this->getMaxDateTime( $db ) &&
608 wfTimestamp( TS_UNIX
, $exptime ) < time()
613 * @param IDatabase $db
616 protected function getMaxDateTime( $db ) {
617 if ( time() > 0x7fffffff ) {
618 return $db->timestamp( 1 << 62 );
620 return $db->timestamp( 0x7fffffff );
625 * @param IDatabase $db
628 protected function occasionallyGarbageCollect( IDatabase
$db ) {
630 // Random purging is enabled
631 $this->purgePeriod
&&
632 // This is not using a replica DB
633 !$this->replicaOnly
&&
634 // Only purge on one in every $this->purgePeriod writes
635 mt_rand( 0, $this->purgePeriod
- 1 ) == 0 &&
636 // Avoid repeating the delete within a few seconds
637 ( time() - $this->lastGarbageCollect
) > self
::GARBAGE_COLLECT_DELAY_SEC
639 $garbageCollector = function () use ( $db ) {
640 $this->deleteServerObjectsExpiringBefore( $db, time(), null, $this->purgeLimit
);
641 $this->lastGarbageCollect
= time();
643 if ( $this->asyncHandler
) {
644 $this->lastGarbageCollect
= time(); // avoid duplicate enqueues
645 ( $this->asyncHandler
)( $garbageCollector );
652 public function expireAll() {
653 $this->deleteObjectsExpiringBefore( time() );
656 public function deleteObjectsExpiringBefore(
658 callable
$progressCallback = null,
661 /** @noinspection PhpUnusedLocalVariableInspection */
662 $silenceScope = $this->silenceTransactionProfiler();
664 $serverIndexes = range( 0, $this->numServers
- 1 );
665 shuffle( $serverIndexes );
669 $keysDeletedCount = 0;
670 foreach ( $serverIndexes as $numServersDone => $serverIndex ) {
673 $db = $this->getDB( $serverIndex );
674 $this->deleteServerObjectsExpiringBefore(
682 } catch ( DBError
$e ) {
683 $this->handleWriteError( $e, $db, $serverIndex );
692 * @param IDatabase $db
693 * @param string|int $timestamp
694 * @param callable|null $progressCallback
696 * @param int $serversDoneCount
697 * @param int &$keysDeletedCount
700 private function deleteServerObjectsExpiringBefore(
705 $serversDoneCount = 0,
706 &$keysDeletedCount = 0
708 $cutoffUnix = wfTimestamp( TS_UNIX
, $timestamp );
709 $shardIndexes = range( 0, $this->shards
- 1 );
710 shuffle( $shardIndexes );
712 foreach ( $shardIndexes as $numShardsDone => $shardIndex ) {
713 $continue = null; // last exptime
714 $lag = null; // purge lag
717 $this->getTableNameByShard( $shardIndex ),
718 [ 'keyname', 'exptime' ],
720 [ 'exptime < ' . $db->addQuotes( $db->timestamp( $cutoffUnix ) ) ],
721 $continue ?
[ 'exptime >= ' . $db->addQuotes( $continue ) ] : []
724 [ 'LIMIT' => min( $limit, 100 ), 'ORDER BY' => 'exptime' ]
727 if ( $res->numRows() ) {
728 $row = $res->current();
729 if ( $lag === null ) {
730 $lag = max( $cutoffUnix - wfTimestamp( TS_UNIX
, $row->exptime
), 1 );
734 foreach ( $res as $row ) {
735 $keys[] = $row->keyname
;
736 $continue = $row->exptime
;
740 $this->getTableNameByShard( $shardIndex ),
742 'exptime < ' . $db->addQuotes( $db->timestamp( $cutoffUnix ) ),
747 $keysDeletedCount +
= $db->affectedRows();
750 if ( is_callable( $progressCallback ) ) {
752 $remainingLag = $cutoffUnix - wfTimestamp( TS_UNIX
, $continue );
753 $processedLag = max( $lag - $remainingLag, 0 );
754 $doneRatio = ( $numShardsDone +
$processedLag / $lag ) / $this->shards
;
759 $overallRatio = ( $doneRatio / $this->numServers
)
760 +
( $serversDoneCount / $this->numServers
);
761 call_user_func( $progressCallback, $overallRatio * 100 );
763 } while ( $res->numRows() && $keysDeletedCount < $limit );
768 * Delete content of shard tables in every server.
769 * Return true if the operation is successful, false otherwise.
772 public function deleteAll() {
773 /** @noinspection PhpUnusedLocalVariableInspection */
774 $silenceScope = $this->silenceTransactionProfiler();
775 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
778 $db = $this->getDB( $serverIndex );
779 for ( $i = 0; $i < $this->shards
; $i++
) {
780 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__
);
782 } catch ( DBError
$e ) {
783 $this->handleWriteError( $e, $db, $serverIndex );
790 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
791 // Avoid deadlocks and allow lock reentry if specified
792 if ( isset( $this->locks
[$key] ) ) {
793 if ( $rclass != '' && $this->locks
[$key]['class'] === $rclass ) {
794 ++
$this->locks
[$key]['depth'];
801 list( $serverIndex ) = $this->getTableByKey( $key );
805 $db = $this->getDB( $serverIndex );
806 $ok = $db->lock( $key, __METHOD__
, $timeout );
808 $this->locks
[$key] = [ 'class' => $rclass, 'depth' => 1 ];
811 $this->logger
->warning(
812 __METHOD__
. " failed due to timeout for {key}.",
813 [ 'key' => $key, 'timeout' => $timeout ]
817 } catch ( DBError
$e ) {
818 $this->handleWriteError( $e, $db, $serverIndex );
825 public function unlock( $key ) {
826 if ( !isset( $this->locks
[$key] ) ) {
830 if ( --$this->locks
[$key]['depth'] <= 0 ) {
831 unset( $this->locks
[$key] );
833 list( $serverIndex ) = $this->getTableByKey( $key );
837 $db = $this->getDB( $serverIndex );
838 $ok = $db->unlock( $key, __METHOD__
);
840 $this->logger
->warning(
841 __METHOD__
. ' failed to release lock for {key}.',
845 } catch ( DBError
$e ) {
846 $this->handleWriteError( $e, $db, $serverIndex );
857 * Serialize an object and, if possible, compress the representation.
858 * On typical message and page data, this can provide a 3X decrease
859 * in storage requirements.
864 protected function serialize( $data ) {
865 $serial = serialize( $data );
867 if ( function_exists( 'gzdeflate' ) ) {
868 return gzdeflate( $serial );
875 * Unserialize and, if necessary, decompress an object.
876 * @param string $serial
879 protected function unserialize( $serial ) {
880 if ( function_exists( 'gzinflate' ) ) {
881 Wikimedia\
suppressWarnings();
882 $decomp = gzinflate( $serial );
883 Wikimedia\restoreWarnings
();
885 if ( $decomp !== false ) {
890 $ret = unserialize( $serial );
896 * Handle a DBError which occurred during a read operation.
898 * @param DBError $exception
899 * @param int $serverIndex
901 protected function handleReadError( DBError
$exception, $serverIndex ) {
902 if ( $exception instanceof DBConnectionError
) {
903 $this->markServerDown( $exception, $serverIndex );
906 $this->setAndLogDBError( $exception );
910 * Handle a DBQueryError which occurred during a write operation.
912 * @param DBError $exception
913 * @param IDatabase|null $db DB handle or null if connection failed
914 * @param int $serverIndex
917 protected function handleWriteError( DBError
$exception, $db, $serverIndex ) {
918 if ( !( $db instanceof IDatabase
) ) {
919 $this->markServerDown( $exception, $serverIndex );
922 $this->setAndLogDBError( $exception );
926 * @param DBError $exception
928 private function setAndLogDBError( DBError
$exception ) {
929 $this->logger
->error( "DBError: {$exception->getMessage()}" );
930 if ( $exception instanceof DBConnectionError
) {
931 $this->setLastError( BagOStuff
::ERR_UNREACHABLE
);
932 $this->logger
->debug( __METHOD__
. ": ignoring connection error" );
934 $this->setLastError( BagOStuff
::ERR_UNEXPECTED
);
935 $this->logger
->debug( __METHOD__
. ": ignoring query error" );
940 * Mark a server down due to a DBConnectionError exception
942 * @param DBError $exception
943 * @param int $serverIndex
945 protected function markServerDown( DBError
$exception, $serverIndex ) {
946 unset( $this->conns
[$serverIndex] ); // bug T103435
948 if ( isset( $this->connFailureTimes
[$serverIndex] ) ) {
949 if ( time() - $this->connFailureTimes
[$serverIndex] >= 60 ) {
950 unset( $this->connFailureTimes
[$serverIndex] );
951 unset( $this->connFailureErrors
[$serverIndex] );
953 $this->logger
->debug( __METHOD__
. ": Server #$serverIndex already down" );
958 $this->logger
->info( __METHOD__
. ": Server #$serverIndex down until " . ( $now +
60 ) );
959 $this->connFailureTimes
[$serverIndex] = $now;
960 $this->connFailureErrors
[$serverIndex] = $exception;
964 * Create shard tables. For use from eval.php.
966 public function createTables() {
967 for ( $serverIndex = 0; $serverIndex < $this->numServers
; $serverIndex++
) {
968 $db = $this->getDB( $serverIndex );
969 if ( $db->getType() !== 'mysql' ) {
970 throw new MWException( __METHOD__
. ' is not supported on this DB server' );
973 for ( $i = 0; $i < $this->shards
; $i++
) {
975 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
976 ' LIKE ' . $db->tableName( 'objectcache' ),
983 * @return bool Whether the main DB is used, e.g. wfGetDB( DB_MASTER )
985 protected function usesMainDB() {
986 return !$this->serverInfos
;
989 protected function waitForReplication() {
990 if ( !$this->usesMainDB() ) {
991 // Custom DB server list; probably doesn't use replication
995 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
996 if ( $lb->getServerCount() <= 1 ) {
997 return true; // no replica DBs
1000 // Main LB is used; wait for any replica DBs to catch up
1002 $masterPos = $lb->getMasterPos();
1003 if ( !$masterPos ) {
1004 return true; // not applicable
1007 $loop = new WaitConditionLoop(
1008 function () use ( $lb, $masterPos ) {
1009 return $lb->waitForAll( $masterPos, 1 );
1012 $this->busyCallbacks
1015 return ( $loop->invoke() === $loop::CONDITION_REACHED
);
1016 } catch ( DBError
$e ) {
1017 $this->setAndLogDBError( $e );
1024 * Returns a ScopedCallback which resets the silence flag in the transaction profiler when it is
1025 * destroyed on the end of a scope, for example on return or throw
1026 * @return ScopedCallback
1029 protected function silenceTransactionProfiler() {
1030 $trxProfiler = Profiler
::instance()->getTransactionProfiler();
1031 $oldSilenced = $trxProfiler->setSilenced( true );
1032 return new ScopedCallback( function () use ( $trxProfiler, $oldSilenced ) {
1033 $trxProfiler->setSilenced( $oldSilenced );