Merge "Declare and document $wgStatsdServer and $wgStatsdMetricPrefix"
[lhc/web/wiklou.git] / includes / db / LoadMonitor.php
1 <?php
2 /**
3 * Database load monitoring.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * An interface for database load monitoring
26 *
27 * @ingroup Database
28 */
29 interface LoadMonitor {
30 /**
31 * Construct a new LoadMonitor with a given LoadBalancer parent
32 *
33 * @param LoadBalancer $parent
34 */
35 public function __construct( $parent );
36
37 /**
38 * Perform pre-connection load ratio adjustment.
39 * @param array $loads
40 * @param string|bool $group The selected query group. Default: false
41 * @param string|bool $wiki Default: false
42 */
43 public function scaleLoads( &$loads, $group = false, $wiki = false );
44
45 /**
46 * Return an estimate of replication lag for each server
47 *
48 * @param array $serverIndexes
49 * @param string $wiki
50 *
51 * @return array Map of (server index => seconds)
52 */
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
68 /**
69 * Basic MySQL load monitor with no external dependencies
70 * Uses memcached to cache the replication lag for a short time
71 *
72 * @ingroup Database
73 */
74 class LoadMonitorMySQL implements LoadMonitor {
75 /** @var LoadBalancer */
76 public $parent;
77 /** @var BagOStuff */
78 protected $srvCache;
79 /** @var BagOStuff */
80 protected $mainCache;
81
82 public function __construct( $parent ) {
83 global $wgMemc;
84
85 $this->parent = $parent;
86
87 $this->srvCache = ObjectCache::newAccelerator( array(), 'hash' );
88 $this->mainCache = $wgMemc ?: wfGetMainCache();
89 }
90
91 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
92 }
93
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 );
98 }
99
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
104 $staleTTL = 60;
105
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
111 }
112 $staleValue = $value ?: false;
113
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" );
119
120 return $value['lagTimes']; // cache hit
121 }
122 $staleValue = $value ?: $staleValue;
123
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 );
130 } );
131 } elseif ( $staleValue ) {
132 # Could not acquire lock but an old cache exists, so use it
133 return $value['lagTimes'];
134 }
135
136 $lagTimes = array();
137 foreach ( $serverIndexes as $i ) {
138 if ( $i == 0 ) { # Master
139 $lagTimes[$i] = 0;
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 );
149 }
150 }
151
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)" );
157
158 return $value['lagTimes'];
159 }
160
161 public function clearCaches() {
162 $key = $this->getLagTimeCacheKey();
163 $this->srvCache->delete( $key );
164 $this->mainCache->delete( $key );
165 }
166
167 private function getLagTimeCacheKey() {
168 # Lag is per-server, not per-DB, so key on the master DB name
169 return wfForeignMemcKey( $this->parent->getServerName( 0 ), '', 'lag_times' );
170 }
171 }