3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\MediaWikiServices
;
22 use MediaWiki\Page\PageIdentity
;
25 * Helper for a Job that updates links to a given page title.
27 * When an asset changes, a base job can be inserted to update all assets that depend on it.
28 * The base job splits into per-title "leaf" jobs and a "remnant" job to handle the remaining
29 * range of backlinks. This recurs until the remnant job's backlink range is small enough that
30 * only leaf jobs are created from it.
32 * For example, if templates A and B are edited (at the same time) the queue will have:
34 * When these jobs run, the queue will have per-title and remnant partition jobs:
35 * (titleX,titleY,titleZ,...,A remnant,titleM,titleN,titleO,...,B remnant)
37 * This works best when the queue is FIFO, for several reasons:
38 * - a) Since the remnant jobs are enqueued after the leaf jobs, the slower leaf jobs have to
39 * get popped prior to the fast remnant jobs. This avoids flooding the queue with leaf jobs
40 * for every single backlink of widely used assets (which can be millions).
41 * - b) Other jobs going in the queue still get a chance to run after a widely used asset changes.
42 * This is due to the large remnant job pushing to the end of the queue with each division.
44 * The size of the queues used in this manner depend on the number of assets changes and the
45 * number of workers. Also, with FIFO-per-partition queues, the queue size can be somewhat larger,
46 * depending on the number of queue partitions.
51 class BacklinkJobUtils
{
53 * Break down $job into approximately ($bSize/$cSize) leaf jobs and a single partition
54 * job that covers the remaining backlink range (if needed). Jobs for the first $bSize
55 * titles are collated ($cSize per job) into leaf jobs to do actual work. All the
56 * resulting jobs are of the same class as $job. No partition job is returned if the
57 * range covered by $job was less than $bSize, as the leaf jobs have full coverage.
59 * The leaf jobs have the 'pages' param set to a (<page ID>:(<namespace>,<DB key>),...)
60 * map so that the run() function knows what pages to act on. The leaf jobs will keep
61 * the same job title as the parent job (e.g. $job).
63 * The partition jobs have the 'range' parameter set to a map of the format
64 * (start:<integer>, end:<integer>, batchSize:<integer>, subranges:((<start>,<end>),...)),
65 * the 'table' parameter set to that of $job, and the 'recursive' parameter set to true.
66 * This method can be called on the resulting job to repeat the process again.
68 * The job provided ($job) must have the 'recursive' parameter set to true and the 'table'
69 * parameter must be set to a backlink table. The job title will be used as the title to
70 * find backlinks for. Any 'range' parameter must follow the same format as mentioned above.
71 * This should be managed by recursive calls to this method.
73 * The first jobs return are always the leaf jobs. This lets the caller use push() to
74 * put them directly into the queue and works well if the queue is FIFO. In such a queue,
75 * the leaf jobs have to get finished first before anything can resolve the next partition
76 * job, which keeps the queue very small.
79 * - params : extra job parameters to include in each job
82 * @param int $bSize BacklinkCache partition size; usually $wgUpdateRowsPerJob
83 * @param int $cSize Max titles per leaf job; Usually 1 or a modest value
84 * @param array $opts Optional parameter map
87 public static function partitionBacklinkJob( Job
$job, $bSize, $cSize, $opts = [] ) {
88 $class = get_class( $job );
89 $title = $job->getTitle();
90 $params = $job->getParams();
92 $backlinkCache = MediaWikiServices
::getInstance()->getBacklinkCacheFactory()
93 ->getBacklinkCache( $title );
94 if ( isset( $params['pages'] ) ||
empty( $params['recursive'] ) ) {
95 // this is a leaf node
98 wfWarn( __METHOD__
. " called on {$job->getType()} leaf job (explosive recursion)." );
99 } elseif ( isset( $params['range'] ) ) {
100 // This is a range job to trigger the insertion of partitioned/title jobs...
101 $ranges = $params['range']['subranges'];
102 $realBSize = $params['range']['batchSize'];
104 // This is a base job to trigger the insertion of partitioned jobs...
105 $ranges = $backlinkCache->partition( $params['table'], $bSize );
109 $extraParams = $opts['params'] ??
[];
112 // Combine the first range (of size $bSize) backlinks into leaf jobs
113 if ( isset( $ranges[0] ) ) {
114 $start = $ranges[0][0];
115 $end = isset( $ranges[1] ) ?
$ranges[1][0] - 1 : false;
117 $iter = $backlinkCache->getLinkPages( $params['table'], $start, $end );
118 $pageSources = iterator_to_array( $iter );
119 /** @var PageIdentity[] $pageBatch */
120 foreach ( array_chunk( $pageSources, $cSize ) as $pageBatch ) {
122 foreach ( $pageBatch as $page ) {
123 $pages[$page->getId()] = [ $page->getNamespace(), $page->getDBkey() ];
125 $jobs[] = new $class(
126 $title, // maintain parent job title
127 [ 'pages' => $pages ] +
$extraParams
131 // Take all of the remaining ranges and build a partition job from it
132 if ( isset( $ranges[1] ) ) {
133 $jobs[] = new $class(
134 $title, // maintain parent job title
137 'table' => $params['table'],
139 'start' => $ranges[1][0],
140 'end' => $ranges[count( $ranges ) - 1][1],
141 'batchSize' => $realBSize,
142 'subranges' => array_slice( $ranges, 1 )
144 // Track how many times the base job divided for debugging
145 'division' => isset( $params['division'] )
146 ?
( $params['division'] +
1 )