Replace some MWExceptions with natives ones in /db
[lhc/web/wiklou.git] / includes / db / loadbalancer / 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 use Psr\Log\LoggerInterface;
23
24 /**
25 * Basic MySQL load monitor with no external dependencies
26 * Uses memcached to cache the replication lag for a short time
27 *
28 * @ingroup Database
29 */
30 class LoadMonitorMySQL implements LoadMonitor {
31 /** @var LoadBalancer */
32 public $parent;
33 /** @var BagOStuff */
34 protected $srvCache;
35 /** @var BagOStuff */
36 protected $mainCache;
37 /** @var LoggerInterface */
38 protected $replLogger;
39
40 public function __construct( LoadBalancer $parent ) {
41 $this->parent = $parent;
42 $this->srvCache = ObjectCache::getLocalServerInstance( 'hash' );
43 $this->mainCache = ObjectCache::getLocalClusterInstance();
44 $this->replLogger = new \Psr\Log\NullLogger();
45 }
46
47 public function setLogger( LoggerInterface $logger ) {
48 $this->replLogger = $logger;
49 }
50
51 public function scaleLoads( &$loads, $group = false, $wiki = false ) {
52 }
53
54 public function getLagTimes( $serverIndexes, $wiki ) {
55 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
56 # Single server only, just return zero without caching
57 return [ 0 => 0 ];
58 }
59
60 $key = $this->getLagTimeCacheKey();
61 # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec)
62 $ttl = mt_rand( 4e6, 5e6 ) / 1e6;
63 # Keep keys around longer as fallbacks
64 $staleTTL = 60;
65
66 # (a) Check the local APC cache
67 $value = $this->srvCache->get( $key );
68 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
69 $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from local cache" );
70 return $value['lagTimes']; // cache hit
71 }
72 $staleValue = $value ?: false;
73
74 # (b) Check the shared cache and backfill APC
75 $value = $this->mainCache->get( $key );
76 if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
77 $this->srvCache->set( $key, $value, $staleTTL );
78 $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from main cache" );
79
80 return $value['lagTimes']; // cache hit
81 }
82 $staleValue = $value ?: $staleValue;
83
84 # (c) Cache key missing or expired; regenerate and backfill
85 if ( $this->mainCache->lock( $key, 0, 10 ) ) {
86 # Let this process alone update the cache value
87 $cache = $this->mainCache;
88 /** @noinspection PhpUnusedLocalVariableInspection */
89 $unlocker = new ScopedCallback( function () use ( $cache, $key ) {
90 $cache->unlock( $key );
91 } );
92 } elseif ( $staleValue ) {
93 # Could not acquire lock but an old cache exists, so use it
94 return $staleValue['lagTimes'];
95 }
96
97 $lagTimes = [];
98 foreach ( $serverIndexes as $i ) {
99 if ( $i == $this->parent->getWriterIndex() ) {
100 $lagTimes[$i] = 0; // master always has no lag
101 continue;
102 }
103
104 $conn = $this->parent->getAnyOpenConnection( $i );
105 if ( $conn ) {
106 $close = false; // already open
107 } else {
108 $conn = $this->parent->openConnection( $i, $wiki );
109 $close = true; // new connection
110 }
111
112 if ( !$conn ) {
113 $lagTimes[$i] = false;
114 $host = $this->parent->getServerName( $i );
115 $this->replLogger->error( __METHOD__ . ": host $host (#$i) is unreachable" );
116 continue;
117 }
118
119 $lagTimes[$i] = $conn->getLag();
120 if ( $lagTimes[$i] === false ) {
121 $host = $this->parent->getServerName( $i );
122 $this->replLogger->error( __METHOD__ . ": host $host (#$i) is not replicating?" );
123 }
124
125 if ( $close ) {
126 # Close the connection to avoid sleeper connections piling up.
127 # Note that the caller will pick one of these DBs and reconnect,
128 # which is slightly inefficient, but this only matters for the lag
129 # time cache miss cache, which is far less common that cache hits.
130 $this->parent->closeConnection( $conn );
131 }
132 }
133
134 # Add a timestamp key so we know when it was cached
135 $value = [ 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) ];
136 $this->mainCache->set( $key, $value, $staleTTL );
137 $this->srvCache->set( $key, $value, $staleTTL );
138 $this->replLogger->info( __METHOD__ . ": re-calculated lag times ($key)" );
139
140 return $value['lagTimes'];
141 }
142
143 public function clearCaches() {
144 $key = $this->getLagTimeCacheKey();
145 $this->srvCache->delete( $key );
146 $this->mainCache->delete( $key );
147 }
148
149 private function getLagTimeCacheKey() {
150 $writerIndex = $this->parent->getWriterIndex();
151 // Lag is per-server, not per-DB, so key on the master DB name
152 return $this->srvCache->makeGlobalKey(
153 'lag-times', $this->parent->getServerName( $writerIndex )
154 );
155 }
156 }