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 string|bool Reason all LBs are read-only or false if not */ | ||
| 46 | protected $readOnlyReason = false; | ||
| 47 | |||
| 48 | const SHUTDOWN_NO_CHRONPROT = 1; // don't save ChronologyProtector positions (for async code) | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Construct a factory based on a configuration array (typically from $wgLBFactoryConf) | ||
| 52 | * @param array $conf | ||
| 53 | * @TODO: inject objects via dependency framework | ||
| 54 | */ | ||
| 55 | 	public function __construct( array $conf ) { | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Disables all load balancers. All connections are closed, and any attempt to | ||
| 79 | * open a new connection will result in a DBAccessError. | ||
| 80 | * @see LoadBalancer::disable() | ||
| 81 | */ | ||
| 82 | 	public function destroy() { | ||
| 86 | |||
| 87 | /** | ||
| 88 | * Disables all access to the load balancer, will cause all database access | ||
| 89 | * to throw a DBAccessError | ||
| 90 | */ | ||
| 91 | 	public static function disableBackend() { | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Get an LBFactory instance | ||
| 97 | * | ||
| 98 | * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead. | ||
| 99 | * | ||
| 100 | * @return LBFactory | ||
| 101 | */ | ||
| 102 | 	public static function singleton() { | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Returns the LBFactory class to use and the load balancer configuration. | ||
| 108 | * | ||
| 109 | * @todo instead of this, use a ServiceContainer for managing the different implementations. | ||
| 110 | * | ||
| 111 | * @param array $config (e.g. $wgLBFactoryConf) | ||
| 112 | * @return string Class name | ||
| 113 | */ | ||
| 114 | 	public static function getLBFactoryClass( array $config ) { | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Shut down, close connections and destroy the cached instance. | ||
| 139 | * | ||
| 140 | * @deprecated since 1.27, use LBFactory::destroy() | ||
| 141 | */ | ||
| 142 | 	public static function destroyInstance() { | ||
| 145 | |||
| 146 | /** | ||
| 147 | * Create a new load balancer object. The resulting object will be untracked, | ||
| 148 | * not chronology-protected, and the caller is responsible for cleaning it up. | ||
| 149 | * | ||
| 150 | * @param bool|string $wiki Wiki ID, or false for the current wiki | ||
| 151 | * @return LoadBalancer | ||
| 152 | */ | ||
| 153 | abstract public function newMainLB( $wiki = false ); | ||
| 154 | |||
| 155 | /** | ||
| 156 | * Get a cached (tracked) load balancer object. | ||
| 157 | * | ||
| 158 | * @param bool|string $wiki Wiki ID, or false for the current wiki | ||
| 159 | * @return LoadBalancer | ||
| 160 | */ | ||
| 161 | abstract public function getMainLB( $wiki = false ); | ||
| 162 | |||
| 163 | /** | ||
| 164 | * Create a new load balancer for external storage. The resulting object will be | ||
| 165 | * untracked, not chronology-protected, and the caller is responsible for | ||
| 166 | * cleaning it up. | ||
| 167 | * | ||
| 168 | * @param string $cluster External storage cluster, or false for core | ||
| 169 | * @param bool|string $wiki Wiki ID, or false for the current wiki | ||
| 170 | * @return LoadBalancer | ||
| 171 | */ | ||
| 172 | abstract protected function newExternalLB( $cluster, $wiki = false ); | ||
| 173 | |||
| 174 | /** | ||
| 175 | * Get a cached (tracked) load balancer for external storage | ||
| 176 | * | ||
| 177 | * @param string $cluster External storage cluster, or false for core | ||
| 178 | * @param bool|string $wiki Wiki ID, or false for the current wiki | ||
| 179 | * @return LoadBalancer | ||
| 180 | */ | ||
| 181 | abstract public function &getExternalLB( $cluster, $wiki = false ); | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Execute a function for each tracked load balancer | ||
| 185 | * The callback is called with the load balancer as the first parameter, | ||
| 186 | * and $params passed as the subsequent parameters. | ||
| 187 | * | ||
| 188 | * @param callable $callback | ||
| 189 | * @param array $params | ||
| 190 | */ | ||
| 191 | abstract public function forEachLB( $callback, array $params = [] ); | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Prepare all tracked load balancers for shutdown | ||
| 195 | * @param integer $flags Supports SHUTDOWN_* flags | ||
| 196 | * STUB | ||
| 197 | */ | ||
| 198 | 	public function shutdown( $flags = 0 ) { | ||
| 200 | |||
| 201 | /** | ||
| 202 | * Call a method of each tracked load balancer | ||
| 203 | * | ||
| 204 | * @param string $methodName | ||
| 205 | * @param array $args | ||
| 206 | */ | ||
| 207 | 	private function forEachLBCallMethod( $methodName, array $args = [] ) { | ||
| 215 | |||
| 216 | /** | ||
| 217 | * Commit on all connections. Done for two reasons: | ||
| 218 | * 1. To commit changes to the masters. | ||
| 219 | * 2. To release the snapshot on all connections, master and slave. | ||
| 220 | * @param string $fname Caller name | ||
| 221 | * @param array $options Options map: | ||
| 222 | * - maxWriteDuration: abort if more than this much time was spent in write queries | ||
| 223 | */ | ||
| 224 | 	public function commitAll( $fname = __METHOD__, array $options = [] ) { | ||
| 228 | |||
| 229 | /** | ||
| 230 | * Commit changes on all master connections | ||
| 231 | * @param string $fname Caller name | ||
| 232 | * @param array $options Options map: | ||
| 233 | * - maxWriteDuration: abort if more than this much time was spent in write queries | ||
| 234 | */ | ||
| 235 | 	public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) { | ||
| 249 | |||
| 250 | /** | ||
| 251 | * Rollback changes on all master connections | ||
| 252 | * @param string $fname Caller name | ||
| 253 | * @since 1.23 | ||
| 254 | */ | ||
| 255 | 	public function rollbackMasterChanges( $fname = __METHOD__ ) { | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Log query info if multi DB transactions are going to be committed now | ||
| 261 | */ | ||
| 262 | 	private function logIfMultiDbTransaction() { | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Determine if any master connection has pending changes | ||
| 284 | * @return bool | ||
| 285 | * @since 1.23 | ||
| 286 | */ | ||
| 287 | 	public function hasMasterChanges() { | ||
| 295 | |||
| 296 | /** | ||
| 297 | * Detemine if any lagged slave connection was used | ||
| 298 | * @since 1.27 | ||
| 299 | * @return bool | ||
| 300 | */ | ||
| 301 | 	public function laggedSlaveUsed() { | ||
| 309 | |||
| 310 | /** | ||
| 311 | * Determine if any master connection has pending/written changes from this request | ||
| 312 | * @return bool | ||
| 313 | * @since 1.27 | ||
| 314 | */ | ||
| 315 | 	public function hasOrMadeRecentMasterChanges() { | ||
| 322 | |||
| 323 | /** | ||
| 324 | * Waits for the slave DBs to catch up to the current master position | ||
| 325 | * | ||
| 326 | * Use this when updating very large numbers of rows, as in maintenance scripts, | ||
| 327 | * to avoid causing too much lag. Of course, this is a no-op if there are no slaves. | ||
| 328 | * | ||
| 329 | * By default this waits on all DB clusters actually used in this request. | ||
| 330 | * This makes sense when lag being waiting on is caused by the code that does this check. | ||
| 331 | * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters | ||
| 332 | * that were not changed since the last wait check. To forcefully wait on a specific cluster | ||
| 333 | * for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster, | ||
| 334 | * use the "cluster" parameter. | ||
| 335 | * | ||
| 336 | * Never call this function after a large DB write that is *still* in a transaction. | ||
| 337 | * It only makes sense to call this after the possible lag inducing changes were committed. | ||
| 338 | * | ||
| 339 | * @param array $opts Optional fields that include: | ||
| 340 | * - wiki : wait on the load balancer DBs that handles the given wiki | ||
| 341 | * - cluster : wait on the given external load balancer DBs | ||
| 342 | * - timeout : Max wait time. Default: ~60 seconds | ||
| 343 | * - ifWritesSince: Only wait if writes were done since this UNIX timestamp | ||
| 344 | * @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster | ||
| 345 | * @since 1.27 | ||
| 346 | */ | ||
| 347 | 	public function waitForReplication( array $opts = [] ) { | ||
| 406 | |||
| 407 | /** | ||
| 408 | * Disable the ChronologyProtector for all load balancers | ||
| 409 | * | ||
| 410 | * This can be called at the start of special API entry points | ||
| 411 | * | ||
| 412 | * @since 1.27 | ||
| 413 | */ | ||
| 414 | 	public function disableChronologyProtection() { | ||
| 417 | |||
| 418 | /** | ||
| 419 | * @return ChronologyProtector | ||
| 420 | */ | ||
| 421 | 	protected function newChronologyProtector() { | ||
| 440 | |||
| 441 | /** | ||
| 442 | * @param ChronologyProtector $cp | ||
| 443 | */ | ||
| 444 | 	protected function shutdownChronologyProtector( ChronologyProtector $cp ) { | ||
| 462 | |||
| 463 | /** | ||
| 464 | * Close all open database connections on all open load balancers. | ||
| 465 | * @since 1.28 | ||
| 466 | */ | ||
| 467 | 	public function closeAll() { | ||
| 470 | |||
| 471 | } | ||
| 472 | |||
| 488 |