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