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