Merge "Change "included" to "transcluded" in messages about cascading"
[lhc/web/wiklou.git] / includes / db / LoadMonitorMySQL.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Database
20 */
21
22 /**
23 * Basic MySQL load monitor with no external dependencies
24 * Uses memcached to cache the replication lag for a short time
25 *
26 * @ingroup Database
27 */
28 class LoadMonitorMySQL implements LoadMonitor {
29 /** @var LoadBalancer */
30 public $parent;
31 /** @var BagOStuff */
32 protected $srvCache;
33 /** @var BagOStuff */
34 protected $mainCache;
35
36 public function __construct( $parent ) {
37 global $wgMemc;
38
39 $this->parent = $parent;
40
41 $this->srvCache = ObjectCache::newAccelerator( array(), 'hash' );
42 $this->mainCache = $wgMemc ?: wfGetMainCache();
43 }
44
45 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
46 }
47
48 public function getLagTimes( $serverIndexes, $wiki ) {
49 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
50 # Single server only, just return zero without caching
51 return array( 0 => 0 );
52 }
53
54 $key = $this->getLagTimeCacheKey();
55 # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
56 $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
57 # Keep keys around longer as fallbacks
58 $staleTTL = 60;
59
60 # (a) Check the local APC cache
61 $value = $this->srvCache->get( $key );
62 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
63 wfDebugLog( 'replication', __FUNCTION__ . ": got lag times ($key) from local cache" );
64 return $value['lagTimes']; // cache hit
65 }
66 $staleValue = $value ?: false;
67
68 # (b) Check the shared cache and backfill APC
69 $value = $this->mainCache->get( $key );
70 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
71 $this->srvCache->set( $key, $value, $staleTTL );
72 wfDebugLog( 'replication', __FUNCTION__ . ": got lag times ($key) from main cache" );
73
74 return $value['lagTimes']; // cache hit
75 }
76 $staleValue = $value ?: $staleValue;
77
78 # (c) Cache key missing or expired; regenerate and backfill
79 if ( $this->mainCache->lock( $key, 0, 10 ) ) {
80 # Let this process alone update the cache value
81 $cache = $this->mainCache;
82 $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
83 $cache->unlock( $key );
84 } );
85 } elseif ( $staleValue ) {
86 # Could not acquire lock but an old cache exists, so use it
87 return $value['lagTimes'];
88 }
89
90 $lagTimes = array();
91 foreach ( $serverIndexes as $i ) {
92 if ( $i == 0 ) { # Master
93 $lagTimes[$i] = 0;
94 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
95 $lagTimes[$i] = $conn->getLag();
96 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
97 $lagTimes[$i] = $conn->getLag();
98 # Close the connection to avoid sleeper connections piling up.
99 # Note that the caller will pick one of these DBs and reconnect,
100 # which is slightly inefficient, but this only matters for the lag
101 # time cache miss cache, which is far less common that cache hits.
102 $this->parent->closeConnection( $conn );
103 }
104 }
105
106 # Add a timestamp key so we know when it was cached
107 $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
108 $this->mainCache->set( $key, $value, $staleTTL );
109 $this->srvCache->set( $key, $value, $staleTTL );
110 wfDebugLog( 'replication', __FUNCTION__ . ": re-calculated lag times ($key)" );
111
112 return $value['lagTimes'];
113 }
114
115 public function clearCaches() {
116 $key = $this->getLagTimeCacheKey();
117 $this->srvCache->delete( $key );
118 $this->mainCache->delete( $key );
119 }
120
121 private function getLagTimeCacheKey() {
122 # Lag is per-server, not per-DB, so key on the master DB name
123 return wfGlobalCacheKey( 'lag-times', $this->parent->getServerName( 0 ) );
124 }
125 }