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 | 41 | public function __construct() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritdoc |
||
| 93 | */ |
||
| 94 | 2 | public function getConnectionFactory() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | */ |
||
| 102 | 17 | public function setConnectionFactory( |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @inheritdoc |
||
| 110 | */ |
||
| 111 | 5 | public function getCredentials() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | */ |
||
| 119 | 19 | public function addServer(Credentials $credentials) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | 1 | public function getPriorityStrategy() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 7 | public function setPriorityStrategy(NodePrioritizerInterface $priorityStrategy) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 20 | public function isConnected() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | 18 | public function connect() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * @inheritdoc |
||
| 182 | */ |
||
| 183 | 7 | public function execute(CommandInterface $command) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritdoc |
||
| 196 | */ |
||
| 197 | 11 | public function getCurrentNode() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get a functional connection to any known node |
||
| 204 | * |
||
| 205 | * Disque suggests the first connection should be chosen randomly |
||
| 206 | * We go through the user-supplied credentials randomly and try to connect. |
||
| 207 | * |
||
| 208 | * @return Node A connected node |
||
| 209 | * |
||
| 210 | * @throws ConnectionException |
||
| 211 | */ |
||
| 212 | 18 | protected function findAvailableConnection() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Connect to the node given in the credentials |
||
| 233 | * |
||
| 234 | * @param Credentials $server |
||
| 235 | * |
||
| 236 | * @return Node A connected node |
||
| 237 | * |
||
| 238 | * @throws ConnectionException |
||
| 239 | * @throws AuthenticationException |
||
| 240 | */ |
||
| 241 | 16 | protected function getNodeConnection(Credentials $server) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Reset node counters that should be reset upon node switch |
||
| 250 | */ |
||
| 251 | 11 | protected function resetNodeCounters() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Hook into the command execution and do anything before it's executed |
||
| 260 | * |
||
| 261 | * Eg. start measuring node latency etc. |
||
| 262 | * |
||
| 263 | * @param CommandInterface $command |
||
| 264 | * |
||
| 265 | * @return CommandInterface $command |
||
| 266 | */ |
||
| 267 | 5 | protected function preprocessExecution(CommandInterface $command) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Postprocess the command execution, eg. update node stats |
||
| 274 | * |
||
| 275 | * @param CommandInterface $command |
||
| 276 | * @param mixed $response |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | * @throws ConnectionException |
||
| 280 | */ |
||
| 281 | 5 | protected function postprocessExecution( |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Update node counters indicating how many jobs the node has produced |
||
| 295 | * |
||
| 296 | * @param array $jobs Jobs |
||
| 297 | */ |
||
| 298 | 4 | protected function updateNodeStats(array $jobs) |
|
| 299 | { |
||
| 300 | 4 | foreach ($jobs as $job) { |
|
| 301 | 4 | $jobId = $job[JobsResponse::KEY_ID]; |
|
| 302 | 4 | $nodeId = $this->getNodeIdFromJobId($jobId); |
|
| 303 | 4 | if (!isset($nodeId) || !isset($this->nodes[$nodeId])) { |
|
| 304 | 1 | continue; |
|
| 305 | } |
||
| 306 | |||
| 307 | 4 | $node = $this->nodes[$nodeId]; |
|
| 308 | 4 | $node->addJobCount(1); |
|
| 309 | 4 | } |
|
| 310 | 4 | } |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Decide if we should switch to a better node |
||
| 314 | * |
||
| 315 | * @throws ConnectionException |
||
| 316 | */ |
||
| 317 | 8 | private function switchNodeIfNeeded() |
|
| 318 | { |
||
| 319 | 8 | $sortedNodes = $this->priorityStrategy->sort( |
|
| 320 | 8 | $this->nodes, |
|
| 321 | 8 | $this->nodeId |
|
| 322 | 8 | ); |
|
| 323 | |||
| 324 | // Try to connect by priority, continue on error, return on success |
||
| 325 | 8 | foreach($sortedNodes as $nodeCandidate) { |
|
| 326 | // If the first recommended node is our current node and it has |
||
| 327 | // a working connection, return early. |
||
| 328 | // If its connection is not working, let's try and reconnect further |
||
| 329 | // below, or find the first best connected node. |
||
| 330 | 6 | if ($nodeCandidate->getId() === $this->nodeId && |
|
| 331 | 6 | $nodeCandidate->isConnected()) { |
|
| 332 | 4 | return; |
|
| 333 | } |
||
| 334 | |||
| 335 | try { |
||
| 336 | 4 | if ($nodeCandidate->isConnected()) { |
|
| 337 | // Say a new HELLO to the node, the cluster might have changed |
||
| 338 | $nodeCandidate->sayHello(); |
||
| 339 | } else { |
||
| 340 | 4 | $nodeCandidate->connect(); |
|
| 341 | } |
||
| 342 | 4 | } catch (ConnectionException $e) { |
|
| 343 | 2 | continue; |
|
| 344 | } |
||
| 345 | |||
| 346 | 2 | $this->switchToNode($nodeCandidate); |
|
| 347 | 2 | return; |
|
| 348 | 3 | } |
|
| 349 | |||
| 350 | 3 | throw new ConnectionException('Could not switch to any node'); |
|
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Get a node ID based off a Job ID |
||
| 355 | * |
||
| 356 | * @param string $jobId Job ID |
||
| 357 | * @return string|null Node ID |
||
| 358 | */ |
||
| 359 | 4 | private function getNodeIdFromJobId($jobId) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Get the node prefix from the job ID |
||
| 374 | * |
||
| 375 | * @param string $jobId |
||
| 376 | * @return string Node prefix |
||
| 377 | */ |
||
| 378 | 4 | private function getNodePrefixFromJobId($jobId) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * We should be connected |
||
| 391 | * |
||
| 392 | * @return void |
||
| 393 | * @throws ConnectionException |
||
| 394 | */ |
||
| 395 | 7 | private function shouldBeConnected() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Create a new Node object |
||
| 409 | * |
||
| 410 | * @param Credentials $credentials |
||
| 411 | * |
||
| 412 | * @return Node An unconnected Node |
||
| 413 | */ |
||
| 414 | 16 | private function createNode(Credentials $credentials) |
|
| 415 | { |
||
| 416 | 16 | $host = $credentials->getHost(); |
|
| 417 | 16 | $port = $credentials->getPort(); |
|
| 418 | 16 | $connection = $this->connectionFactory->create($host, $port); |
|
| 419 | |||
| 420 | 16 | return new Node($credentials, $connection); |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Switch to the given node and map the cluster from its HELLO |
||
| 425 | * |
||
| 426 | * @param Node $node |
||
| 427 | */ |
||
| 428 | 11 | private function switchToNode(Node $node) |
|
| 429 | { |
||
| 430 | 11 | $nodeId = $node->getId(); |
|
| 431 | // Return early if we're trying to switch to the current node. |
||
| 432 | 11 | if (($this->nodeId === $nodeId)) { |
|
| 433 | // But return early only if the current node is connected to Disque. |
||
| 434 | // If it is disconnected, we want to overwrite it with the node |
||
| 435 | // from the method argument, because that one is connected. |
||
| 436 | 1 | if ($this->getCurrentNode()->isConnected()) { |
|
| 437 | return; |
||
| 438 | } |
||
| 439 | |||
| 440 | // Copy the stats from the now-disconnected node object |
||
| 441 | 1 | $this->copyNodeStats($this->getCurrentNode(), $node); |
|
| 442 | 1 | } |
|
| 443 | |||
| 444 | 11 | $this->resetNodeCounters(); |
|
| 445 | |||
| 446 | 11 | $this->nodeId = $nodeId; |
|
| 447 | 11 | $this->nodes[$nodeId] = $node; |
|
| 448 | 11 | $this->revealClusterFromHello($node); |
|
| 449 | 11 | } |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Reveal the whole Disque cluster from a node HELLO response |
||
| 453 | * |
||
| 454 | * The HELLO response from a Disque node contains addresses of all other |
||
| 455 | * nodes in the cluster. We want to learn about them and save them, so that |
||
| 456 | * we can switch to them later, if needed. |
||
| 457 | * |
||
| 458 | * @param Node $node The current node |
||
| 459 | */ |
||
| 460 | 11 | private function revealClusterFromHello(Node $node) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Reveal a single node from a HELLO response, or use an existing node |
||
| 481 | * |
||
| 482 | * @param string $nodeId The node ID |
||
| 483 | * @param array $node Node information as returned by the HELLO command |
||
| 484 | * |
||
| 485 | * @return Node $node A node in the current cluster |
||
| 486 | */ |
||
| 487 | 11 | private function revealNodeFromHello($nodeId, array $node) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Check if the manager held a connection to Disque already |
||
| 522 | * |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | 18 | private function wasAlreadyConnected() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Copy node stats from the old to the new node |
||
| 532 | * |
||
| 533 | * @param Node $oldNode |
||
| 534 | * @param Node $newNode |
||
| 535 | */ |
||
| 536 | 1 | private function copyNodeStats(Node $oldNode, Node $newNode) |
|
| 541 | } |
||
| 542 |