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