LoadMonitorMySQL::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
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 extends LoadMonitor {
29
	/** @var float What buffer pool use ratio counts as "warm" (e.g. 0.5 for 50% usage) */
30
	private $warmCacheRatio;
31
32
	public function __construct(
33
		ILoadBalancer $lb, BagOStuff $srvCache, BagOStuff $cache, array $options = []
34
	) {
35
		parent::__construct( $lb, $srvCache, $cache, $options );
36
37
		$this->warmCacheRatio = isset( $options['warmCacheRatio'] )
38
			? $options['warmCacheRatio']
39
			: 0.0;
40
	}
41
42
	protected function getWeightScale( $index, IDatabase $conn = null ) {
43
		if ( !$conn ) {
44
			return 0.0;
45
		}
46
47
		$weight = 1.0;
48
		if ( $this->warmCacheRatio > 0 ) {
49
			$res = $conn->query( 'SHOW STATUS', false );
50
			$s = $res ? $conn->fetchObject( $res ) : false;
0 ignored issues
show
Bug introduced by
It seems like $res defined by $conn->query('SHOW STATUS', false) on line 49 can also be of type boolean; however, IDatabase::fetchObject() does only seem to accept object<ResultWrapper>|object<stdClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51
			if ( $s === false ) {
52
				$host = $this->parent->getServerName( $index );
53
				$this->replLogger->error( __METHOD__ . ": could not get status for $host" );
54
			} else {
55
				// https://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
56
				if ( $s->Innodb_buffer_pool_pages_total > 0 ) {
57
					$ratio = $s->Innodb_buffer_pool_pages_data / $s->Innodb_buffer_pool_pages_total;
58
				} elseif ( $s->Qcache_total_blocks > 0 ) {
59
					$ratio = 1.0 - $s->Qcache_free_blocks / $s->Qcache_total_blocks;
60
				} else {
61
					$ratio = 1.0;
62
				}
63
				// Stop caring once $ratio >= $this->warmCacheRatio
64
				$weight *= min( $ratio / $this->warmCacheRatio, 1.0 );
65
			}
66
		}
67
68
		return $weight;
69
	}
70
}
71