MediaWiki  1.28.0
LoadMonitor.php
Go to the documentation of this file.
1 <?php
24 
31 class LoadMonitor implements ILoadMonitor {
33  protected $parent;
35  protected $srvCache;
37  protected $mainCache;
39  protected $replLogger;
40 
42  private $movingAveRatio;
43 
44  const VERSION = 1; // cache key version
45 
46  public function __construct(
48  ) {
49  $this->parent = $lb;
50  $this->srvCache = $srvCache;
51  $this->mainCache = $cache;
52  $this->replLogger = new \Psr\Log\NullLogger();
53 
54  $this->movingAveRatio = isset( $options['movingAveRatio'] )
55  ? $options['movingAveRatio']
56  : 0.1;
57  }
58 
59  public function setLogger( LoggerInterface $logger ) {
60  $this->replLogger = $logger;
61  }
62 
63  public function scaleLoads( array &$weightByServer, $domain ) {
64  $serverIndexes = array_keys( $weightByServer );
65  $states = $this->getServerStates( $serverIndexes, $domain );
66  $coefficientsByServer = $states['weightScales'];
67  foreach ( $weightByServer as $i => $weight ) {
68  if ( isset( $coefficientsByServer[$i] ) ) {
69  $weightByServer[$i] = $weight * $coefficientsByServer[$i];
70  } else { // server recently added to config?
71  $host = $this->parent->getServerName( $i );
72  $this->replLogger->error( __METHOD__ . ": host $host not in cache" );
73  }
74  }
75  }
76 
77  public function getLagTimes( array $serverIndexes, $domain ) {
78  $states = $this->getServerStates( $serverIndexes, $domain );
79 
80  return $states['lagTimes'];
81  }
82 
83  protected function getServerStates( array $serverIndexes, $domain ) {
84  $writerIndex = $this->parent->getWriterIndex();
85  if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == $writerIndex ) {
86  # Single server only, just return zero without caching
87  return [
88  'lagTimes' => [ $writerIndex => 0 ],
89  'weightScales' => [ $writerIndex => 1.0 ]
90  ];
91  }
92 
93  $key = $this->getCacheKey( $serverIndexes );
94  # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
95  $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
96  # Keep keys around longer as fallbacks
97  $staleTTL = 60;
98 
99  # (a) Check the local APC cache
100  $value = $this->srvCache->get( $key );
101  if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
102  $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from local cache" );
103  return $value; // cache hit
104  }
105  $staleValue = $value ?: false;
106 
107  # (b) Check the shared cache and backfill APC
108  $value = $this->mainCache->get( $key );
109  if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
110  $this->srvCache->set( $key, $value, $staleTTL );
111  $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from main cache" );
112 
113  return $value; // cache hit
114  }
115  $staleValue = $value ?: $staleValue;
116 
117  # (c) Cache key missing or expired; regenerate and backfill
118  if ( $this->mainCache->lock( $key, 0, 10 ) ) {
119  # Let this process alone update the cache value
122  $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
123  $cache->unlock( $key );
124  } );
125  } elseif ( $staleValue ) {
126  # Could not acquire lock but an old cache exists, so use it
127  return $staleValue;
128  }
129 
130  $lagTimes = [];
131  $weightScales = [];
132  $movAveRatio = $this->movingAveRatio;
133  foreach ( $serverIndexes as $i ) {
134  if ( $i == $this->parent->getWriterIndex() ) {
135  $lagTimes[$i] = 0; // master always has no lag
136  $weightScales[$i] = 1.0; // nominal weight
137  continue;
138  }
139 
140  $conn = $this->parent->getAnyOpenConnection( $i );
141  if ( $conn ) {
142  $close = false; // already open
143  } else {
144  $conn = $this->parent->openConnection( $i, $domain );
145  $close = true; // new connection
146  }
147 
148  $lastWeight = isset( $staleValue['weightScales'][$i] )
149  ? $staleValue['weightScales'][$i]
150  : 1.0;
151  $coefficient = $this->getWeightScale( $i, $conn ?: null );
152  $newWeight = $movAveRatio * $coefficient + ( 1 - $movAveRatio ) * $lastWeight;
153 
154  // Scale from 10% to 100% of nominal weight
155  $weightScales[$i] = max( $newWeight, .10 );
156 
157  if ( !$conn ) {
158  $lagTimes[$i] = false;
159  $host = $this->parent->getServerName( $i );
160  $this->replLogger->error( __METHOD__ . ": host $host is unreachable" );
161  continue;
162  }
163 
164  if ( $conn->getLBInfo( 'is static' ) ) {
165  $lagTimes[$i] = 0;
166  } else {
167  $lagTimes[$i] = $conn->getLag();
168  if ( $lagTimes[$i] === false ) {
169  $host = $this->parent->getServerName( $i );
170  $this->replLogger->error( __METHOD__ . ": host $host is not replicating?" );
171  }
172  }
173 
174  if ( $close ) {
175  # Close the connection to avoid sleeper connections piling up.
176  # Note that the caller will pick one of these DBs and reconnect,
177  # which is slightly inefficient, but this only matters for the lag
178  # time cache miss cache, which is far less common that cache hits.
179  $this->parent->closeConnection( $conn );
180  }
181  }
182 
183  # Add a timestamp key so we know when it was cached
184  $value = [
185  'lagTimes' => $lagTimes,
186  'weightScales' => $weightScales,
187  'timestamp' => microtime( true )
188  ];
189  $this->mainCache->set( $key, $value, $staleTTL );
190  $this->srvCache->set( $key, $value, $staleTTL );
191  $this->replLogger->info( __METHOD__ . ": re-calculated lag times ($key)" );
192 
193  return $value;
194  }
195 
201  protected function getWeightScale( $index, IDatabase $conn = null ) {
202  return $conn ? 1.0 : 0.0;
203  }
204 
205  private function getCacheKey( array $serverIndexes ) {
206  sort( $serverIndexes );
207  // Lag is per-server, not per-DB, so key on the master DB name
208  return $this->srvCache->makeGlobalKey(
209  'lag-times',
210  self::VERSION,
211  $this->parent->getServerName( $this->parent->getWriterIndex() ),
212  implode( '-', $serverIndexes )
213  );
214  }
215 }
An interface for database load monitoring.
BagOStuff $mainCache
Definition: LoadMonitor.php:37
scaleLoads(array &$weightByServer, $domain)
Perform load ratio adjustment before deciding which server to use.
Definition: LoadMonitor.php:63
Basic DB load monitor with no external dependencies Uses memcached to cache the replication lag for a...
Definition: LoadMonitor.php:31
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$value
Database cluster connection, tracking, load balancing, and transaction manager interface.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
getLagTimes(array $serverIndexes, $domain)
Get an estimate of replication lag (in seconds) for each server.
Definition: LoadMonitor.php:77
LoggerInterface $replLogger
Definition: LoadMonitor.php:39
$cache
Definition: mcc.php:33
BagOStuff $srvCache
Definition: LoadMonitor.php:35
getServerStates(array $serverIndexes, $domain)
Definition: LoadMonitor.php:83
ILoadBalancer $parent
Definition: LoadMonitor.php:33
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
getCacheKey(array $serverIndexes)
const VERSION
Definition: LoadMonitor.php:44
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
float $movingAveRatio
Moving average ratio (e.g.
Definition: LoadMonitor.php:42
__construct(ILoadBalancer $lb, BagOStuff $srvCache, BagOStuff $cache, array $options=[])
Construct a new LoadMonitor with a given LoadBalancer parent.
Definition: LoadMonitor.php:46
setLogger(LoggerInterface $logger)
Definition: LoadMonitor.php:59
getWeightScale($index, IDatabase $conn=null)
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:34