Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Manager implements ManagerInterface |
||
26 | { |
||
27 | /** |
||
28 | * Servers we can connect to initially, without knowing the cluster |
||
29 | * |
||
30 | * After connecting to one, the server returns a list of other nodes |
||
31 | * in the cluster so we can connect to them automatically, unless |
||
32 | * the discovered nodes are secured with a password. |
||
33 | * |
||
34 | * 'serverAddress' => Credentials |
||
35 | * |
||
36 | * @var Credentials[] |
||
37 | */ |
||
38 | protected $credentials = []; |
||
39 | |||
40 | /** |
||
41 | * A strategy to prioritize nodes and find the best one to switch to |
||
42 | * |
||
43 | * The default strategy is the ConservativeJobCountPrioritizer. It |
||
44 | * prioritizes nodes by their job count, but prefers the current node |
||
45 | * in order to avoid switching until there is a clearly better node. |
||
46 | * |
||
47 | * @var NodePrioritizerInterface |
||
48 | */ |
||
49 | protected $priorityStrategy; |
||
50 | |||
51 | /** |
||
52 | * List of nodes, ie Disque instances available in the cluster |
||
53 | * |
||
54 | * 'nodeId' => Node |
||
55 | * |
||
56 | * @var Node[] |
||
57 | */ |
||
58 | protected $nodes = []; |
||
59 | |||
60 | /** |
||
61 | * Node prefixes and their corresponding node ID |
||
62 | * |
||
63 | * Node prefix consists of the first 8 bytes from the node ID. Because job |
||
64 | * IDs contain the node prefix, it can be used to identify on which node |
||
65 | * a job lives. |
||
66 | * |
||
67 | * 'nodePrefix' => 'nodeId' |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $nodePrefixes = []; |
||
72 | |||
73 | /** |
||
74 | * The ID of the node we are currently connected to |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $nodeId; |
||
79 | |||
80 | /** |
||
81 | * @var ConnectionFactoryInterface |
||
82 | */ |
||
83 | private $connectionFactory; |
||
84 | |||
85 | 39 | public function __construct() |
|
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 2 | public function getConnectionFactory() |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 15 | public function setConnectionFactory( |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 5 | public function getCredentials() |
|
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | 17 | public function addServer(Credentials $credentials) |
|
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | 1 | public function getPriorityStrategy() |
|
132 | |||
133 | /** |
||
134 | * @inheritdoc |
||
135 | */ |
||
136 | 5 | public function setPriorityStrategy(NodePrioritizerInterface $priorityStrategy) |
|
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | 7 | public function isConnected() |
|
145 | { |
||
146 | return ( |
||
147 | 7 | isset($this->nodeId) && |
|
148 | 5 | $this->nodes[$this->nodeId]->getConnection()->isConnected() |
|
149 | 7 | ); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 16 | public function connect() |
|
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | 7 | public function execute(CommandInterface $command) |
|
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 4 | public function getCurrentNode() |
|
184 | |||
185 | /** |
||
186 | * Get a functional connection to any known node |
||
187 | * |
||
188 | * Disque suggests the first connection should be chosen randomly |
||
189 | * We go through the user-supplied credentials randomly and try to connect. |
||
190 | * |
||
191 | * @return Node A connected node |
||
192 | * |
||
193 | * @throws ConnectionException |
||
194 | */ |
||
195 | 16 | protected function findAvailableConnection() |
|
196 | { |
||
197 | 16 | $servers = $this->credentials; |
|
198 | 16 | shuffle($servers); |
|
199 | 16 | foreach ($servers as $server) { |
|
200 | try { |
||
201 | 14 | $node = $this->getNodeConnection($server); |
|
202 | 14 | } catch (ConnectionException $e) { |
|
203 | 6 | continue; |
|
204 | } |
||
205 | |||
206 | 9 | if ($node->getConnection()->isConnected()) { |
|
207 | 9 | return $node; |
|
208 | } |
||
209 | 7 | } |
|
210 | |||
211 | 7 | throw new ConnectionException('No servers available'); |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * Connect to the node given in the credentials |
||
216 | * |
||
217 | * @param Credentials $server |
||
218 | * |
||
219 | * @return Node A connected node |
||
220 | * |
||
221 | * @throws ConnectionException |
||
222 | * @throws AuthenticationException |
||
223 | */ |
||
224 | 14 | protected function getNodeConnection(Credentials $server) |
|
230 | |||
231 | /** |
||
232 | * Reset node counters that should be reset upon node switch |
||
233 | */ |
||
234 | 9 | protected function resetNodeCounters() |
|
235 | { |
||
236 | 9 | foreach($this->nodes as $node) { |
|
237 | 1 | $node->resetJobCount(); |
|
238 | 9 | } |
|
239 | 9 | } |
|
240 | |||
241 | /** |
||
242 | * Hook into the command execution and do anything before it's executed |
||
243 | * |
||
244 | * Eg. start measuring node latency etc. |
||
245 | * |
||
246 | * @param CommandInterface $command |
||
247 | * |
||
248 | * @return CommandInterface $command |
||
249 | */ |
||
250 | 5 | protected function preprocessExecution(CommandInterface $command) |
|
254 | |||
255 | /** |
||
256 | * Postprocess the command execution, eg. update node stats |
||
257 | * |
||
258 | * @param CommandInterface $command |
||
259 | * @param mixed $response |
||
260 | * |
||
261 | * @return mixed |
||
262 | * @throws ConnectionException |
||
263 | */ |
||
264 | 5 | protected function postprocessExecution( |
|
265 | CommandInterface $command, |
||
266 | $response |
||
267 | ) { |
||
268 | 5 | if ($command instanceof GetJob) { |
|
269 | 4 | $this->updateNodeStats($command->parse($response)); |
|
270 | 4 | $this->switchNodeIfNeeded(); |
|
271 | 4 | } |
|
272 | |||
273 | 5 | return $response; |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * Update node counters indicating how many jobs the node has produced |
||
278 | * |
||
279 | * @param array $jobs Jobs |
||
280 | */ |
||
281 | 4 | protected function updateNodeStats(array $jobs) |
|
282 | { |
||
283 | 4 | foreach ($jobs as $job) { |
|
284 | 4 | $jobId = $job[JobsResponse::KEY_ID]; |
|
285 | 4 | $nodeId = $this->getNodeIdFromJobId($jobId); |
|
286 | 4 | if (!isset($nodeId) || !isset($this->nodes[$nodeId])) { |
|
287 | 1 | continue; |
|
288 | } |
||
289 | |||
290 | 4 | $node = $this->nodes[$nodeId]; |
|
291 | 4 | $node->addJobCount(1); |
|
292 | 4 | } |
|
293 | 4 | } |
|
294 | |||
295 | /** |
||
296 | * Decide if we should switch to a better node |
||
297 | * |
||
298 | * @throws ConnectionException |
||
299 | */ |
||
300 | 4 | private function switchNodeIfNeeded() |
|
301 | { |
||
302 | 4 | $sortedNodes = $this->priorityStrategy->sort( |
|
303 | 4 | $this->nodes, |
|
304 | 4 | $this->nodeId |
|
305 | 4 | ); |
|
306 | |||
307 | // Try to connect by priority, continue on error, return on success |
||
308 | 4 | foreach($sortedNodes as $nodeCandidate) { |
|
309 | 4 | if ($nodeCandidate->getId() === $this->nodeId) { |
|
310 | 4 | return; |
|
311 | } |
||
312 | |||
313 | try { |
||
314 | 2 | if ($nodeCandidate->getConnection()->isConnected()) { |
|
315 | // Say a new HELLO to the node, the cluster might have changed |
||
316 | $nodeCandidate->sayHello(); |
||
317 | } else { |
||
318 | 2 | $nodeCandidate->connect(); |
|
319 | } |
||
320 | 2 | } catch (ConnectionException $e) { |
|
321 | 1 | continue; |
|
322 | } |
||
323 | |||
324 | 1 | $this->switchToNode($nodeCandidate); |
|
325 | 1 | return; |
|
326 | } |
||
327 | |||
328 | throw new ConnectionException('Could not switch to any node'); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get a node ID based off a Job ID |
||
333 | * |
||
334 | * @param string $jobId Job ID |
||
335 | * @return string|null Node ID |
||
336 | */ |
||
337 | 4 | private function getNodeIdFromJobId($jobId) |
|
338 | { |
||
339 | 4 | $nodePrefix = $this->getNodePrefixFromJobId($jobId); |
|
340 | if ( |
||
341 | 4 | isset($this->nodePrefixes[$nodePrefix]) && |
|
342 | 4 | array_key_exists($this->nodePrefixes[$nodePrefix], $this->nodes) |
|
343 | 4 | ) { |
|
344 | 4 | return $this->nodePrefixes[$nodePrefix]; |
|
345 | } |
||
346 | |||
347 | 1 | return null; |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * Get the node prefix from the job ID |
||
352 | * |
||
353 | * @param string $jobId |
||
354 | * @return string Node prefix |
||
355 | */ |
||
356 | 4 | private function getNodePrefixFromJobId($jobId) |
|
357 | { |
||
358 | 4 | $nodePrefix = substr( |
|
359 | 4 | $jobId, |
|
360 | 4 | JobsResponse::ID_NODE_PREFIX_START, |
|
361 | Node::PREFIX_LENGTH |
||
362 | 4 | ); |
|
363 | |||
364 | 4 | return $nodePrefix; |
|
365 | } |
||
366 | |||
367 | /** |
||
368 | * We should be connected |
||
369 | * |
||
370 | * @return void |
||
371 | * @throws ConnectionException |
||
372 | */ |
||
373 | 7 | private function shouldBeConnected() |
|
379 | |||
380 | /** |
||
381 | * Create a new Node object |
||
382 | * |
||
383 | * @param Credentials $credentials |
||
384 | * |
||
385 | * @return Node An unconnected Node |
||
386 | */ |
||
387 | 14 | private function createNode(Credentials $credentials) |
|
395 | |||
396 | /** |
||
397 | * Switch to the given node and map the cluster from its HELLO |
||
398 | * |
||
399 | * @param Node $node |
||
400 | */ |
||
401 | 9 | private function switchToNode(Node $node) |
|
414 | |||
415 | /** |
||
416 | * Reveal the whole Disque cluster from a node HELLO response |
||
417 | * |
||
418 | * The HELLO response from a Disque node contains addresses of all other |
||
419 | * nodes in the cluster. We want to learn about them and save them, so that |
||
420 | * we can switch to them later, if needed. |
||
421 | * |
||
422 | * @param Node $node The current node |
||
423 | */ |
||
424 | 9 | private function revealClusterFromHello(Node $node) |
|
442 | |||
443 | /** |
||
444 | * Reveal a single node from a HELLO response, or use an existing node |
||
445 | * |
||
446 | * @param string $nodeId The node ID |
||
447 | * @param array $node Node information as returned by the HELLO command |
||
448 | * |
||
449 | * @return Node $node A node in the current cluster |
||
450 | */ |
||
451 | 9 | private function revealNodeFromHello($nodeId, array $node) |
|
483 | } |