Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MultiClusterRemediatorHelper | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
sendBatch | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Sanity; |
4 | |
5 | use InvalidArgumentException; |
6 | |
7 | /** |
8 | * Helper to maintain the association with Remediators and theirs corresponding |
9 | * BufferedRemediator. |
10 | * When calling sendBatch this class evaluates if all action scheduled to the |
11 | * BufferedRemediator are the same and send them to the all cluster remediator. |
12 | * The aim is to minimize the number of generated LinksUpdate jobs in a setup |
13 | * where there are many clusters. If the same elements are to be verified on |
14 | * all the clusters then it is equivalent and more efficient to send these |
15 | * elements to all clusters using the [ "cluster" => null ] job param as some |
16 | * steps (link counting) will be shared accross all the clusters as opposed |
17 | * to sending a LinksUpdate job per cluster. |
18 | */ |
19 | class MultiClusterRemediatorHelper { |
20 | /** @var Remediator[] */ |
21 | private $perClusterRemediators; |
22 | |
23 | /** @var AllClustersQueueingRemediator */ |
24 | private $allClusterRemediator; |
25 | |
26 | /** @var BufferedRemediator[] */ |
27 | private $perClusterBufferedRemediator; |
28 | |
29 | /** |
30 | * @param Remediator[] $perClusterRemediators |
31 | * @param BufferedRemediator[] $perClusterBufferedRemediator |
32 | * @param AllClustersQueueingRemediator $allClusterRemediator |
33 | * @see \CirrusSearch\Job\CirrusGenericJob::decideClusters() |
34 | */ |
35 | public function __construct( |
36 | array $perClusterRemediators, |
37 | array $perClusterBufferedRemediator, |
38 | AllClustersQueueingRemediator $allClusterRemediator |
39 | ) { |
40 | if ( array_keys( $perClusterRemediators ) != array_keys( $perClusterBufferedRemediator ) ) { |
41 | throw new InvalidArgumentException( '$perClusterRemediators and $perClusterBufferedRemediator must have the same keys' ); |
42 | } |
43 | $this->perClusterRemediators = $perClusterRemediators; |
44 | $this->perClusterBufferedRemediator = $perClusterBufferedRemediator; |
45 | $this->allClusterRemediator = $allClusterRemediator; |
46 | } |
47 | |
48 | /** |
49 | * Evaluate if all BufferedRemediator contain the same elements and replay |
50 | * the recorded actions to the all cluster remediator or to their corresponding |
51 | * remediators otherwize. |
52 | */ |
53 | public function sendBatch() { |
54 | $allSame = true; |
55 | $firstRemediator = null; |
56 | foreach ( $this->perClusterBufferedRemediator as $remediator ) { |
57 | if ( $firstRemediator === null ) { |
58 | $firstRemediator = $remediator; |
59 | } elseif ( !$remediator->hasSameActions( $firstRemediator ) ) { |
60 | $allSame = false; |
61 | break; |
62 | } |
63 | } |
64 | if ( $allSame && $this->allClusterRemediator->canSendOptimizedJob( array_keys( $this->perClusterRemediators ) ) ) { |
65 | $firstRemediator->replayOn( $this->allClusterRemediator ); |
66 | } else { |
67 | foreach ( $this->perClusterBufferedRemediator as $cluster => $remediator ) { |
68 | $remediator->replayOn( $this->perClusterRemediators[$cluster] ); |
69 | } |
70 | } |
71 | foreach ( $this->perClusterBufferedRemediator as $remediator ) { |
72 | $remediator->resetActions(); |
73 | } |
74 | } |
75 | } |