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'] ) ) {
85 $this->numServers = count( $this->serverInfos );
87 $this->serverInfos =
false;
88 $this->numServers = 1;
90 if ( isset(
$params[
'purgePeriod'] ) ) {
91 $this->purgePeriod = intval(
$params[
'purgePeriod'] );
93 if ( isset(
$params[
'tableName'] ) ) {
96 if ( isset(
$params[
'shards'] ) ) {
97 $this->shards = intval(
$params[
'shards'] );
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" );
141 $db = $this->lb->getConnection(
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];
162 if ( $this->shards > 1 ) {
163 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
168 if ( $this->numServers > 1 ) {
171 reset( $sortedServers );
172 $serverIndex =
key( $sortedServers );
185 if ( $this->shards > 1 ) {
186 $decimals = strlen( $this->shards - 1 );
188 sprintf(
"%0{$decimals}d", $index );
199 public function get( $key, &$casToken = null ) {
201 if ( array_key_exists( $key, $values ) ) {
202 $casToken = $values[$key];
203 return $values[$key];
215 $keysByTable =
array();
218 $keysByTable[$serverIndex][
$tableName][] = $key;
224 foreach ( $keysByTable
as $serverIndex => $serverKeys ) {
226 $db = $this->
getDB( $serverIndex );
229 array(
'keyname',
'value',
'exptime' ),
230 array(
'keyname' => $tableKeys ),
232 if (
$res ===
false ) {
235 foreach (
$res as $row ) {
236 $row->serverIndex = $serverIndex;
238 $dataRows[$row->keyname] = $row;
247 if ( isset( $dataRows[$key] ) ) {
248 $row = $dataRows[$key];
249 $this->
debug(
"get: retrieved data; expiry time is " . $row->exptime );
251 $db = $this->
getDB( $row->serverIndex );
252 if ( $this->
isExpired( $db, $row->exptime ) ) {
253 $this->
debug(
"get: key has expired, deleting" );
254 $db->commit( __METHOD__,
'flush' );
255 # Put the expiry time in the WHERE condition to avoid deleting a
256 # newly-inserted value
257 $db->delete( $row->tableName,
258 array(
'keyname' => $key,
'exptime' => $row->exptime ),
260 $db->commit( __METHOD__,
'flush' );
261 $values[$key] =
false;
263 $values[$key] = $this->
unserialize( $db->decodeBlob( $row->value ) );
269 $values[$key] =
false;
270 $this->
debug(
'get: no matching rows' );
283 public function set( $key,
$value, $exptime = 0 ) {
286 $db = $this->
getDB( $serverIndex );
287 $exptime = intval( $exptime );
289 if ( $exptime < 0 ) {
293 if ( $exptime == 0 ) {
296 if ( $exptime < 3.16e8 ) { # ~10 years
300 $encExpiry = $db->timestamp( $exptime );
302 $db->commit( __METHOD__,
'flush' );
310 'value' => $db->encodeBlob( $this->serialize(
$value ) ),
311 'exptime' => $encExpiry
313 $db->commit( __METHOD__,
'flush' );
329 public function cas( $casToken, $key,
$value, $exptime = 0 ) {
332 $db = $this->
getDB( $serverIndex );
333 $exptime = intval( $exptime );
335 if ( $exptime < 0 ) {
339 if ( $exptime == 0 ) {
342 if ( $exptime < 3.16e8 ) { # ~10 years
345 $encExpiry = $db->timestamp( $exptime );
347 $db->commit( __METHOD__,
'flush' );
354 'value' => $db->encodeBlob( $this->serialize(
$value ) ),
355 'exptime' => $encExpiry
359 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
363 $db->commit( __METHOD__,
'flush' );
370 return (
bool)$db->affectedRows();
378 public function delete( $key,
$time = 0 ) {
381 $db = $this->
getDB( $serverIndex );
382 $db->commit( __METHOD__,
'flush' );
385 array(
'keyname' => $key ),
387 $db->commit( __METHOD__,
'flush' );
401 public function incr( $key, $step = 1 ) {
404 $db = $this->
getDB( $serverIndex );
405 $step = intval( $step );
406 $db->commit( __METHOD__,
'flush' );
407 $row = $db->selectRow(
409 array(
'value',
'exptime' ),
410 array(
'keyname' => $key ),
412 array(
'FOR UPDATE' ) );
413 if ( $row ===
false ) {
415 $db->commit( __METHOD__,
'flush' );
420 if ( $this->
isExpired( $db, $row->exptime ) ) {
422 $db->commit( __METHOD__,
'flush' );
427 $oldValue = intval( $this->
unserialize( $db->decodeBlob( $row->value ) ) );
428 $newValue = $oldValue + $step;
432 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
433 'exptime' => $row->exptime
434 ), __METHOD__,
'IGNORE' );
436 if ( $db->affectedRows() == 0 ) {
440 $db->commit( __METHOD__,
'flush' );
453 protected function isExpired( $db, $exptime ) {
461 if ( time() > 0x7fffffff ) {
462 return $db->timestamp( 1 << 62 );
464 return $db->timestamp( 0x7fffffff );
469 if ( !$this->purgePeriod ) {
474 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
479 if ( $now > ( $this->lastExpireAll + 1 ) ) {
480 $this->lastExpireAll = $now;
498 $db = $this->
getDB( $serverIndex );
500 $totalSeconds =
false;
501 $baseConds =
array(
'exptime < ' . $db->addQuotes( $dbTimestamp ) );
506 if ( $maxExpTime !==
false ) {
507 $conds[] =
'exptime > ' . $db->addQuotes( $maxExpTime );
511 array(
'keyname',
'exptime' ),
514 array(
'LIMIT' => 100,
'ORDER BY' =>
'exptime' ) );
515 if ( $rows ===
false || !$rows->numRows() ) {
519 $row = $rows->current();
520 $minExpTime = $row->exptime;
521 if ( $totalSeconds ===
false ) {
525 foreach ( $rows
as $row ) {
526 $keys[] = $row->keyname;
527 $maxExpTime = $row->exptime;
530 $db->commit( __METHOD__,
'flush' );
534 'exptime >= ' . $db->addQuotes( $minExpTime ),
535 'exptime < ' . $db->addQuotes( $dbTimestamp ),
539 $db->commit( __METHOD__,
'flush' );
541 if ( $progressCallback ) {
542 if ( intval( $totalSeconds ) === 0 ) {
547 if ( $remainingSeconds > $totalSeconds ) {
548 $totalSeconds = $remainingSeconds;
550 $percent = ( $i + $remainingSeconds / $totalSeconds )
551 / $this->shards * 100;
554 + ( $serverIndex / $this->numServers * 100 );
555 call_user_func( $progressCallback, $percent );
570 $db = $this->
getDB( $serverIndex );
572 $db->commit( __METHOD__,
'flush' );
574 $db->commit( __METHOD__,
'flush' );
595 if ( function_exists(
'gzdeflate' ) ) {
596 return gzdeflate( $serial );
608 if ( function_exists(
'gzinflate' ) ) {
610 $decomp = gzinflate( $serial );
613 if (
false !== $decomp ) {
630 wfDebugLog(
'SQLBagOStuff',
"DBError: {$exception->getMessage()}" );
633 wfDebug( __METHOD__ .
": ignoring connection error\n" );
636 wfDebug( __METHOD__ .
": ignoring query error\n" );
647 if ( $exception->db && $exception->db->wasReadOnlyError() ) {
649 $exception->db->rollback( __METHOD__ );
652 wfDebugLog(
'SQLBagOStuff',
"DBError: {$exception->getMessage()}" );
655 wfDebug( __METHOD__ .
": ignoring connection error\n" );
658 wfDebug( __METHOD__ .
": ignoring query error\n" );
666 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
667 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
668 unset( $this->connFailureTimes[$serverIndex] );
669 unset( $this->connFailureErrors[$serverIndex] );
671 wfDebug( __METHOD__ .
": Server #$serverIndex already down\n" );
676 wfDebug( __METHOD__ .
": Server #$serverIndex down until " . ( $now + 60 ) .
"\n" );
677 $this->connFailureTimes[$serverIndex] = $now;
678 $this->connFailureErrors[$serverIndex] = $exception;
686 $db = $this->
getDB( $serverIndex );
687 if ( $db->getType() !==
'mysql' ) {
688 throw new MWException( __METHOD__ .
' is not supported on this DB server' );
692 $db->commit( __METHOD__,
'flush' );
694 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
695 ' LIKE ' . $db->tableName(
'objectcache' ),
697 $db->commit( __METHOD__,
'flush' );