Use consistent hashing for SqlBagOStuff servers
[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 /**
31 * @var LoadBalancer
32 */
33 var $lb;
34
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';
43
44 protected $connFailureTimes = array(); // UNIX timestamps
45 protected $connFailureErrors = array(); // exceptions
46
47 /**
48 * Constructor. Parameters are:
49 * - server: A server info structure in the format required by each
50 * element in $wgDBServers.
51 *
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.
55 *
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.
62 *
63 * - tableName: The table name to use, default is "objectcache".
64 *
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.
72 *
73 * @param $params array
74 */
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";
82 }
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;
89 }
90 if ( isset( $params['purgePeriod'] ) ) {
91 $this->purgePeriod = intval( $params['purgePeriod'] );
92 }
93 if ( isset( $params['tableName'] ) ) {
94 $this->tableName = $params['tableName'];
95 }
96 if ( isset( $params['shards'] ) ) {
97 $this->shards = intval( $params['shards'] );
98 }
99 }
100
101 /**
102 * Get a connection to the specified database
103 *
104 * @param $serverIndex integer
105 * @return DatabaseBase
106 */
107 protected function getDB( $serverIndex ) {
108 global $wgDebugDBTransactions;
109
110 if ( !isset( $this->conns[$serverIndex] ) ) {
111 if ( $serverIndex >= $this->numServers ) {
112 throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
113 }
114
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 )
118 {
119 throw $this->connFailureErrors[$serverIndex];
120 }
121
122 # If server connection info was given, use that
123 if ( $this->serverInfos ) {
124 if ( $wgDebugDBTransactions ) {
125 wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
126 }
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 {
134 /*
135 * We must keep a separate connection to MySQL in order to avoid deadlocks
136 * However, SQLite has an opposite behaviour. And PostgreSQL needs to know
137 * if we are in transaction or no
138 */
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 );
145 }
146 }
147 if ( $wgDebugDBTransactions ) {
148 wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
149 }
150 $this->conns[$serverIndex] = $db;
151 }
152
153 return $this->conns[$serverIndex];
154 }
155
156 /**
157 * Get the server index and table name for a given key
158 * @param $key string
159 * @return Array: server index and table name
160 */
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;
167 }
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;
175 }
176 return array( $serverIndex, $this->getTableNameByShard( $tableIndex ) );
177 }
178
179 /**
180 * Get the table name for a given shard index
181 * @param $index int
182 * @return string
183 */
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;
191 }
192 }
193
194 /**
195 * @param $key string
196 * @return mixed
197 */
198 public function get( $key ) {
199 $values = $this->getMulti( array( $key ) );
200 return array_key_exists( $key, $values ) ? $values[$key] : false;
201 }
202
203 /**
204 * @param $keys array
205 * @return Array
206 */
207 public function getMulti( array $keys ) {
208 $values = array(); // array of (key => value)
209
210 $keysByTable = array();
211 foreach ( $keys as $key ) {
212 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
213 $keysByTable[$serverIndex][$tableName][] = $key;
214 }
215
216 $this->garbageCollect(); // expire old entries if any
217
218 $dataRows = array();
219 foreach ( $keysByTable as $serverIndex => $serverKeys ) {
220 $db = $this->getDB( $serverIndex );
221 try {
222 foreach ( $serverKeys as $tableName => $tableKeys ) {
223 $res = $db->select( $tableName,
224 array( 'keyname', 'value', 'exptime' ),
225 array( 'keyname' => $tableKeys ),
226 __METHOD__ );
227 foreach ( $res as $row ) {
228 $row->serverIndex = $serverIndex;
229 $row->tableName = $tableName;
230 $dataRows[$row->keyname] = $row;
231 }
232 }
233 } catch ( DBError $e ) {
234 $this->handleReadError( $e, $serverIndex );
235 }
236 }
237
238 foreach ( $keys as $key ) {
239 if ( isset( $dataRows[$key] ) ) { // HIT?
240 $row = $dataRows[$key];
241 $this->debug( "get: retrieved data; expiry time is " . $row->exptime );
242 $db = $this->getDB( $row->serverIndex );
243 if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
244 $this->debug( "get: key has expired, deleting" );
245 try {
246 $db->begin( __METHOD__ );
247 # Put the expiry time in the WHERE condition to avoid deleting a
248 # newly-inserted value
249 $db->delete( $row->tableName,
250 array( 'keyname' => $key, 'exptime' => $row->exptime ),
251 __METHOD__ );
252 $db->commit( __METHOD__ );
253 } catch ( DBQueryError $e ) {
254 $this->handleWriteError( $e, $row->serverIndex );
255 }
256 $values[$key] = false;
257 } else { // HIT
258 $values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
259 }
260 } else { // MISS
261 $values[$key] = false;
262 $this->debug( 'get: no matching rows' );
263 }
264 }
265
266 return $values;
267 }
268
269 /**
270 * @param $key string
271 * @param $value mixed
272 * @param $exptime int
273 * @return bool
274 */
275 public function set( $key, $value, $exptime = 0 ) {
276 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
277 try {
278 $db = $this->getDB( $serverIndex );
279 $exptime = intval( $exptime );
280
281 if ( $exptime < 0 ) {
282 $exptime = 0;
283 }
284
285 if ( $exptime == 0 ) {
286 $encExpiry = $this->getMaxDateTime( $db );
287 } else {
288 if ( $exptime < 3.16e8 ) { # ~10 years
289 $exptime += time();
290 }
291
292 $encExpiry = $db->timestamp( $exptime );
293 }
294 $db->begin( __METHOD__ );
295 // (bug 24425) use a replace if the db supports it instead of
296 // delete/insert to avoid clashes with conflicting keynames
297 $db->replace(
298 $tableName,
299 array( 'keyname' ),
300 array(
301 'keyname' => $key,
302 'value' => $db->encodeBlob( $this->serialize( $value ) ),
303 'exptime' => $encExpiry
304 ), __METHOD__ );
305 $db->commit( __METHOD__ );
306 } catch ( DBError $e ) {
307 $this->handleWriteError( $e, $serverIndex );
308 return false;
309 }
310
311 return true;
312 }
313
314 /**
315 * @param $key string
316 * @param $time int
317 * @return bool
318 */
319 public function delete( $key, $time = 0 ) {
320 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
321 try {
322 $db = $this->getDB( $serverIndex );
323 $db->begin( __METHOD__ );
324 $db->delete(
325 $tableName,
326 array( 'keyname' => $key ),
327 __METHOD__ );
328 $db->commit( __METHOD__ );
329 } catch ( DBError $e ) {
330 $this->handleWriteError( $e, $serverIndex );
331 return false;
332 }
333
334 return true;
335 }
336
337 /**
338 * @param $key string
339 * @param $step int
340 * @return int|null
341 */
342 public function incr( $key, $step = 1 ) {
343 list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
344 try {
345 $db = $this->getDB( $serverIndex );
346 $step = intval( $step );
347 $db->begin( __METHOD__ );
348 $row = $db->selectRow(
349 $tableName,
350 array( 'value', 'exptime' ),
351 array( 'keyname' => $key ),
352 __METHOD__,
353 array( 'FOR UPDATE' ) );
354 if ( $row === false ) {
355 // Missing
356 $db->commit( __METHOD__ );
357
358 return null;
359 }
360 $db->delete( $tableName, array( 'keyname' => $key ), __METHOD__ );
361 if ( $this->isExpired( $db, $row->exptime ) ) {
362 // Expired, do not reinsert
363 $db->commit( __METHOD__ );
364
365 return null;
366 }
367
368 $oldValue = intval( $this->unserialize( $db->decodeBlob( $row->value ) ) );
369 $newValue = $oldValue + $step;
370 $db->insert( $tableName,
371 array(
372 'keyname' => $key,
373 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
374 'exptime' => $row->exptime
375 ), __METHOD__, 'IGNORE' );
376
377 if ( $db->affectedRows() == 0 ) {
378 // Race condition. See bug 28611
379 $newValue = null;
380 }
381 $db->commit( __METHOD__ );
382 } catch ( DBError $e ) {
383 $this->handleWriteError( $e, $serverIndex );
384 return null;
385 }
386
387 return $newValue;
388 }
389
390 /**
391 * @return Array
392 */
393 public function keys() {
394 $result = array();
395
396 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
397 try {
398 $db = $this->getDB( $serverIndex );
399 for ( $i = 0; $i < $this->shards; $i++ ) {
400 $res = $db->select( $this->getTableNameByShard( $i ),
401 array( 'keyname' ), false, __METHOD__ );
402 foreach ( $res as $row ) {
403 $result[] = $row->keyname;
404 }
405 }
406 } catch ( DBError $e ) {
407 $this->handleReadError( $e, $serverIndex );
408 }
409 }
410 return $result;
411 }
412
413 /**
414 * @param $exptime string
415 * @return bool
416 */
417 protected function isExpired( $db, $exptime ) {
418 return $exptime != $this->getMaxDateTime( $db ) && wfTimestamp( TS_UNIX, $exptime ) < time();
419 }
420
421 /**
422 * @return string
423 */
424 protected function getMaxDateTime( $db ) {
425 if ( time() > 0x7fffffff ) {
426 return $db->timestamp( 1 << 62 );
427 } else {
428 return $db->timestamp( 0x7fffffff );
429 }
430 }
431
432 protected function garbageCollect() {
433 if ( !$this->purgePeriod ) {
434 // Disabled
435 return;
436 }
437 // Only purge on one in every $this->purgePeriod requests.
438 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
439 return;
440 }
441 $now = time();
442 // Avoid repeating the delete within a few seconds
443 if ( $now > ( $this->lastExpireAll + 1 ) ) {
444 $this->lastExpireAll = $now;
445 $this->expireAll();
446 }
447 }
448
449 public function expireAll() {
450 $this->deleteObjectsExpiringBefore( wfTimestampNow() );
451 }
452
453 /**
454 * Delete objects from the database which expire before a certain date.
455 * @param $timestamp string
456 * @param $progressCallback bool|callback
457 * @return bool
458 */
459 public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
460 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
461 try {
462 $db = $this->getDB( $serverIndex );
463 $dbTimestamp = $db->timestamp( $timestamp );
464 $totalSeconds = false;
465 $baseConds = array( 'exptime < ' . $db->addQuotes( $dbTimestamp ) );
466 for ( $i = 0; $i < $this->shards; $i++ ) {
467 $maxExpTime = false;
468 while ( true ) {
469 $conds = $baseConds;
470 if ( $maxExpTime !== false ) {
471 $conds[] = 'exptime > ' . $db->addQuotes( $maxExpTime );
472 }
473 $rows = $db->select(
474 $this->getTableNameByShard( $i ),
475 array( 'keyname', 'exptime' ),
476 $conds,
477 __METHOD__,
478 array( 'LIMIT' => 100, 'ORDER BY' => 'exptime' ) );
479 if ( !$rows->numRows() ) {
480 break;
481 }
482 $keys = array();
483 $row = $rows->current();
484 $minExpTime = $row->exptime;
485 if ( $totalSeconds === false ) {
486 $totalSeconds = wfTimestamp( TS_UNIX, $timestamp )
487 - wfTimestamp( TS_UNIX, $minExpTime );
488 }
489 foreach ( $rows as $row ) {
490 $keys[] = $row->keyname;
491 $maxExpTime = $row->exptime;
492 }
493
494 $db->begin( __METHOD__ );
495 $db->delete(
496 $this->getTableNameByShard( $i ),
497 array(
498 'exptime >= ' . $db->addQuotes( $minExpTime ),
499 'exptime < ' . $db->addQuotes( $dbTimestamp ),
500 'keyname' => $keys
501 ),
502 __METHOD__ );
503 $db->commit( __METHOD__ );
504
505 if ( $progressCallback ) {
506 if ( intval( $totalSeconds ) === 0 ) {
507 $percent = 0;
508 } else {
509 $remainingSeconds = wfTimestamp( TS_UNIX, $timestamp )
510 - wfTimestamp( TS_UNIX, $maxExpTime );
511 if ( $remainingSeconds > $totalSeconds ) {
512 $totalSeconds = $remainingSeconds;
513 }
514 $percent = ( $i + $remainingSeconds / $totalSeconds )
515 / $this->shards * 100;
516 }
517 $percent = ( $percent / $this->numServers )
518 + ( $serverIndex / $this->numServers * 100 );
519 call_user_func( $progressCallback, $percent );
520 }
521 }
522 }
523 } catch ( DBError $e ) {
524 $this->handleWriteError( $e, $serverIndex );
525 return false;
526 }
527 }
528 return true;
529 }
530
531 public function deleteAll() {
532 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
533 try {
534 $db = $this->getDB( $serverIndex );
535 for ( $i = 0; $i < $this->shards; $i++ ) {
536 $db->begin( __METHOD__ );
537 $db->delete( $this->getTableNameByShard( $i ), '*', __METHOD__ );
538 $db->commit( __METHOD__ );
539 }
540 } catch ( DBError $e ) {
541 $this->handleWriteError( $e, $serverIndex );
542 return false;
543 }
544 }
545 return true;
546 }
547
548 /**
549 * Serialize an object and, if possible, compress the representation.
550 * On typical message and page data, this can provide a 3X decrease
551 * in storage requirements.
552 *
553 * @param $data mixed
554 * @return string
555 */
556 protected function serialize( &$data ) {
557 $serial = serialize( $data );
558
559 if ( function_exists( 'gzdeflate' ) ) {
560 return gzdeflate( $serial );
561 } else {
562 return $serial;
563 }
564 }
565
566 /**
567 * Unserialize and, if necessary, decompress an object.
568 * @param $serial string
569 * @return mixed
570 */
571 protected function unserialize( $serial ) {
572 if ( function_exists( 'gzinflate' ) ) {
573 wfSuppressWarnings();
574 $decomp = gzinflate( $serial );
575 wfRestoreWarnings();
576
577 if ( false !== $decomp ) {
578 $serial = $decomp;
579 }
580 }
581
582 $ret = unserialize( $serial );
583
584 return $ret;
585 }
586
587 /**
588 * Handle a DBError which occurred during a read operation.
589 */
590 protected function handleReadError( DBError $exception, $serverIndex ) {
591 if ( $exception instanceof DBConnectionError ) {
592 $this->markServerDown( $exception, $serverIndex );
593 }
594 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
595 if ( $exception instanceof DBConnectionError ) {
596 wfDebug( __METHOD__ . ": ignoring connection error\n" );
597 } else {
598 wfDebug( __METHOD__ . ": ignoring query error\n" );
599 }
600 }
601
602 /**
603 * Handle a DBQueryError which occurred during a write operation.
604 */
605 protected function handleWriteError( DBError $exception, $serverIndex ) {
606 if ( $exception instanceof DBConnectionError ) {
607 $this->markServerDown( $exception, $serverIndex );
608 }
609 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
610 try {
611 $exception->db->rollback( __METHOD__ );
612 } catch ( DBError $e ) {}
613 }
614 wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
615 if ( $exception instanceof DBConnectionError ) {
616 wfDebug( __METHOD__ . ": ignoring connection error\n" );
617 } else {
618 wfDebug( __METHOD__ . ": ignoring query error\n" );
619 }
620 }
621
622 /**
623 * Mark a server down due to a DBConnectionError exception
624 */
625 protected function markServerDown( $exception, $serverIndex ) {
626 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
627 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
628 unset( $this->connFailureTimes[$serverIndex] );
629 unset( $this->connFailureErrors[$serverIndex] );
630 } else {
631 wfDebug( __METHOD__.": Server #$serverIndex already down\n" );
632 return;
633 }
634 }
635 $now = time();
636 wfDebug( __METHOD__.": Server #$serverIndex down until " . ( $now + 60 ) . "\n" );
637 $this->connFailureTimes[$serverIndex] = $now;
638 $this->connFailureErrors[$serverIndex] = $exception;
639 }
640
641 /**
642 * Create shard tables. For use from eval.php.
643 */
644 public function createTables() {
645 for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
646 $db = $this->getDB( $serverIndex );
647 if ( $db->getType() !== 'mysql'
648 || version_compare( $db->getServerVersion(), '4.1.0', '<' ) )
649 {
650 throw new MWException( __METHOD__ . ' is not supported on this DB server' );
651 }
652
653 for ( $i = 0; $i < $this->shards; $i++ ) {
654 $db->begin( __METHOD__ );
655 $db->query(
656 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
657 ' LIKE ' . $db->tableName( 'objectcache' ),
658 __METHOD__ );
659 $db->commit( __METHOD__ );
660 }
661 }
662 }
663 }
664
665 /**
666 * Backwards compatibility alias
667 */
668 class MediaWikiBagOStuff extends SqlBagOStuff { }
669