TableDiffFormatter: Don't repeatedly call array_shift()
[lhc/web/wiklou.git] / includes / objectcache / SqlBagOStuff.php
1 <?php
2 /**
3 * Object caching using a SQL database.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Class to store objects in the database
26 *
27 * @ingroup Cache
28 */
29 class SqlBagOStuff extends BagOStuff {
30 /** @var array */
31 protected $serverInfos;
32 /** @var array */
33 protected $serverNames;
34 /** @var int */
35 protected $numServers;
36 /** @var int */
37 protected $lastExpireAll = 0;
38 /** @var int */
39 protected $purgePeriod = 100;
40 /** @var int */
41 protected $shards = 1;
42 /** @var string */
43 protected $tableName = 'objectcache';
44 /** @var bool */
45 protected $slaveOnly = false;
46 /** @var int */
47 protected $syncTimeout = 3;
48
49 /** @var array */
50 protected $conns;
51 /** @var array UNIX timestamps */
52 protected $connFailureTimes = [];
53 /** @var array Exceptions */
54 protected $connFailureErrors = [];
55
56 /**
57 * Constructor. Parameters are:
58 * - server: A server info structure in the format required by each
59 * element in $wgDBServers.
60 *
61 * - servers: An array of server info structures describing a set of
62 * database servers to distribute keys to. If this is
63 * specified, the "server" option will be ignored.
64 *
65 * - purgePeriod: The average number of object cache requests in between
66 * garbage collection operations, where expired entries
67 * are removed from the database. Or in other words, the
68 * reciprocal of the probability of purging on any given
69 * request. If this is set to zero, purging will never be
70 * done.
71 *
72 * - tableName: The table name to use, default is "objectcache".
73 *
74 * - shards: The number of tables to use for data storage on each server.
75 * If this is more than 1, table names will be formed in the style
76 * objectcacheNNN where NNN is the shard index, between 0 and
77 * shards-1. The number of digits will be the minimum number
78 * required to hold the largest shard index. Data will be
79 * distributed across all tables by key hash. This is for
80 * MySQL bugs 61735 and 61736.
81 * - slaveOnly: Whether to only use slave DBs and avoid triggering
82 * garbage collection logic of expired items. This only
83 * makes sense if the primary DB is used and only if get()
84 * calls will be used. This is used by ReplicatedBagOStuff.
85 * - syncTimeout: Max seconds to wait for slaves to catch up for WRITE_SYNC.
86 *
87 * @param array $params
88 */
89 public function __construct( $params ) {
90 parent::__construct( $params );
91 if ( isset( $params['servers'] ) ) {
92 $this->serverInfos = $params['servers'];
93 $this->numServers = count( $this->serverInfos );
94 $this->serverNames = [];
95 foreach ( $this->serverInfos as $i => $info ) {
96 $this->serverNames[$i] = isset( $info['host'] ) ? $info['host'] : "#$i";
97 }
98 } elseif ( isset( $params['server'] ) ) {
99 $this->serverInfos = [ $params['server'] ];
100 $this->numServers = count( $this->serverInfos );
101 } else {
102 $this->serverInfos = false;
103 $this->numServers = 1;
104 }
105 if ( isset( $params['purgePeriod'] ) ) {
106 $this->purgePeriod = intval( $params['purgePeriod'] );
107 }
108 if ( isset( $params['tableName'] ) ) {
109 $this->tableName = $params['tableName'];
110 }
111 if ( isset( $params['shards'] ) ) {
112 $this->shards = intval( $params['shards'] );
113 }
114 if ( isset( $params['syncTimeout'] ) ) {
115 $this->syncTimeout = $params['syncTimeout'];
116 }
117 $this->slaveOnly = !empty( $params['slaveOnly'] );
118 }
119
120 /**
121 * Get a connection to the specified database
122 *
123 * @param int $serverIndex
124 * @return IDatabase
125 * @throws MWException
126 */
127 protected function getDB( $serverIndex ) {
128 if ( !isset( $this->conns[$serverIndex] ) ) {
129 if ( $serverIndex >= $this->numServers ) {
130 throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
131 }
132
133 # Don't keep timing out trying to connect for each call if the DB is down
134 if ( isset( $this->connFailureErrors[$serverIndex] )
135 && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
136 ) {
137 throw $this->connFailureErrors[$serverIndex];
138 }
139
140 # If server connection info was given, use that
141 if ( $this->serverInfos ) {
142 $info = $this->serverInfos[$serverIndex];
143 $type = isset( $info['type'] ) ? $info['type'] : 'mysql';
144 $host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
145 $this->logger->debug( __CLASS__ . ": connecting to $host" );
146 // Use a blank trx profiler to ignore expections as this is a cache
147 $info['trxProfiler'] = new TransactionProfiler();
148 $db = DatabaseBase::factory( $type, $info );
149 $db->clearFlag( DBO_TRX );
150 } else {
151 // We must keep a separate connection to MySQL in order to avoid deadlocks
152 // However, SQLite has an opposite behavior. And PostgreSQL needs to know
153 // if we are in transaction or not (@TODO: find some work-around).
154 $index = $this->slaveOnly ? DB_SLAVE : DB_MASTER;
155 if ( wfGetDB( $index )->getType() == 'mysql' ) {
156 $lb = wfGetLBFactory()->newMainLB();
157 $db = $lb->getConnection( $index );
158 $db->clearFlag( DBO_TRX ); // auto-commit mode
159 } else {
160 $db = wfGetDB( $index );
161 }
162 }
163 $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
164 $this->conns[$serverIndex] = $db;
165 }
166
167 return $this->conns[$serverIndex];
168 }
169
170 /**
171 * Get the server index and table name for a given key
172 * @param string $key
173 * @return array Server index and table name
174 */
175 protected function getTableByKey( $key ) {
176 if ( $this->shards > 1 ) {
177 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
178 $tableIndex = $hash % $this->shards;
179 } else {
180 $tableIndex = 0;
181 }
182 if ( $this->numServers > 1 ) {
183 $sortedServers = $this->serverNames;
184 ArrayUtils::consistentHashSort( $sortedServers, $key );
185 reset( $sortedServers );
186 $serverIndex = key( $sortedServers );
187 } else {
188 $serverIndex = 0;
189 }
190 return [ $serverIndex, $this->getTableNameByShard( $tableIndex ) ];
191 }
192
193 /**
194 * Get the table name for a given shard index
195 * @param int $index
196 * @return string
197 */
198 protected function getTableNameByShard( $index ) {
199 if ( $this->shards > 1 ) {
200 $decimals = strlen( $this->shards - 1 );
201 return $this->tableName .
202 sprintf( "%0{$decimals}d", $index );
203 } else {
204 return $this->tableName;
205 }
206 }
207
208 protected function doGet( $key, $flags = 0 ) {
209 $casToken = null;
210
211 return $this->getWithToken( $key, $casToken, $flags );
212 }
213
214 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
215 $values = $this->getMulti( [ $key ] );
216 if ( array_key_exists( $key, $values ) ) {
217 $casToken = $values[$key];
218 return $values[$key];
219 }
220 return false;
221 }
222
223 public function getMulti( array $keys, $flags = 0 ) {
224 $values = []; // array of (key => value)
225
226 $keysByTable = [];
227 foreach ( $keys as $key ) {
228 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
229 $keysByTable[$serverIndex][$tableName][] = $key;
230 }
231
232 $this->garbageCollect(); // expire old entries if any
233
234 $dataRows = [];
235 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
236 try {
237 $db = $this->getDB( $serverIndex );
238 foreach ( $serverKeys as $tableName => $tableKeys ) {
239 $res = $db->select( $tableName,
240 [ 'keyname', 'value', 'exptime' ],
241 [ 'keyname' => $tableKeys ],
242 __METHOD__,
243 // Approximate write-on-the-fly BagOStuff API via blocking.
244 // This approximation fails if a ROLLBACK happens (which is rare).
245 // We do not want to flush the TRX as that can break callers.
246 $db->trxLevel() ? [ 'LOCK IN SHARE MODE' ] : []
247 );
248 if ( $res === false ) {
249 continue;
250 }
251 foreach ( $res as $row ) {
252 $row->serverIndex = $serverIndex;
253 $row->tableName = $tableName;
254 $dataRows[$row->keyname] = $row;
255 }
256 }
257 } catch ( DBError $e ) {
258 $this->handleReadError( $e, $serverIndex );
259 }
260 }
261
262 foreach ( $keys as $key ) {
263 if ( isset( $dataRows[$key] ) ) { // HIT?
264 $row = $dataRows[$key];
265 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
266 try {
267 $db = $this->getDB( $row->serverIndex );
268 if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
269 $this->debug( "get: key has expired" );
270 } else { // HIT
271 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
272 }
273 } catch ( DBQueryError $e ) {
274 $this->handleWriteError( $e, $row->serverIndex );
275 }
276 } else { // MISS
277 $this->debug( 'get: no matching rows' );
278 }
279 }
280
281 return $values;
282 }
283
284 public function setMulti( array $data, $expiry = 0 ) {
285 $keysByTable = [];
286 foreach ( $data as $key => $value ) {
287 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
288 $keysByTable[$serverIndex][$tableName][] = $key;
289 }
290
291 $this->garbageCollect(); // expire old entries if any
292
293 $result = true;
294 $exptime = (int)$expiry;
295 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
296 try {
297 $db = $this->getDB( $serverIndex );
298 } catch ( DBError $e ) {
299 $this->handleWriteError( $e, $serverIndex );
300 $result = false;
301 continue;
302 }
303
304 if ( $exptime < 0 ) {
305 $exptime = 0;
306 }
307
308 if ( $exptime == 0 ) {
309 $encExpiry = $this->getMaxDateTime( $db );
310 } else {
311 $exptime = $this->convertExpiry( $exptime );
312 $encExpiry = $db->timestamp( $exptime );
313 }
314 foreach ( $serverKeys as $tableName => $tableKeys ) {
315 $rows = [];
316 foreach ( $tableKeys as $key ) {
317 $rows[] = [
318 'keyname' => $key,
319 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
320 'exptime' => $encExpiry,
321 ];
322 }
323
324 try {
325 $db->replace(
326 $tableName,
327 [ 'keyname' ],
328 $rows,
329 __METHOD__
330 );
331 } catch ( DBError $e ) {
332 $this->handleWriteError( $e, $serverIndex );
333 $result = false;
334 }
335
336 }
337
338 }
339
340 return $result;
341 }
342
343 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
344 $ok = $this->setMulti( [ $key => $value ], $exptime );
345 if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
346 $ok = $ok && $this->waitForSlaves();
347 }
348
349 return $ok;
350 }
351
352 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
353 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
354 try {
355 $db = $this->getDB( $serverIndex );
356 $exptime = intval( $exptime );
357
358 if ( $exptime < 0 ) {
359 $exptime = 0;
360 }
361
362 if ( $exptime == 0 ) {
363 $encExpiry = $this->getMaxDateTime( $db );
364 } else {
365 $exptime = $this->convertExpiry( $exptime );
366 $encExpiry = $db->timestamp( $exptime );
367 }
368 // (bug 24425) use a replace if the db supports it instead of
369 // delete/insert to avoid clashes with conflicting keynames
370 $db->update(
371 $tableName,
372 [
373 'keyname' => $key,
374 'value' => $db->encodeBlob( $this->serialize( $value ) ),
375 'exptime' => $encExpiry
376 ],
377 [
378 'keyname' => $key,
379 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
380 ],
381 __METHOD__
382 );
383 } catch ( DBQueryError $e ) {
384 $this->handleWriteError( $e, $serverIndex );
385
386 return false;
387 }
388
389 return (bool)$db->affectedRows();
390 }
391
392 public function delete( $key ) {
393 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
394 try {
395 $db = $this->getDB( $serverIndex );
396 $db->delete(
397 $tableName,
398 [ 'keyname' => $key ],
399 __METHOD__ );
400 } catch ( DBError $e ) {
401 $this->handleWriteError( $e, $serverIndex );
402 return false;
403 }
404
405 return true;
406 }
407
408 public function incr( $key, $step = 1 ) {
409 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
410 try {
411 $db = $this->getDB( $serverIndex );
412 $step = intval( $step );
413 $row = $db->selectRow(
414 $tableName,
415 [ 'value', 'exptime' ],
416 [ 'keyname' => $key ],
417 __METHOD__,
418 [ 'FOR UPDATE' ] );
419 if ( $row === false ) {
420 // Missing
421
422 return null;
423 }
424 $db->delete( $tableName, [ 'keyname' => $key ], __METHOD__ );
425 if ( $this->isExpired( $db, $row->exptime ) ) {
426 // Expired, do not reinsert
427
428 return null;
429 }
430
431 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
432 $newValue = $oldValue + $step;
433 $db->insert( $tableName,
434 [
435 'keyname' => $key,
436 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
437 'exptime' => $row->exptime
438 ], __METHOD__, 'IGNORE' );
439
440 if ( $db->affectedRows() == 0 ) {
441 // Race condition. See bug 28611
442 $newValue = null;
443 }
444 } catch ( DBError $e ) {
445 $this->handleWriteError( $e, $serverIndex );
446 return null;
447 }
448
449 return $newValue;
450 }
451
452 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
453 if ( !is_callable( $callback ) ) {
454 throw new Exception( "Got invalid callback." );
455 }
456
457 $ok = $this->mergeViaCas( $key, $callback, $exptime, $attempts );
458 if ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
459 $ok = $ok && $this->waitForSlaves();
460 }
461
462 return $ok;
463 }
464
465 /**
466 * @param IDatabase $db
467 * @param string $exptime
468 * @return bool
469 */
470 protected function isExpired( $db, $exptime ) {
471 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
472 }
473
474 /**
475 * @param IDatabase $db
476 * @return string
477 */
478 protected function getMaxDateTime( $db ) {
479 if ( time() > 0x7fffffff ) {
480 return $db->timestamp( 1 << 62 );
481 } else {
482 return $db->timestamp( 0x7fffffff );
483 }
484 }
485
486 protected function garbageCollect() {
487 if ( !$this->purgePeriod || $this->slaveOnly ) {
488 // Disabled
489 return;
490 }
491 // Only purge on one in every $this->purgePeriod requests.
492 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
493 return;
494 }
495 $now = time();
496 // Avoid repeating the delete within a few seconds
497 if ( $now > ( $this->lastExpireAll + 1 ) ) {
498 $this->lastExpireAll = $now;
499 $this->expireAll();
500 }
501 }
502
503 public function expireAll() {
504 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
505 }
506
507 /**
508 * Delete objects from the database which expire before a certain date.
509 * @param string $timestamp
510 * @param bool|callable $progressCallback
511 * @return bool
512 */
513 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
514 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
515 try {
516 $db = $this->getDB( $serverIndex );
517 $dbTimestamp = $db->timestamp( $timestamp );
518 $totalSeconds = false;
519 $baseConds = [ 'exptime < ' . $db->addQuotes( $dbTimestamp ) ];
520 for ( $i = 0; $i < $this->shards; $i++ ) {
521 $maxExpTime = false;
522 while ( true ) {
523 $conds = $baseConds;
524 if ( $maxExpTime !== false ) {
525 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
526 }
527 $rows = $db->select(
528 $this->getTableNameByShard( $i ),
529 [ 'keyname', 'exptime' ],
530 $conds,
531 __METHOD__,
532 [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ] );
533 if ( $rows === false || !$rows->numRows() ) {
534 break;
535 }
536 $keys = [];
537 $row = $rows->current();
538 $minExpTime = $row->exptime;
539 if ( $totalSeconds === false ) {
540 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
541 - wfTimestamp( TS_UNIX, $minExpTime );
542 }
543 foreach ( $rows as $row ) {
544 $keys[] = $row->keyname;
545 $maxExpTime = $row->exptime;
546 }
547
548 $db->delete(
549 $this->getTableNameByShard( $i ),
550 [
551 'exptime >= ' . $db->addQuotes( $minExpTime ),
552 'exptime < ' . $db->addQuotes( $dbTimestamp ),
553 'keyname' => $keys
554 ],
555 __METHOD__ );
556
557 if ( $progressCallback ) {
558 if ( intval( $totalSeconds ) === 0 ) {
559 $percent = 0;
560 } else {
561 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
562 - wfTimestamp( TS_UNIX, $maxExpTime );
563 if ( $remainingSeconds > $totalSeconds ) {
564 $totalSeconds = $remainingSeconds;
565 }
566 $processedSeconds = $totalSeconds - $remainingSeconds;
567 $percent = ( $i + $processedSeconds / $totalSeconds )
568 / $this->shards * 100;
569 }
570 $percent = ( $percent / $this->numServers )
571 + ( $serverIndex / $this->numServers * 100 );
572 call_user_func( $progressCallback, $percent );
573 }
574 }
575 }
576 } catch ( DBError $e ) {
577 $this->handleWriteError( $e, $serverIndex );
578 return false;
579 }
580 }
581 return true;
582 }
583
584 /**
585 * Delete content of shard tables in every server.
586 * Return true if the operation is successful, false otherwise.
587 * @return bool
588 */
589 public function deleteAll() {
590 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
591 try {
592 $db = $this->getDB( $serverIndex );
593 for ( $i = 0; $i < $this->shards; $i++ ) {
594 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
595 }
596 } catch ( DBError $e ) {
597 $this->handleWriteError( $e, $serverIndex );
598 return false;
599 }
600 }
601 return true;
602 }
603
604 /**
605 * Serialize an object and, if possible, compress the representation.
606 * On typical message and page data, this can provide a 3X decrease
607 * in storage requirements.
608 *
609 * @param mixed $data
610 * @return string
611 */
612 protected function serialize( &$data ) {
613 $serial = serialize( $data );
614
615 if ( function_exists( 'gzdeflate' ) ) {
616 return gzdeflate( $serial );
617 } else {
618 return $serial;
619 }
620 }
621
622 /**
623 * Unserialize and, if necessary, decompress an object.
624 * @param string $serial
625 * @return mixed
626 */
627 protected function unserialize( $serial ) {
628 if ( function_exists( 'gzinflate' ) ) {
629 MediaWiki\suppressWarnings();
630 $decomp = gzinflate( $serial );
631 MediaWiki\restoreWarnings();
632
633 if ( false !== $decomp ) {
634 $serial = $decomp;
635 }
636 }
637
638 $ret = unserialize( $serial );
639
640 return $ret;
641 }
642
643 /**
644 * Handle a DBError which occurred during a read operation.
645 *
646 * @param DBError $exception
647 * @param int $serverIndex
648 */
649 protected function handleReadError( DBError $exception, $serverIndex ) {
650 if ( $exception instanceof DBConnectionError ) {
651 $this->markServerDown( $exception, $serverIndex );
652 }
653 $this->logger->error( "DBError: {$exception->getMessage()}" );
654 if ( $exception instanceof DBConnectionError ) {
655 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
656 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
657 } else {
658 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
659 $this->logger->debug( __METHOD__ . ": ignoring query error" );
660 }
661 }
662
663 /**
664 * Handle a DBQueryError which occurred during a write operation.
665 *
666 * @param DBError $exception
667 * @param int $serverIndex
668 */
669 protected function handleWriteError( DBError $exception, $serverIndex ) {
670 if ( $exception instanceof DBConnectionError ) {
671 $this->markServerDown( $exception, $serverIndex );
672 }
673 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
674 if ( $exception->db->trxLevel() ) {
675 try {
676 $exception->db->rollback( __METHOD__ );
677 } catch ( DBError $e ) {
678 }
679 }
680 }
681
682 $this->logger->error( "DBError: {$exception->getMessage()}" );
683 if ( $exception instanceof DBConnectionError ) {
684 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
685 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
686 } else {
687 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
688 $this->logger->debug( __METHOD__ . ": ignoring query error" );
689 }
690 }
691
692 /**
693 * Mark a server down due to a DBConnectionError exception
694 *
695 * @param DBError $exception
696 * @param int $serverIndex
697 */
698 protected function markServerDown( $exception, $serverIndex ) {
699 unset( $this->conns[$serverIndex] ); // bug T103435
700
701 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
702 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
703 unset( $this->connFailureTimes[$serverIndex] );
704 unset( $this->connFailureErrors[$serverIndex] );
705 } else {
706 $this->logger->debug( __METHOD__ . ": Server #$serverIndex already down" );
707 return;
708 }
709 }
710 $now = time();
711 $this->logger->info( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) );
712 $this->connFailureTimes[$serverIndex] = $now;
713 $this->connFailureErrors[$serverIndex] = $exception;
714 }
715
716 /**
717 * Create shard tables. For use from eval.php.
718 */
719 public function createTables() {
720 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
721 $db = $this->getDB( $serverIndex );
722 if ( $db->getType() !== 'mysql' ) {
723 throw new MWException( __METHOD__ . ' is not supported on this DB server' );
724 }
725
726 for ( $i = 0; $i < $this->shards; $i++ ) {
727 $db->query(
728 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
729 ' LIKE ' . $db->tableName( 'objectcache' ),
730 __METHOD__ );
731 }
732 }
733 }
734
735 protected function waitForSlaves() {
736 if ( !$this->serverInfos ) {
737 // Main LB is used; wait for any slaves to catch up
738 try {
739 wfGetLBFactory()->waitForReplication( [ 'wiki' => wfWikiID() ] );
740 return true;
741 } catch ( DBReplicationWaitError $e ) {
742 return false;
743 }
744 } else {
745 // Custom DB server list; probably doesn't use replication
746 return true;
747 }
748 }
749 }