30 use \Wikimedia\WaitConditionLoop;
31 use \Wikimedia\Rdbms\TransactionProfiler;
105 parent::__construct(
$params );
110 if ( isset(
$params[
'servers'] ) ) {
111 $this->serverInfos = [];
112 $this->serverTags = [];
115 foreach (
$params[
'servers']
as $tag => $info ) {
116 $this->serverInfos[$index] = $info;
117 if ( is_string( $tag ) ) {
118 $this->serverTags[$index] = $tag;
120 $this->serverTags[$index] = isset( $info[
'host'] ) ? $info[
'host'] :
"#$index";
124 } elseif ( isset(
$params[
'server'] ) ) {
125 $this->serverInfos = [
$params[
'server'] ];
126 $this->numServers =
count( $this->serverInfos );
129 $this->serverInfos =
false;
130 $this->numServers = 1;
133 if ( isset(
$params[
'purgePeriod'] ) ) {
134 $this->purgePeriod = intval(
$params[
'purgePeriod'] );
136 if ( isset(
$params[
'tableName'] ) ) {
139 if ( isset(
$params[
'shards'] ) ) {
140 $this->shards = intval(
$params[
'shards'] );
142 if ( isset(
$params[
'syncTimeout'] ) ) {
143 $this->syncTimeout =
$params[
'syncTimeout'];
145 $this->replicaOnly = !empty(
$params[
'slaveOnly'] );
155 protected function getDB( $serverIndex ) {
156 if ( !isset( $this->conns[$serverIndex] ) ) {
157 if ( $serverIndex >= $this->numServers ) {
158 throw new MWException( __METHOD__ .
": Invalid server index \"$serverIndex\"" );
161 # Don't keep timing out trying to connect for each call if the DB is down
162 if ( isset( $this->connFailureErrors[$serverIndex] )
163 && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
165 throw $this->connFailureErrors[$serverIndex];
168 if ( $this->serverInfos ) {
170 $info = $this->serverInfos[$serverIndex];
171 $type = isset( $info[
'type'] ) ? $info[
'type'] :
'mysql';
172 $host = isset( $info[
'host'] ) ? $info[
'host'] :
'[unknown]';
173 $this->logger->debug( __CLASS__ .
": connecting to $host" );
176 $db = Database::factory(
$type, $info );
180 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
182 if ( $lb->getServerType( $lb->getWriterIndex() ) !==
'sqlite' ) {
184 $db = $lb->getConnection( $index, [],
false, $lb::CONN_TRX_AUTO );
189 $db = $lb->getConnection( $index );
193 $this->logger->debug( sprintf(
"Connection %s will be used for SqlBagOStuff", $db ) );
194 $this->conns[$serverIndex] = $db;
197 return $this->conns[$serverIndex];
206 if ( $this->shards > 1 ) {
207 $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
212 if ( $this->numServers > 1 ) {
215 reset( $sortedServers );
216 $serverIndex =
key( $sortedServers );
229 if ( $this->shards > 1 ) {
230 $decimals = strlen( $this->shards - 1 );
232 sprintf(
"%0{$decimals}d", $index );
245 $values = $this->
getMulti( [ $key ] );
246 if ( array_key_exists( $key, $values ) ) {
247 $casToken = $values[$key];
248 return $values[$key];
259 $keysByTable[$serverIndex][
$tableName][] = $key;
265 foreach ( $keysByTable
as $serverIndex => $serverKeys ) {
267 $db = $this->
getDB( $serverIndex );
270 [
'keyname',
'value',
'exptime' ],
271 [
'keyname' => $tableKeys ],
276 $db->trxLevel() ? [
'LOCK IN SHARE MODE' ] : []
278 if (
$res ===
false ) {
281 foreach (
$res as $row ) {
282 $row->serverIndex = $serverIndex;
284 $dataRows[$row->keyname] = $row;
293 if ( isset( $dataRows[$key] ) ) {
294 $row = $dataRows[$key];
295 $this->
debug(
"get: retrieved data; expiry time is " . $row->exptime );
298 $db = $this->
getDB( $row->serverIndex );
299 if ( $this->
isExpired( $db, $row->exptime ) ) {
300 $this->
debug(
"get: key has expired" );
302 $values[$key] = $this->
unserialize( $db->decodeBlob( $row->value ) );
308 $this->
debug(
'get: no matching rows' );
317 foreach ( $data
as $key =>
$value ) {
319 $keysByTable[$serverIndex][
$tableName][] = $key;
325 $exptime = (int)$expiry;
326 foreach ( $keysByTable
as $serverIndex => $serverKeys ) {
329 $db = $this->
getDB( $serverIndex );
336 if ( $exptime < 0 ) {
340 if ( $exptime == 0 ) {
344 $encExpiry = $db->timestamp( $exptime );
348 foreach ( $tableKeys
as $key ) {
351 'value' => $db->encodeBlob( $this->
serialize( $data[$key] ) ),
352 'exptime' => $encExpiry,
377 if ( (
$flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
384 protected function cas( $casToken, $key,
$value, $exptime = 0 ) {
388 $db = $this->
getDB( $serverIndex );
389 $exptime = intval( $exptime );
391 if ( $exptime < 0 ) {
395 if ( $exptime == 0 ) {
399 $encExpiry = $db->timestamp( $exptime );
407 'value' => $db->encodeBlob( $this->serialize(
$value ) ),
408 'exptime' => $encExpiry
412 'value' => $db->encodeBlob( $this->serialize( $casToken ) )
422 return (
bool)$db->affectedRows();
425 public function delete( $key ) {
429 $db = $this->
getDB( $serverIndex );
432 [
'keyname' => $key ],
442 public function incr( $key, $step = 1 ) {
446 $db = $this->
getDB( $serverIndex );
447 $step = intval( $step );
448 $row = $db->selectRow(
450 [
'value',
'exptime' ],
451 [
'keyname' => $key ],
454 if ( $row ===
false ) {
459 $db->delete(
$tableName, [
'keyname' => $key ], __METHOD__ );
460 if ( $this->
isExpired( $db, $row->exptime ) ) {
466 $oldValue = intval( $this->
unserialize( $db->decodeBlob( $row->value ) ) );
467 $newValue = $oldValue + $step;
471 'value' => $db->encodeBlob( $this->serialize( $newValue ) ),
472 'exptime' => $row->exptime
473 ], __METHOD__,
'IGNORE' );
475 if ( $db->affectedRows() == 0 ) {
487 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10,
$flags = 0 ) {
488 $ok = $this->
mergeViaCas( $key, $callback, $exptime, $attempts );
489 if ( (
$flags & self::WRITE_SYNC ) == self::WRITE_SYNC ) {
500 $db = $this->
getDB( $serverIndex );
503 [
'exptime' => $db->timestamp( $this->convertExpiry( $expiry ) ) ],
504 [
'keyname' => $key,
'exptime > ' . $db->addQuotes( $db->timestamp( time() ) ) ],
507 if ( $db->affectedRows() == 0 ) {
532 if ( time() > 0x7fffffff ) {
533 return $db->timestamp( 1 << 62 );
535 return $db->timestamp( 0x7fffffff );
540 if ( !$this->purgePeriod || $this->replicaOnly ) {
545 if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
550 if ( $now > ( $this->lastExpireAll + 1 ) ) {
551 $this->lastExpireAll = $now;
570 $db = $this->
getDB( $serverIndex );
571 $dbTimestamp = $db->timestamp( $timestamp );
572 $totalSeconds =
false;
573 $baseConds = [
'exptime < ' . $db->addQuotes( $dbTimestamp ) ];
578 if ( $maxExpTime !==
false ) {
579 $conds[] =
'exptime >= ' . $db->addQuotes( $maxExpTime );
583 [
'keyname',
'exptime' ],
586 [
'LIMIT' => 100,
'ORDER BY' =>
'exptime' ] );
591 $row =
$rows->current();
592 $minExpTime = $row->exptime;
593 if ( $totalSeconds ===
false ) {
598 $keys[] = $row->keyname;
599 $maxExpTime = $row->exptime;
605 'exptime >= ' . $db->addQuotes( $minExpTime ),
606 'exptime < ' . $db->addQuotes( $dbTimestamp ),
611 if ( $progressCallback ) {
612 if ( intval( $totalSeconds ) === 0 ) {
615 $remainingSeconds =
wfTimestamp( TS_UNIX, $timestamp )
617 if ( $remainingSeconds > $totalSeconds ) {
618 $totalSeconds = $remainingSeconds;
620 $processedSeconds = $totalSeconds - $remainingSeconds;
621 $percent = ( $i + $processedSeconds / $totalSeconds )
622 / $this->shards * 100;
625 + ( $serverIndex / $this->numServers * 100 );
626 call_user_func( $progressCallback, $percent );
647 $db = $this->
getDB( $serverIndex );
670 if ( function_exists(
'gzdeflate' ) ) {
671 return gzdeflate( $serial );
683 if ( function_exists(
'gzinflate' ) ) {
684 MediaWiki\suppressWarnings();
685 $decomp = gzinflate( $serial );
686 MediaWiki\restoreWarnings();
688 if (
false !== $decomp ) {
708 $this->logger->error(
"DBError: {$exception->getMessage()}" );
711 $this->logger->debug( __METHOD__ .
": ignoring connection error" );
714 $this->logger->debug( __METHOD__ .
": ignoring query error" );
729 } elseif ( $db->wasReadOnlyError() ) {
730 if ( $db->trxLevel() && $this->
usesMainDB() ) {
738 $this->logger->error(
"DBError: {$exception->getMessage()}" );
741 $this->logger->debug( __METHOD__ .
": ignoring connection error" );
744 $this->logger->debug( __METHOD__ .
": ignoring query error" );
755 unset( $this->conns[$serverIndex] );
757 if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
758 if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
759 unset( $this->connFailureTimes[$serverIndex] );
760 unset( $this->connFailureErrors[$serverIndex] );
762 $this->logger->debug( __METHOD__ .
": Server #$serverIndex already down" );
767 $this->logger->info( __METHOD__ .
": Server #$serverIndex down until " . ( $now + 60 ) );
768 $this->connFailureTimes[$serverIndex] = $now;
769 $this->connFailureErrors[$serverIndex] = $exception;
777 $db = $this->
getDB( $serverIndex );
778 if ( $db->getType() !==
'mysql' ) {
779 throw new MWException( __METHOD__ .
' is not supported on this DB server' );
784 'CREATE TABLE ' . $db->tableName( $this->getTableNameByShard( $i ) ) .
785 ' LIKE ' . $db->tableName(
'objectcache' ),
804 $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
805 if ( $lb->getServerCount() <= 1 ) {
810 $masterPos = $lb->getMasterPos();
812 $loop =
new WaitConditionLoop(
813 function ()
use ( $lb, $masterPos ) {
814 return $lb->waitForAll( $masterPos, 1 );
820 return ( $loop->invoke() === $loop::CONDITION_REACHED );