Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / db / loadbalancer / LoadMonitorMySQL.php
blob31f616366ebfd1d08b58e6a7fa773ca742f5c0fd
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Database
22 /**
23 * Basic MySQL load monitor with no external dependencies
24 * Uses memcached to cache the replication lag for a short time
26 * @ingroup Database
28 class LoadMonitorMySQL implements LoadMonitor {
29 /** @var LoadBalancer */
30 public $parent;
31 /** @var BagOStuff */
32 protected $srvCache;
33 /** @var BagOStuff */
34 protected $mainCache;
36 public function __construct( $parent ) {
37 $this->parent = $parent;
39 $this->srvCache = ObjectCache::getLocalServerInstance( 'hash' );
40 $this->mainCache = ObjectCache::getLocalClusterInstance();
43 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
46 public function getLagTimes( $serverIndexes, $wiki ) {
47 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
48 # Single server only, just return zero without caching
49 return array( 0 => 0 );
52 $key = $this->getLagTimeCacheKey();
53 # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
54 $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
55 # Keep keys around longer as fallbacks
56 $staleTTL = 60;
58 # (a) Check the local APC cache
59 $value = $this->srvCache->get( $key );
60 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
61 wfDebugLog( 'replication', __METHOD__ . ": got lag times ($key) from local cache" );
62 return $value['lagTimes']; // cache hit
64 $staleValue = $value ?: false;
66 # (b) Check the shared cache and backfill APC
67 $value = $this->mainCache->get( $key );
68 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
69 $this->srvCache->set( $key, $value, $staleTTL );
70 wfDebugLog( 'replication', __METHOD__ . ": got lag times ($key) from main cache" );
72 return $value['lagTimes']; // cache hit
74 $staleValue = $value ?: $staleValue;
76 # (c) Cache key missing or expired; regenerate and backfill
77 if ( $this->mainCache->lock( $key, 0, 10 ) ) {
78 # Let this process alone update the cache value
79 $cache = $this->mainCache;
80 /** @noinspection PhpUnusedLocalVariableInspection */
81 $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
82 $cache->unlock( $key );
83 } );
84 } elseif ( $staleValue ) {
85 # Could not acquire lock but an old cache exists, so use it
86 return $staleValue['lagTimes'];
89 $lagTimes = array();
90 foreach ( $serverIndexes as $i ) {
91 if ( $i == 0 ) { # Master
92 $lagTimes[$i] = 0;
93 continue;
95 $conn = $this->parent->getAnyOpenConnection( $i );
96 if ( $conn !== false ) {
97 $lagTimes[$i] = $conn->getLag();
98 continue;
100 $conn = $this->parent->openConnection( $i, $wiki );
101 if ( $conn !== false ) {
102 $lagTimes[$i] = $conn->getLag();
103 # Close the connection to avoid sleeper connections piling up.
104 # Note that the caller will pick one of these DBs and reconnect,
105 # which is slightly inefficient, but this only matters for the lag
106 # time cache miss cache, which is far less common that cache hits.
107 $this->parent->closeConnection( $conn );
111 # Add a timestamp key so we know when it was cached
112 $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
113 $this->mainCache->set( $key, $value, $staleTTL );
114 $this->srvCache->set( $key, $value, $staleTTL );
115 wfDebugLog( 'replication', __METHOD__ . ": re-calculated lag times ($key)" );
117 return $value['lagTimes'];
120 public function clearCaches() {
121 $key = $this->getLagTimeCacheKey();
122 $this->srvCache->delete( $key );
123 $this->mainCache->delete( $key );
126 private function getLagTimeCacheKey() {
127 // Lag is per-server, not per-DB, so key on the master DB name
128 return $this->srvCache->makeGlobalKey( 'lag-times', $this->parent->getServerName( 0 ) );