Don't check namespace in SpecialWantedtemplates
[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
363 /**
364 * @param string $key
365 * @param mixed $value
366 * @param int $exptime
367 * @return bool
368 */
369 public function set( $key, $value, $exptime = 0 ) {
370 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
371 try {
372 $db = $this->getDB( $serverIndex );
373 $exptime = intval( $exptime );
374
375 if ( $exptime < 0 ) {
376 $exptime = 0;
377 }
378
379 if ( $exptime == 0 ) {
380 $encExpiry = $this->getMaxDateTime( $db );
381 } else {
382 $exptime = $this->convertExpiry( $exptime );
383 $encExpiry = $db->timestamp( $exptime );
384 }
385 // (bug 24425) use a replace if the db supports it instead of
386 // delete/insert to avoid clashes with conflicting keynames
387 $db->replace(
388 $tableName,
389 array( 'keyname' ),
390 array(
391 'keyname' => $key,
392 'value' => $db->encodeBlob( $this->serialize( $value ) ),
393 'exptime' => $encExpiry
394 ), __METHOD__ );
395 } catch ( DBError $e ) {
396 $this->handleWriteError( $e, $serverIndex );
397 return false;
398 }
399
400 return true;
401 }
402
403 /**
404 * @param mixed $casToken
405 * @param string $key
406 * @param mixed $value
407 * @param int $exptime
408 * @return bool
409 */
410 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
411 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
412 try {
413 $db = $this->getDB( $serverIndex );
414 $exptime = intval( $exptime );
415
416 if ( $exptime < 0 ) {
417 $exptime = 0;
418 }
419
420 if ( $exptime == 0 ) {
421 $encExpiry = $this->getMaxDateTime( $db );
422 } else {
423 $exptime = $this->convertExpiry( $exptime );
424 $encExpiry = $db->timestamp( $exptime );
425 }
426 // (bug 24425) use a replace if the db supports it instead of
427 // delete/insert to avoid clashes with conflicting keynames
428 $db->update(
429 $tableName,
430 array(
431 'keyname' => $key,
432 'value' => $db->encodeBlob( $this->serialize( $value ) ),
433 'exptime' => $encExpiry
434 ),
435 array(
436 'keyname' => $key,
437 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
438 ),
439 __METHOD__
440 );
441 } catch ( DBQueryError $e ) {
442 $this->handleWriteError( $e, $serverIndex );
443
444 return false;
445 }
446
447 return (bool)$db->affectedRows();
448 }
449
450 /**
451 * @param string $key
452 * @return bool
453 */
454 public function delete( $key ) {
455 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
456 try {
457 $db = $this->getDB( $serverIndex );
458 $db->delete(
459 $tableName,
460 array( 'keyname' => $key ),
461 __METHOD__ );
462 } catch ( DBError $e ) {
463 $this->handleWriteError( $e, $serverIndex );
464 return false;
465 }
466
467 return true;
468 }
469
470 /**
471 * @param string $key
472 * @param int $step
473 * @return int|null
474 */
475 public function incr( $key, $step = 1 ) {
476 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
477 try {
478 $db = $this->getDB( $serverIndex );
479 $step = intval( $step );
480 $row = $db->selectRow(
481 $tableName,
482 array( 'value', 'exptime' ),
483 array( 'keyname' => $key ),
484 __METHOD__,
485 array( 'FOR UPDATE' ) );
486 if ( $row === false ) {
487 // Missing
488
489 return null;
490 }
491 $db->delete( $tableName, array( 'keyname' => $key ), __METHOD__ );
492 if ( $this->isExpired( $db, $row->exptime ) ) {
493 // Expired, do not reinsert
494
495 return null;
496 }
497
498 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
499 $newValue = $oldValue + $step;
500 $db->insert( $tableName,
501 array(
502 'keyname' => $key,
503 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
504 'exptime' => $row->exptime
505 ), __METHOD__, 'IGNORE' );
506
507 if ( $db->affectedRows() == 0 ) {
508 // Race condition. See bug 28611
509 $newValue = null;
510 }
511 } catch ( DBError $e ) {
512 $this->handleWriteError( $e, $serverIndex );
513 return null;
514 }
515
516 return $newValue;
517 }
518
519 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
520 if ( !is_callable( $callback ) ) {
521 throw new Exception( "Got invalid callback." );
522 }
523
524 return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
525 }
526
527 /**
528 * @param DatabaseBase $db
529 * @param string $exptime
530 * @return bool
531 */
532 protected function isExpired( $db, $exptime ) {
533 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
534 }
535
536 /**
537 * @param DatabaseBase $db
538 * @return string
539 */
540 protected function getMaxDateTime( $db ) {
541 if ( time() > 0x7fffffff ) {
542 return $db->timestamp( 1 << 62 );
543 } else {
544 return $db->timestamp( 0x7fffffff );
545 }
546 }
547
548 protected function garbageCollect() {
549 if ( !$this->purgePeriod ) {
550 // Disabled
551 return;
552 }
553 // Only purge on one in every $this->purgePeriod requests.
554 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
555 return;
556 }
557 $now = time();
558 // Avoid repeating the delete within a few seconds
559 if ( $now > ( $this->lastExpireAll + 1 ) ) {
560 $this->lastExpireAll = $now;
561 $this->expireAll();
562 }
563 }
564
565 public function expireAll() {
566 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
567 }
568
569 /**
570 * Delete objects from the database which expire before a certain date.
571 * @param string $timestamp
572 * @param bool|callable $progressCallback
573 * @return bool
574 */
575 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
576 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
577 try {
578 $db = $this->getDB( $serverIndex );
579 $dbTimestamp = $db->timestamp( $timestamp );
580 $totalSeconds = false;
581 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
582 for ( $i = 0; $i < $this->shards; $i++ ) {
583 $maxExpTime = false;
584 while ( true ) {
585 $conds = $baseConds;
586 if ( $maxExpTime !== false ) {
587 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
588 }
589 $rows = $db->select(
590 $this->getTableNameByShard( $i ),
591 array( 'keyname', 'exptime' ),
592 $conds,
593 __METHOD__,
594 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
595 if ( $rows === false || !$rows->numRows() ) {
596 break;
597 }
598 $keys = array();
599 $row = $rows->current();
600 $minExpTime = $row->exptime;
601 if ( $totalSeconds === false ) {
602 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
603 - wfTimestamp( TS_UNIX, $minExpTime );
604 }
605 foreach ( $rows as $row ) {
606 $keys[] = $row->keyname;
607 $maxExpTime = $row->exptime;
608 }
609
610 $db->delete(
611 $this->getTableNameByShard( $i ),
612 array(
613 'exptime >= ' . $db->addQuotes( $minExpTime ),
614 'exptime < ' . $db->addQuotes( $dbTimestamp ),
615 'keyname' => $keys
616 ),
617 __METHOD__ );
618
619 if ( $progressCallback ) {
620 if ( intval( $totalSeconds ) === 0 ) {
621 $percent = 0;
622 } else {
623 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
624 - wfTimestamp( TS_UNIX, $maxExpTime );
625 if ( $remainingSeconds > $totalSeconds ) {
626 $totalSeconds = $remainingSeconds;
627 }
628 $processedSeconds = $totalSeconds - $remainingSeconds;
629 $percent = ( $i + $processedSeconds / $totalSeconds )
630 / $this->shards * 100;
631 }
632 $percent = ( $percent / $this->numServers )
633 + ( $serverIndex / $this->numServers * 100 );
634 call_user_func( $progressCallback, $percent );
635 }
636 }
637 }
638 } catch ( DBError $e ) {
639 $this->handleWriteError( $e, $serverIndex );
640 return false;
641 }
642 }
643 return true;
644 }
645
646 /**
647 * Delete content of shard tables in every server.
648 * Return true if the operation is successful, false otherwise.
649 * @return bool
650 */
651 public function deleteAll() {
652 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
653 try {
654 $db = $this->getDB( $serverIndex );
655 for ( $i = 0; $i < $this->shards; $i++ ) {
656 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
657 }
658 } catch ( DBError $e ) {
659 $this->handleWriteError( $e, $serverIndex );
660 return false;
661 }
662 }
663 return true;
664 }
665
666 /**
667 * Serialize an object and, if possible, compress the representation.
668 * On typical message and page data, this can provide a 3X decrease
669 * in storage requirements.
670 *
671 * @param mixed $data
672 * @return string
673 */
674 protected function serialize( &$data ) {
675 $serial = serialize( $data );
676
677 if ( function_exists( 'gzdeflate' ) ) {
678 return gzdeflate( $serial );
679 } else {
680 return $serial;
681 }
682 }
683
684 /**
685 * Unserialize and, if necessary, decompress an object.
686 * @param string $serial
687 * @return mixed
688 */
689 protected function unserialize( $serial ) {
690 if ( function_exists( 'gzinflate' ) ) {
691 MediaWiki\suppressWarnings();
692 $decomp = gzinflate( $serial );
693 MediaWiki\restoreWarnings();
694
695 if ( false !== $decomp ) {
696 $serial = $decomp;
697 }
698 }
699
700 $ret = unserialize( $serial );
701
702 return $ret;
703 }
704
705 /**
706 * Handle a DBError which occurred during a read operation.
707 *
708 * @param DBError $exception
709 * @param int $serverIndex
710 */
711 protected function handleReadError( DBError $exception, $serverIndex ) {
712 if ( $exception instanceof DBConnectionError ) {
713 $this->markServerDown( $exception, $serverIndex );
714 }
715 $this->logger->error( "DBError: {$exception->getMessage()}" );
716 if ( $exception instanceof DBConnectionError ) {
717 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
718 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
719 } else {
720 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
721 $this->logger->debug( __METHOD__ . ": ignoring query error" );
722 }
723 }
724
725 /**
726 * Handle a DBQueryError which occurred during a write operation.
727 *
728 * @param DBError $exception
729 * @param int $serverIndex
730 */
731 protected function handleWriteError( DBError $exception, $serverIndex ) {
732 if ( $exception instanceof DBConnectionError ) {
733 $this->markServerDown( $exception, $serverIndex );
734 }
735 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
736 if ( $exception->db->trxLevel() ) {
737 try {
738 $exception->db->rollback( __METHOD__ );
739 } catch ( DBError $e ) {
740 }
741 }
742 }
743
744 $this->logger->error( "DBError: {$exception->getMessage()}" );
745 if ( $exception instanceof DBConnectionError ) {
746 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
747 $this->logger->debug( __METHOD__ . ": ignoring connection error" );
748 } else {
749 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
750 $this->logger->debug( __METHOD__ . ": ignoring query error" );
751 }
752 }
753
754 /**
755 * Mark a server down due to a DBConnectionError exception
756 *
757 * @param DBError $exception
758 * @param int $serverIndex
759 */
760 protected function markServerDown( $exception, $serverIndex ) {
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;
768 }
769 }
770 $now = time();
771 $this->logger->info( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) );
772 $this->connFailureTimes[$serverIndex] = $now;
773 $this->connFailureErrors[$serverIndex] = $exception;
774 }
775
776 /**
777 * Create shard tables. For use from eval.php.
778 */
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' );
784 }
785
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__ );
791 }
792 }
793 }
794 }