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 | |||
35 | /** @var ChronologyProtector */ |
||
36 | protected $chronProt; |
||
37 | |||
38 | /** @var TransactionProfiler */ |
||
39 | protected $trxProfiler; |
||
40 | |||
41 | /** @var LoggerInterface */ |
||
42 | protected $logger; |
||
43 | |||
44 | /** @var string|bool Reason all LBs are read-only or false if not */ |
||
45 | protected $readOnlyReason = false; |
||
46 | |||
47 | const SHUTDOWN_NO_CHRONPROT = 1; // don't save ChronologyProtector positions (for async code) |
||
48 | |||
49 | /** |
||
50 | * Construct a factory based on a configuration array (typically from $wgLBFactoryConf) |
||
51 | * @param array $conf |
||
52 | */ |
||
53 | public function __construct( array $conf ) { |
||
62 | |||
63 | /** |
||
64 | * Disables all load balancers. All connections are closed, and any attempt to |
||
65 | * open a new connection will result in a DBAccessError. |
||
66 | * @see LoadBalancer::disable() |
||
67 | */ |
||
68 | public function destroy() { |
||
72 | |||
73 | /** |
||
74 | * Disables all access to the load balancer, will cause all database access |
||
75 | * to throw a DBAccessError |
||
76 | */ |
||
77 | public static function disableBackend() { |
||
80 | |||
81 | /** |
||
82 | * Get an LBFactory instance |
||
83 | * |
||
84 | * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead. |
||
85 | * |
||
86 | * @return LBFactory |
||
87 | */ |
||
88 | public static function singleton() { |
||
91 | |||
92 | /** |
||
93 | * Returns the LBFactory class to use and the load balancer configuration. |
||
94 | * |
||
95 | * @todo instead of this, use a ServiceContainer for managing the different implementations. |
||
96 | * |
||
97 | * @param array $config (e.g. $wgLBFactoryConf) |
||
98 | * @return string Class name |
||
99 | */ |
||
100 | public static function getLBFactoryClass( array $config ) { |
||
122 | |||
123 | /** |
||
124 | * Shut down, close connections and destroy the cached instance. |
||
125 | * |
||
126 | * @deprecated since 1.27, use LBFactory::destroy() |
||
127 | */ |
||
128 | public static function destroyInstance() { |
||
131 | |||
132 | /** |
||
133 | * Create a new load balancer object. The resulting object will be untracked, |
||
134 | * not chronology-protected, and the caller is responsible for cleaning it up. |
||
135 | * |
||
136 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
137 | * @return LoadBalancer |
||
138 | */ |
||
139 | abstract public function newMainLB( $wiki = false ); |
||
140 | |||
141 | /** |
||
142 | * Get a cached (tracked) load balancer object. |
||
143 | * |
||
144 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
145 | * @return LoadBalancer |
||
146 | */ |
||
147 | abstract public function getMainLB( $wiki = false ); |
||
148 | |||
149 | /** |
||
150 | * Create a new load balancer for external storage. The resulting object will be |
||
151 | * untracked, not chronology-protected, and the caller is responsible for |
||
152 | * cleaning it up. |
||
153 | * |
||
154 | * @param string $cluster External storage cluster, or false for core |
||
155 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
156 | * @return LoadBalancer |
||
157 | */ |
||
158 | abstract protected function newExternalLB( $cluster, $wiki = false ); |
||
159 | |||
160 | /** |
||
161 | * Get a cached (tracked) load balancer for external storage |
||
162 | * |
||
163 | * @param string $cluster External storage cluster, or false for core |
||
164 | * @param bool|string $wiki Wiki ID, or false for the current wiki |
||
165 | * @return LoadBalancer |
||
166 | */ |
||
167 | abstract public function &getExternalLB( $cluster, $wiki = false ); |
||
168 | |||
169 | /** |
||
170 | * Execute a function for each tracked load balancer |
||
171 | * The callback is called with the load balancer as the first parameter, |
||
172 | * and $params passed as the subsequent parameters. |
||
173 | * |
||
174 | * @param callable $callback |
||
175 | * @param array $params |
||
176 | */ |
||
177 | abstract public function forEachLB( $callback, array $params = [] ); |
||
178 | |||
179 | /** |
||
180 | * Prepare all tracked load balancers for shutdown |
||
181 | * @param integer $flags Supports SHUTDOWN_* flags |
||
182 | * STUB |
||
183 | */ |
||
184 | public function shutdown( $flags = 0 ) { |
||
186 | |||
187 | /** |
||
188 | * Call a method of each tracked load balancer |
||
189 | * |
||
190 | * @param string $methodName |
||
191 | * @param array $args |
||
192 | */ |
||
193 | private function forEachLBCallMethod( $methodName, array $args = [] ) { |
||
201 | |||
202 | /** |
||
203 | * Commit on all connections. Done for two reasons: |
||
204 | * 1. To commit changes to the masters. |
||
205 | * 2. To release the snapshot on all connections, master and slave. |
||
206 | * @param string $fname Caller name |
||
207 | * @param array $options Options map: |
||
208 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
209 | */ |
||
210 | public function commitAll( $fname = __METHOD__, array $options = [] ) { |
||
214 | |||
215 | /** |
||
216 | * Commit changes on all master connections |
||
217 | * @param string $fname Caller name |
||
218 | * @param array $options Options map: |
||
219 | * - maxWriteDuration: abort if more than this much time was spent in write queries |
||
220 | */ |
||
221 | public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) { |
||
235 | |||
236 | /** |
||
237 | * Rollback changes on all master connections |
||
238 | * @param string $fname Caller name |
||
239 | * @since 1.23 |
||
240 | */ |
||
241 | public function rollbackMasterChanges( $fname = __METHOD__ ) { |
||
244 | |||
245 | /** |
||
246 | * Log query info if multi DB transactions are going to be committed now |
||
247 | */ |
||
248 | private function logIfMultiDbTransaction() { |
||
267 | |||
268 | /** |
||
269 | * Determine if any master connection has pending changes |
||
270 | * @return bool |
||
271 | * @since 1.23 |
||
272 | */ |
||
273 | public function hasMasterChanges() { |
||
281 | |||
282 | /** |
||
283 | * Detemine if any lagged slave connection was used |
||
284 | * @since 1.27 |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function laggedSlaveUsed() { |
||
295 | |||
296 | /** |
||
297 | * Determine if any master connection has pending/written changes from this request |
||
298 | * @return bool |
||
299 | * @since 1.27 |
||
300 | */ |
||
301 | public function hasOrMadeRecentMasterChanges() { |
||
308 | |||
309 | /** |
||
310 | * Waits for the slave DBs to catch up to the current master position |
||
311 | * |
||
312 | * Use this when updating very large numbers of rows, as in maintenance scripts, |
||
313 | * to avoid causing too much lag. Of course, this is a no-op if there are no slaves. |
||
314 | * |
||
315 | * By default this waits on all DB clusters actually used in this request. |
||
316 | * This makes sense when lag being waiting on is caused by the code that does this check. |
||
317 | * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters |
||
318 | * that were not changed since the last wait check. To forcefully wait on a specific cluster |
||
319 | * for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster, |
||
320 | * use the "cluster" parameter. |
||
321 | * |
||
322 | * Never call this function after a large DB write that is *still* in a transaction. |
||
323 | * It only makes sense to call this after the possible lag inducing changes were committed. |
||
324 | * |
||
325 | * @param array $opts Optional fields that include: |
||
326 | * - wiki : wait on the load balancer DBs that handles the given wiki |
||
327 | * - cluster : wait on the given external load balancer DBs |
||
328 | * - timeout : Max wait time. Default: ~60 seconds |
||
329 | * - ifWritesSince: Only wait if writes were done since this UNIX timestamp |
||
330 | * @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster |
||
331 | * @since 1.27 |
||
332 | */ |
||
333 | public function waitForReplication( array $opts = [] ) { |
||
392 | |||
393 | /** |
||
394 | * Disable the ChronologyProtector for all load balancers |
||
395 | * |
||
396 | * This can be called at the start of special API entry points |
||
397 | * |
||
398 | * @since 1.27 |
||
399 | */ |
||
400 | public function disableChronologyProtection() { |
||
403 | |||
404 | /** |
||
405 | * @return ChronologyProtector |
||
406 | */ |
||
407 | protected function newChronologyProtector() { |
||
426 | |||
427 | /** |
||
428 | * @param ChronologyProtector $cp |
||
429 | */ |
||
430 | protected function shutdownChronologyProtector( ChronologyProtector $cp ) { |
||
448 | |||
449 | /** |
||
450 | * Close all open database connections on all open load balancers. |
||
451 | * @since 1.28 |
||
452 | */ |
||
453 | public function closeAll() { |
||
456 | |||
457 | } |
||
458 | |||
474 |