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 = 0; // don't save DB positions at all |
||
55 | const SHUTDOWN_CHRONPROT_ASYNC = 1; // save DB positions, but don't wait on remote DCs |
||
56 | const SHUTDOWN_CHRONPROT_SYNC = 2; // save DB positions, waiting on all DCs |
||
57 | |||
58 | /** |
||
59 | * Construct a factory based on a configuration array (typically from $wgLBFactoryConf) |
||
60 | * @param array $conf |
||
61 | * @TODO: inject objects via dependency framework |
||
62 | */ |
||
63 | public function __construct( array $conf ) { |
||
85 | |||
86 | /** |
||
87 | * Disables all load balancers. All connections are closed, and any attempt to |
||
88 | * open a new connection will result in a DBAccessError. |
||
89 | * @see LoadBalancer::disable() |
||
90 | */ |
||
91 | public function destroy() { |
||
95 | |||
96 | /** |
||
97 | * Disables all access to the load balancer, will cause all database access |
||
98 | * to throw a DBAccessError |
||
99 | */ |
||
100 | public static function disableBackend() { |
||
103 | |||
104 | /** |
||
105 | * Get an LBFactory instance |
||
106 | * |
||
107 | * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead. |
||
108 | * |
||
109 | * @return LBFactory |
||
110 | */ |
||
111 | public static function singleton() { |
||
114 | |||
115 | /** |
||
116 | * Returns the LBFactory class to use and the load balancer configuration. |
||
117 | * |
||
118 | * @todo instead of this, use a ServiceContainer for managing the different implementations. |
||
119 | * |
||
120 | * @param array $config (e.g. $wgLBFactoryConf) |
||
121 | * @return string Class name |
||
122 | */ |
||
123 | public static function getLBFactoryClass( array $config ) { |
||
145 | |||
146 | /** |
||
147 | * Shut down, close connections and destroy the cached instance. |
||
148 | * |
||
149 | * @deprecated since 1.27, use LBFactory::destroy() |
||
150 | */ |
||
151 | public static function destroyInstance() { |
||
154 | |||
155 | /** |
||
156 | * Create a new load balancer object. The resulting object will be untracked, |
||
157 | * not chronology-protected, and the caller is responsible for cleaning it up. |
||
158 | * |
||
159 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
160 | * @return LoadBalancer |
||
161 | */ |
||
162 | abstract public function newMainLB( $wiki = false ); |
||
163 | |||
164 | /** |
||
165 | * Get a cached (tracked) load balancer object. |
||
166 | * |
||
167 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
168 | * @return LoadBalancer |
||
169 | */ |
||
170 | abstract public function getMainLB( $wiki = false ); |
||
171 | |||
172 | /** |
||
173 | * Create a new load balancer for external storage. The resulting object will be |
||
174 | * untracked, not chronology-protected, and the caller is responsible for |
||
175 | * cleaning it up. |
||
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 protected function newExternalLB( $cluster, $wiki = false ); |
||
182 | |||
183 | /** |
||
184 | * Get a cached (tracked) load balancer for external storage |
||
185 | * |
||
186 | * @param string $cluster External storage cluster, or false for core |
||
187 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
188 | * @return LoadBalancer |
||
189 | */ |
||
190 | abstract public function getExternalLB( $cluster, $wiki = false ); |
||
191 | |||
192 | /** |
||
193 | * Execute a function for each tracked load balancer |
||
194 | * The callback is called with the load balancer as the first parameter, |
||
195 | * and $params passed as the subsequent parameters. |
||
196 | * |
||
197 | * @param callable $callback |
||
198 | * @param array $params |
||
199 | */ |
||
200 | abstract public function forEachLB( $callback, array $params = [] ); |
||
201 | |||
202 | /** |
||
203 | * Prepare all tracked load balancers for shutdown |
||
204 | * @param integer $mode One of the class SHUTDOWN_* constants |
||
205 | * @param callable|null $workCallback Work to mask ChronologyProtector writes |
||
206 | */ |
||
207 | public function shutdown( |
||
218 | |||
219 | /** |
||
220 | * Call a method of each tracked load balancer |
||
221 | * |
||
222 | * @param string $methodName |
||
223 | * @param array $args |
||
224 | */ |
||
225 | private function forEachLBCallMethod( $methodName, array $args = [] ) { |
||
233 | |||
234 | /** |
||
235 | * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot |
||
236 | * |
||
237 | * @param string $fname Caller name |
||
238 | * @since 1.28 |
||
239 | */ |
||
240 | public function flushReplicaSnapshots( $fname = __METHOD__ ) { |
||
243 | |||
244 | /** |
||
245 | * Commit on all connections. Done for two reasons: |
||
246 | * 1. To commit changes to the masters. |
||
247 | * 2. To release the snapshot on all connections, master and replica DB. |
||
248 | * @param string $fname Caller name |
||
249 | * @param array $options Options map: |
||
250 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
251 | */ |
||
252 | public function commitAll( $fname = __METHOD__, array $options = [] ) { |
||
256 | |||
257 | /** |
||
258 | * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) |
||
259 | * |
||
260 | * The DBO_TRX setting will be reverted to the default in each of these methods: |
||
261 | * - commitMasterChanges() |
||
262 | * - rollbackMasterChanges() |
||
263 | * - commitAll() |
||
264 | * |
||
265 | * This allows for custom transaction rounds from any outer transaction scope. |
||
266 | * |
||
267 | * @param string $fname |
||
268 | * @throws DBTransactionError |
||
269 | * @since 1.28 |
||
270 | */ |
||
271 | public function beginMasterChanges( $fname = __METHOD__ ) { |
||
282 | |||
283 | /** |
||
284 | * Commit changes on all master connections |
||
285 | * @param string $fname Caller name |
||
286 | * @param array $options Options map: |
||
287 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
288 | * @throws Exception |
||
289 | */ |
||
290 | public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) { |
||
320 | |||
321 | /** |
||
322 | * Rollback changes on all master connections |
||
323 | * @param string $fname Caller name |
||
324 | * @since 1.23 |
||
325 | */ |
||
326 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
335 | |||
336 | /** |
||
337 | * Log query info if multi DB transactions are going to be committed now |
||
338 | */ |
||
339 | private function logIfMultiDbTransaction() { |
||
358 | |||
359 | /** |
||
360 | * Determine if any master connection has pending changes |
||
361 | * @return bool |
||
362 | * @since 1.23 |
||
363 | */ |
||
364 | public function hasMasterChanges() { |
||
372 | |||
373 | /** |
||
374 | * Detemine if any lagged replica DB connection was used |
||
375 | * @return bool |
||
376 | * @since 1.28 |
||
377 | */ |
||
378 | public function laggedReplicaUsed() { |
||
386 | |||
387 | /** |
||
388 | * @return bool |
||
389 | * @since 1.27 |
||
390 | * @deprecated Since 1.28; use laggedReplicaUsed() |
||
391 | */ |
||
392 | public function laggedSlaveUsed() { |
||
395 | |||
396 | /** |
||
397 | * Determine if any master connection has pending/written changes from this request |
||
398 | * @param float $age How many seconds ago is "recent" [defaults to LB lag wait timeout] |
||
399 | * @return bool |
||
400 | * @since 1.27 |
||
401 | */ |
||
402 | public function hasOrMadeRecentMasterChanges( $age = null ) { |
||
409 | |||
410 | /** |
||
411 | * Waits for the replica DBs to catch up to the current master position |
||
412 | * |
||
413 | * Use this when updating very large numbers of rows, as in maintenance scripts, |
||
414 | * to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs. |
||
415 | * |
||
416 | * By default this waits on all DB clusters actually used in this request. |
||
417 | * This makes sense when lag being waiting on is caused by the code that does this check. |
||
418 | * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters |
||
419 | * that were not changed since the last wait check. To forcefully wait on a specific cluster |
||
420 | * for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster, |
||
421 | * use the "cluster" parameter. |
||
422 | * |
||
423 | * Never call this function after a large DB write that is *still* in a transaction. |
||
424 | * It only makes sense to call this after the possible lag inducing changes were committed. |
||
425 | * |
||
426 | * @param array $opts Optional fields that include: |
||
427 | * - wiki : wait on the load balancer DBs that handles the given wiki |
||
428 | * - cluster : wait on the given external load balancer DBs |
||
429 | * - timeout : Max wait time. Default: ~60 seconds |
||
430 | * - ifWritesSince: Only wait if writes were done since this UNIX timestamp |
||
431 | * @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster |
||
432 | * @since 1.27 |
||
433 | */ |
||
434 | public function waitForReplication( array $opts = [] ) { |
||
499 | |||
500 | /** |
||
501 | * Add a callback to be run in every call to waitForReplication() before waiting |
||
502 | * |
||
503 | * Callbacks must clear any transactions that they start |
||
504 | * |
||
505 | * @param string $name Callback name |
||
506 | * @param callable|null $callback Use null to unset a callback |
||
507 | * @since 1.28 |
||
508 | */ |
||
509 | public function setWaitForReplicationListener( $name, callable $callback = null ) { |
||
516 | |||
517 | /** |
||
518 | * Get a token asserting that no transaction writes are active |
||
519 | * |
||
520 | * @param string $fname Caller name (e.g. __METHOD__) |
||
521 | * @return mixed A value to pass to commitAndWaitForReplication() |
||
522 | * @since 1.28 |
||
523 | */ |
||
524 | public function getEmptyTransactionTicket( $fname ) { |
||
532 | |||
533 | /** |
||
534 | * Convenience method for safely running commitMasterChanges()/waitForReplication() |
||
535 | * |
||
536 | * This will commit and wait unless $ticket indicates it is unsafe to do so |
||
537 | * |
||
538 | * @param string $fname Caller name (e.g. __METHOD__) |
||
539 | * @param mixed $ticket Result of getEmptyTransactionTicket() |
||
540 | * @param array $opts Options to waitForReplication() |
||
541 | * @throws DBReplicationWaitError |
||
542 | * @since 1.28 |
||
543 | */ |
||
544 | public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) { |
||
568 | |||
569 | /** |
||
570 | * @param string $dbName DB master name (e.g. "db1052") |
||
571 | * @return float|bool UNIX timestamp when client last touched the DB or false if not recent |
||
572 | * @since 1.28 |
||
573 | */ |
||
574 | public function getChronologyProtectorTouched( $dbName ) { |
||
577 | |||
578 | /** |
||
579 | * Disable the ChronologyProtector for all load balancers |
||
580 | * |
||
581 | * This can be called at the start of special API entry points |
||
582 | * |
||
583 | * @since 1.27 |
||
584 | */ |
||
585 | public function disableChronologyProtection() { |
||
588 | |||
589 | /** |
||
590 | * @return ChronologyProtector |
||
591 | */ |
||
592 | protected function newChronologyProtector() { |
||
612 | |||
613 | /** |
||
614 | * Get and record all of the staged DB positions into persistent memory storage |
||
615 | * |
||
616 | * @param ChronologyProtector $cp |
||
617 | * @param callable|null $workCallback Work to do instead of waiting on syncing positions |
||
618 | * @param string $mode One of (sync, async); whether to wait on remote datacenters |
||
619 | */ |
||
620 | protected function shutdownChronologyProtector( |
||
645 | |||
646 | /** |
||
647 | * @param LoadBalancer $lb |
||
648 | */ |
||
649 | protected function initLoadBalancer( LoadBalancer $lb ) { |
||
654 | |||
655 | /** |
||
656 | * Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed |
||
657 | * |
||
658 | * Note that unlike cookies, this works accross domains |
||
659 | * |
||
660 | * @param string $url |
||
661 | * @param float $time UNIX timestamp just before shutdown() was called |
||
662 | * @return string |
||
663 | * @since 1.28 |
||
664 | */ |
||
665 | public function appendPreShutdownTimeAsQuery( $url, $time ) { |
||
677 | |||
678 | /** |
||
679 | * Close all open database connections on all open load balancers. |
||
680 | * @since 1.28 |
||
681 | */ |
||
682 | public function closeAll() { |
||
685 | } |
||
686 |
This check looks for function calls that miss required arguments.