Move Blob class to Rdbms namespaces
[mediawiki.git] / includes / jobqueue / utils / BacklinkJobUtils.php
blob7f500554bdd6ee942fae2b704cb79c1d8646b71f
1 <?php
2 /**
3 * Job to update links for a given title.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup JobQueue
22 * @author Aaron Schulz
25 /**
26 * Class with Backlink related Job helper methods
28 * When an asset changes, a base job can be inserted to update all assets that depend on it.
29 * The base job splits into per-title "leaf" jobs and a "remnant" job to handle the remaining
30 * range of backlinks. This recurs until the remnant job's backlink range is small enough that
31 * only leaf jobs are created from it.
33 * For example, if templates A and B are edited (at the same time) the queue will have:
34 * (A base, B base)
35 * When these jobs run, the queue will have per-title and remnant partition jobs:
36 * (titleX,titleY,titleZ,...,A remnant,titleM,titleN,titleO,...,B remnant)
38 * This works best when the queue is FIFO, for several reasons:
39 * - a) Since the remnant jobs are enqueued after the leaf jobs, the slower leaf jobs have to
40 * get popped prior to the fast remnant jobs. This avoids flooding the queue with leaf jobs
41 * for every single backlink of widely used assets (which can be millions).
42 * - b) Other jobs going in the queue still get a chance to run after a widely used asset changes.
43 * This is due to the large remnant job pushing to the end of the queue with each division.
45 * The size of the queues used in this manner depend on the number of assets changes and the
46 * number of workers. Also, with FIFO-per-partition queues, the queue size can be somewhat larger,
47 * depending on the number of queue partitions.
49 * @ingroup JobQueue
50 * @since 1.23
52 class BacklinkJobUtils {
53 /**
54 * Break down $job into approximately ($bSize/$cSize) leaf jobs and a single partition
55 * job that covers the remaining backlink range (if needed). Jobs for the first $bSize
56 * titles are collated ($cSize per job) into leaf jobs to do actual work. All the
57 * resulting jobs are of the same class as $job. No partition job is returned if the
58 * range covered by $job was less than $bSize, as the leaf jobs have full coverage.
60 * The leaf jobs have the 'pages' param set to a (<page ID>:(<namespace>,<DB key>),...)
61 * map so that the run() function knows what pages to act on. The leaf jobs will keep
62 * the same job title as the parent job (e.g. $job).
64 * The partition jobs have the 'range' parameter set to a map of the format
65 * (start:<integer>, end:<integer>, batchSize:<integer>, subranges:((<start>,<end>),...)),
66 * the 'table' parameter set to that of $job, and the 'recursive' parameter set to true.
67 * This method can be called on the resulting job to repeat the process again.
69 * The job provided ($job) must have the 'recursive' parameter set to true and the 'table'
70 * parameter must be set to a backlink table. The job title will be used as the title to
71 * find backlinks for. Any 'range' parameter must follow the same format as mentioned above.
72 * This should be managed by recursive calls to this method.
74 * The first jobs return are always the leaf jobs. This lets the caller use push() to
75 * put them directly into the queue and works well if the queue is FIFO. In such a queue,
76 * the leaf jobs have to get finished first before anything can resolve the next partition
77 * job, which keeps the queue very small.
79 * $opts includes:
80 * - params : extra job parameters to include in each job
82 * @param Job $job
83 * @param int $bSize BacklinkCache partition size; usually $wgUpdateRowsPerJob
84 * @param int $cSize Max titles per leaf job; Usually 1 or a modest value
85 * @param array $opts Optional parameter map
86 * @return Job[] List of Job objects
88 public static function partitionBacklinkJob( Job $job, $bSize, $cSize, $opts = [] ) {
89 $class = get_class( $job );
90 $title = $job->getTitle();
91 $params = $job->getParams();
93 if ( isset( $params['pages'] ) || empty( $params['recursive'] ) ) {
94 $ranges = []; // sanity; this is a leaf node
95 $realBSize = 0;
96 wfWarn( __METHOD__ . " called on {$job->getType()} leaf job (explosive recursion)." );
97 } elseif ( isset( $params['range'] ) ) {
98 // This is a range job to trigger the insertion of partitioned/title jobs...
99 $ranges = $params['range']['subranges'];
100 $realBSize = $params['range']['batchSize'];
101 } else {
102 // This is a base job to trigger the insertion of partitioned jobs...
103 $ranges = $title->getBacklinkCache()->partition( $params['table'], $bSize );
104 $realBSize = $bSize;
107 $extraParams = isset( $opts['params'] ) ? $opts['params'] : [];
109 $jobs = [];
110 // Combine the first range (of size $bSize) backlinks into leaf jobs
111 if ( isset( $ranges[0] ) ) {
112 list( $start, $end ) = $ranges[0];
113 $iter = $title->getBacklinkCache()->getLinks( $params['table'], $start, $end );
114 $titles = iterator_to_array( $iter );
115 /** @var Title[] $titleBatch */
116 foreach ( array_chunk( $titles, $cSize ) as $titleBatch ) {
117 $pages = [];
118 foreach ( $titleBatch as $tl ) {
119 $pages[$tl->getArticleID()] = [ $tl->getNamespace(), $tl->getDBkey() ];
121 $jobs[] = new $class(
122 $title, // maintain parent job title
123 [ 'pages' => $pages ] + $extraParams
127 // Take all of the remaining ranges and build a partition job from it
128 if ( isset( $ranges[1] ) ) {
129 $jobs[] = new $class(
130 $title, // maintain parent job title
132 'recursive' => true,
133 'table' => $params['table'],
134 'range' => [
135 'start' => $ranges[1][0],
136 'end' => $ranges[count( $ranges ) - 1][1],
137 'batchSize' => $realBSize,
138 'subranges' => array_slice( $ranges, 1 )
140 // Track how many times the base job divided for debugging
141 'division' => isset( $params['division'] )
142 ? ( $params['division'] + 1 )
144 ] + $extraParams
148 return $jobs;