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 LBFactory 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 LBFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | abstract class LBFactory implements DestructibleService { |
||
| 34 | /** @var ChronologyProtector */ |
||
| 35 | protected $chronProt; |
||
| 36 | /** @var TransactionProfiler */ |
||
| 37 | protected $trxProfiler; |
||
| 38 | /** @var LoggerInterface */ |
||
| 39 | protected $trxLogger; |
||
| 40 | /** @var BagOStuff */ |
||
| 41 | protected $srvCache; |
||
| 42 | /** @var WANObjectCache */ |
||
| 43 | protected $wanCache; |
||
| 44 | |||
| 45 | /** @var mixed */ |
||
| 46 | protected $ticket; |
||
| 47 | /** @var string|bool String if a requested DBO_TRX transaction round is active */ |
||
| 48 | protected $trxRoundId = false; |
||
| 49 | /** @var string|bool Reason all LBs are read-only or false if not */ |
||
| 50 | protected $readOnlyReason = false; |
||
| 51 | /** @var callable[] */ |
||
| 52 | protected $replicationWaitCallbacks = []; |
||
| 53 | |||
| 54 | const SHUTDOWN_NO_CHRONPROT = 1; // don't save ChronologyProtector positions (for async code) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Construct a factory based on a configuration array (typically from $wgLBFactoryConf) |
||
| 58 | * @param array $conf |
||
| 59 | * @TODO: inject objects via dependency framework |
||
| 60 | */ |
||
| 61 | public function __construct( array $conf ) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Disables all load balancers. All connections are closed, and any attempt to |
||
| 86 | * open a new connection will result in a DBAccessError. |
||
| 87 | * @see LoadBalancer::disable() |
||
| 88 | */ |
||
| 89 | public function destroy() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Disables all access to the load balancer, will cause all database access |
||
| 96 | * to throw a DBAccessError |
||
| 97 | */ |
||
| 98 | public static function disableBackend() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get an LBFactory instance |
||
| 104 | * |
||
| 105 | * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead. |
||
| 106 | * |
||
| 107 | * @return LBFactory |
||
| 108 | */ |
||
| 109 | public static function singleton() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the LBFactory class to use and the load balancer configuration. |
||
| 115 | * |
||
| 116 | * @todo instead of this, use a ServiceContainer for managing the different implementations. |
||
| 117 | * |
||
| 118 | * @param array $config (e.g. $wgLBFactoryConf) |
||
| 119 | * @return string Class name |
||
| 120 | */ |
||
| 121 | public static function getLBFactoryClass( array $config ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Shut down, close connections and destroy the cached instance. |
||
| 146 | * |
||
| 147 | * @deprecated since 1.27, use LBFactory::destroy() |
||
| 148 | */ |
||
| 149 | public static function destroyInstance() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Create a new load balancer object. The resulting object will be untracked, |
||
| 155 | * not chronology-protected, and the caller is responsible for cleaning it up. |
||
| 156 | * |
||
| 157 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
| 158 | * @return LoadBalancer |
||
| 159 | */ |
||
| 160 | abstract public function newMainLB( $wiki = false ); |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get a cached (tracked) load balancer object. |
||
| 164 | * |
||
| 165 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
| 166 | * @return LoadBalancer |
||
| 167 | */ |
||
| 168 | abstract public function getMainLB( $wiki = false ); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Create a new load balancer for external storage. The resulting object will be |
||
| 172 | * untracked, not chronology-protected, and the caller is responsible for |
||
| 173 | * cleaning it up. |
||
| 174 | * |
||
| 175 | * @param string $cluster External storage cluster, or false for core |
||
| 176 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
| 177 | * @return LoadBalancer |
||
| 178 | */ |
||
| 179 | abstract protected function newExternalLB( $cluster, $wiki = false ); |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get a cached (tracked) load balancer for external storage |
||
| 183 | * |
||
| 184 | * @param string $cluster External storage cluster, or false for core |
||
| 185 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
| 186 | * @return LoadBalancer |
||
| 187 | */ |
||
| 188 | abstract public function getExternalLB( $cluster, $wiki = false ); |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Execute a function for each tracked load balancer |
||
| 192 | * The callback is called with the load balancer as the first parameter, |
||
| 193 | * and $params passed as the subsequent parameters. |
||
| 194 | * |
||
| 195 | * @param callable $callback |
||
| 196 | * @param array $params |
||
| 197 | */ |
||
| 198 | abstract public function forEachLB( $callback, array $params = [] ); |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Prepare all tracked load balancers for shutdown |
||
| 202 | * @param integer $flags Supports SHUTDOWN_* flags |
||
| 203 | */ |
||
| 204 | public function shutdown( $flags = 0 ) { |
||
| 205 | if ( !( $flags & self::SHUTDOWN_NO_CHRONPROT ) ) { |
||
| 206 | $this->shutdownChronologyProtector( $this->chronProt ); |
||
| 207 | } |
||
| 208 | $this->commitMasterChanges( __METHOD__ ); // sanity |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Call a method of each tracked load balancer |
||
| 213 | * |
||
| 214 | * @param string $methodName |
||
| 215 | * @param array $args |
||
| 216 | */ |
||
| 217 | private function forEachLBCallMethod( $methodName, array $args = [] ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
||
| 228 | * |
||
| 229 | * The DBO_TRX setting will be reverted to the default in each of these methods: |
||
| 230 | * - commitMasterChanges() |
||
| 231 | * - rollbackMasterChanges() |
||
| 232 | * - commitAll() |
||
| 233 | * |
||
| 234 | * This allows for custom transaction rounds from any outer transaction scope. |
||
| 235 | * |
||
| 236 | * @param string $fname |
||
| 237 | * @throws DBTransactionError |
||
| 238 | * @since 1.28 |
||
| 239 | */ |
||
| 240 | public function beginMasterChanges( $fname = __METHOD__ ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot |
||
| 254 | * |
||
| 255 | * @param string $fname Caller name |
||
| 256 | * @since 1.28 |
||
| 257 | */ |
||
| 258 | public function flushReplicaSnapshots( $fname = __METHOD__ ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Commit on all connections. Done for two reasons: |
||
| 264 | * 1. To commit changes to the masters. |
||
| 265 | * 2. To release the snapshot on all connections, master and replica DB. |
||
| 266 | * @param string $fname Caller name |
||
| 267 | * @param array $options Options map: |
||
| 268 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
| 269 | */ |
||
| 270 | public function commitAll( $fname = __METHOD__, array $options = [] ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Commit changes on all master connections |
||
| 277 | * @param string $fname Caller name |
||
| 278 | * @param array $options Options map: |
||
| 279 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
| 280 | * @throws Exception |
||
| 281 | */ |
||
| 282 | public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Rollback changes on all master connections |
||
| 315 | * @param string $fname Caller name |
||
| 316 | * @since 1.23 |
||
| 317 | */ |
||
| 318 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Log query info if multi DB transactions are going to be committed now |
||
| 330 | */ |
||
| 331 | private function logIfMultiDbTransaction() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Determine if any master connection has pending changes |
||
| 353 | * @return bool |
||
| 354 | * @since 1.23 |
||
| 355 | */ |
||
| 356 | public function hasMasterChanges() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Detemine if any lagged replica DB connection was used |
||
| 367 | * @return bool |
||
| 368 | * @since 1.28 |
||
| 369 | */ |
||
| 370 | public function laggedReplicaUsed() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return bool |
||
| 381 | * @since 1.27 |
||
| 382 | * @deprecated Since 1.28; use laggedReplicaUsed() |
||
| 383 | */ |
||
| 384 | public function laggedSlaveUsed() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Determine if any master connection has pending/written changes from this request |
||
| 390 | * @return bool |
||
| 391 | * @since 1.27 |
||
| 392 | */ |
||
| 393 | public function hasOrMadeRecentMasterChanges() { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Waits for the replica DBs to catch up to the current master position |
||
| 403 | * |
||
| 404 | * Use this when updating very large numbers of rows, as in maintenance scripts, |
||
| 405 | * to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs. |
||
| 406 | * |
||
| 407 | * By default this waits on all DB clusters actually used in this request. |
||
| 408 | * This makes sense when lag being waiting on is caused by the code that does this check. |
||
| 409 | * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters |
||
| 410 | * that were not changed since the last wait check. To forcefully wait on a specific cluster |
||
| 411 | * for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster, |
||
| 412 | * use the "cluster" parameter. |
||
| 413 | * |
||
| 414 | * Never call this function after a large DB write that is *still* in a transaction. |
||
| 415 | * It only makes sense to call this after the possible lag inducing changes were committed. |
||
| 416 | * |
||
| 417 | * @param array $opts Optional fields that include: |
||
| 418 | * - wiki : wait on the load balancer DBs that handles the given wiki |
||
| 419 | * - cluster : wait on the given external load balancer DBs |
||
| 420 | * - timeout : Max wait time. Default: ~60 seconds |
||
| 421 | * - ifWritesSince: Only wait if writes were done since this UNIX timestamp |
||
| 422 | * @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster |
||
| 423 | * @since 1.27 |
||
| 424 | */ |
||
| 425 | public function waitForReplication( array $opts = [] ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Add a callback to be run in every call to waitForReplication() before waiting |
||
| 491 | * |
||
| 492 | * Callbacks must clear any transactions that they start |
||
| 493 | * |
||
| 494 | * @param string $name Callback name |
||
| 495 | * @param callable|null $callback Use null to unset a callback |
||
| 496 | * @since 1.28 |
||
| 497 | */ |
||
| 498 | public function setWaitForReplicationListener( $name, callable $callback = null ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get a token asserting that no transaction writes are active |
||
| 508 | * |
||
| 509 | * @param string $fname Caller name (e.g. __METHOD__) |
||
| 510 | * @return mixed A value to pass to commitAndWaitForReplication() |
||
| 511 | * @since 1.28 |
||
| 512 | */ |
||
| 513 | public function getEmptyTransactionTicket( $fname ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Convenience method for safely running commitMasterChanges()/waitForReplication() |
||
| 524 | * |
||
| 525 | * This will commit and wait unless $ticket indicates it is unsafe to do so |
||
| 526 | * |
||
| 527 | * @param string $fname Caller name (e.g. __METHOD__) |
||
| 528 | * @param mixed $ticket Result of getEmptyTransactionTicket() |
||
| 529 | * @param array $opts Options to waitForReplication() |
||
| 530 | * @throws DBReplicationWaitError |
||
| 531 | * @since 1.28 |
||
| 532 | */ |
||
| 533 | public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Disable the ChronologyProtector for all load balancers |
||
| 560 | * |
||
| 561 | * This can be called at the start of special API entry points |
||
| 562 | * |
||
| 563 | * @since 1.27 |
||
| 564 | */ |
||
| 565 | public function disableChronologyProtection() { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @return ChronologyProtector |
||
| 571 | */ |
||
| 572 | protected function newChronologyProtector() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param ChronologyProtector $cp |
||
| 594 | */ |
||
| 595 | protected function shutdownChronologyProtector( ChronologyProtector $cp ) { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * @param LoadBalancer $lb |
||
| 616 | */ |
||
| 617 | protected function initLoadBalancer( LoadBalancer $lb ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Close all open database connections on all open load balancers. |
||
| 625 | * @since 1.28 |
||
| 626 | */ |
||
| 627 | public function closeAll() { |
||
| 630 | |||
| 631 | } |
||
| 632 |
This check looks for function calls that miss required arguments.