Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / libs / rdbms / loadmonitor / LoadMonitorMySQL.php
blobc0db68c3a4d37a8a006038c464fe5ffe78e07430
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 namespace Wikimedia\Rdbms;
24 use IDatabase;
25 use BagOStuff;
27 /**
28 * Basic MySQL load monitor with no external dependencies
29 * Uses memcached to cache the replication lag for a short time
31 * @ingroup Database
33 class LoadMonitorMySQL extends LoadMonitor {
34 /** @var float What buffer pool use ratio counts as "warm" (e.g. 0.5 for 50% usage) */
35 private $warmCacheRatio;
37 public function __construct(
38 ILoadBalancer $lb, BagOStuff $srvCache, BagOStuff $cache, array $options = []
39 ) {
40 parent::__construct( $lb, $srvCache, $cache, $options );
42 $this->warmCacheRatio = isset( $options['warmCacheRatio'] )
43 ? $options['warmCacheRatio']
44 : 0.0;
47 protected function getWeightScale( $index, IDatabase $conn = null ) {
48 if ( !$conn ) {
49 return 0.0;
52 $weight = 1.0;
53 if ( $this->warmCacheRatio > 0 ) {
54 $res = $conn->query( 'SHOW STATUS', false );
55 $s = $res ? $conn->fetchObject( $res ) : false;
56 if ( $s === false ) {
57 $host = $this->parent->getServerName( $index );
58 $this->replLogger->error( __METHOD__ . ": could not get status for $host" );
59 } else {
60 // https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
61 if ( $s->Innodb_buffer_pool_pages_total > 0 ) {
62 $ratio = $s->Innodb_buffer_pool_pages_data / $s->Innodb_buffer_pool_pages_total;
63 } else {
64 $ratio = 1.0;
66 // Stop caring once $ratio >= $this->warmCacheRatio
67 $weight *= min( $ratio / $this->warmCacheRatio, 1.0 );
71 return $weight;