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