84 final public function scaleLoads( array &$weightByServer, $domain ) {
85 $serverIndexes = array_keys( $weightByServer );
87 $newScalesByServer = $states[
'weightScales'];
88 foreach ( $weightByServer as $i => $weight ) {
89 if ( isset( $newScalesByServer[$i] ) ) {
90 $weightByServer[$i] = $weight * $newScalesByServer[$i];
92 $host = $this->parent->getServerName( $i );
93 $this->replLogger->error( __METHOD__ .
": host $host not in cache" );
103 $writerIndex = $this->parent->getWriterIndex();
104 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == $writerIndex ) {
105 # Single server only, just return zero without caching
107 'lagTimes' => [ $writerIndex => 0 ],
108 'weightScales' => [ $writerIndex => 1.0 ]
113 # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
114 $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
115 # Keep keys around longer as fallbacks
118 # (a) Check the local APC cache
119 $value = $this->srvCache->get( $key );
120 if (
$value &&
$value[
'timestamp'] > ( microtime(
true ) - $ttl ) ) {
121 $this->replLogger->debug( __METHOD__ .
": got lag times ($key) from local cache" );
124 $staleValue =
$value ?:
false;
126 # (b) Check the shared cache and backfill APC
127 $value = $this->wanCache->get( $key );
128 if (
$value &&
$value[
'timestamp'] > ( microtime(
true ) - $ttl ) ) {
129 $this->srvCache->set( $key,
$value, $staleTTL );
130 $this->replLogger->debug( __METHOD__ .
": got lag times ($key) from main cache" );
134 $staleValue =
$value ?: $staleValue;
136 # (c) Cache key missing or expired; regenerate and backfill
137 if ( $this->srvCache->lock( $key, 0, 10 ) ) {
138 # Let only this process update the cache value on this server
141 $unlocker =
new ScopedCallback(
function () use ( $sCache, $key ) {
142 $sCache->unlock( $key );
144 } elseif ( $staleValue ) {
145 # Could not acquire lock but an old cache exists, so use it
152 foreach ( $serverIndexes as $i ) {
153 if ( $i == $this->parent->getWriterIndex() ) {
155 $weightScales[$i] = 1.0;
159 # Handles with open transactions are avoided since they might be subject
160 # to REPEATABLE-READ snapshots, which could affect the lag estimate query.
161 $flags = ILoadBalancer::CONN_TRX_AUTOCOMMIT;
162 $conn = $this->parent->getAnyOpenConnection( $i, $flags );
166 $conn = $this->parent->openConnection( $i, ILoadBalancer::DOMAIN_ANY, $flags );
170 $lastWeight = isset( $staleValue[
'weightScales'][$i] )
171 ? $staleValue[
'weightScales'][$i]
174 $newWeight = $movAveRatio * $coefficient + ( 1 - $movAveRatio ) * $lastWeight;
177 $weightScales[$i] = max( $newWeight, 0.10 );
179 $host = $this->parent->getServerName( $i );
182 $lagTimes[$i] =
false;
183 $this->replLogger->error(
184 __METHOD__ .
": host {db_server} is unreachable",
185 [
'db_server' => $host ]
190 if ( $conn->getLBInfo(
'is static' ) ) {
193 $lagTimes[$i] = $conn->getLag();
194 if ( $lagTimes[$i] ===
false ) {
195 $this->replLogger->error(
196 __METHOD__ .
": host {db_server} is not replicating?",
197 [
'db_server' => $host ]
199 } elseif ( $lagTimes[$i] > $this->lagWarnThreshold ) {
200 $this->replLogger->error(
201 "Server {host} has {lag} seconds of lag (>= {maxlag})",
204 'lag' => $lagTimes[$i],
205 'maxlag' => $this->lagWarnThreshold
212 # Close the connection to avoid sleeper connections piling up.
213 # Note that the caller will pick one of these DBs and reconnect,
214 # which is slightly inefficient, but this only matters for the lag
215 # time cache miss cache, which is far less common that cache hits.
216 $this->parent->closeConnection( $conn );
220 # Add a timestamp key so we know when it was cached
222 'lagTimes' => $lagTimes,
223 'weightScales' => $weightScales,
224 'timestamp' => microtime(
true )
226 $this->wanCache->set( $key,
$value, $staleTTL );
227 $this->srvCache->set( $key,
$value, $staleTTL );
228 $this->replLogger->info( __METHOD__ .
": re-calculated lag times ($key)" );