Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / objectcache / SqlBagOStuff.php
blobb6bf3f8b4125c1cb87ebb5d2a293b0ba7eba7f02
1 <?php
2 /**
3 * Object caching using a SQL database.
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.
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.
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
20 * @file
21 * @ingroup Cache
24 /**
25 * Class to store objects in the database
27 * @ingroup Cache
29 class SqlBagOStuff extends BagOStuff {
30 /**
31 * @var LoadBalancer
33 var $lb;
35 var $serverInfos;
36 var $serverNames;
37 var $numServers;
38 var $conns;
39 var $lastExpireAll = 0;
40 var $purgePeriod = 100;
41 var $shards = 1;
42 var $tableName = 'objectcache';
44 protected $connFailureTimes = array(); // UNIX timestamps
45 protected $connFailureErrors = array(); // exceptions
47 /**
48 * Constructor. Parameters are:
49 * - server: A server info structure in the format required by each
50 * element in $wgDBServers.
52 * - servers: An array of server info structures describing a set of
53 * database servers to distribute keys to. If this is
54 * specified, the "server" option will be ignored.
56 * - purgePeriod: The average number of object cache requests in between
57 * garbage collection operations, where expired entries
58 * are removed from the database. Or in other words, the
59 * reciprocal of the probability of purging on any given
60 * request. If this is set to zero, purging will never be
61 * done.
63 * - tableName: The table name to use, default is "objectcache".
65 * - shards: The number of tables to use for data storage on each server.
66 * If this is more than 1, table names will be formed in the style
67 * objectcacheNNN where NNN is the shard index, between 0 and
68 * shards-1. The number of digits will be the minimum number
69 * required to hold the largest shard index. Data will be
70 * distributed across all tables by key hash. This is for
71 * MySQL bugs 61735 and 61736.
73 * @param $params array
75 public function __construct( $params ) {
76 if ( isset( $params['servers'] ) ) {
77 $this->serverInfos = $params['servers'];
78 $this->numServers = count( $this->serverInfos );
79 $this->serverNames = array();
80 foreach ( $this->serverInfos as $i => $info ) {
81 $this->serverNames[$i] = isset( $info['host'] ) ? $info['host'] : "#$i";
83 } elseif ( isset( $params['server'] ) ) {
84 $this->serverInfos = array( $params['server'] );
85 $this->numServers = count( $this->serverInfos );
86 } else {
87 $this->serverInfos = false;
88 $this->numServers = 1;
90 if ( isset( $params['purgePeriod'] ) ) {
91 $this->purgePeriod = intval( $params['purgePeriod'] );
93 if ( isset( $params['tableName'] ) ) {
94 $this->tableName = $params['tableName'];
96 if ( isset( $params['shards'] ) ) {
97 $this->shards = intval( $params['shards'] );
102 * Get a connection to the specified database
104 * @param $serverIndex integer
105 * @return DatabaseBase
107 protected function getDB( $serverIndex ) {
108 global $wgDebugDBTransactions;
110 if ( !isset( $this->conns[$serverIndex] ) ) {
111 if ( $serverIndex >= $this->numServers ) {
112 throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
115 # Don't keep timing out trying to connect for each call if the DB is down
116 if ( isset( $this->connFailureErrors[$serverIndex] )
117 && ( time() - $this->connFailureTimes[$serverIndex] ) < 60 )
119 throw $this->connFailureErrors[$serverIndex];
122 # If server connection info was given, use that
123 if ( $this->serverInfos ) {
124 if ( $wgDebugDBTransactions ) {
125 wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
127 $info = $this->serverInfos[$serverIndex];
128 $type = isset( $info['type'] ) ? $info['type'] : 'mysql';
129 $host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
130 wfDebug( __CLASS__ . ": connecting to $host\n" );
131 $db = DatabaseBase::factory( $type, $info );
132 $db->clearFlag( DBO_TRX );
133 } else {
135 * We must keep a separate connection to MySQL in order to avoid deadlocks
136 * However, SQLite has an opposite behavior. And PostgreSQL needs to know
137 * if we are in transaction or no
139 if ( wfGetDB( DB_MASTER )->getType() == 'mysql' ) {
140 $this->lb = wfGetLBFactory()->newMainLB();
141 $db = $this->lb->getConnection( DB_MASTER );
142 $db->clearFlag( DBO_TRX ); // auto-commit mode
143 } else {
144 $db = wfGetDB( DB_MASTER );
147 if ( $wgDebugDBTransactions ) {
148 wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
150 $this->conns[$serverIndex] = $db;
153 return $this->conns[$serverIndex];
157 * Get the server index and table name for a given key
158 * @param $key string
159 * @return Array: server index and table name
161 protected function getTableByKey( $key ) {
162 if ( $this->shards > 1 ) {
163 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
164 $tableIndex = $hash % $this->shards;
165 } else {
166 $tableIndex = 0;
168 if ( $this->numServers > 1 ) {
169 $sortedServers = $this->serverNames;
170 ArrayUtils::consistentHashSort( $sortedServers, $key );
171 reset( $sortedServers );
172 $serverIndex = key( $sortedServers );
173 } else {
174 $serverIndex = 0;
176 return array( $serverIndex, $this->getTableNameByShard( $tableIndex ) );
180 * Get the table name for a given shard index
181 * @param $index int
182 * @return string
184 protected function getTableNameByShard( $index ) {
185 if ( $this->shards > 1 ) {
186 $decimals = strlen( $this->shards - 1 );
187 return $this->tableName .
188 sprintf( "%0{$decimals}d", $index );
189 } else {
190 return $this->tableName;
195 * @param $key string
196 * @param $casToken[optional] mixed
197 * @return mixed
199 public function get( $key, &$casToken = null ) {
200 $values = $this->getMulti( array( $key ) );
201 if ( array_key_exists( $key, $values ) ) {
202 $casToken = $values[$key];
203 return $values[$key];
205 return false;
209 * @param $keys array
210 * @return Array
212 public function getMulti( array $keys ) {
213 $values = array(); // array of (key => value)
215 $keysByTable = array();
216 foreach ( $keys as $key ) {
217 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
218 $keysByTable[$serverIndex][$tableName][] = $key;
221 $this->garbageCollect(); // expire old entries if any
223 $dataRows = array();
224 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
225 try {
226 $db = $this->getDB( $serverIndex );
227 foreach ( $serverKeys as $tableName => $tableKeys ) {
228 $res = $db->select( $tableName,
229 array( 'keyname', 'value', 'exptime' ),
230 array( 'keyname' => $tableKeys ),
231 __METHOD__ );
232 foreach ( $res as $row ) {
233 $row->serverIndex = $serverIndex;
234 $row->tableName = $tableName;
235 $dataRows[$row->keyname] = $row;
238 } catch ( DBError $e ) {
239 $this->handleReadError( $e, $serverIndex );
243 foreach ( $keys as $key ) {
244 if ( isset( $dataRows[$key] ) ) { // HIT?
245 $row = $dataRows[$key];
246 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
247 try {
248 $db = $this->getDB( $row->serverIndex );
249 if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
250 $this->debug( "get: key has expired, deleting" );
251 $db->begin( __METHOD__ );
252 # Put the expiry time in the WHERE condition to avoid deleting a
253 # newly-inserted value
254 $db->delete( $row->tableName,
255 array( 'keyname' => $key, 'exptime' => $row->exptime ),
256 __METHOD__ );
257 $db->commit( __METHOD__ );
258 $values[$key] = false;
259 } else { // HIT
260 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
262 } catch ( DBQueryError $e ) {
263 $this->handleWriteError( $e, $row->serverIndex );
265 } else { // MISS
266 $values[$key] = false;
267 $this->debug( 'get: no matching rows' );
271 return $values;
275 * @param $key string
276 * @param $value mixed
277 * @param $exptime int
278 * @return bool
280 public function set( $key, $value, $exptime = 0 ) {
281 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
282 try {
283 $db = $this->getDB( $serverIndex );
284 $exptime = intval( $exptime );
286 if ( $exptime < 0 ) {
287 $exptime = 0;
290 if ( $exptime == 0 ) {
291 $encExpiry = $this->getMaxDateTime( $db );
292 } else {
293 if ( $exptime < 3.16e8 ) { # ~10 years
294 $exptime += time();
297 $encExpiry = $db->timestamp( $exptime );
299 $db->begin( __METHOD__ );
300 // (bug 24425) use a replace if the db supports it instead of
301 // delete/insert to avoid clashes with conflicting keynames
302 $db->replace(
303 $tableName,
304 array( 'keyname' ),
305 array(
306 'keyname' => $key,
307 'value' => $db->encodeBlob( $this->serialize( $value ) ),
308 'exptime' => $encExpiry
309 ), __METHOD__ );
310 $db->commit( __METHOD__ );
311 } catch ( DBError $e ) {
312 $this->handleWriteError( $e, $serverIndex );
313 return false;
316 return true;
320 * @param $casToken mixed
321 * @param $key string
322 * @param $value mixed
323 * @param $exptime int
324 * @return bool
326 public function cas( $casToken, $key, $value, $exptime = 0 ) {
327 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
328 try {
329 $db = $this->getDB( $serverIndex );
330 $exptime = intval( $exptime );
332 if ( $exptime < 0 ) {
333 $exptime = 0;
336 if ( $exptime == 0 ) {
337 $encExpiry = $this->getMaxDateTime( $db );
338 } else {
339 if ( $exptime < 3.16e8 ) { # ~10 years
340 $exptime += time();
342 $encExpiry = $db->timestamp( $exptime );
344 $db->begin( __METHOD__ );
345 // (bug 24425) use a replace if the db supports it instead of
346 // delete/insert to avoid clashes with conflicting keynames
347 $db->update(
348 $tableName,
349 array(
350 'keyname' => $key,
351 'value' => $db->encodeBlob( $this->serialize( $value ) ),
352 'exptime' => $encExpiry
354 array(
355 'keyname' => $key,
356 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
358 __METHOD__
360 $db->commit( __METHOD__ );
361 } catch ( DBQueryError $e ) {
362 $this->handleWriteError( $e, $serverIndex );
364 return false;
367 return (bool) $db->affectedRows();
371 * @param $key string
372 * @param $time int
373 * @return bool
375 public function delete( $key, $time = 0 ) {
376 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
377 try {
378 $db = $this->getDB( $serverIndex );
379 $db->begin( __METHOD__ );
380 $db->delete(
381 $tableName,
382 array( 'keyname' => $key ),
383 __METHOD__ );
384 $db->commit( __METHOD__ );
385 } catch ( DBError $e ) {
386 $this->handleWriteError( $e, $serverIndex );
387 return false;
390 return true;
394 * @param $key string
395 * @param $step int
396 * @return int|null
398 public function incr( $key, $step = 1 ) {
399 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
400 try {
401 $db = $this->getDB( $serverIndex );
402 $step = intval( $step );
403 $db->begin( __METHOD__ );
404 $row = $db->selectRow(
405 $tableName,
406 array( 'value', 'exptime' ),
407 array( 'keyname' => $key ),
408 __METHOD__,
409 array( 'FOR UPDATE' ) );
410 if ( $row === false ) {
411 // Missing
412 $db->commit( __METHOD__ );
414 return null;
416 $db->delete( $tableName, array( 'keyname' => $key ), __METHOD__ );
417 if ( $this->isExpired( $db, $row->exptime ) ) {
418 // Expired, do not reinsert
419 $db->commit( __METHOD__ );
421 return null;
424 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
425 $newValue = $oldValue + $step;
426 $db->insert( $tableName,
427 array(
428 'keyname' => $key,
429 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
430 'exptime' => $row->exptime
431 ), __METHOD__, 'IGNORE' );
433 if ( $db->affectedRows() == 0 ) {
434 // Race condition. See bug 28611
435 $newValue = null;
437 $db->commit( __METHOD__ );
438 } catch ( DBError $e ) {
439 $this->handleWriteError( $e, $serverIndex );
440 return null;
443 return $newValue;
447 * @param $exptime string
448 * @return bool
450 protected function isExpired( $db, $exptime ) {
451 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
455 * @return string
457 protected function getMaxDateTime( $db ) {
458 if ( time() > 0x7fffffff ) {
459 return $db->timestamp( 1 << 62 );
460 } else {
461 return $db->timestamp( 0x7fffffff );
465 protected function garbageCollect() {
466 if ( !$this->purgePeriod ) {
467 // Disabled
468 return;
470 // Only purge on one in every $this->purgePeriod requests.
471 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
472 return;
474 $now = time();
475 // Avoid repeating the delete within a few seconds
476 if ( $now > ( $this->lastExpireAll + 1 ) ) {
477 $this->lastExpireAll = $now;
478 $this->expireAll();
482 public function expireAll() {
483 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
487 * Delete objects from the database which expire before a certain date.
488 * @param $timestamp string
489 * @param $progressCallback bool|callback
490 * @return bool
492 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
493 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
494 try {
495 $db = $this->getDB( $serverIndex );
496 $dbTimestamp = $db->timestamp( $timestamp );
497 $totalSeconds = false;
498 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
499 for ( $i = 0; $i < $this->shards; $i++ ) {
500 $maxExpTime = false;
501 while ( true ) {
502 $conds = $baseConds;
503 if ( $maxExpTime !== false ) {
504 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
506 $rows = $db->select(
507 $this->getTableNameByShard( $i ),
508 array( 'keyname', 'exptime' ),
509 $conds,
510 __METHOD__,
511 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
512 if ( !$rows->numRows() ) {
513 break;
515 $keys = array();
516 $row = $rows->current();
517 $minExpTime = $row->exptime;
518 if ( $totalSeconds === false ) {
519 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
520 - wfTimestamp( TS_UNIX, $minExpTime );
522 foreach ( $rows as $row ) {
523 $keys[] = $row->keyname;
524 $maxExpTime = $row->exptime;
527 $db->begin( __METHOD__ );
528 $db->delete(
529 $this->getTableNameByShard( $i ),
530 array(
531 'exptime >= ' . $db->addQuotes( $minExpTime ),
532 'exptime < ' . $db->addQuotes( $dbTimestamp ),
533 'keyname' => $keys
535 __METHOD__ );
536 $db->commit( __METHOD__ );
538 if ( $progressCallback ) {
539 if ( intval( $totalSeconds ) === 0 ) {
540 $percent = 0;
541 } else {
542 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
543 - wfTimestamp( TS_UNIX, $maxExpTime );
544 if ( $remainingSeconds > $totalSeconds ) {
545 $totalSeconds = $remainingSeconds;
547 $percent = ( $i + $remainingSeconds / $totalSeconds )
548 / $this->shards * 100;
550 $percent = ( $percent / $this->numServers )
551 + ( $serverIndex / $this->numServers * 100 );
552 call_user_func( $progressCallback, $percent );
556 } catch ( DBError $e ) {
557 $this->handleWriteError( $e, $serverIndex );
558 return false;
561 return true;
564 public function deleteAll() {
565 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
566 try {
567 $db = $this->getDB( $serverIndex );
568 for ( $i = 0; $i < $this->shards; $i++ ) {
569 $db->begin( __METHOD__ );
570 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
571 $db->commit( __METHOD__ );
573 } catch ( DBError $e ) {
574 $this->handleWriteError( $e, $serverIndex );
575 return false;
578 return true;
582 * Serialize an object and, if possible, compress the representation.
583 * On typical message and page data, this can provide a 3X decrease
584 * in storage requirements.
586 * @param $data mixed
587 * @return string
589 protected function serialize( &$data ) {
590 $serial = serialize( $data );
592 if ( function_exists( 'gzdeflate' ) ) {
593 return gzdeflate( $serial );
594 } else {
595 return $serial;
600 * Unserialize and, if necessary, decompress an object.
601 * @param $serial string
602 * @return mixed
604 protected function unserialize( $serial ) {
605 if ( function_exists( 'gzinflate' ) ) {
606 wfSuppressWarnings();
607 $decomp = gzinflate( $serial );
608 wfRestoreWarnings();
610 if ( false !== $decomp ) {
611 $serial = $decomp;
615 $ret = unserialize( $serial );
617 return $ret;
621 * Handle a DBError which occurred during a read operation.
623 protected function handleReadError( DBError $exception, $serverIndex ) {
624 if ( $exception instanceof DBConnectionError ) {
625 $this->markServerDown( $exception, $serverIndex );
627 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
628 if ( $exception instanceof DBConnectionError ) {
629 wfDebug( __METHOD__ . ": ignoring connection error\n" );
630 } else {
631 wfDebug( __METHOD__ . ": ignoring query error\n" );
636 * Handle a DBQueryError which occurred during a write operation.
638 protected function handleWriteError( DBError $exception, $serverIndex ) {
639 if ( $exception instanceof DBConnectionError ) {
640 $this->markServerDown( $exception, $serverIndex );
642 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
643 try {
644 $exception->db->rollback( __METHOD__ );
645 } catch ( DBError $e ) {}
647 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
648 if ( $exception instanceof DBConnectionError ) {
649 wfDebug( __METHOD__ . ": ignoring connection error\n" );
650 } else {
651 wfDebug( __METHOD__ . ": ignoring query error\n" );
656 * Mark a server down due to a DBConnectionError exception
658 protected function markServerDown( $exception, $serverIndex ) {
659 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
660 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
661 unset( $this->connFailureTimes[$serverIndex] );
662 unset( $this->connFailureErrors[$serverIndex] );
663 } else {
664 wfDebug( __METHOD__ . ": Server #$serverIndex already down\n" );
665 return;
668 $now = time();
669 wfDebug( __METHOD__ . ": Server #$serverIndex down until " . ( $now + 60 ) . "\n" );
670 $this->connFailureTimes[$serverIndex] = $now;
671 $this->connFailureErrors[$serverIndex] = $exception;
675 * Create shard tables. For use from eval.php.
677 public function createTables() {
678 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
679 $db = $this->getDB( $serverIndex );
680 if ( $db->getType() !== 'mysql'
681 || version_compare( $db->getServerVersion(), '4.1.0', '<' ) )
683 throw new MWException( __METHOD__ . ' is not supported on this DB server' );
686 for ( $i = 0; $i < $this->shards; $i++ ) {
687 $db->begin( __METHOD__ );
688 $db->query(
689 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
690 ' LIKE ' . $db->tableName( 'objectcache' ),
691 __METHOD__ );
692 $db->commit( __METHOD__ );
699 * Backwards compatibility alias
701 class MediaWikiBagOStuff extends SqlBagOStuff { }