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 | use MediaWiki\MediaWikiServices; |
13 | |
14 | /** |
15 | * Traits for CirrusSearch Jobs. |
16 | */ |
17 | trait JobTraits { |
18 | /** |
19 | * @return array |
20 | */ |
21 | abstract public function getParams(); |
22 | |
23 | /** |
24 | * @return SearchConfig |
25 | */ |
26 | abstract public function getSearchConfig(): SearchConfig; |
27 | |
28 | /** |
29 | * @return string |
30 | */ |
31 | abstract public function getType(); |
32 | |
33 | /** |
34 | * Actually perform the labor of the job. |
35 | * The Job will be retried if true is returned from allowRetries() when |
36 | * this method fails (thrown exception or returning false from this |
37 | * method). |
38 | * @return bool true for success, false for failures |
39 | */ |
40 | abstract protected function doJob(); |
41 | |
42 | /** |
43 | * @param int $retryCount The number of times the job has errored out. |
44 | * @return int Number of seconds to delay. With the default minimum exponent |
45 | * of 6 the possible return values are 64, 128, 256, 512 and 1024 giving a |
46 | * maximum delay of 17 minutes. |
47 | */ |
48 | public function backoffDelay( $retryCount ) { |
49 | $exponent = $this->getSearchConfig()->get( 'CirrusSearchWriteBackoffExponent' ); |
50 | $minIncrease = 0; |
51 | if ( $retryCount > 1 ) { |
52 | // Delay at least 2 minutes for everything that fails more than once |
53 | $minIncrease = 1; |
54 | } |
55 | return ceil( pow( 2, $exponent + rand( $minIncrease, min( $retryCount, 4 ) ) ) ); |
56 | } |
57 | |
58 | /** |
59 | * Construct the list of connections suited for this job. |
60 | * NOTE: only suited for jobs that work on multiple clusters by |
61 | * inspecting the 'cluster' job param |
62 | * |
63 | * @return Connection[] indexed by cluster name |
64 | */ |
65 | protected function decideClusters() { |
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(); |
74 | } elseif ( $assignment->canWriteToCluster( $cluster ) ) { |
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 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 | $timeout = $config->get( 'CirrusSearchClientSideUpdateTimeout' ); |
118 | foreach ( $conns as $connection ) { |
119 | $connection->setTimeout( $timeout ); |
120 | } |
121 | |
122 | return $conns; |
123 | } |
124 | |
125 | /** |
126 | * Some boilerplate stuff for all jobs goes here |
127 | * |
128 | * @return bool |
129 | */ |
130 | public function run() { |
131 | if ( $this->getSearchConfig()->get( MainConfigNames::DisableSearchUpdate ) || |
132 | $this->getSearchConfig()->get( 'CirrusSearchDisableUpdate' ) |
133 | ) { |
134 | LoggerFactory::getInstance( 'CirrusSearch' )->debug( "Skipping job: search updates disabled by config" ); |
135 | return true; |
136 | } |
137 | |
138 | return $this->doJob(); |
139 | } |
140 | |
141 | /** |
142 | * Get options to enable delayed jobs. Note that this might not be possible the JobQueue |
143 | * implementation handling this job doesn't support it (JobQueueDB) but is possible |
144 | * for the high performance JobQueueRedis. Note also that delays are minimums - |
145 | * at least JobQueueRedis makes no effort to remove the delay as soon as possible |
146 | * after it has expired. By default it only checks every five minutes or so. |
147 | * Note yet again that if another delay has been set that is longer then this one |
148 | * then the _longer_ delay stays. |
149 | * |
150 | * @param string $jobClass name of the job class |
151 | * @param int $delay seconds to delay this job if possible |
152 | * @return array options to set to add to the job param |
153 | */ |
154 | public static function buildJobDelayOptions( $jobClass, $delay ): array { |
155 | $jobQueue = MediaWikiServices::getInstance()->getJobQueueGroup()->get( $jobClass ); |
156 | if ( !$delay || !$jobQueue->delayedJobsEnabled() ) { |
157 | return []; |
158 | } |
159 | return [ 'jobReleaseTimestamp' => time() + $delay ]; |
160 | } |
161 | |
162 | /** |
163 | * @param string $clazz |
164 | * @return string |
165 | */ |
166 | public static function buildJobName( $clazz ) { |
167 | return 'cirrusSearch' . str_replace( 'CirrusSearch\\Job\\', '', $clazz ); |
168 | } |
169 | |
170 | } |