01d2f954721599e42b0de1613cacec9e67ab42bb
3 * Database load monitoring.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * An interface for database load monitoring
29 interface LoadMonitor
{
31 * Construct a new LoadMonitor with a given LoadBalancer parent
33 * @param LoadBalancer $parent
35 public function __construct( $parent );
38 * Perform pre-connection load ratio adjustment.
40 * @param string|bool $group The selected query group. Default: false
41 * @param string|bool $wiki Default: false
43 public function scaleLoads( &$loads, $group = false, $wiki = false );
46 * Return an estimate of replication lag for each server
48 * @param array $serverIndexes
51 * @return array Map of (server index => seconds)
53 public function getLagTimes( $serverIndexes, $wiki );
56 class LoadMonitorNull
implements LoadMonitor
{
57 public function __construct( $parent ) {
60 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
63 public function getLagTimes( $serverIndexes, $wiki ) {
64 return array_fill_keys( $serverIndexes, 0 );
69 * Basic MySQL load monitor with no external dependencies
70 * Uses memcached to cache the replication lag for a short time
74 class LoadMonitorMySQL
implements LoadMonitor
{
75 /** @var LoadBalancer */
82 public function __construct( $parent ) {
85 $this->parent
= $parent;
87 $this->srvCache
= ObjectCache
::newAccelerator( array(), 'hash' );
88 $this->mainCache
= $wgMemc ?
: wfGetMainCache();
91 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
94 public function getLagTimes( $serverIndexes, $wiki ) {
95 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
96 # Single server only, just return zero without caching
97 return array( 0 => 0 );
100 $key = $this->getLagTimeCacheKey();
101 # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
102 $ttl = mt_rand( 4e6
, 5e6
) / 1e6
;
103 # Keep keys around longer as fallbacks
106 # (a) Check the local APC cache
107 $value = $this->srvCache
->get( $key );
108 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
109 wfDebugLog( 'replication', __FUNCTION__
. ": got lag times ($key) from local cache" );
110 return $value['lagTimes']; // cache hit
112 $staleValue = $value ?
: false;
114 # (b) Check the shared cache and backfill APC
115 $value = $this->mainCache
->get( $key );
116 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
117 $this->srvCache
->set( $key, $value, $staleTTL );
118 wfDebugLog( 'replication', __FUNCTION__
. ": got lag times ($key) from main cache" );
120 return $value['lagTimes']; // cache hit
122 $staleValue = $value ?
: $staleValue;
124 # (c) Cache key missing or expired; regenerate and backfill
125 if ( $this->mainCache
->lock( $key, 0, 10 ) ) {
126 # Let this process alone update the cache value
127 $cache = $this->mainCache
;
128 $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
129 $cache->unlock( $key );
131 } elseif ( $staleValue ) {
132 # Could not acquire lock but an old cache exists, so use it
133 return $value['lagTimes'];
137 foreach ( $serverIndexes as $i ) {
138 if ( $i == 0 ) { # Master
140 } elseif ( false !== ( $conn = $this->parent
->getAnyOpenConnection( $i ) ) ) {
141 $lagTimes[$i] = $conn->getLag();
142 } elseif ( false !== ( $conn = $this->parent
->openConnection( $i, $wiki ) ) ) {
143 $lagTimes[$i] = $conn->getLag();
144 # Close the connection to avoid sleeper connections piling up.
145 # Note that the caller will pick one of these DBs and reconnect,
146 # which is slightly inefficient, but this only matters for the lag
147 # time cache miss cache, which is far less common that cache hits.
148 $this->parent
->closeConnection( $conn );
152 # Add a timestamp key so we know when it was cached
153 $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
154 $this->mainCache
->set( $key, $value, $staleTTL );
155 $this->srvCache
->set( $key, $value, $staleTTL );
156 wfDebugLog( 'replication', __FUNCTION__
. ": re-calculated lag times ($key)" );
158 return $value['lagTimes'];
161 public function clearCaches() {
162 $key = $this->getLagTimeCacheKey();
163 $this->srvCache
->delete( $key );
164 $this->mainCache
->delete( $key );
167 private function getLagTimeCacheKey() {
168 # Lag is per-server, not per-DB, so key on the master DB name
169 return wfGlobalCacheKey( 'lag-times', $this->parent
->getServerName( 0 ) );