MediaWiki  1.23.1
LoadMonitor.php
Go to the documentation of this file.
1 <?php
29 interface LoadMonitor {
35  public function __construct( $parent );
36 
43  public function scaleLoads( &$loads, $group = false, $wiki = false );
44 
53  public function getLagTimes( $serverIndexes, $wiki );
54 }
55 
56 class LoadMonitorNull implements LoadMonitor {
57  public function __construct( $parent ) {
58  }
59 
60  public function scaleLoads( &$loads, $group = false, $wiki = false ) {
61  }
62 
63  public function getLagTimes( $serverIndexes, $wiki ) {
64  return array_fill_keys( $serverIndexes, 0 );
65  }
66 }
67 
74 class LoadMonitorMySQL implements LoadMonitor {
76  public $parent;
77 
78  public function __construct( $parent ) {
79  $this->parent = $parent;
80  }
81 
82  public function scaleLoads( &$loads, $group = false, $wiki = false ) {
83  }
84 
85  public function getLagTimes( $serverIndexes, $wiki ) {
86  if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
87  // Single server only, just return zero without caching
88  return array( 0 => 0 );
89  }
90 
91  $section = new ProfileSection( __METHOD__ );
92 
93  $expiry = 5;
94  $requestRate = 10;
95 
97  if ( empty( $wgMemc ) ) {
99  }
100 
101  $masterName = $this->parent->getServerName( 0 );
102  $memcKey = wfMemcKey( 'lag_times', $masterName );
103  $times = $wgMemc->get( $memcKey );
104  if ( is_array( $times ) ) {
105  # Randomly recache with probability rising over $expiry
106  $elapsed = time() - $times['timestamp'];
107  $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
108  if ( mt_rand( 0, $chance ) != 0 ) {
109  unset( $times['timestamp'] ); // hide from caller
110 
111  return $times;
112  }
113  wfIncrStats( 'lag_cache_miss_expired' );
114  } else {
115  wfIncrStats( 'lag_cache_miss_absent' );
116  }
117 
118  # Cache key missing or expired
119  if ( $wgMemc->add( "$memcKey:lock", 1, 10 ) ) {
120  # Let this process alone update the cache value
121  $unlocker = new ScopedCallback( function () use ( $wgMemc, $memcKey ) {
122  $wgMemc->delete( $memcKey );
123  } );
124  } elseif ( is_array( $times ) ) {
125  # Could not acquire lock but an old cache exists, so use it
126  unset( $times['timestamp'] ); // hide from caller
127 
128  return $times;
129  }
130 
131  $times = array();
132  foreach ( $serverIndexes as $i ) {
133  if ( $i == 0 ) { # Master
134  $times[$i] = 0;
135  } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
136  $times[$i] = $conn->getLag();
137  } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
138  $times[$i] = $conn->getLag();
139  }
140  }
141 
142  # Add a timestamp key so we know when it was cached
143  $times['timestamp'] = time();
144  $wgMemc->set( $memcKey, $times, $expiry + 10 );
145  unset( $times['timestamp'] ); // hide from caller
146 
147  return $times;
148  }
149 }
LoadMonitorNull\getLagTimes
getLagTimes( $serverIndexes, $wiki)
Return an estimate of replication lag for each server.
Definition: LoadMonitor.php:63
LoadMonitorNull\__construct
__construct( $parent)
Construct a new LoadMonitor with a given LoadBalancer parent.
Definition: LoadMonitor.php:57
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
LoadMonitorNull\scaleLoads
scaleLoads(&$loads, $group=false, $wiki=false)
Perform pre-connection load ratio adjustment.
Definition: LoadMonitor.php:60
LoadMonitor\getLagTimes
getLagTimes( $serverIndexes, $wiki)
Return an estimate of replication lag for each server.
LoadMonitor
An interface for database load monitoring.
Definition: LoadMonitor.php:29
LoadMonitorNull
Definition: LoadMonitor.php:56
LoadMonitor\__construct
__construct( $parent)
Construct a new LoadMonitor with a given LoadBalancer parent.
LoadMonitorMySQL\getLagTimes
getLagTimes( $serverIndexes, $wiki)
Return an estimate of replication lag for each server.
Definition: LoadMonitor.php:84
ProfileSection
Class for handling function-scope profiling.
Definition: Profiler.php:60
wfGetMainCache
wfGetMainCache()
Get the main cache object.
Definition: GlobalFunctions.php:3957
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
wfIncrStats
wfIncrStats( $key, $count=1)
Increment a statistics counter.
Definition: GlobalFunctions.php:1304
LoadMonitorMySQL
Basic MySQL load monitor with no external dependencies Uses memcached to cache the replication lag fo...
Definition: LoadMonitor.php:74
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
LoadMonitorMySQL\$parent
LoadBalancer $parent
Definition: LoadMonitor.php:75
LoadBalancer
Database load balancing object.
Definition: LoadBalancer.php:30
ScopedCallback
Class for asserting that a callback happens when an dummy object leaves scope.
Definition: ScopedCallback.php:28
$section
$section
Definition: Utf8Test.php:88
LoadMonitorMySQL\scaleLoads
scaleLoads(&$loads, $group=false, $wiki=false)
Perform pre-connection load ratio adjustment.
Definition: LoadMonitor.php:81
as
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
LoadMonitorMySQL\__construct
__construct( $parent)
Construct a new LoadMonitor with a given LoadBalancer parent.
Definition: LoadMonitor.php:77
LoadMonitor\scaleLoads
scaleLoads(&$loads, $group=false, $wiki=false)
Perform pre-connection load ratio adjustment.