Ignore some ScopedCallback IDE warnings
[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 /** @noinspection PhpUnusedLocalVariableInspection */
83 $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
84 $cache->unlock( $key );
85 } );
86 } elseif ( $staleValue ) {
87 # Could not acquire lock but an old cache exists, so use it
88 return $value['lagTimes'];
89 }
90
91 $lagTimes = array();
92 foreach ( $serverIndexes as $i ) {
93 if ( $i == 0 ) { # Master
94 $lagTimes[$i] = 0;
95 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
96 $lagTimes[$i] = $conn->getLag();
97 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
98 $lagTimes[$i] = $conn->getLag();
99 # Close the connection to avoid sleeper connections piling up.
100 # Note that the caller will pick one of these DBs and reconnect,
101 # which is slightly inefficient, but this only matters for the lag
102 # time cache miss cache, which is far less common that cache hits.
103 $this->parent->closeConnection( $conn );
104 }
105 }
106
107 # Add a timestamp key so we know when it was cached
108 $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
109 $this->mainCache->set( $key, $value, $staleTTL );
110 $this->srvCache->set( $key, $value, $staleTTL );
111 wfDebugLog( 'replication', __FUNCTION__ . ": re-calculated lag times ($key)" );
112
113 return $value['lagTimes'];
114 }
115
116 public function clearCaches() {
117 $key = $this->getLagTimeCacheKey();
118 $this->srvCache->delete( $key );
119 $this->mainCache->delete( $key );
120 }
121
122 private function getLagTimeCacheKey() {
123 # Lag is per-server, not per-DB, so key on the master DB name
124 return wfGlobalCacheKey( 'lag-times', $this->parent->getServerName( 0 ) );
125 }
126 }