f8bf3af1d3ce9d937fe8a7c686f0e526c57a388e
[lhc/web/wiklou.git] / includes / db / LoadMonitor.php
1 <?php
2 /**
3 * Database load monitoring
4 *
5 * @file
6 * @ingroup Database
7 */
8
9 /**
10 * An interface for database load monitoring
11 *
12 * @ingroup Database
13 */
14 interface LoadMonitor {
15 /**
16 * Construct a new LoadMonitor with a given LoadBalancer parent
17 *
18 * @param LoadBalancer $parent
19 */
20 function __construct( $parent );
21
22 /**
23 * Perform pre-connection load ratio adjustment.
24 * @param $loads array
25 * @param $group String: the selected query group
26 * @param $wiki String
27 */
28 function scaleLoads( &$loads, $group = false, $wiki = false );
29
30 /**
31 * Perform post-connection backoff.
32 *
33 * If the connection is in overload, this should return a backoff factor
34 * which will be used to control polling time. The number of threads
35 * connected is a good measure.
36 *
37 * If there is no overload, zero can be returned.
38 *
39 * A threshold thread count is given, the concrete class may compare this
40 * to the running thread count. The threshold may be false, which indicates
41 * that the sysadmin has not configured this feature.
42 *
43 * @param $conn DatabaseBase
44 * @param $threshold Float
45 */
46 function postConnectionBackoff( $conn, $threshold );
47
48 /**
49 * Return an estimate of replication lag for each server
50 */
51 function getLagTimes( $serverIndexes, $wiki );
52 }
53
54
55 /**
56 * Basic MySQL load monitor with no external dependencies
57 * Uses memcached to cache the replication lag for a short time
58 *
59 * @ingroup Database
60 */
61 class LoadMonitor_MySQL implements LoadMonitor {
62
63 /**
64 * @var LoadBalancer
65 */
66 var $parent;
67
68 /**
69 * @param LoadBalancer $parent
70 */
71 function __construct( $parent ) {
72 $this->parent = $parent;
73 }
74
75 /**
76 * @param $loads
77 * @param $group bool
78 * @param $wiki bool
79 */
80 function scaleLoads( &$loads, $group = false, $wiki = false ) {
81 }
82
83 /**
84 * @param $serverIndexes
85 * @param $wiki
86 * @return array
87 */
88 function getLagTimes( $serverIndexes, $wiki ) {
89 wfProfileIn( __METHOD__ );
90 $expiry = 5;
91 $requestRate = 10;
92
93 global $wgMemc;
94 if ( empty( $wgMemc ) )
95 $wgMemc = wfGetMainCache();
96
97 $masterName = $this->parent->getServerName( 0 );
98 $memcKey = wfMemcKey( 'lag_times', $masterName );
99 $times = $wgMemc->get( $memcKey );
100 if ( $times ) {
101 # Randomly recache with probability rising over $expiry
102 $elapsed = time() - $times['timestamp'];
103 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
104 if ( mt_rand( 0, $chance ) != 0 ) {
105 unset( $times['timestamp'] );
106 wfProfileOut( __METHOD__ );
107 return $times;
108 }
109 wfIncrStats( 'lag_cache_miss_expired' );
110 } else {
111 wfIncrStats( 'lag_cache_miss_absent' );
112 }
113
114 # Cache key missing or expired
115
116 $times = array();
117 foreach ( $serverIndexes as $i ) {
118 if ($i == 0) { # Master
119 $times[$i] = 0;
120 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
121 $times[$i] = $conn->getLag();
122 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
123 $times[$i] = $conn->getLag();
124 }
125 }
126
127 # Add a timestamp key so we know when it was cached
128 $times['timestamp'] = time();
129 $wgMemc->set( $memcKey, $times, $expiry );
130
131 # But don't give the timestamp to the caller
132 unset($times['timestamp']);
133 $lagTimes = $times;
134
135 wfProfileOut( __METHOD__ );
136 return $lagTimes;
137 }
138
139 /**
140 * @param $conn DatabaseBase
141 * @param $threshold
142 * @return int
143 */
144 function postConnectionBackoff( $conn, $threshold ) {
145 if ( !$threshold ) {
146 return 0;
147 }
148 $status = $conn->getStatus("Thread%");
149 if ( $status['Threads_running'] > $threshold ) {
150 return $status['Threads_connected'];
151 } else {
152 return 0;
153 }
154 }
155 }
156