Fix missing commit() flag in postgres savepoint class
[mediawiki.git] / includes / objectcache / SqlBagOStuff.php
blob47dae788501c56f5a56b098fa5d561f8f1c4cf2e
1 <?php
2 /**
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
20 * @file
21 * @ingroup Cache
24 use \MediaWiki\MediaWikiServices;
26 /**
27 * Class to store objects in the database
29 * @ingroup Cache
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;
36 /** @var int */
37 protected $numServers;
38 /** @var int */
39 protected $lastExpireAll = 0;
40 /** @var int */
41 protected $purgePeriod = 100;
42 /** @var int */
43 protected $shards = 1;
44 /** @var string */
45 protected $tableName = 'objectcache';
46 /** @var bool */
47 protected $replicaOnly = false;
48 /** @var int */
49 protected $syncTimeout = 3;
51 /** @var LoadBalancer|null */
52 protected $separateMainLB;
53 /** @var array */
54 protected $conns;
55 /** @var array UNIX timestamps */
56 protected $connFailureTimes = [];
57 /** @var array Exceptions */
58 protected $connFailureErrors = [];
60 /**
61 * Constructor. Parameters are:
62 * - server: A server info structure in the format required by each
63 * element in $wgDBServers.
65 * - servers: An array of server info structures describing a set of database servers
66 * to distribute keys to. If this is specified, the "server" option will be
67 * ignored. If string keys are used, then they will be used for consistent
68 * hashing *instead* of the host name (from the server config). This is useful
69 * when a cluster is replicated to another site (with different host names)
70 * but each server has a corresponding replica in the other cluster.
72 * - purgePeriod: The average number of object cache requests in between
73 * garbage collection operations, where expired entries
74 * are removed from the database. Or in other words, the
75 * reciprocal of the probability of purging on any given
76 * request. If this is set to zero, purging will never be
77 * done.
79 * - tableName: The table name to use, default is "objectcache".
81 * - shards: The number of tables to use for data storage on each server.
82 * If this is more than 1, table names will be formed in the style
83 * objectcacheNNN where NNN is the shard index, between 0 and
84 * shards-1. The number of digits will be the minimum number
85 * required to hold the largest shard index. Data will be
86 * distributed across all tables by key hash. This is for
87 * MySQL bugs 61735 and 61736.
88 * - slaveOnly: Whether to only use replica DBs and avoid triggering
89 * garbage collection logic of expired items. This only
90 * makes sense if the primary DB is used and only if get()
91 * calls will be used. This is used by ReplicatedBagOStuff.
92 * - syncTimeout: Max seconds to wait for replica DBs to catch up for WRITE_SYNC.
94 * @param array $params
96 public function __construct( $params ) {
97 parent::__construct( $params );
99 $this->attrMap[self::ATTR_EMULATION] = self::QOS_EMULATION_SQL;
100 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_NONE;
102 if ( isset( $params['servers'] ) ) {
103 $this->serverInfos = [];
104 $this->serverTags = [];
105 $this->numServers = count( $params['servers'] );
106 $index = 0;
107 foreach ( $params['servers'] as $tag => $info ) {
108 $this->serverInfos[$index] = $info;
109 if ( is_string( $tag ) ) {
110 $this->serverTags[$index] = $tag;
111 } else {
112 $this->serverTags[$index] = isset( $info['host'] ) ? $info['host'] : "#$index";
114 ++$index;
116 } elseif ( isset( $params['server'] ) ) {
117 $this->serverInfos = [ $params['server'] ];
118 $this->numServers = count( $this->serverInfos );
119 } else {
120 // Default to using the main wiki's database servers
121 $this->serverInfos = false;
122 $this->numServers = 1;
123 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_BE;
125 if ( isset( $params['purgePeriod'] ) ) {
126 $this->purgePeriod = intval( $params['purgePeriod'] );
128 if ( isset( $params['tableName'] ) ) {
129 $this->tableName = $params['tableName'];
131 if ( isset( $params['shards'] ) ) {
132 $this->shards = intval( $params['shards'] );
134 if ( isset( $params['syncTimeout'] ) ) {
135 $this->syncTimeout = $params['syncTimeout'];
137 $this->replicaOnly = !empty( $params['slaveOnly'] );
140 protected function getSeparateMainLB() {
141 global $wgDBtype;
143 if ( $wgDBtype === 'mysql' && $this->usesMainDB() ) {
144 if ( !$this->separateMainLB ) {
145 // We must keep a separate connection to MySQL in order to avoid deadlocks
146 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
147 $this->separateMainLB = $lbFactory->newMainLB();
149 return $this->separateMainLB;
150 } else {
151 // However, SQLite has an opposite behavior. And PostgreSQL needs to know
152 // if we are in transaction or not (@TODO: find some PostgreSQL work-around).
153 return null;
158 * Get a connection to the specified database
160 * @param int $serverIndex
161 * @return IDatabase
162 * @throws MWException
164 protected function getDB( $serverIndex ) {
165 if ( !isset( $this->conns[$serverIndex] ) ) {
166 if ( $serverIndex >= $this->numServers ) {
167 throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
170 # Don't keep timing out trying to connect for each call if the DB is down
171 if ( isset( $this->connFailureErrors[$serverIndex] )
172 && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
174 throw $this->connFailureErrors[$serverIndex];
177 # If server connection info was given, use that
178 if ( $this->serverInfos ) {
179 $info = $this->serverInfos[$serverIndex];
180 $type = isset( $info['type'] ) ? $info['type'] : 'mysql';
181 $host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
182 $this->logger->debug( __CLASS__ . ": connecting to $host" );
183 // Use a blank trx profiler to ignore expections as this is a cache
184 $info['trxProfiler'] = new TransactionProfiler();
185 $db = Database::factory( $type, $info );
186 $db->clearFlag( DBO_TRX );
187 } else {
188 $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER;
189 if ( $this->getSeparateMainLB() ) {
190 $db = $this->getSeparateMainLB()->getConnection( $index );
191 $db->clearFlag( DBO_TRX ); // auto-commit mode
192 } else {
193 $db = wfGetDB( $index );
194 // Can't mess with transaction rounds (DBO_TRX) :(
197 $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
198 $this->conns[$serverIndex] = $db;
201 return $this->conns[$serverIndex];
205 * Get the server index and table name for a given key
206 * @param string $key
207 * @return array Server index and table name
209 protected function getTableByKey( $key ) {
210 if ( $this->shards > 1 ) {
211 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
212 $tableIndex = $hash % $this->shards;
213 } else {
214 $tableIndex = 0;
216 if ( $this->numServers > 1 ) {
217 $sortedServers = $this->serverTags;
218 ArrayUtils::consistentHashSort( $sortedServers, $key );
219 reset( $sortedServers );
220 $serverIndex = key( $sortedServers );
221 } else {
222 $serverIndex = 0;
224 return [ $serverIndex, $this->getTableNameByShard( $tableIndex ) ];
228 * Get the table name for a given shard index
229 * @param int $index
230 * @return string
232 protected function getTableNameByShard( $index ) {
233 if ( $this->shards > 1 ) {
234 $decimals = strlen( $this->shards - 1 );
235 return $this->tableName .
236 sprintf( "%0{$decimals}d", $index );
237 } else {
238 return $this->tableName;
242 protected function doGet( $key, $flags = 0 ) {
243 $casToken = null;
245 return $this->getWithToken( $key, $casToken, $flags );
248 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
249 $values = $this->getMulti( [ $key ] );
250 if ( array_key_exists( $key, $values ) ) {
251 $casToken = $values[$key];
252 return $values[$key];
254 return false;
257 public function getMulti( array $keys, $flags = 0 ) {
258 $values = []; // array of (key => value)
260 $keysByTable = [];
261 foreach ( $keys as $key ) {
262 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
263 $keysByTable[$serverIndex][$tableName][] = $key;
266 $this->garbageCollect(); // expire old entries if any
268 $dataRows = [];
269 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
270 try {
271 $db = $this->getDB( $serverIndex );
272 foreach ( $serverKeys as $tableName => $tableKeys ) {
273 $res = $db->select( $tableName,
274 [ 'keyname', 'value', 'exptime' ],
275 [ 'keyname' => $tableKeys ],
276 __METHOD__,
277 // Approximate write-on-the-fly BagOStuff API via blocking.
278 // This approximation fails if a ROLLBACK happens (which is rare).
279 // We do not want to flush the TRX as that can break callers.
280 $db->trxLevel() ? [ 'LOCK IN SHARE MODE' ] : []
282 if ( $res === false ) {
283 continue;
285 foreach ( $res as $row ) {
286 $row->serverIndex = $serverIndex;
287 $row->tableName = $tableName;
288 $dataRows[$row->keyname] = $row;
291 } catch ( DBError $e ) {
292 $this->handleReadError( $e, $serverIndex );
296 foreach ( $keys as $key ) {
297 if ( isset( $dataRows[$key] ) ) { // HIT?
298 $row = $dataRows[$key];
299 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
300 $db = null;
301 try {
302 $db = $this->getDB( $row->serverIndex );
303 if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
304 $this->debug( "get: key has expired" );
305 } else { // HIT
306 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
308 } catch ( DBQueryError $e ) {
309 $this->handleWriteError( $e, $db, $row->serverIndex );
311 } else { // MISS
312 $this->debug( 'get: no matching rows' );
316 return $values;
319 public function setMulti( array $data, $expiry = 0 ) {
320 $keysByTable = [];
321 foreach ( $data as $key => $value ) {
322 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
323 $keysByTable[$serverIndex][$tableName][] = $key;
326 $this->garbageCollect(); // expire old entries if any
328 $result = true;
329 $exptime = (int)$expiry;
330 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
331 $db = null;
332 try {
333 $db = $this->getDB( $serverIndex );
334 } catch ( DBError $e ) {
335 $this->handleWriteError( $e, $db, $serverIndex );
336 $result = false;
337 continue;
340 if ( $exptime < 0 ) {
341 $exptime = 0;
344 if ( $exptime == 0 ) {
345 $encExpiry = $this->getMaxDateTime( $db );
346 } else {
347 $exptime = $this->convertExpiry( $exptime );
348 $encExpiry = $db->timestamp( $exptime );
350 foreach ( $serverKeys as $tableName => $tableKeys ) {
351 $rows = [];
352 foreach ( $tableKeys as $key ) {
353 $rows[] = [
354 'keyname' => $key,
355 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
356 'exptime' => $encExpiry,
360 try {
361 $db->replace(
362 $tableName,
363 [ 'keyname' ],
364 $rows,
365 __METHOD__
367 } catch ( DBError $e ) {
368 $this->handleWriteError( $e, $db, $serverIndex );
369 $result = false;
376 return $result;
379 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
380 $ok = $this->setMulti( [ $key => $value ], $exptime );
381 if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
382 $ok = $this->waitForReplication() && $ok;
385 return $ok;
388 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
389 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
390 $db = null;
391 try {
392 $db = $this->getDB( $serverIndex );
393 $exptime = intval( $exptime );
395 if ( $exptime < 0 ) {
396 $exptime = 0;
399 if ( $exptime == 0 ) {
400 $encExpiry = $this->getMaxDateTime( $db );
401 } else {
402 $exptime = $this->convertExpiry( $exptime );
403 $encExpiry = $db->timestamp( $exptime );
405 // (bug 24425) use a replace if the db supports it instead of
406 // delete/insert to avoid clashes with conflicting keynames
407 $db->update(
408 $tableName,
410 'keyname' => $key,
411 'value' => $db->encodeBlob( $this->serialize( $value ) ),
412 'exptime' => $encExpiry
415 'keyname' => $key,
416 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
418 __METHOD__
420 } catch ( DBQueryError $e ) {
421 $this->handleWriteError( $e, $db, $serverIndex );
423 return false;
426 return (bool)$db->affectedRows();
429 public function delete( $key ) {
430 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
431 $db = null;
432 try {
433 $db = $this->getDB( $serverIndex );
434 $db->delete(
435 $tableName,
436 [ 'keyname' => $key ],
437 __METHOD__ );
438 } catch ( DBError $e ) {
439 $this->handleWriteError( $e, $db, $serverIndex );
440 return false;
443 return true;
446 public function incr( $key, $step = 1 ) {
447 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
448 $db = null;
449 try {
450 $db = $this->getDB( $serverIndex );
451 $step = intval( $step );
452 $row = $db->selectRow(
453 $tableName,
454 [ 'value', 'exptime' ],
455 [ 'keyname' => $key ],
456 __METHOD__,
457 [ 'FOR UPDATE' ] );
458 if ( $row === false ) {
459 // Missing
461 return null;
463 $db->delete( $tableName, [ 'keyname' => $key ], __METHOD__ );
464 if ( $this->isExpired( $db, $row->exptime ) ) {
465 // Expired, do not reinsert
467 return null;
470 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
471 $newValue = $oldValue + $step;
472 $db->insert( $tableName,
474 'keyname' => $key,
475 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
476 'exptime' => $row->exptime
477 ], __METHOD__, 'IGNORE' );
479 if ( $db->affectedRows() == 0 ) {
480 // Race condition. See bug 28611
481 $newValue = null;
483 } catch ( DBError $e ) {
484 $this->handleWriteError( $e, $db, $serverIndex );
485 return null;
488 return $newValue;
491 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
492 $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts );
493 if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
494 $ok = $this->waitForReplication() && $ok;
497 return $ok;
500 public function changeTTL( $key, $expiry = 0 ) {
501 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
502 $db = null;
503 try {
504 $db = $this->getDB( $serverIndex );
505 $db->update(
506 $tableName,
507 [ 'exptime' => $db->timestamp( $this->convertExpiry( $expiry ) ) ],
508 [ 'keyname' => $key, 'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
509 __METHOD__
511 if ( $db->affectedRows() == 0 ) {
512 return false;
514 } catch ( DBError $e ) {
515 $this->handleWriteError( $e, $db, $serverIndex );
516 return false;
519 return true;
523 * @param IDatabase $db
524 * @param string $exptime
525 * @return bool
527 protected function isExpired( $db, $exptime ) {
528 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
532 * @param IDatabase $db
533 * @return string
535 protected function getMaxDateTime( $db ) {
536 if ( time() > 0x7fffffff ) {
537 return $db->timestamp( 1 << 62 );
538 } else {
539 return $db->timestamp( 0x7fffffff );
543 protected function garbageCollect() {
544 if ( !$this->purgePeriod || $this->replicaOnly ) {
545 // Disabled
546 return;
548 // Only purge on one in every $this->purgePeriod requests.
549 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
550 return;
552 $now = time();
553 // Avoid repeating the delete within a few seconds
554 if ( $now > ( $this->lastExpireAll + 1 ) ) {
555 $this->lastExpireAll = $now;
556 $this->expireAll();
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
568 * @return bool
570 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
571 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
572 $db = null;
573 try {
574 $db = $this->getDB( $serverIndex );
575 $dbTimestamp = $db->timestamp( $timestamp );
576 $totalSeconds = false;
577 $baseConds = [ 'exptime < ' . $db->addQuotes( $dbTimestamp ) ];
578 for ( $i = 0; $i < $this->shards; $i++ ) {
579 $maxExpTime = false;
580 while ( true ) {
581 $conds = $baseConds;
582 if ( $maxExpTime !== false ) {
583 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
585 $rows = $db->select(
586 $this->getTableNameByShard( $i ),
587 [ 'keyname', 'exptime' ],
588 $conds,
589 __METHOD__,
590 [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ] );
591 if ( $rows === false || !$rows->numRows() ) {
592 break;
594 $keys = [];
595 $row = $rows->current();
596 $minExpTime = $row->exptime;
597 if ( $totalSeconds === false ) {
598 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
599 - wfTimestamp( TS_UNIX, $minExpTime );
601 foreach ( $rows as $row ) {
602 $keys[] = $row->keyname;
603 $maxExpTime = $row->exptime;
606 $db->delete(
607 $this->getTableNameByShard( $i ),
609 'exptime >= ' . $db->addQuotes( $minExpTime ),
610 'exptime < ' . $db->addQuotes( $dbTimestamp ),
611 'keyname' => $keys
613 __METHOD__ );
615 if ( $progressCallback ) {
616 if ( intval( $totalSeconds ) === 0 ) {
617 $percent = 0;
618 } else {
619 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
620 - wfTimestamp( TS_UNIX, $maxExpTime );
621 if ( $remainingSeconds > $totalSeconds ) {
622 $totalSeconds = $remainingSeconds;
624 $processedSeconds = $totalSeconds - $remainingSeconds;
625 $percent = ( $i + $processedSeconds / $totalSeconds )
626 / $this->shards * 100;
628 $percent = ( $percent / $this->numServers )
629 + ( $serverIndex / $this->numServers * 100 );
630 call_user_func( $progressCallback, $percent );
634 } catch ( DBError $e ) {
635 $this->handleWriteError( $e, $db, $serverIndex );
636 return false;
639 return true;
643 * Delete content of shard tables in every server.
644 * Return true if the operation is successful, false otherwise.
645 * @return bool
647 public function deleteAll() {
648 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
649 $db = null;
650 try {
651 $db = $this->getDB( $serverIndex );
652 for ( $i = 0; $i < $this->shards; $i++ ) {
653 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
655 } catch ( DBError $e ) {
656 $this->handleWriteError( $e, $db, $serverIndex );
657 return false;
660 return true;
664 * Serialize an object and, if possible, compress the representation.
665 * On typical message and page data, this can provide a 3X decrease
666 * in storage requirements.
668 * @param mixed $data
669 * @return string
671 protected function serialize( &$data ) {
672 $serial = serialize( $data );
674 if ( function_exists( 'gzdeflate' ) ) {
675 return gzdeflate( $serial );
676 } else {
677 return $serial;
682 * Unserialize and, if necessary, decompress an object.
683 * @param string $serial
684 * @return mixed
686 protected function unserialize( $serial ) {
687 if ( function_exists( 'gzinflate' ) ) {
688 MediaWiki\suppressWarnings();
689 $decomp = gzinflate( $serial );
690 MediaWiki\restoreWarnings();
692 if ( false !== $decomp ) {
693 $serial = $decomp;
697 $ret = unserialize( $serial );
699 return $ret;
703 * Handle a DBError which occurred during a read operation.
705 * @param DBError $exception
706 * @param int $serverIndex
708 protected function handleReadError( DBError $exception, $serverIndex ) {
709 if ( $exception instanceof DBConnectionError ) {
710 $this->markServerDown( $exception, $serverIndex );
712 $this->logger->error( "DBError: {$exception->getMessage()}" );
713 if ( $exception instanceof DBConnectionError ) {
714 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
715 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
716 } else {
717 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
718 $this->logger->debug( __METHOD__ . ": ignoring query error" );
723 * Handle a DBQueryError which occurred during a write operation.
725 * @param DBError $exception
726 * @param IDatabase|null $db DB handle or null if connection failed
727 * @param int $serverIndex
728 * @throws Exception
730 protected function handleWriteError( DBError $exception, IDatabase $db = null, $serverIndex ) {
731 if ( !$db ) {
732 $this->markServerDown( $exception, $serverIndex );
733 } elseif ( $db->wasReadOnlyError() ) {
734 if ( $db->trxLevel() && $this->usesMainDB() ) {
735 // Errors like deadlocks and connection drops already cause rollback.
736 // For consistency, we have no choice but to throw an error and trigger
737 // complete rollback if the main DB is also being used as the cache DB.
738 throw $exception;
742 $this->logger->error( "DBError: {$exception->getMessage()}" );
743 if ( $exception instanceof DBConnectionError ) {
744 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
745 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
746 } else {
747 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
748 $this->logger->debug( __METHOD__ . ": ignoring query error" );
753 * Mark a server down due to a DBConnectionError exception
755 * @param DBError $exception
756 * @param int $serverIndex
758 protected function markServerDown( DBError $exception, $serverIndex ) {
759 unset( $this->conns[$serverIndex] ); // bug T103435
761 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
762 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
763 unset( $this->connFailureTimes[$serverIndex] );
764 unset( $this->connFailureErrors[$serverIndex] );
765 } else {
766 $this->logger->debug( __METHOD__ . ": Server #$serverIndex already down" );
767 return;
770 $now = time();
771 $this->logger->info( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) );
772 $this->connFailureTimes[$serverIndex] = $now;
773 $this->connFailureErrors[$serverIndex] = $exception;
777 * Create shard tables. For use from eval.php.
779 public function createTables() {
780 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
781 $db = $this->getDB( $serverIndex );
782 if ( $db->getType() !== 'mysql' ) {
783 throw new MWException( __METHOD__ . ' is not supported on this DB server' );
786 for ( $i = 0; $i < $this->shards; $i++ ) {
787 $db->query(
788 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
789 ' LIKE ' . $db->tableName( 'objectcache' ),
790 __METHOD__ );
796 * @return bool Whether the main DB is used, e.g. wfGetDB( DB_MASTER )
798 protected function usesMainDB() {
799 return !$this->serverInfos;
802 protected function waitForReplication() {
803 if ( !$this->usesMainDB() ) {
804 // Custom DB server list; probably doesn't use replication
805 return true;
808 $lb = $this->getSeparateMainLB()
809 ?: 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 $loop = new WaitConditionLoop(
819 function () use ( $lb, $masterPos ) {
820 return $lb->waitForAll( $masterPos, 1 );
822 $this->syncTimeout,
823 $this->busyCallbacks
826 return ( $loop->invoke() === $loop::CONDITION_REACHED );