Fix duplicate removal. Was completely broken, with a "job_id IS NULL" condition in...
[lhc/web/wiklou.git] / includes / job / JobQueue.php
1 <?php
2 /**
3 * Job queue base code
4 *
5 * @file
6 * @defgroup JobQueue JobQueue
7 */
8
9 if ( !defined( 'MEDIAWIKI' ) ) {
10 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
11 }
12
13 /**
14 * Class to both describe a background job and handle jobs.
15 *
16 * @ingroup JobQueue
17 */
18 abstract class Job {
19
20 /**
21 * @var Title
22 */
23 var $title;
24
25 var $command,
26 $params,
27 $id,
28 $removeDuplicates,
29 $error;
30
31 /*-------------------------------------------------------------------------
32 * Abstract functions
33 *------------------------------------------------------------------------*/
34
35 /**
36 * Run the job
37 * @return boolean success
38 */
39 abstract function run();
40
41 /*-------------------------------------------------------------------------
42 * Static functions
43 *------------------------------------------------------------------------*/
44
45 /**
46 * Pop a job of a certain type. This tries less hard than pop() to
47 * actually find a job; it may be adversely affected by concurrent job
48 * runners.
49 */
50 static function pop_type( $type ) {
51 wfProfilein( __METHOD__ );
52
53 $dbw = wfGetDB( DB_MASTER );
54
55 $row = $dbw->selectRow(
56 'job',
57 '*',
58 array( 'job_cmd' => $type ),
59 __METHOD__,
60 array( 'LIMIT' => 1 )
61 );
62
63 if ( $row === false ) {
64 wfProfileOut( __METHOD__ );
65 return false;
66 }
67
68 /* Ensure we "own" this row */
69 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
70 $affected = $dbw->affectedRows();
71 $dbw->commit();
72
73 if ( $affected == 0 ) {
74 wfProfileOut( __METHOD__ );
75 return false;
76 }
77
78 wfIncrStats( 'job-pop' );
79 $namespace = $row->job_namespace;
80 $dbkey = $row->job_title;
81 $title = Title::makeTitleSafe( $namespace, $dbkey );
82 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ),
83 $row->job_id );
84
85 $job->removeDuplicates();
86
87 wfProfileOut( __METHOD__ );
88 return $job;
89 }
90
91 /**
92 * Pop a job off the front of the queue
93 *
94 * @param $offset Integer: Number of jobs to skip
95 * @return Job or false if there's no jobs
96 */
97 static function pop( $offset = 0 ) {
98 wfProfileIn( __METHOD__ );
99
100 $dbr = wfGetDB( DB_SLAVE );
101
102 /* Get a job from the slave, start with an offset,
103 scan full set afterwards, avoid hitting purged rows
104
105 NB: If random fetch previously was used, offset
106 will always be ahead of few entries
107 */
108
109 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
110 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
111
112 // Refetching without offset is needed as some of job IDs could have had delayed commits
113 // and have lower IDs than jobs already executed, blame concurrency :)
114 //
115 if ( $row === false ) {
116 if ( $offset != 0 ) {
117 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
118 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
119 }
120
121 if ( $row === false ) {
122 wfProfileOut( __METHOD__ );
123 return false;
124 }
125 }
126
127 // Try to delete it from the master
128 $dbw = wfGetDB( DB_MASTER );
129 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
130 $affected = $dbw->affectedRows();
131 $dbw->commit();
132
133 if ( !$affected ) {
134 // Failed, someone else beat us to it
135 // Try getting a random row
136 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
137 'MAX(job_id) as maxjob' ), '1=1', __METHOD__ );
138 if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
139 // No jobs to get
140 wfProfileOut( __METHOD__ );
141 return false;
142 }
143 // Get the random row
144 $row = $dbw->selectRow( 'job', '*',
145 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
146 if ( $row === false ) {
147 // Random job gone before we got the chance to select it
148 // Give up
149 wfProfileOut( __METHOD__ );
150 return false;
151 }
152 // Delete the random row
153 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
154 $affected = $dbw->affectedRows();
155 $dbw->commit();
156
157 if ( !$affected ) {
158 // Random job gone before we exclusively deleted it
159 // Give up
160 wfProfileOut( __METHOD__ );
161 return false;
162 }
163 }
164
165 // If execution got to here, there's a row in $row that has been deleted from the database
166 // by this thread. Hence the concurrent pop was successful.
167 wfIncrStats( 'job-pop' );
168 $namespace = $row->job_namespace;
169 $dbkey = $row->job_title;
170 $title = Title::makeTitleSafe( $namespace, $dbkey );
171 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
172
173 // Remove any duplicates it may have later in the queue
174 $job->removeDuplicates();
175
176 wfProfileOut( __METHOD__ );
177 return $job;
178 }
179
180 /**
181 * Create the appropriate object to handle a specific job
182 *
183 * @param $command String: Job command
184 * @param $title Title: Associated title
185 * @param $params Array: Job parameters
186 * @param $id Int: Job identifier
187 * @return Job
188 */
189 static function factory( $command, $title, $params = false, $id = 0 ) {
190 global $wgJobClasses;
191 if( isset( $wgJobClasses[$command] ) ) {
192 $class = $wgJobClasses[$command];
193 return new $class( $title, $params, $id );
194 }
195 throw new MWException( "Invalid job command `{$command}`" );
196 }
197
198 static function makeBlob( $params ) {
199 if ( $params !== false ) {
200 return serialize( $params );
201 } else {
202 return '';
203 }
204 }
205
206 static function extractBlob( $blob ) {
207 if ( (string)$blob !== '' ) {
208 return unserialize( $blob );
209 } else {
210 return false;
211 }
212 }
213
214 /**
215 * Batch-insert a group of jobs into the queue.
216 * This will be wrapped in a transaction with a forced commit.
217 *
218 * This may add duplicate at insert time, but they will be
219 * removed later on, when the first one is popped.
220 *
221 * @param $jobs array of Job objects
222 */
223 static function batchInsert( $jobs ) {
224 if( !count( $jobs ) ) {
225 return;
226 }
227 $dbw = wfGetDB( DB_MASTER );
228 $rows = array();
229 foreach( $jobs as $job ) {
230 $rows[] = $job->insertFields();
231 if ( count( $rows ) >= 50 ) {
232 # Do a small transaction to avoid slave lag
233 $dbw->begin();
234 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
235 $dbw->commit();
236 $rows = array();
237 }
238 }
239 if ( $rows ) {
240 wfIncrStats( 'job-insert', count( $rows ) );
241 $dbw->begin();
242 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
243 $dbw->commit();
244 }
245 }
246
247 /*-------------------------------------------------------------------------
248 * Non-static functions
249 *------------------------------------------------------------------------*/
250
251 /**
252 * @param $command
253 * @param $title
254 * @param $params array
255 * @param int $id
256 */
257 function __construct( $command, $title, $params = false, $id = 0 ) {
258 $this->command = $command;
259 $this->title = $title;
260 $this->params = $params;
261 $this->id = $id;
262
263 // A bit of premature generalisation
264 // Oh well, the whole class is premature generalisation really
265 $this->removeDuplicates = true;
266 }
267
268 /**
269 * Insert a single job into the queue.
270 * @return bool true on success
271 */
272 function insert() {
273 $fields = $this->insertFields();
274
275 $dbw = wfGetDB( DB_MASTER );
276
277 if ( $this->removeDuplicates ) {
278 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
279 if ( $dbw->numRows( $res ) ) {
280 return;
281 }
282 }
283 wfIncrStats( 'job-insert' );
284 return $dbw->insert( 'job', $fields, __METHOD__ );
285 }
286
287 protected function insertFields() {
288 $dbw = wfGetDB( DB_MASTER );
289 return array(
290 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
291 'job_cmd' => $this->command,
292 'job_namespace' => $this->title->getNamespace(),
293 'job_title' => $this->title->getDBkey(),
294 'job_params' => Job::makeBlob( $this->params )
295 );
296 }
297
298 /**
299 * Remove jobs in the job queue which are duplicates of this job.
300 * This is deadlock-prone and so starts its own transaction.
301 */
302 function removeDuplicates() {
303 if ( !$this->removeDuplicates ) {
304 return;
305 }
306
307 $fields = $this->insertFields();
308 unset( $fields['job_id'] );
309 $dbw = wfGetDB( DB_MASTER );
310 $dbw->begin();
311 $dbw->delete( 'job', $fields, __METHOD__ );
312 $affected = $dbw->affectedRows();
313 $dbw->commit();
314 if ( $affected ) {
315 wfIncrStats( 'job-dup-delete', $affected );
316 }
317 }
318
319 function toString() {
320 $paramString = '';
321 if ( $this->params ) {
322 foreach ( $this->params as $key => $value ) {
323 if ( $paramString != '' ) {
324 $paramString .= ' ';
325 }
326 $paramString .= "$key=$value";
327 }
328 }
329
330 if ( is_object( $this->title ) ) {
331 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
332 if ( $paramString !== '' ) {
333 $s .= ' ' . $paramString;
334 }
335 return $s;
336 } else {
337 return "{$this->command} $paramString";
338 }
339 }
340
341 protected function setLastError( $error ) {
342 $this->error = $error;
343 }
344
345 function getLastError() {
346 return $this->error;
347 }
348 }