Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LoadBalancer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LoadBalancer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class LoadBalancer { |
||
| 31 | /** @var array[] Map of (server index => server config array) */ |
||
| 32 | private $mServers; |
||
| 33 | /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */ |
||
| 34 | private $mConns; |
||
| 35 | /** @var array Map of (server index => weight) */ |
||
| 36 | private $mLoads; |
||
| 37 | /** @var array[] Map of (group => server index => weight) */ |
||
| 38 | private $mGroupLoads; |
||
| 39 | /** @var bool Whether to disregard replica DB lag as a factor in replica DB selection */ |
||
| 40 | private $mAllowLagged; |
||
| 41 | /** @var integer Seconds to spend waiting on replica DB lag to resolve */ |
||
| 42 | private $mWaitTimeout; |
||
| 43 | /** @var array LBFactory information */ |
||
| 44 | private $mParentInfo; |
||
| 45 | |||
| 46 | /** @var string The LoadMonitor subclass name */ |
||
| 47 | private $mLoadMonitorClass; |
||
| 48 | /** @var LoadMonitor */ |
||
| 49 | private $mLoadMonitor; |
||
| 50 | /** @var BagOStuff */ |
||
| 51 | private $srvCache; |
||
| 52 | /** @var WANObjectCache */ |
||
| 53 | private $wanCache; |
||
| 54 | /** @var TransactionProfiler */ |
||
| 55 | protected $trxProfiler; |
||
| 56 | |||
| 57 | /** @var bool|DatabaseBase Database connection that caused a problem */ |
||
| 58 | private $mErrorConnection; |
||
| 59 | /** @var integer The generic (not query grouped) replica DB index (of $mServers) */ |
||
| 60 | private $mReadIndex; |
||
| 61 | /** @var bool|DBMasterPos False if not set */ |
||
| 62 | private $mWaitForPos; |
||
| 63 | /** @var bool Whether the generic reader fell back to a lagged replica DB */ |
||
| 64 | private $laggedSlaveMode = false; |
||
| 65 | /** @var bool Whether the generic reader fell back to a lagged replica DB */ |
||
| 66 | private $slavesDownMode = false; |
||
| 67 | /** @var string The last DB selection or connection error */ |
||
| 68 | private $mLastError = 'Unknown error'; |
||
| 69 | /** @var string|bool Reason the LB is read-only or false if not */ |
||
| 70 | private $readOnlyReason = false; |
||
| 71 | /** @var integer Total connections opened */ |
||
| 72 | private $connsOpened = 0; |
||
| 73 | /** @var string|bool String if a requested DBO_TRX transaction round is active */ |
||
| 74 | private $trxRoundId = false; |
||
| 75 | /** @var array[] Map of (name => callable) */ |
||
| 76 | private $trxRecurringCallbacks = []; |
||
| 77 | |||
| 78 | /** @var integer Warn when this many connection are held */ |
||
| 79 | const CONN_HELD_WARN_THRESHOLD = 10; |
||
| 80 | /** @var integer Default 'max lag' when unspecified */ |
||
| 81 | const MAX_LAG = 10; |
||
| 82 | /** @var integer Max time to wait for a replica DB to catch up (e.g. ChronologyProtector) */ |
||
| 83 | const POS_WAIT_TIMEOUT = 10; |
||
| 84 | /** @var integer Seconds to cache master server read-only status */ |
||
| 85 | const TTL_CACHE_READONLY = 5; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var boolean |
||
| 89 | */ |
||
| 90 | private $disabled = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param array $params Array with keys: |
||
| 94 | * - servers : Required. Array of server info structures. |
||
| 95 | * - loadMonitor : Name of a class used to fetch server lag and load. |
||
| 96 | * - readOnlyReason : Reason the master DB is read-only if so [optional] |
||
| 97 | * - srvCache : BagOStuff object [optional] |
||
| 98 | * - wanCache : WANObjectCache object [optional] |
||
| 99 | * @throws MWException |
||
| 100 | */ |
||
| 101 | public function __construct( array $params ) { |
||
| 102 | if ( !isset( $params['servers'] ) ) { |
||
| 103 | throw new MWException( __CLASS__ . ': missing servers parameter' ); |
||
| 104 | } |
||
| 105 | $this->mServers = $params['servers']; |
||
| 106 | $this->mWaitTimeout = self::POS_WAIT_TIMEOUT; |
||
| 107 | |||
| 108 | $this->mReadIndex = -1; |
||
| 109 | $this->mWriteIndex = -1; |
||
|
|
|||
| 110 | $this->mConns = [ |
||
| 111 | 'local' => [], |
||
| 112 | 'foreignUsed' => [], |
||
| 113 | 'foreignFree' => [] ]; |
||
| 114 | $this->mLoads = []; |
||
| 115 | $this->mWaitForPos = false; |
||
| 116 | $this->mErrorConnection = false; |
||
| 117 | $this->mAllowLagged = false; |
||
| 118 | |||
| 119 | View Code Duplication | if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) { |
|
| 120 | $this->readOnlyReason = $params['readOnlyReason']; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( isset( $params['loadMonitor'] ) ) { |
||
| 124 | $this->mLoadMonitorClass = $params['loadMonitor']; |
||
| 125 | } else { |
||
| 126 | $master = reset( $params['servers'] ); |
||
| 127 | if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { |
||
| 128 | $this->mLoadMonitorClass = 'LoadMonitorMySQL'; |
||
| 129 | } else { |
||
| 130 | $this->mLoadMonitorClass = 'LoadMonitorNull'; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | foreach ( $params['servers'] as $i => $server ) { |
||
| 135 | $this->mLoads[$i] = $server['load']; |
||
| 136 | if ( isset( $server['groupLoads'] ) ) { |
||
| 137 | foreach ( $server['groupLoads'] as $group => $ratio ) { |
||
| 138 | if ( !isset( $this->mGroupLoads[$group] ) ) { |
||
| 139 | $this->mGroupLoads[$group] = []; |
||
| 140 | } |
||
| 141 | $this->mGroupLoads[$group][$i] = $ratio; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ( isset( $params['srvCache'] ) ) { |
||
| 147 | $this->srvCache = $params['srvCache']; |
||
| 148 | } else { |
||
| 149 | $this->srvCache = new EmptyBagOStuff(); |
||
| 150 | } |
||
| 151 | if ( isset( $params['wanCache'] ) ) { |
||
| 152 | $this->wanCache = $params['wanCache']; |
||
| 153 | } else { |
||
| 154 | $this->wanCache = WANObjectCache::newEmpty(); |
||
| 155 | } |
||
| 156 | if ( isset( $params['trxProfiler'] ) ) { |
||
| 157 | $this->trxProfiler = $params['trxProfiler']; |
||
| 158 | } else { |
||
| 159 | $this->trxProfiler = new TransactionProfiler(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get a LoadMonitor instance |
||
| 165 | * |
||
| 166 | * @return LoadMonitor |
||
| 167 | */ |
||
| 168 | private function getLoadMonitor() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get or set arbitrary data used by the parent object, usually an LBFactory |
||
| 179 | * @param mixed $x |
||
| 180 | * @return mixed |
||
| 181 | */ |
||
| 182 | public function parentInfo( $x = null ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $loads |
||
| 188 | * @param bool|string $wiki Wiki to get non-lagged for |
||
| 189 | * @param int $maxLag Restrict the maximum allowed lag to this many seconds |
||
| 190 | * @return bool|int|string |
||
| 191 | */ |
||
| 192 | private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) { |
||
| 193 | $lags = $this->getLagTimes( $wiki ); |
||
| 194 | |||
| 195 | # Unset excessively lagged servers |
||
| 196 | foreach ( $lags as $i => $lag ) { |
||
| 197 | if ( $i != 0 ) { |
||
| 198 | $maxServerLag = $maxLag; |
||
| 199 | View Code Duplication | if ( isset( $this->mServers[$i]['max lag'] ) ) { |
|
| 200 | $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] ); |
||
| 201 | } |
||
| 202 | |||
| 203 | $host = $this->getServerName( $i ); |
||
| 204 | if ( $lag === false ) { |
||
| 205 | wfDebugLog( 'replication', "Server $host (#$i) is not replicating?" ); |
||
| 206 | unset( $loads[$i] ); |
||
| 207 | } elseif ( $lag > $maxServerLag ) { |
||
| 208 | wfDebugLog( 'replication', "Server $host (#$i) has >= $lag seconds of lag" ); |
||
| 209 | unset( $loads[$i] ); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | # Find out if all the replica DBs with non-zero load are lagged |
||
| 215 | $sum = 0; |
||
| 216 | foreach ( $loads as $load ) { |
||
| 217 | $sum += $load; |
||
| 218 | } |
||
| 219 | if ( $sum == 0 ) { |
||
| 220 | # No appropriate DB servers except maybe the master and some replica DBs with zero load |
||
| 221 | # Do NOT use the master |
||
| 222 | # Instead, this function will return false, triggering read-only mode, |
||
| 223 | # and a lagged replica DB will be used instead. |
||
| 224 | return false; |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( count( $loads ) == 0 ) { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | # Return a random representative of the remainder |
||
| 232 | return ArrayUtils::pickRandom( $loads ); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the index of the reader connection, which may be a replica DB |
||
| 237 | * This takes into account load ratios and lag times. It should |
||
| 238 | * always return a consistent index during a given invocation |
||
| 239 | * |
||
| 240 | * Side effect: opens connections to databases |
||
| 241 | * @param string|bool $group Query group, or false for the generic reader |
||
| 242 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 243 | * @throws MWException |
||
| 244 | * @return bool|int|string |
||
| 245 | */ |
||
| 246 | public function getReaderIndex( $group = false, $wiki = false ) { |
||
| 247 | global $wgDBtype; |
||
| 248 | |||
| 249 | # @todo FIXME: For now, only go through all this for mysql databases |
||
| 250 | if ( $wgDBtype != 'mysql' ) { |
||
| 251 | return $this->getWriterIndex(); |
||
| 252 | } |
||
| 253 | |||
| 254 | if ( count( $this->mServers ) == 1 ) { |
||
| 255 | # Skip the load balancing if there's only one server |
||
| 256 | return 0; |
||
| 257 | } elseif ( $group === false && $this->mReadIndex >= 0 ) { |
||
| 258 | # Shortcut if generic reader exists already |
||
| 259 | return $this->mReadIndex; |
||
| 260 | } |
||
| 261 | |||
| 262 | # Find the relevant load array |
||
| 263 | if ( $group !== false ) { |
||
| 264 | if ( isset( $this->mGroupLoads[$group] ) ) { |
||
| 265 | $nonErrorLoads = $this->mGroupLoads[$group]; |
||
| 266 | } else { |
||
| 267 | # No loads for this group, return false and the caller can use some other group |
||
| 268 | wfDebugLog( 'connect', __METHOD__ . ": no loads for group $group\n" ); |
||
| 269 | |||
| 270 | return false; |
||
| 271 | } |
||
| 272 | } else { |
||
| 273 | $nonErrorLoads = $this->mLoads; |
||
| 274 | } |
||
| 275 | |||
| 276 | if ( !count( $nonErrorLoads ) ) { |
||
| 277 | throw new MWException( "Empty server array given to LoadBalancer" ); |
||
| 278 | } |
||
| 279 | |||
| 280 | # Scale the configured load ratios according to the dynamic load (if the load monitor supports it) |
||
| 281 | $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki ); |
||
| 282 | |||
| 283 | $laggedSlaveMode = false; |
||
| 284 | |||
| 285 | # No server found yet |
||
| 286 | $i = false; |
||
| 287 | $conn = false; |
||
| 288 | # First try quickly looking through the available servers for a server that |
||
| 289 | # meets our criteria |
||
| 290 | $currentLoads = $nonErrorLoads; |
||
| 291 | while ( count( $currentLoads ) ) { |
||
| 292 | if ( $this->mAllowLagged || $laggedSlaveMode ) { |
||
| 293 | $i = ArrayUtils::pickRandom( $currentLoads ); |
||
| 294 | } else { |
||
| 295 | $i = false; |
||
| 296 | if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) { |
||
| 297 | # ChronologyProtecter causes mWaitForPos to be set via sessions. |
||
| 298 | # This triggers doWait() after connect, so it's especially good to |
||
| 299 | # avoid lagged servers so as to avoid just blocking in that method. |
||
| 300 | $ago = microtime( true ) - $this->mWaitForPos->asOfTime(); |
||
| 301 | # Aim for <= 1 second of waiting (being too picky can backfire) |
||
| 302 | $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 ); |
||
| 303 | } |
||
| 304 | if ( $i === false ) { |
||
| 305 | # Any server with less lag than it's 'max lag' param is preferable |
||
| 306 | $i = $this->getRandomNonLagged( $currentLoads, $wiki ); |
||
| 307 | } |
||
| 308 | if ( $i === false && count( $currentLoads ) != 0 ) { |
||
| 309 | # All replica DBs lagged. Switch to read-only mode |
||
| 310 | wfDebugLog( 'replication', "All replica DBs lagged. Switch to read-only mode" ); |
||
| 311 | $i = ArrayUtils::pickRandom( $currentLoads ); |
||
| 312 | $laggedSlaveMode = true; |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | if ( $i === false ) { |
||
| 317 | # pickRandom() returned false |
||
| 318 | # This is permanent and means the configuration or the load monitor |
||
| 319 | # wants us to return false. |
||
| 320 | wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" ); |
||
| 321 | |||
| 322 | return false; |
||
| 323 | } |
||
| 324 | |||
| 325 | $serverName = $this->getServerName( $i ); |
||
| 326 | wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." ); |
||
| 327 | |||
| 328 | $conn = $this->openConnection( $i, $wiki ); |
||
| 329 | if ( !$conn ) { |
||
| 330 | wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" ); |
||
| 331 | unset( $nonErrorLoads[$i] ); |
||
| 332 | unset( $currentLoads[$i] ); |
||
| 333 | $i = false; |
||
| 334 | continue; |
||
| 335 | } |
||
| 336 | |||
| 337 | // Decrement reference counter, we are finished with this connection. |
||
| 338 | // It will be incremented for the caller later. |
||
| 339 | if ( $wiki !== false ) { |
||
| 340 | $this->reuseConnection( $conn ); |
||
| 341 | } |
||
| 342 | |||
| 343 | # Return this server |
||
| 344 | break; |
||
| 345 | } |
||
| 346 | |||
| 347 | # If all servers were down, quit now |
||
| 348 | if ( !count( $nonErrorLoads ) ) { |
||
| 349 | wfDebugLog( 'connect', "All servers down" ); |
||
| 350 | } |
||
| 351 | |||
| 352 | if ( $i !== false ) { |
||
| 353 | # replica DB connection successful |
||
| 354 | # Wait for the session master pos for a short time |
||
| 355 | View Code Duplication | if ( $this->mWaitForPos && $i > 0 ) { |
|
| 356 | if ( !$this->doWait( $i ) ) { |
||
| 357 | $this->mServers[$i]['slave pos'] = $conn->getSlavePos(); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) { |
||
| 361 | $this->mReadIndex = $i; |
||
| 362 | # Record if the generic reader index is in "lagged replica DB" mode |
||
| 363 | if ( $laggedSlaveMode ) { |
||
| 364 | $this->laggedSlaveMode = true; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | $serverName = $this->getServerName( $i ); |
||
| 368 | wfDebugLog( 'connect', __METHOD__ . |
||
| 369 | ": using server $serverName for group '$group'\n" ); |
||
| 370 | } |
||
| 371 | |||
| 372 | return $i; |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Set the master wait position |
||
| 377 | * If a DB_SLAVE connection has been opened already, waits |
||
| 378 | * Otherwise sets a variable telling it to wait if such a connection is opened |
||
| 379 | * @param DBMasterPos $pos |
||
| 380 | */ |
||
| 381 | public function waitFor( $pos ) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set the master wait position and wait for a "generic" replica DB to catch up to it |
||
| 395 | * |
||
| 396 | * This can be used a faster proxy for waitForAll() |
||
| 397 | * |
||
| 398 | * @param DBMasterPos $pos |
||
| 399 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 400 | * @return bool Success (able to connect and no timeouts reached) |
||
| 401 | * @since 1.26 |
||
| 402 | */ |
||
| 403 | public function waitForOne( $pos, $timeout = null ) { |
||
| 404 | $this->mWaitForPos = $pos; |
||
| 405 | |||
| 406 | $i = $this->mReadIndex; |
||
| 407 | if ( $i <= 0 ) { |
||
| 408 | // Pick a generic replica DB if there isn't one yet |
||
| 409 | $readLoads = $this->mLoads; |
||
| 410 | unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only |
||
| 411 | $readLoads = array_filter( $readLoads ); // with non-zero load |
||
| 412 | $i = ArrayUtils::pickRandom( $readLoads ); |
||
| 413 | } |
||
| 414 | |||
| 415 | View Code Duplication | if ( $i > 0 ) { |
|
| 416 | $ok = $this->doWait( $i, true, $timeout ); |
||
| 417 | } else { |
||
| 418 | $ok = true; // no applicable loads |
||
| 419 | } |
||
| 420 | |||
| 421 | return $ok; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Set the master wait position and wait for ALL replica DBs to catch up to it |
||
| 426 | * @param DBMasterPos $pos |
||
| 427 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 428 | * @return bool Success (able to connect and no timeouts reached) |
||
| 429 | */ |
||
| 430 | public function waitForAll( $pos, $timeout = null ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get any open connection to a given server index, local or foreign |
||
| 446 | * Returns false if there is no connection open |
||
| 447 | * |
||
| 448 | * @param int $i |
||
| 449 | * @return DatabaseBase|bool False on failure |
||
| 450 | */ |
||
| 451 | public function getAnyOpenConnection( $i ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Wait for a given replica DB to catch up to the master pos stored in $this |
||
| 463 | * @param int $index Server index |
||
| 464 | * @param bool $open Check the server even if a new connection has to be made |
||
| 465 | * @param int $timeout Max seconds to wait; default is mWaitTimeout |
||
| 466 | * @return bool |
||
| 467 | */ |
||
| 468 | protected function doWait( $index, $open = false, $timeout = null ) { |
||
| 469 | $close = false; // close the connection afterwards |
||
| 470 | |||
| 471 | // Check if we already know that the DB has reached this point |
||
| 472 | $server = $this->getServerName( $index ); |
||
| 473 | $key = $this->srvCache->makeGlobalKey( __CLASS__, 'last-known-pos', $server ); |
||
| 474 | /** @var DBMasterPos $knownReachedPos */ |
||
| 475 | $knownReachedPos = $this->srvCache->get( $key ); |
||
| 476 | if ( $knownReachedPos && $knownReachedPos->hasReached( $this->mWaitForPos ) ) { |
||
| 477 | wfDebugLog( 'replication', __METHOD__ . |
||
| 478 | ": replica DB $server known to be caught up (pos >= $knownReachedPos).\n" ); |
||
| 479 | return true; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Find a connection to wait on, creating one if needed and allowed |
||
| 483 | $conn = $this->getAnyOpenConnection( $index ); |
||
| 484 | if ( !$conn ) { |
||
| 485 | if ( !$open ) { |
||
| 486 | wfDebugLog( 'replication', __METHOD__ . ": no connection open for $server\n" ); |
||
| 487 | |||
| 488 | return false; |
||
| 489 | } else { |
||
| 490 | $conn = $this->openConnection( $index, '' ); |
||
| 491 | if ( !$conn ) { |
||
| 492 | wfDebugLog( 'replication', __METHOD__ . ": failed to connect to $server\n" ); |
||
| 493 | |||
| 494 | return false; |
||
| 495 | } |
||
| 496 | // Avoid connection spam in waitForAll() when connections |
||
| 497 | // are made just for the sake of doing this lag check. |
||
| 498 | $close = true; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | wfDebugLog( 'replication', __METHOD__ . ": Waiting for replica DB $server to catch up...\n" ); |
||
| 503 | $timeout = $timeout ?: $this->mWaitTimeout; |
||
| 504 | $result = $conn->masterPosWait( $this->mWaitForPos, $timeout ); |
||
| 505 | |||
| 506 | if ( $result == -1 || is_null( $result ) ) { |
||
| 507 | // Timed out waiting for replica DB, use master instead |
||
| 508 | $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}"; |
||
| 509 | wfDebugLog( 'replication', "$msg\n" ); |
||
| 510 | wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) ); |
||
| 511 | $ok = false; |
||
| 512 | } else { |
||
| 513 | wfDebugLog( 'replication', __METHOD__ . ": Done\n" ); |
||
| 514 | $ok = true; |
||
| 515 | // Remember that the DB reached this point |
||
| 516 | $this->srvCache->set( $key, $this->mWaitForPos, BagOStuff::TTL_DAY ); |
||
| 517 | } |
||
| 518 | |||
| 519 | if ( $close ) { |
||
| 520 | $this->closeConnection( $conn ); |
||
| 521 | } |
||
| 522 | |||
| 523 | return $ok; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get a connection by index |
||
| 528 | * This is the main entry point for this class. |
||
| 529 | * |
||
| 530 | * @param int $i Server index |
||
| 531 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 532 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 533 | * |
||
| 534 | * @throws MWException |
||
| 535 | * @return DatabaseBase |
||
| 536 | */ |
||
| 537 | public function getConnection( $i, $groups = [], $wiki = false ) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Mark a foreign connection as being available for reuse under a different |
||
| 606 | * DB name or prefix. This mechanism is reference-counted, and must be called |
||
| 607 | * the same number of times as getConnection() to work. |
||
| 608 | * |
||
| 609 | * @param DatabaseBase $conn |
||
| 610 | * @throws MWException |
||
| 611 | */ |
||
| 612 | public function reuseConnection( $conn ) { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Get a database connection handle reference |
||
| 652 | * |
||
| 653 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 654 | * |
||
| 655 | * @see LoadBalancer::getConnection() for parameter information |
||
| 656 | * |
||
| 657 | * @param int $db |
||
| 658 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 659 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 660 | * @return DBConnRef |
||
| 661 | */ |
||
| 662 | public function getConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get a database connection handle reference without connecting yet |
||
| 668 | * |
||
| 669 | * The handle's methods wrap simply wrap those of a DatabaseBase handle |
||
| 670 | * |
||
| 671 | * @see LoadBalancer::getConnection() for parameter information |
||
| 672 | * |
||
| 673 | * @param int $db |
||
| 674 | * @param array|string|bool $groups Query group(s), or false for the generic reader |
||
| 675 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 676 | * @return DBConnRef |
||
| 677 | */ |
||
| 678 | public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Open a connection to the server given by the specified index |
||
| 684 | * Index must be an actual index into the array. |
||
| 685 | * If the server is already open, returns it. |
||
| 686 | * |
||
| 687 | * On error, returns false, and the connection which caused the |
||
| 688 | * error will be available via $this->mErrorConnection. |
||
| 689 | * |
||
| 690 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 691 | * |
||
| 692 | * @param int $i Server index |
||
| 693 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 694 | * @return DatabaseBase|bool Returns false on errors |
||
| 695 | */ |
||
| 696 | public function openConnection( $i, $wiki = false ) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Open a connection to a foreign DB, or return one if it is already open. |
||
| 730 | * |
||
| 731 | * Increments a reference count on the returned connection which locks the |
||
| 732 | * connection to the requested wiki. This reference count can be |
||
| 733 | * decremented by calling reuseConnection(). |
||
| 734 | * |
||
| 735 | * If a connection is open to the appropriate server already, but with the wrong |
||
| 736 | * database, it will be switched to the right database and returned, as long as |
||
| 737 | * it has been freed first with reuseConnection(). |
||
| 738 | * |
||
| 739 | * On error, returns false, and the connection which caused the |
||
| 740 | * error will be available via $this->mErrorConnection. |
||
| 741 | * |
||
| 742 | * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. |
||
| 743 | * |
||
| 744 | * @param int $i Server index |
||
| 745 | * @param string $wiki Wiki ID to open |
||
| 746 | * @return DatabaseBase |
||
| 747 | */ |
||
| 748 | private function openForeignConnection( $i, $wiki ) { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Test if the specified index represents an open connection |
||
| 807 | * |
||
| 808 | * @param int $index Server index |
||
| 809 | * @access private |
||
| 810 | * @return bool |
||
| 811 | */ |
||
| 812 | private function isOpen( $index ) { |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Really opens a connection. Uncached. |
||
| 822 | * Returns a Database object whether or not the connection was successful. |
||
| 823 | * @access private |
||
| 824 | * |
||
| 825 | * @param array $server |
||
| 826 | * @param bool $dbNameOverride |
||
| 827 | * @throws MWException |
||
| 828 | * @return DatabaseBase |
||
| 829 | */ |
||
| 830 | protected function reallyOpenConnection( $server, $dbNameOverride = false ) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @throws DBConnectionError |
||
| 884 | * @return bool |
||
| 885 | */ |
||
| 886 | private function reportConnectionError() { |
||
| 915 | |||
| 916 | /** |
||
| 917 | * @return int |
||
| 918 | * @since 1.26 |
||
| 919 | */ |
||
| 920 | public function getWriterIndex() { |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Returns true if the specified index is a valid server index |
||
| 926 | * |
||
| 927 | * @param string $i |
||
| 928 | * @return bool |
||
| 929 | */ |
||
| 930 | public function haveIndex( $i ) { |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Returns true if the specified index is valid and has non-zero load |
||
| 936 | * |
||
| 937 | * @param string $i |
||
| 938 | * @return bool |
||
| 939 | */ |
||
| 940 | public function isNonZeroLoad( $i ) { |
||
| 943 | |||
| 944 | /** |
||
| 945 | * Get the number of defined servers (not the number of open connections) |
||
| 946 | * |
||
| 947 | * @return int |
||
| 948 | */ |
||
| 949 | public function getServerCount() { |
||
| 952 | |||
| 953 | /** |
||
| 954 | * Get the host name or IP address of the server with the specified index |
||
| 955 | * Prefer a readable name if available. |
||
| 956 | * @param string $i |
||
| 957 | * @return string |
||
| 958 | */ |
||
| 959 | public function getServerName( $i ) { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Return the server info structure for a given index, or false if the index is invalid. |
||
| 973 | * @param int $i |
||
| 974 | * @return array|bool |
||
| 975 | */ |
||
| 976 | public function getServerInfo( $i ) { |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Sets the server info structure for the given index. Entry at index $i |
||
| 986 | * is created if it doesn't exist |
||
| 987 | * @param int $i |
||
| 988 | * @param array $serverInfo |
||
| 989 | */ |
||
| 990 | public function setServerInfo( $i, array $serverInfo ) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Get the current master position for chronology control purposes |
||
| 996 | * @return mixed |
||
| 997 | */ |
||
| 998 | public function getMasterPos() { |
||
| 999 | # If this entire request was served from a replica DB without opening a connection to the |
||
| 1000 | # master (however unlikely that may be), then we can fetch the position from the replica DB. |
||
| 1001 | $masterConn = $this->getAnyOpenConnection( 0 ); |
||
| 1002 | if ( !$masterConn ) { |
||
| 1003 | $serverCount = count( $this->mServers ); |
||
| 1004 | for ( $i = 1; $i < $serverCount; $i++ ) { |
||
| 1005 | $conn = $this->getAnyOpenConnection( $i ); |
||
| 1006 | if ( $conn ) { |
||
| 1007 | return $conn->getSlavePos(); |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | } else { |
||
| 1011 | return $masterConn->getMasterPos(); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | return false; |
||
| 1015 | } |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Disable this load balancer. All connections are closed, and any attempt to |
||
| 1019 | * open a new connection will result in a DBAccessError. |
||
| 1020 | * |
||
| 1021 | * @since 1.27 |
||
| 1022 | */ |
||
| 1023 | public function disable() { |
||
| 1027 | |||
| 1028 | /** |
||
| 1029 | * Close all open connections |
||
| 1030 | */ |
||
| 1031 | public function closeAll() { |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Close a connection |
||
| 1046 | * Using this function makes sure the LoadBalancer knows the connection is closed. |
||
| 1047 | * If you use $conn->close() directly, the load balancer won't update its state. |
||
| 1048 | * @param DatabaseBase $conn |
||
| 1049 | */ |
||
| 1050 | public function closeConnection( $conn ) { |
||
| 1069 | |||
| 1070 | /** |
||
| 1071 | * Commit transactions on all open connections |
||
| 1072 | * @param string $fname Caller name |
||
| 1073 | * @throws DBExpectedError |
||
| 1074 | */ |
||
| 1075 | public function commitAll( $fname = __METHOD__ ) { |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Perform all pre-commit callbacks that remain part of the atomic transactions |
||
| 1104 | * and disable any post-commit callbacks until runMasterPostTrxCallbacks() |
||
| 1105 | * @since 1.28 |
||
| 1106 | */ |
||
| 1107 | public function finalizeMasterChanges() { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Perform all pre-commit checks for things like replication safety |
||
| 1119 | * @param array $options Includes: |
||
| 1120 | * - maxWriteDuration : max write query duration time in seconds |
||
| 1121 | * @throws DBTransactionError |
||
| 1122 | * @since 1.28 |
||
| 1123 | */ |
||
| 1124 | public function approveMasterChanges( array $options ) { |
||
| 1155 | |||
| 1156 | /** |
||
| 1157 | * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
||
| 1158 | * |
||
| 1159 | * The DBO_TRX setting will be reverted to the default in each of these methods: |
||
| 1160 | * - commitMasterChanges() |
||
| 1161 | * - rollbackMasterChanges() |
||
| 1162 | * - commitAll() |
||
| 1163 | * This allows for custom transaction rounds from any outer transaction scope. |
||
| 1164 | * |
||
| 1165 | * @param string $fname |
||
| 1166 | * @throws DBExpectedError |
||
| 1167 | * @since 1.28 |
||
| 1168 | */ |
||
| 1169 | public function beginMasterChanges( $fname = __METHOD__ ) { |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Issue COMMIT on all master connections where writes where done |
||
| 1203 | * @param string $fname Caller name |
||
| 1204 | * @throws DBExpectedError |
||
| 1205 | */ |
||
| 1206 | public function commitMasterChanges( $fname = __METHOD__ ) { |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Issue all pending post-COMMIT/ROLLBACK callbacks |
||
| 1239 | * @param integer $type IDatabase::TRIGGER_* constant |
||
| 1240 | * @return Exception|null The first exception or null if there were none |
||
| 1241 | * @since 1.28 |
||
| 1242 | */ |
||
| 1243 | public function runMasterPostTrxCallbacks( $type ) { |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Issue ROLLBACK only on master, only if queries were done on connection |
||
| 1266 | * @param string $fname Caller name |
||
| 1267 | * @throws DBExpectedError |
||
| 1268 | * @since 1.23 |
||
| 1269 | */ |
||
| 1270 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 1284 | |||
| 1285 | /** |
||
| 1286 | * Suppress all pending post-COMMIT/ROLLBACK callbacks |
||
| 1287 | * @return Exception|null The first exception or null if there were none |
||
| 1288 | * @since 1.28 |
||
| 1289 | */ |
||
| 1290 | public function suppressTransactionEndCallbacks() { |
||
| 1295 | |||
| 1296 | /** |
||
| 1297 | * @param DatabaseBase $conn |
||
| 1298 | */ |
||
| 1299 | private function applyTransactionRoundFlags( DatabaseBase $conn ) { |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * @param DatabaseBase $conn |
||
| 1312 | */ |
||
| 1313 | private function undoTransactionRoundFlags( DatabaseBase $conn ) { |
||
| 1318 | |||
| 1319 | /** |
||
| 1320 | * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot |
||
| 1321 | * |
||
| 1322 | * @param string $fname Caller name |
||
| 1323 | * @since 1.28 |
||
| 1324 | */ |
||
| 1325 | public function flushReplicaSnapshots( $fname = __METHOD__ ) { |
||
| 1330 | |||
| 1331 | /** |
||
| 1332 | * @return bool Whether a master connection is already open |
||
| 1333 | * @since 1.24 |
||
| 1334 | */ |
||
| 1335 | public function hasMasterConnection() { |
||
| 1338 | |||
| 1339 | /** |
||
| 1340 | * Determine if there are pending changes in a transaction by this thread |
||
| 1341 | * @since 1.23 |
||
| 1342 | * @return bool |
||
| 1343 | */ |
||
| 1344 | public function hasMasterChanges() { |
||
| 1359 | |||
| 1360 | /** |
||
| 1361 | * Get the timestamp of the latest write query done by this thread |
||
| 1362 | * @since 1.25 |
||
| 1363 | * @return float|bool UNIX timestamp or false |
||
| 1364 | */ |
||
| 1365 | View Code Duplication | public function lastMasterChangeTimestamp() { |
|
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Check if this load balancer object had any recent or still |
||
| 1382 | * pending writes issued against it by this PHP thread |
||
| 1383 | * |
||
| 1384 | * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout] |
||
| 1385 | * @return bool |
||
| 1386 | * @since 1.25 |
||
| 1387 | */ |
||
| 1388 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * Get the list of callers that have pending master changes |
||
| 1397 | * |
||
| 1398 | * @return array |
||
| 1399 | * @since 1.27 |
||
| 1400 | */ |
||
| 1401 | View Code Duplication | public function pendingMasterChangeCallers() { |
|
| 1417 | |||
| 1418 | /** |
||
| 1419 | * @param mixed $value |
||
| 1420 | * @return mixed |
||
| 1421 | */ |
||
| 1422 | public function waitTimeout( $value = null ) { |
||
| 1425 | |||
| 1426 | /** |
||
| 1427 | * @note This method will trigger a DB connection if not yet done |
||
| 1428 | * |
||
| 1429 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1430 | * @return bool Whether the generic connection for reads is highly "lagged" |
||
| 1431 | */ |
||
| 1432 | public function getLaggedSlaveMode( $wiki = false ) { |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * @note This method will never cause a new DB connection |
||
| 1451 | * @return bool Whether any generic connection used for reads was highly "lagged" |
||
| 1452 | * @since 1.27 |
||
| 1453 | */ |
||
| 1454 | public function laggedSlaveUsed() { |
||
| 1457 | |||
| 1458 | /** |
||
| 1459 | * @note This method may trigger a DB connection if not yet done |
||
| 1460 | * @param string|bool $wiki Wiki ID, or false for the current wiki |
||
| 1461 | * @param DatabaseBase|null DB master connection; used to avoid loops [optional] |
||
| 1462 | * @return string|bool Reason the master is read-only or false if it is not |
||
| 1463 | * @since 1.27 |
||
| 1464 | */ |
||
| 1465 | public function getReadOnlyReason( $wiki = false, DatabaseBase $conn = null ) { |
||
| 1466 | if ( $this->readOnlyReason !== false ) { |
||
| 1467 | return $this->readOnlyReason; |
||
| 1468 | } elseif ( $this->getLaggedSlaveMode( $wiki ) ) { |
||
| 1469 | if ( $this->slavesDownMode ) { |
||
| 1470 | return 'The database has been automatically locked ' . |
||
| 1471 | 'until the replica database servers become available'; |
||
| 1472 | } else { |
||
| 1473 | return 'The database has been automatically locked ' . |
||
| 1474 | 'while the replica database servers catch up to the master.'; |
||
| 1475 | } |
||
| 1476 | } elseif ( $this->masterRunningReadOnly( $wiki, $conn ) ) { |
||
| 1477 | return 'The database master is running in read-only mode.'; |
||
| 1478 | } |
||
| 1479 | |||
| 1480 | return false; |
||
| 1481 | } |
||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * @param string $wiki Wiki ID, or false for the current wiki |
||
| 1485 | * @param DatabaseBase|null DB master connectionl used to avoid loops [optional] |
||
| 1486 | * @return bool |
||
| 1487 | */ |
||
| 1488 | private function masterRunningReadOnly( $wiki, DatabaseBase $conn = null ) { |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Disables/enables lag checks |
||
| 1512 | * @param null|bool $mode |
||
| 1513 | * @return bool |
||
| 1514 | */ |
||
| 1515 | public function allowLagged( $mode = null ) { |
||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * @return bool |
||
| 1526 | */ |
||
| 1527 | public function pingAll() { |
||
| 1537 | |||
| 1538 | /** |
||
| 1539 | * Call a function with each open connection object |
||
| 1540 | * @param callable $callback |
||
| 1541 | * @param array $params |
||
| 1542 | */ |
||
| 1543 | View Code Duplication | public function forEachOpenConnection( $callback, array $params = [] ) { |
|
| 1553 | |||
| 1554 | /** |
||
| 1555 | * Call a function with each open connection object to a master |
||
| 1556 | * @param callable $callback |
||
| 1557 | * @param array $params |
||
| 1558 | * @since 1.28 |
||
| 1559 | */ |
||
| 1560 | public function forEachOpenMasterConnection( $callback, array $params = [] ) { |
||
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Call a function with each open replica DB connection object |
||
| 1575 | * @param callable $callback |
||
| 1576 | * @param array $params |
||
| 1577 | * @since 1.28 |
||
| 1578 | */ |
||
| 1579 | View Code Duplication | public function forEachOpenReplicaConnection( $callback, array $params = [] ) { |
|
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Get the hostname and lag time of the most-lagged replica DB |
||
| 1595 | * |
||
| 1596 | * This is useful for maintenance scripts that need to throttle their updates. |
||
| 1597 | * May attempt to open connections to replica DBs on the default DB. If there is |
||
| 1598 | * no lag, the maximum lag will be reported as -1. |
||
| 1599 | * |
||
| 1600 | * @param bool|string $wiki Wiki ID, or false for the default database |
||
| 1601 | * @return array ( host, max lag, index of max lagged host ) |
||
| 1602 | */ |
||
| 1603 | public function getMaxLag( $wiki = false ) { |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * Get an estimate of replication lag (in seconds) for each server |
||
| 1626 | * |
||
| 1627 | * Results are cached for a short time in memcached/process cache |
||
| 1628 | * |
||
| 1629 | * Values may be "false" if replication is too broken to estimate |
||
| 1630 | * |
||
| 1631 | * @param string|bool $wiki |
||
| 1632 | * @return int[] Map of (server index => float|int|bool) |
||
| 1633 | */ |
||
| 1634 | public function getLagTimes( $wiki = false ) { |
||
| 1642 | |||
| 1643 | /** |
||
| 1644 | * Get the lag in seconds for a given connection, or zero if this load |
||
| 1645 | * balancer does not have replication enabled. |
||
| 1646 | * |
||
| 1647 | * This should be used in preference to Database::getLag() in cases where |
||
| 1648 | * replication may not be in use, since there is no way to determine if |
||
| 1649 | * replication is in use at the connection level without running |
||
| 1650 | * potentially restricted queries such as SHOW SLAVE STATUS. Using this |
||
| 1651 | * function instead of Database::getLag() avoids a fatal error in this |
||
| 1652 | * case on many installations. |
||
| 1653 | * |
||
| 1654 | * @param IDatabase $conn |
||
| 1655 | * @return int|bool Returns false on error |
||
| 1656 | */ |
||
| 1657 | public function safeGetLag( IDatabase $conn ) { |
||
| 1664 | |||
| 1665 | /** |
||
| 1666 | * Wait for a replica DB to reach a specified master position |
||
| 1667 | * |
||
| 1668 | * This will connect to the master to get an accurate position if $pos is not given |
||
| 1669 | * |
||
| 1670 | * @param IDatabase $conn Replica DB |
||
| 1671 | * @param DBMasterPos|bool $pos Master position; default: current position |
||
| 1672 | * @param integer $timeout Timeout in seconds |
||
| 1673 | * @return bool Success |
||
| 1674 | * @since 1.27 |
||
| 1675 | */ |
||
| 1676 | public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = 10 ) { |
||
| 1699 | |||
| 1700 | /** |
||
| 1701 | * Clear the cache for slag lag delay times |
||
| 1702 | * |
||
| 1703 | * This is only used for testing |
||
| 1704 | */ |
||
| 1705 | public function clearLagTimeCache() { |
||
| 1708 | |||
| 1709 | /** |
||
| 1710 | * Set a callback via DatabaseBase::setTransactionListener() on |
||
| 1711 | * all current and future master connections of this load balancer |
||
| 1712 | * |
||
| 1713 | * @param string $name Callback name |
||
| 1714 | * @param callable|null $callback |
||
| 1715 | * @since 1.28 |
||
| 1716 | */ |
||
| 1717 | public function setTransactionListener( $name, callable $callback = null ) { |
||
| 1729 | } |
||
| 1730 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: