Merge "Make a VirtualRESTService class for Parsoid"
[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 if ( isset( $params['servers'] ) ) {
92 $this->serverInfos = $params['servers'];
93 $this->numServers = count( $this->serverInfos );
94 $this->serverNames = array();
95 foreach ( $this->serverInfos as $i => $info ) {
96 $this->serverNames[$i] = isset( $info['host'] ) ? $info['host'] : "#$i";
97 }
98 } elseif ( isset( $params['server'] ) ) {
99 $this->serverInfos = array( $params['server'] );
100 $this->numServers = count( $this->serverInfos );
101 } else {
102 $this->serverInfos = false;
103 $this->numServers = 1;
104 }
105 if ( isset( $params['purgePeriod'] ) ) {
106 $this->purgePeriod = intval( $params['purgePeriod'] );
107 }
108 if ( isset( $params['tableName'] ) ) {
109 $this->tableName = $params['tableName'];
110 }
111 if ( isset( $params['shards'] ) ) {
112 $this->shards = intval( $params['shards'] );
113 }
114 }
115
116 /**
117 * Get a connection to the specified database
118 *
119 * @param int $serverIndex
120 * @return DatabaseBase
121 * @throws MWException
122 */
123 protected function getDB( $serverIndex ) {
124 global $wgDebugDBTransactions;
125
126 if ( !isset( $this->conns[$serverIndex] ) ) {
127 if ( $serverIndex >= $this->numServers ) {
128 throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
129 }
130
131 # Don't keep timing out trying to connect for each call if the DB is down
132 if ( isset( $this->connFailureErrors[$serverIndex] )
133 && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
134 ) {
135 throw $this->connFailureErrors[$serverIndex];
136 }
137
138 # If server connection info was given, use that
139 if ( $this->serverInfos ) {
140 if ( $wgDebugDBTransactions ) {
141 wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
142 }
143 $info = $this->serverInfos[$serverIndex];
144 $type = isset( $info['type'] ) ? $info['type'] : 'mysql';
145 $host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
146 wfDebug( __CLASS__ . ": connecting to $host\n" );
147 $db = DatabaseBase::factory( $type, $info );
148 $db->clearFlag( DBO_TRX );
149 } else {
150 /*
151 * We must keep a separate connection to MySQL in order to avoid deadlocks
152 * However, SQLite has an opposite behavior. And PostgreSQL needs to know
153 * if we are in transaction or no
154 */
155 if ( wfGetDB( DB_MASTER )->getType() == 'mysql' ) {
156 $this->lb = wfGetLBFactory()->newMainLB();
157 $db = $this->lb->getConnection( DB_MASTER );
158 $db->clearFlag( DBO_TRX ); // auto-commit mode
159 } else {
160 $db = wfGetDB( DB_MASTER );
161 }
162 }
163 if ( $wgDebugDBTransactions ) {
164 wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
165 }
166 $this->conns[$serverIndex] = $db;
167 }
168
169 return $this->conns[$serverIndex];
170 }
171
172 /**
173 * Get the server index and table name for a given key
174 * @param string $key
175 * @return array Server index and table name
176 */
177 protected function getTableByKey( $key ) {
178 if ( $this->shards > 1 ) {
179 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
180 $tableIndex = $hash % $this->shards;
181 } else {
182 $tableIndex = 0;
183 }
184 if ( $this->numServers > 1 ) {
185 $sortedServers = $this->serverNames;
186 ArrayUtils::consistentHashSort( $sortedServers, $key );
187 reset( $sortedServers );
188 $serverIndex = key( $sortedServers );
189 } else {
190 $serverIndex = 0;
191 }
192 return array( $serverIndex, $this->getTableNameByShard( $tableIndex ) );
193 }
194
195 /**
196 * Get the table name for a given shard index
197 * @param int $index
198 * @return string
199 */
200 protected function getTableNameByShard( $index ) {
201 if ( $this->shards > 1 ) {
202 $decimals = strlen( $this->shards - 1 );
203 return $this->tableName .
204 sprintf( "%0{$decimals}d", $index );
205 } else {
206 return $this->tableName;
207 }
208 }
209
210 /**
211 * @param string $key
212 * @param mixed $casToken [optional]
213 * @return mixed
214 */
215 public function get( $key, &$casToken = null ) {
216 $values = $this->getMulti( array( $key ) );
217 if ( array_key_exists( $key, $values ) ) {
218 $casToken = $values[$key];
219 return $values[$key];
220 }
221 return false;
222 }
223
224 /**
225 * @param array $keys
226 * @return array
227 */
228 public function getMulti( array $keys ) {
229 $values = array(); // array of (key => value)
230
231 $keysByTable = array();
232 foreach ( $keys as $key ) {
233 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
234 $keysByTable[$serverIndex][$tableName][] = $key;
235 }
236
237 $this->garbageCollect(); // expire old entries if any
238
239 $dataRows = array();
240 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
241 try {
242 $db = $this->getDB( $serverIndex );
243 foreach ( $serverKeys as $tableName => $tableKeys ) {
244 $res = $db->select( $tableName,
245 array( 'keyname', 'value', 'exptime' ),
246 array( 'keyname' => $tableKeys ),
247 __METHOD__,
248 // Approximate write-on-the-fly BagOStuff API via blocking.
249 // This approximation fails if a ROLLBACK happens (which is rare).
250 // We do not want to flush the TRX as that can break callers.
251 $db->trxLevel() ? array( 'LOCK IN SHARE MODE' ) : array()
252 );
253 if ( $res === false ) {
254 continue;
255 }
256 foreach ( $res as $row ) {
257 $row->serverIndex = $serverIndex;
258 $row->tableName = $tableName;
259 $dataRows[$row->keyname] = $row;
260 }
261 }
262 } catch ( DBError $e ) {
263 $this->handleReadError( $e, $serverIndex );
264 }
265 }
266
267 foreach ( $keys as $key ) {
268 if ( isset( $dataRows[$key] ) ) { // HIT?
269 $row = $dataRows[$key];
270 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
271 try {
272 $db = $this->getDB( $row->serverIndex );
273 if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
274 $this->debug( "get: key has expired, deleting" );
275 # Put the expiry time in the WHERE condition to avoid deleting a
276 # newly-inserted value
277 $db->delete( $row->tableName,
278 array( 'keyname' => $key, 'exptime' => $row->exptime ),
279 __METHOD__ );
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 /**
295 * @param array $data
296 * @param int $expiry
297 * @return bool
298 */
299 public function setMulti( array $data, $expiry = 0 ) {
300 $keysByTable = array();
301 foreach ( $data as $key => $value ) {
302 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
303 $keysByTable[$serverIndex][$tableName][] = $key;
304 }
305
306 $this->garbageCollect(); // expire old entries if any
307
308 $result = true;
309 $exptime = (int)$expiry;
310 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
311 try {
312 $db = $this->getDB( $serverIndex );
313 } catch ( DBError $e ) {
314 $this->handleWriteError( $e, $serverIndex );
315 $result = false;
316 continue;
317 }
318
319 if ( $exptime < 0 ) {
320 $exptime = 0;
321 }
322
323 if ( $exptime == 0 ) {
324 $encExpiry = $this->getMaxDateTime( $db );
325 } else {
326 $exptime = $this->convertExpiry( $exptime );
327 $encExpiry = $db->timestamp( $exptime );
328 }
329 foreach ( $serverKeys as $tableName => $tableKeys ) {
330 $rows = array();
331 foreach ( $tableKeys as $key ) {
332 $rows[] = array(
333 'keyname' => $key,
334 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
335 'exptime' => $encExpiry,
336 );
337 }
338
339 try {
340 $db->replace(
341 $tableName,
342 array( 'keyname' ),
343 $rows,
344 __METHOD__
345 );
346 } catch ( DBError $e ) {
347 $this->handleWriteError( $e, $serverIndex );
348 $result = false;
349 }
350
351 }
352
353 }
354
355 return $result;
356 }
357
358
359
360 /**
361 * @param string $key
362 * @param mixed $value
363 * @param int $exptime
364 * @return bool
365 */
366 public function set( $key, $value, $exptime = 0 ) {
367 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
368 try {
369 $db = $this->getDB( $serverIndex );
370 $exptime = intval( $exptime );
371
372 if ( $exptime < 0 ) {
373 $exptime = 0;
374 }
375
376 if ( $exptime == 0 ) {
377 $encExpiry = $this->getMaxDateTime( $db );
378 } else {
379 $exptime = $this->convertExpiry( $exptime );
380 $encExpiry = $db->timestamp( $exptime );
381 }
382 // (bug 24425) use a replace if the db supports it instead of
383 // delete/insert to avoid clashes with conflicting keynames
384 $db->replace(
385 $tableName,
386 array( 'keyname' ),
387 array(
388 'keyname' => $key,
389 'value' => $db->encodeBlob( $this->serialize( $value ) ),
390 'exptime' => $encExpiry
391 ), __METHOD__ );
392 } catch ( DBError $e ) {
393 $this->handleWriteError( $e, $serverIndex );
394 return false;
395 }
396
397 return true;
398 }
399
400 /**
401 * @param mixed $casToken
402 * @param string $key
403 * @param mixed $value
404 * @param int $exptime
405 * @return bool
406 */
407 public function cas( $casToken, $key, $value, $exptime = 0 ) {
408 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
409 try {
410 $db = $this->getDB( $serverIndex );
411 $exptime = intval( $exptime );
412
413 if ( $exptime < 0 ) {
414 $exptime = 0;
415 }
416
417 if ( $exptime == 0 ) {
418 $encExpiry = $this->getMaxDateTime( $db );
419 } else {
420 $exptime = $this->convertExpiry( $exptime );
421 $encExpiry = $db->timestamp( $exptime );
422 }
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 } catch ( DBQueryError $e ) {
439 $this->handleWriteError( $e, $serverIndex );
440
441 return false;
442 }
443
444 return (bool)$db->affectedRows();
445 }
446
447 /**
448 * @param string $key
449 * @param int $time
450 * @return bool
451 */
452 public function delete( $key, $time = 0 ) {
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 /**
518 * @param DatabaseBase $db
519 * @param string $exptime
520 * @return bool
521 */
522 protected function isExpired( $db, $exptime ) {
523 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
524 }
525
526 /**
527 * @param DatabaseBase $db
528 * @return string
529 */
530 protected function getMaxDateTime( $db ) {
531 if ( time() > 0x7fffffff ) {
532 return $db->timestamp( 1 << 62 );
533 } else {
534 return $db->timestamp( 0x7fffffff );
535 }
536 }
537
538 protected function garbageCollect() {
539 if ( !$this->purgePeriod ) {
540 // Disabled
541 return;
542 }
543 // Only purge on one in every $this->purgePeriod requests.
544 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
545 return;
546 }
547 $now = time();
548 // Avoid repeating the delete within a few seconds
549 if ( $now > ( $this->lastExpireAll + 1 ) ) {
550 $this->lastExpireAll = $now;
551 $this->expireAll();
552 }
553 }
554
555 public function expireAll() {
556 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
557 }
558
559 /**
560 * Delete objects from the database which expire before a certain date.
561 * @param string $timestamp
562 * @param bool|callable $progressCallback
563 * @return bool
564 */
565 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
566 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
567 try {
568 $db = $this->getDB( $serverIndex );
569 $dbTimestamp = $db->timestamp( $timestamp );
570 $totalSeconds = false;
571 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
572 for ( $i = 0; $i < $this->shards; $i++ ) {
573 $maxExpTime = false;
574 while ( true ) {
575 $conds = $baseConds;
576 if ( $maxExpTime !== false ) {
577 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
578 }
579 $rows = $db->select(
580 $this->getTableNameByShard( $i ),
581 array( 'keyname', 'exptime' ),
582 $conds,
583 __METHOD__,
584 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
585 if ( $rows === false || !$rows->numRows() ) {
586 break;
587 }
588 $keys = array();
589 $row = $rows->current();
590 $minExpTime = $row->exptime;
591 if ( $totalSeconds === false ) {
592 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
593 - wfTimestamp( TS_UNIX, $minExpTime );
594 }
595 foreach ( $rows as $row ) {
596 $keys[] = $row->keyname;
597 $maxExpTime = $row->exptime;
598 }
599
600 $db->delete(
601 $this->getTableNameByShard( $i ),
602 array(
603 'exptime >= ' . $db->addQuotes( $minExpTime ),
604 'exptime < ' . $db->addQuotes( $dbTimestamp ),
605 'keyname' => $keys
606 ),
607 __METHOD__ );
608
609 if ( $progressCallback ) {
610 if ( intval( $totalSeconds ) === 0 ) {
611 $percent = 0;
612 } else {
613 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
614 - wfTimestamp( TS_UNIX, $maxExpTime );
615 if ( $remainingSeconds > $totalSeconds ) {
616 $totalSeconds = $remainingSeconds;
617 }
618 $processedSeconds = $totalSeconds - $remainingSeconds;
619 $percent = ( $i + $processedSeconds / $totalSeconds )
620 / $this->shards * 100;
621 }
622 $percent = ( $percent / $this->numServers )
623 + ( $serverIndex / $this->numServers * 100 );
624 call_user_func( $progressCallback, $percent );
625 }
626 }
627 }
628 } catch ( DBError $e ) {
629 $this->handleWriteError( $e, $serverIndex );
630 return false;
631 }
632 }
633 return true;
634 }
635
636 /**
637 * Delete content of shard tables in every server.
638 * Return true if the operation is successful, false otherwise.
639 * @return bool
640 */
641 public function deleteAll() {
642 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
643 try {
644 $db = $this->getDB( $serverIndex );
645 for ( $i = 0; $i < $this->shards; $i++ ) {
646 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
647 }
648 } catch ( DBError $e ) {
649 $this->handleWriteError( $e, $serverIndex );
650 return false;
651 }
652 }
653 return true;
654 }
655
656 /**
657 * Serialize an object and, if possible, compress the representation.
658 * On typical message and page data, this can provide a 3X decrease
659 * in storage requirements.
660 *
661 * @param mixed $data
662 * @return string
663 */
664 protected function serialize( &$data ) {
665 $serial = serialize( $data );
666
667 if ( function_exists( 'gzdeflate' ) ) {
668 return gzdeflate( $serial );
669 } else {
670 return $serial;
671 }
672 }
673
674 /**
675 * Unserialize and, if necessary, decompress an object.
676 * @param string $serial
677 * @return mixed
678 */
679 protected function unserialize( $serial ) {
680 if ( function_exists( 'gzinflate' ) ) {
681 wfSuppressWarnings();
682 $decomp = gzinflate( $serial );
683 wfRestoreWarnings();
684
685 if ( false !== $decomp ) {
686 $serial = $decomp;
687 }
688 }
689
690 $ret = unserialize( $serial );
691
692 return $ret;
693 }
694
695 /**
696 * Handle a DBError which occurred during a read operation.
697 *
698 * @param DBError $exception
699 * @param int $serverIndex
700 */
701 protected function handleReadError( DBError $exception, $serverIndex ) {
702 if ( $exception instanceof DBConnectionError ) {
703 $this->markServerDown( $exception, $serverIndex );
704 }
705 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
706 if ( $exception instanceof DBConnectionError ) {
707 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
708 wfDebug( __METHOD__ . ": ignoring connection error\n" );
709 } else {
710 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
711 wfDebug( __METHOD__ . ": ignoring query error\n" );
712 }
713 }
714
715 /**
716 * Handle a DBQueryError which occurred during a write operation.
717 *
718 * @param DBError $exception
719 * @param int $serverIndex
720 */
721 protected function handleWriteError( DBError $exception, $serverIndex ) {
722 if ( $exception instanceof DBConnectionError ) {
723 $this->markServerDown( $exception, $serverIndex );
724 }
725 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
726 try {
727 $exception->db->rollback( __METHOD__ );
728 } catch ( DBError $e ) {
729 }
730 }
731 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
732 if ( $exception instanceof DBConnectionError ) {
733 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
734 wfDebug( __METHOD__ . ": ignoring connection error\n" );
735 } else {
736 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
737 wfDebug( __METHOD__ . ": ignoring query error\n" );
738 }
739 }
740
741 /**
742 * Mark a server down due to a DBConnectionError exception
743 *
744 * @param DBError $exception
745 * @param int $serverIndex
746 */
747 protected function markServerDown( $exception, $serverIndex ) {
748 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
749 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
750 unset( $this->connFailureTimes[$serverIndex] );
751 unset( $this->connFailureErrors[$serverIndex] );
752 } else {
753 wfDebug( __METHOD__ . ": Server #$serverIndex already down\n" );
754 return;
755 }
756 }
757 $now = time();
758 wfDebug( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) . "\n" );
759 $this->connFailureTimes[$serverIndex] = $now;
760 $this->connFailureErrors[$serverIndex] = $exception;
761 }
762
763 /**
764 * Create shard tables. For use from eval.php.
765 */
766 public function createTables() {
767 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
768 $db = $this->getDB( $serverIndex );
769 if ( $db->getType() !== 'mysql' ) {
770 throw new MWException( __METHOD__ . ' is not supported on this DB server' );
771 }
772
773 for ( $i = 0; $i < $this->shards; $i++ ) {
774 $db->query(
775 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
776 ' LIKE ' . $db->tableName( 'objectcache' ),
777 __METHOD__ );
778 }
779 }
780 }
781 }
782
783 /**
784 * Backwards compatibility alias
785 */
786 class MediaWikiBagOStuff extends SqlBagOStuff {
787 }