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