General database-related code cleanup:
[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 * @param $serverIndexes
52 * @param $wiki
53 *
54 * @return array
55 */
56 function getLagTimes( $serverIndexes, $wiki );
57 }
58
59 class LoadMonitor_Null implements LoadMonitor {
60 function __construct( $parent ) {
61 }
62
63 function scaleLoads( &$loads, $group = false, $wiki = false ) {
64 }
65
66 function postConnectionBackoff( $conn, $threshold ) {
67 }
68
69 function getLagTimes( $serverIndexes, $wiki ) {
70 return array_fill_keys( $serverIndexes, 0 );
71 }
72 }
73
74
75 /**
76 * Basic MySQL load monitor with no external dependencies
77 * Uses memcached to cache the replication lag for a short time
78 *
79 * @ingroup Database
80 */
81 class LoadMonitor_MySQL implements LoadMonitor {
82
83 /**
84 * @var LoadBalancer
85 */
86 var $parent;
87
88 /**
89 * @param LoadBalancer $parent
90 */
91 function __construct( $parent ) {
92 $this->parent = $parent;
93 }
94
95 /**
96 * @param $loads
97 * @param $group bool
98 * @param $wiki bool
99 */
100 function scaleLoads( &$loads, $group = false, $wiki = false ) {
101 }
102
103 /**
104 * @param $serverIndexes
105 * @param $wiki
106 * @return array
107 */
108 function getLagTimes( $serverIndexes, $wiki ) {
109 wfProfileIn( __METHOD__ );
110 $expiry = 5;
111 $requestRate = 10;
112
113 global $wgMemc;
114 if ( empty( $wgMemc ) )
115 $wgMemc = wfGetMainCache();
116
117 $masterName = $this->parent->getServerName( 0 );
118 $memcKey = wfMemcKey( 'lag_times', $masterName );
119 $times = $wgMemc->get( $memcKey );
120 if ( $times ) {
121 # Randomly recache with probability rising over $expiry
122 $elapsed = time() - $times['timestamp'];
123 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
124 if ( mt_rand( 0, $chance ) != 0 ) {
125 unset( $times['timestamp'] );
126 wfProfileOut( __METHOD__ );
127 return $times;
128 }
129 wfIncrStats( 'lag_cache_miss_expired' );
130 } else {
131 wfIncrStats( 'lag_cache_miss_absent' );
132 }
133
134 # Cache key missing or expired
135
136 $times = array();
137 foreach ( $serverIndexes as $i ) {
138 if ($i == 0) { # Master
139 $times[$i] = 0;
140 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
141 $times[$i] = $conn->getLag();
142 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
143 $times[$i] = $conn->getLag();
144 }
145 }
146
147 # Add a timestamp key so we know when it was cached
148 $times['timestamp'] = time();
149 $wgMemc->set( $memcKey, $times, $expiry );
150
151 # But don't give the timestamp to the caller
152 unset($times['timestamp']);
153 $lagTimes = $times;
154
155 wfProfileOut( __METHOD__ );
156 return $lagTimes;
157 }
158
159 /**
160 * @param $conn DatabaseBase
161 * @param $threshold
162 * @return int
163 */
164 function postConnectionBackoff( $conn, $threshold ) {
165 if ( !$threshold ) {
166 return 0;
167 }
168 $status = $conn->getMysqlStatus("Thread%");
169 if ( $status['Threads_running'] > $threshold ) {
170 return $status['Threads_connected'];
171 } else {
172 return 0;
173 }
174 }
175 }
176