'errorLogger' => $this->errorLogger,
'hostname' => $this->hostname,
'cliMode' => $this->cliMode,
- 'agent' => $this->agent
+ 'agent' => $this->agent,
+ 'chronologyProtector' => $this->getChronologyProtector()
];
}
public function getMainLB( $domain = false ) {
$section = $this->getSectionForDomain( $domain );
if ( !isset( $this->mainLBs[$section] ) ) {
- $lb = $this->newMainLB( $domain );
- $this->getChronologyProtector()->initLB( $lb );
- $this->mainLBs[$section] = $lb;
+ $this->mainLBs[$section] = $this->newMainLB( $domain );
}
return $this->mainLBs[$section];
public function getExternalLB( $cluster ) {
if ( !isset( $this->extLBs[$cluster] ) ) {
$this->extLBs[$cluster] = $this->newExternalLB( $cluster );
- $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
}
return $this->extLBs[$cluster];
public function getMainLB( $domain = false ) {
if ( !isset( $this->mainLB ) ) {
$this->mainLB = $this->newMainLB( $domain );
- $this->getChronologyProtector()->initLB( $this->mainLB );
}
return $this->mainLB;
public function getExternalLB( $cluster ) {
if ( !isset( $this->extLBs[$cluster] ) ) {
$this->extLBs[$cluster] = $this->newExternalLB( $cluster );
- $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
}
return $this->extLBs[$cluster];
* - srvCache : BagOStuff object for server cache [optional]
* - memCache : BagOStuff object for cluster memory cache [optional]
* - wanCache : WANObjectCache object [optional]
+ * - chronologyProtector: ChronologyProtector object [optional]
* - hostname : The name of the current server [optional]
* - cliMode: Whether the execution context is a CLI script. [optional]
* - profiler : Class name or instance with profileIn()/profileOut() methods. [optional]
* If a DB_REPLICA connection has been opened already, then wait immediately.
* Otherwise sets a variable telling it to wait if such a connection is opened.
*
- * @param DBMasterPos $pos
+ * @param DBMasterPos|bool $pos Master position or false
*/
public function waitFor( $pos );
*
* This can be used a faster proxy for waitForAll()
*
- * @param DBMasterPos $pos
+ * @param DBMasterPos|bool $pos Master position or false
* @param int $timeout Max seconds to wait; default is mWaitTimeout
* @return bool Success (able to connect and no timeouts reached)
*/
/**
* Set the master wait position and wait for ALL replica DBs to catch up to it
*
- * @param DBMasterPos $pos
+ * @param DBMasterPos|bool $pos Master position or false
* @param int $timeout Max seconds to wait; default is mWaitTimeout
* @return bool Success (able to connect and no timeouts reached)
*/
/** @var ILoadMonitor */
private $loadMonitor;
+ /** @var ChronologyProtector|null */
+ private $chronProt;
/** @var BagOStuff */
private $srvCache;
/** @var BagOStuff */
/** @var boolean */
private $disabled = false;
+ /** @var boolean */
+ private $chronProtInitialized = false;
/** @var integer Warn when this many connection are held */
const CONN_HELD_WARN_THRESHOLD = 10;
: ( gethostname() ?: 'unknown' );
$this->cliMode = isset( $params['cliMode'] ) ? $params['cliMode'] : PHP_SAPI === 'cli';
$this->agent = isset( $params['agent'] ) ? $params['agent'] : '';
+
+ if ( isset( $params['chronologyProtector'] ) ) {
+ $this->chronProt = $params['chronologyProtector'];
+ }
}
/**
return $i;
}
- /**
- * @param DBMasterPos|false $pos
- */
public function waitFor( $pos ) {
+ $oldPos = $this->mWaitForPos;
$this->mWaitForPos = $pos;
- $i = $this->mReadIndex;
+ // If a generic reader connection was already established, then wait now
+ $i = $this->mReadIndex;
if ( $i > 0 ) {
if ( !$this->doWait( $i ) ) {
$this->laggedReplicaMode = true;
}
}
+
+ // Restore the older position if it was higher
+ $this->setWaitForPositionIfHigher( $oldPos );
}
public function waitForOne( $pos, $timeout = null ) {
+ $oldPos = $this->mWaitForPos;
$this->mWaitForPos = $pos;
$i = $this->mReadIndex;
$ok = true; // no applicable loads
}
+ // Restore the older position if it was higher
+ $this->setWaitForPositionIfHigher( $oldPos );
+
return $ok;
}
public function waitForAll( $pos, $timeout = null ) {
+ $oldPos = $this->mWaitForPos;
$this->mWaitForPos = $pos;
$serverCount = count( $this->mServers );
}
}
+ // Restore the older position if it was higher
+ $this->setWaitForPositionIfHigher( $oldPos );
+
return $ok;
}
+ /**
+ * @param DBMasterPos|bool $pos
+ */
+ private function setWaitForPositionIfHigher( $pos ) {
+ if ( !$pos ) {
+ return;
+ }
+
+ if ( !$this->mWaitForPos || $pos->hasReached( $this->mWaitForPos ) ) {
+ $this->mWaitForPos = $pos;
+ }
+ }
+
/**
* @param int $i
* @return IDatabase|bool
$domain = false; // local connection requested
}
+ if ( !$this->chronProtInitialized && $this->chronProt ) {
+ $this->connLogger->debug( __METHOD__ . ': calling initLB() before first connection.' );
+ // Load CP positions before connecting so that doWait() triggers later if needed
+ $this->chronProtInitialized = true;
+ $this->chronProt->initLB( $this );
+ }
+
if ( $domain !== false ) {
$conn = $this->openForeignConnection( $i, $domain );
} elseif ( isset( $this->mConns['local'][$i][0] ) ) {