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