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