* http://www.gnu.org/copyleft/gpl.html
*
* @file
- * @ingroup Database
*/
namespace Wikimedia\Rdbms;
private $disabled = false;
/** @var bool */
private $chronProtInitialized = false;
+ /** @var int */
+ private $maxLag = self::MAX_LAG_DEFAULT;
/** @var int Warn when this many connection are held */
const CONN_HELD_WARN_THRESHOLD = 10;
- /** @var int Default 'max lag' when unspecified */
+ /** @var int Default 'maxLag' when unspecified */
const MAX_LAG_DEFAULT = 10;
/** @var int Seconds to cache master server read-only status */
const TTL_CACHE_READONLY = 5;
$this->readOnlyReason = $params['readOnlyReason'];
}
+ if ( isset( $params['maxLag'] ) ) {
+ $this->maxLag = $params['maxLag'];
+ }
+
if ( isset( $params['loadMonitor'] ) ) {
$this->loadMonitorConfig = $params['loadMonitor'];
} else {
$this->loadMonitorConfig = [ 'class' => 'LoadMonitorNull' ];
}
+ $this->loadMonitorConfig += [ 'lagWarnThreshold' => $this->maxLag ];
foreach ( $params['servers'] as $i => $server ) {
$this->mLoads[$i] = $server['load'];
# How much lag this server nominally is allowed to have
$maxServerLag = isset( $this->mServers[$i]['max lag'] )
? $this->mServers[$i]['max lag']
- : self::MAX_LAG_DEFAULT; // default
+ : $this->maxLag; // default
# Constrain that futher by $maxLag argument
$maxServerLag = min( $maxServerLag, $maxLag );
"Server {host} is not replicating?", [ 'host' => $host ] );
unset( $loads[$i] );
} elseif ( $lag > $maxServerLag ) {
- $this->replLogger->warning(
+ $this->replLogger->info(
"Server {host} has {lag} seconds of lag (>= {maxlag})",
[ 'host' => $host, 'lag' => $lag, 'maxlag' => $maxServerLag ]
);
$knownReachedPos->hasReached( $this->mWaitForPos )
) {
$this->replLogger->debug( __METHOD__ .
- ": replica DB $server known to be caught up (pos >= $knownReachedPos)." );
+ ': replica DB {dbserver} known to be caught up (pos >= $knownReachedPos).',
+ [ 'dbserver' => $server ] );
return true;
}
$conn = $this->getAnyOpenConnection( $index );
if ( !$conn ) {
if ( !$open ) {
- $this->replLogger->debug( __METHOD__ . ": no connection open for $server" );
+ $this->replLogger->debug( __METHOD__ . ': no connection open for {dbserver}',
+ [ 'dbserver' => $server ] );
return false;
} else {
$conn = $this->openConnection( $index, self::DOMAIN_ANY );
if ( !$conn ) {
- $this->replLogger->warning( __METHOD__ . ": failed to connect to $server" );
+ $this->replLogger->warning( __METHOD__ . ': failed to connect to {dbserver}',
+ [ 'dbserver' => $server ] );
return false;
}
}
}
- $this->replLogger->info( __METHOD__ . ": Waiting for replica DB $server to catch up..." );
+ $this->replLogger->info( __METHOD__ . ': Waiting for replica DB {dbserver} to catch up...',
+ [ 'dbserver' => $server ] );
$timeout = $timeout ?: $this->mWaitTimeout;
$result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
if ( $result == -1 || is_null( $result ) ) {
// Timed out waiting for replica DB, use master instead
$this->replLogger->warning(
- __METHOD__ . ": Timed out waiting on {host} pos {$this->mWaitForPos}",
- [ 'host' => $server ]
+ __METHOD__ . ': Timed out waiting on {host} pos {pos}',
+ [ 'host' => $server, 'pos' => $this->mWaitForPos ]
);
$ok = false;
} else {
$oldConnsOpened = $this->connsOpened; // connections open now
if ( $i == self::DB_MASTER ) {
+ if ( $flags & self::CONN_NO_WRITE ) {
+ throw new InvalidArgumentException(
+ 'Cannot set CONN_NO_WRITE flag on master connection!'
+ );
+ }
+
$i = $this->getWriterIndex();
} else {
# Try to find an available server in any the query groups (in order)
break;
}
}
+
+ // Request no-write connection, even if $i == $this->getWriterIndex().
+ $flags |= self::CONN_NO_WRITE;
}
# Operation-based index
$this->reportConnectionError();
return null; // not reached
}
+
+ // Request no-write connection, even if $i == $this->getWriterIndex().
+ $flags |= self::CONN_NO_WRITE;
}
# Now we have an explicit index into the servers array
// a) those are usually set to implicitly use transaction rounds via DBO_TRX
// b) those must support the use of explicit transaction rounds via beginMasterChanges()
$autoCommit = ( ( $flags & self::CONN_TRX_AUTO ) == self::CONN_TRX_AUTO );
+ $noWrite = ( ( $flags & self::CONN_NO_WRITE ) == self::CONN_NO_WRITE );
+
+ if ( $noWrite && $i === $this->getWriterIndex() ) {
+ // We can't disable writes on the master connection!
+ // TODO: Wrap the master connection, so write operations fail!
+ $noWrite = false;
+ }
if ( $domain !== false ) {
// Connection is to a foreign domain
// Open a new connection
$server = $this->mServers[$i];
$server['serverIndex'] = $i;
+ $server['noWrite'] = $noWrite;
$server['autoCommitOnly'] = $autoCommit;
$conn = $this->reallyOpenConnection( $server, false );
$host = $this->getServerName( $i );
$dbName = $domainInstance->getDatabase();
$prefix = $domainInstance->getTablePrefix();
$autoCommit = ( ( $flags & self::CONN_TRX_AUTO ) == self::CONN_TRX_AUTO );
+ $noWrite = ( ( $flags & self::CONN_NO_WRITE ) == self::CONN_NO_WRITE );
+
+ if ( $noWrite && $i === $this->getWriterIndex() ) {
+ // We can't disable writes on the master connection!
+ // TODO: Wrap the master connection, so write operations fail!
+ $noWrite = false;
+ }
if ( $autoCommit ) {
$connFreeKey = self::KEY_FOREIGN_FREE_NOROUND;
$server['foreignPoolRefCount'] = 0;
$server['foreign'] = true;
$server['autoCommitOnly'] = $autoCommit;
+ $server['noWrite'] = $noWrite;
$conn = $this->reallyOpenConnection( $server, $dbName );
if ( !$conn->isOpen() ) {
$this->connLogger->warning( __METHOD__ . ": connection error for $i/$domain" );
if ( $pos instanceof DBMasterPos ) {
$result = $conn->masterPosWait( $pos, $timeout );
if ( $result == -1 || is_null( $result ) ) {
- $msg = __METHOD__ . ": Timed out waiting on {$conn->getServer()} pos {$pos}";
- $this->replLogger->warning( "$msg" );
+ $msg = __METHOD__ . ': Timed out waiting on {host} pos {pos}';
+ $this->replLogger->warning( $msg,
+ [ 'host' => $conn->getServer(), 'pos' => $pos ] );
$ok = false;
} else {
- $this->replLogger->info( __METHOD__ . ": Done" );
+ $this->replLogger->info( __METHOD__ . ': Done' );
$ok = true;
}
} else {
$ok = false; // something is misconfigured
- $this->replLogger->error( "Could not get master pos for {$conn->getServer()}." );
+ $this->replLogger->error( 'Could not get master pos for {host}',
+ [ 'host' => $conn->getServer() ] );
}
return $ok;