Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
JobTraits | |
0.00% |
0 / 48 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
getParams | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getSearchConfig | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getType | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
doJob | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
backoffDelay | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
decideClusters | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
56 | |||
run | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
buildJobDelayOptions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
buildJobName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace CirrusSearch\Job; |
4 | |
5 | use CirrusSearch\ClusterSettings; |
6 | use CirrusSearch\Connection; |
7 | use CirrusSearch\ExternalIndex; |
8 | use CirrusSearch\HashSearchConfig; |
9 | use CirrusSearch\SearchConfig; |
10 | use MediaWiki\Logger\LoggerFactory; |
11 | use MediaWiki\MainConfigNames; |
12 | |
13 | /** |
14 | * Traits for CirrusSearch Jobs. |
15 | */ |
16 | trait JobTraits { |
17 | /** |
18 | * @return array |
19 | */ |
20 | abstract public function getParams(); |
21 | |
22 | /** |
23 | * @return SearchConfig |
24 | */ |
25 | abstract public function getSearchConfig(): SearchConfig; |
26 | |
27 | /** |
28 | * @return string |
29 | */ |
30 | abstract public function getType(); |
31 | |
32 | /** |
33 | * Actually perform the labor of the job. |
34 | * The Job will be retried if true is returned from allowRetries() when |
35 | * this method fails (thrown exception or returning false from this |
36 | * method). |
37 | * @return bool true for success, false for failures |
38 | */ |
39 | abstract protected function doJob(); |
40 | |
41 | /** |
42 | * @param int $retryCount The number of times the job has errored out. |
43 | * @return int Number of seconds to delay. With the default minimum exponent |
44 | * of 6 the possible return values are 64, 128, 256, 512 and 1024 giving a |
45 | * maximum delay of 17 minutes. |
46 | */ |
47 | public function backoffDelay( $retryCount ) { |
48 | $exponent = $this->getSearchConfig()->get( 'CirrusSearchWriteBackoffExponent' ); |
49 | $minIncrease = 0; |
50 | if ( $retryCount > 1 ) { |
51 | // Delay at least 2 minutes for everything that fails more than once |
52 | $minIncrease = 1; |
53 | } |
54 | return ceil( pow( 2, $exponent + rand( $minIncrease, min( $retryCount, 4 ) ) ) ); |
55 | } |
56 | |
57 | /** |
58 | * Construct the list of connections suited for this job. |
59 | * NOTE: only suited for jobs that work on multiple clusters by |
60 | * inspecting the 'cluster' job param |
61 | * |
62 | * @param string $updateGroup UpdateGroup::* constant |
63 | * @return Connection[] indexed by cluster name |
64 | */ |
65 | protected function decideClusters( string $updateGroup ) { |
66 | $params = $this->getParams(); |
67 | $searchConfig = $this->getSearchConfig(); |
68 | $jobType = $this->getType(); |
69 | |
70 | $cluster = $params['cluster'] ?? null; |
71 | $assignment = $searchConfig->getClusterAssignment(); |
72 | if ( $cluster === null ) { |
73 | $clusterNames = $assignment->getWritableClusters( $updateGroup ); |
74 | } elseif ( $assignment->canWriteToCluster( $cluster, $updateGroup ) ) { |
75 | $clusterNames = [ $cluster ]; |
76 | } else { |
77 | // Just in case a job is present in the queue but its cluster |
78 | // has been removed from the config file. |
79 | LoggerFactory::getInstance( 'CirrusSearch' )->warning( |
80 | "Received {command} job for unwritable cluster {cluster}", |
81 | [ |
82 | 'command' => $jobType, |
83 | 'cluster' => $cluster |
84 | ] |
85 | ); |
86 | // this job does not allow retries so we just need to throw an exception |
87 | throw new \RuntimeException( "Received {$jobType} job with {$updateGroup} updates for an unwritable cluster $cluster." ); |
88 | } |
89 | |
90 | $config = $searchConfig; |
91 | if ( isset( $params['external-index'] ) ) { |
92 | $otherIndex = new ExternalIndex( $searchConfig, $params['external-index'] ); |
93 | if ( $otherIndex->getCrossClusterName() !== null ) { |
94 | // We assume that the cluster configs is mostly shared across cluster groups |
95 | // e.g. this group config is available in CirrusSearchClusters |
96 | // So that changing the CirrusSearchReplicaGroup to the CrossClusterName of the external |
97 | // index we build the correct config to write to desired replica group. |
98 | $config = new HashSearchConfig( [ 'CirrusSearchReplicaGroup' => $otherIndex->getCrossClusterName() ], |
99 | [ HashSearchConfig::FLAG_INHERIT ], $config ); |
100 | } |
101 | } |
102 | |
103 | // Limit private data writes, such as archive index, to appropriately |
104 | // flagged clusters |
105 | if ( $params['private_data'] ?? false ) { |
106 | // $clusterNames could be empty after this filter. All consumers |
107 | // must work appropriately with no connections returned, typically |
108 | // by looping over the connections and doing nothing when no |
109 | // connections are provided. |
110 | $clusterNames = array_filter( $clusterNames, static function ( $name ) use ( $config ) { |
111 | $settings = new ClusterSettings( $config, $name ); |
112 | return $settings->isPrivateCluster(); |
113 | } ); |
114 | } |
115 | |
116 | $conns = Connection::getClusterConnections( $clusterNames, $config ); |
117 | |
118 | $timeout = $config->get( 'CirrusSearchClientSideUpdateTimeout' ); |
119 | foreach ( $conns as $connection ) { |
120 | $connection->setTimeout( $timeout ); |
121 | } |
122 | |
123 | return $conns; |
124 | } |
125 | |
126 | /** |
127 | * Some boilerplate stuff for all jobs goes here |
128 | * |
129 | * @return bool |
130 | */ |
131 | public function run() { |
132 | if ( $this->getSearchConfig()->get( MainConfigNames::DisableSearchUpdate ) || |
133 | $this->getSearchConfig()->get( 'CirrusSearchDisableUpdate' ) |
134 | ) { |
135 | LoggerFactory::getInstance( 'CirrusSearch' )->debug( "Skipping job: search updates disabled by config" ); |
136 | return true; |
137 | } |
138 | |
139 | return $this->doJob(); |
140 | } |
141 | |
142 | /** |
143 | * Get options to enable delayed jobs. Note that this might not be possible the JobQueue |
144 | * implementation handling this job doesn't support it (JobQueueDB) but is possible |
145 | * for the high performance JobQueueRedis. Note also that delays are minimums - |
146 | * at least JobQueueRedis makes no effort to remove the delay as soon as possible |
147 | * after it has expired. By default it only checks every five minutes or so. |
148 | * Note yet again that if another delay has been set that is longer then this one |
149 | * then the _longer_ delay stays. |
150 | * |
151 | * @param string $jobClass name of the job class |
152 | * @param int $delay seconds to delay this job if possible |
153 | * @param \JobQueueGroup $jobQueueGroup |
154 | * @return array options to set to add to the job param |
155 | */ |
156 | public static function buildJobDelayOptions( $jobClass, $delay, \JobQueueGroup $jobQueueGroup ): array { |
157 | $jobQueue = $jobQueueGroup->get( $jobClass ); |
158 | if ( !$delay || !$jobQueue->delayedJobsEnabled() ) { |
159 | return []; |
160 | } |
161 | return [ 'jobReleaseTimestamp' => time() + $delay ]; |
162 | } |
163 | |
164 | /** |
165 | * @param string $clazz |
166 | * @return string |
167 | */ |
168 | public static function buildJobName( $clazz ) { |
169 | return 'cirrusSearch' . str_replace( 'CirrusSearch\\Job\\', '', $clazz ); |
170 | } |
171 | } |