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