Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / maintenance / storage / recompressTracked.php
1 <?php
2 /**
3 * Moves blobs indexed by trackBlobs.php to a specified list of destination
4 * clusters, and recompresses them in the process.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance ExternalStorage
23 */
24
25 use MediaWiki\Logger\LegacyLogger;
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Shell\Shell;
28 use Wikimedia\Rdbms\IDatabase;
29
30 $optionsWithArgs = RecompressTracked::getOptionsWithArgs();
31 require __DIR__ . '/../commandLine.inc';
32
33 if ( count( $args ) < 1 ) {
34 echo "Usage: php recompressTracked.php [options] <cluster> [... <cluster>...]
35 Moves blobs indexed by trackBlobs.php to a specified list of destination clusters,
36 and recompresses them in the process. Restartable.
37
38 Options:
39 --procs <procs> Set the number of child processes (default 1)
40 --copy-only Copy only, do not update the text table. Restart
41 without this option to complete.
42 --debug-log <file> Log debugging data to the specified file
43 --info-log <file> Log progress messages to the specified file
44 --critical-log <file> Log error messages to the specified file
45 ";
46 exit( 1 );
47 }
48
49 $job = RecompressTracked::newFromCommandLine( $args, $options );
50 $job->execute();
51
52 /**
53 * Maintenance script that moves blobs indexed by trackBlobs.php to a specified
54 * list of destination clusters, and recompresses them in the process.
55 *
56 * @ingroup Maintenance ExternalStorage
57 */
58 class RecompressTracked {
59 public $destClusters;
60 public $batchSize = 1000;
61 public $orphanBatchSize = 1000;
62 public $reportingInterval = 10;
63 public $numProcs = 1;
64 public $numBatches = 0;
65 public $pageBlobClass, $orphanBlobClass;
66 public $replicaPipes, $replicaProcs, $prevReplicaId;
67 public $copyOnly = false;
68 public $isChild = false;
69 public $replicaId = false;
70 public $noCount = false;
71 public $debugLog, $infoLog, $criticalLog;
72 /** @var ExternalStoreDB */
73 public $store;
74
75 private static $optionsWithArgs = [
76 'procs',
77 'replica-id',
78 'debug-log',
79 'info-log',
80 'critical-log'
81 ];
82
83 private static $cmdLineOptionMap = [
84 'no-count' => 'noCount',
85 'procs' => 'numProcs',
86 'copy-only' => 'copyOnly',
87 'child' => 'isChild',
88 'replica-id' => 'replicaId',
89 'debug-log' => 'debugLog',
90 'info-log' => 'infoLog',
91 'critical-log' => 'criticalLog',
92 ];
93
94 static function getOptionsWithArgs() {
95 return self::$optionsWithArgs;
96 }
97
98 static function newFromCommandLine( $args, $options ) {
99 $jobOptions = [ 'destClusters' => $args ];
100 foreach ( self::$cmdLineOptionMap as $cmdOption => $classOption ) {
101 if ( isset( $options[$cmdOption] ) ) {
102 $jobOptions[$classOption] = $options[$cmdOption];
103 }
104 }
105
106 return new self( $jobOptions );
107 }
108
109 function __construct( $options ) {
110 foreach ( $options as $name => $value ) {
111 $this->$name = $value;
112 }
113 $esFactory = MediaWikiServices::getInstance()->getExternalStoreFactory();
114 $this->store = $esFactory->getStore( 'DB' );
115 if ( !$this->isChild ) {
116 $GLOBALS['wgDebugLogPrefix'] = "RCT M: ";
117 } elseif ( $this->replicaId !== false ) {
118 $GLOBALS['wgDebugLogPrefix'] = "RCT {$this->replicaId}: ";
119 }
120 $this->pageBlobClass = function_exists( 'xdiff_string_bdiff' ) ?
121 DiffHistoryBlob::class : ConcatenatedGzipHistoryBlob::class;
122 $this->orphanBlobClass = ConcatenatedGzipHistoryBlob::class;
123 }
124
125 function debug( $msg ) {
126 wfDebug( "$msg\n" );
127 if ( $this->debugLog ) {
128 $this->logToFile( $msg, $this->debugLog );
129 }
130 }
131
132 function info( $msg ) {
133 echo "$msg\n";
134 if ( $this->infoLog ) {
135 $this->logToFile( $msg, $this->infoLog );
136 }
137 }
138
139 function critical( $msg ) {
140 echo "$msg\n";
141 if ( $this->criticalLog ) {
142 $this->logToFile( $msg, $this->criticalLog );
143 }
144 }
145
146 function logToFile( $msg, $file ) {
147 $header = '[' . date( 'd\TH:i:s' ) . '] ' . wfHostname() . ' ' . posix_getpid();
148 if ( $this->replicaId !== false ) {
149 $header .= "({$this->replicaId})";
150 }
151 $header .= ' ' . WikiMap::getCurrentWikiDbDomain()->getId();
152 LegacyLogger::emit( sprintf( "%-50s %s\n", $header, $msg ), $file );
153 }
154
155 /**
156 * Wait until the selected replica DB has caught up to the master.
157 * This allows us to use the replica DB for things that were committed in a
158 * previous part of this batch process.
159 */
160 function syncDBs() {
161 $dbw = wfGetDB( DB_MASTER );
162 $dbr = wfGetDB( DB_REPLICA );
163 $pos = $dbw->getMasterPos();
164 $dbr->masterPosWait( $pos, 100000 );
165 }
166
167 /**
168 * Execute parent or child depending on the isChild option
169 */
170 function execute() {
171 if ( $this->isChild ) {
172 $this->executeChild();
173 } else {
174 $this->executeParent();
175 }
176 }
177
178 /**
179 * Execute the parent process
180 */
181 function executeParent() {
182 if ( !$this->checkTrackingTable() ) {
183 return;
184 }
185
186 $this->syncDBs();
187 $this->startReplicaProcs();
188 $this->doAllPages();
189 $this->doAllOrphans();
190 $this->killReplicaProcs();
191 }
192
193 /**
194 * Make sure the tracking table exists and isn't empty
195 * @return bool
196 */
197 function checkTrackingTable() {
198 $dbr = wfGetDB( DB_REPLICA );
199 if ( !$dbr->tableExists( 'blob_tracking' ) ) {
200 $this->critical( "Error: blob_tracking table does not exist" );
201
202 return false;
203 }
204 $row = $dbr->selectRow( 'blob_tracking', '*', '', __METHOD__ );
205 if ( !$row ) {
206 $this->info( "Warning: blob_tracking table contains no rows, skipping this wiki." );
207
208 return false;
209 }
210
211 return true;
212 }
213
214 /**
215 * Start the worker processes.
216 * These processes will listen on stdin for commands.
217 * This necessary because text recompression is slow: loading, compressing and
218 * writing are all slow.
219 */
220 function startReplicaProcs() {
221 $wiki = WikiMap::getWikiIdFromDbDomain( WikiMap::getCurrentWikiDbDomain() );
222
223 $cmd = 'php ' . Shell::escape( __FILE__ );
224 foreach ( self::$cmdLineOptionMap as $cmdOption => $classOption ) {
225 if ( $cmdOption == 'replica-id' ) {
226 continue;
227 } elseif ( in_array( $cmdOption, self::$optionsWithArgs ) && isset( $this->$classOption ) ) {
228 $cmd .= " --$cmdOption " . Shell::escape( $this->$classOption );
229 } elseif ( $this->$classOption ) {
230 $cmd .= " --$cmdOption";
231 }
232 }
233 $cmd .= ' --child' .
234 ' --wiki ' . Shell::escape( $wiki ) .
235 ' ' . Shell::escape( ...$this->destClusters );
236
237 $this->replicaPipes = $this->replicaProcs = [];
238 for ( $i = 0; $i < $this->numProcs; $i++ ) {
239 $pipes = [];
240 $spec = [
241 [ 'pipe', 'r' ],
242 [ 'file', 'php://stdout', 'w' ],
243 [ 'file', 'php://stderr', 'w' ]
244 ];
245 Wikimedia\suppressWarnings();
246 $proc = proc_open( "$cmd --replica-id $i", $spec, $pipes );
247 Wikimedia\restoreWarnings();
248 if ( !$proc ) {
249 $this->critical( "Error opening replica DB process: $cmd" );
250 exit( 1 );
251 }
252 $this->replicaProcs[$i] = $proc;
253 $this->replicaPipes[$i] = $pipes[0];
254 }
255 $this->prevReplicaId = -1;
256 }
257
258 /**
259 * Gracefully terminate the child processes
260 */
261 function killReplicaProcs() {
262 $this->info( "Waiting for replica DB processes to finish..." );
263 for ( $i = 0; $i < $this->numProcs; $i++ ) {
264 $this->dispatchToReplica( $i, 'quit' );
265 }
266 for ( $i = 0; $i < $this->numProcs; $i++ ) {
267 $status = proc_close( $this->replicaProcs[$i] );
268 if ( $status ) {
269 $this->critical( "Warning: child #$i exited with status $status" );
270 }
271 }
272 $this->info( "Done." );
273 }
274
275 /**
276 * Dispatch a command to the next available replica DB.
277 * This may block until a replica DB finishes its work and becomes available.
278 */
279 function dispatch( ...$args ) {
280 $pipes = $this->replicaPipes;
281 $x = [];
282 $y = [];
283 $numPipes = stream_select( $x, $pipes, $y, 3600 );
284 if ( !$numPipes ) {
285 $this->critical( "Error waiting to write to replica DBs. Aborting" );
286 exit( 1 );
287 }
288 for ( $i = 0; $i < $this->numProcs; $i++ ) {
289 $replicaId = ( $i + $this->prevReplicaId + 1 ) % $this->numProcs;
290 if ( isset( $pipes[$replicaId] ) ) {
291 $this->prevReplicaId = $replicaId;
292 $this->dispatchToReplica( $replicaId, $args );
293
294 return;
295 }
296 }
297 $this->critical( "Unreachable" );
298 exit( 1 );
299 }
300
301 /**
302 * Dispatch a command to a specified replica DB
303 * @param int $replicaId
304 * @param array|string $args
305 */
306 function dispatchToReplica( $replicaId, $args ) {
307 $args = (array)$args;
308 $cmd = implode( ' ', $args );
309 fwrite( $this->replicaPipes[$replicaId], "$cmd\n" );
310 }
311
312 /**
313 * Move all tracked pages to the new clusters
314 */
315 function doAllPages() {
316 $dbr = wfGetDB( DB_REPLICA );
317 $i = 0;
318 $startId = 0;
319 if ( $this->noCount ) {
320 $numPages = '[unknown]';
321 } else {
322 $numPages = $dbr->selectField( 'blob_tracking',
323 'COUNT(DISTINCT bt_page)',
324 # A condition is required so that this query uses the index
325 [ 'bt_moved' => 0 ],
326 __METHOD__
327 );
328 }
329 if ( $this->copyOnly ) {
330 $this->info( "Copying pages..." );
331 } else {
332 $this->info( "Moving pages..." );
333 }
334 while ( true ) {
335 $res = $dbr->select( 'blob_tracking',
336 [ 'bt_page' ],
337 [
338 'bt_moved' => 0,
339 'bt_page > ' . $dbr->addQuotes( $startId )
340 ],
341 __METHOD__,
342 [
343 'DISTINCT',
344 'ORDER BY' => 'bt_page',
345 'LIMIT' => $this->batchSize,
346 ]
347 );
348 if ( !$res->numRows() ) {
349 break;
350 }
351 foreach ( $res as $row ) {
352 $startId = $row->bt_page;
353 $this->dispatch( 'doPage', $row->bt_page );
354 $i++;
355 }
356 $this->report( 'pages', $i, $numPages );
357 }
358 $this->report( 'pages', $i, $numPages );
359 if ( $this->copyOnly ) {
360 $this->info( "All page copies queued." );
361 } else {
362 $this->info( "All page moves queued." );
363 }
364 }
365
366 /**
367 * Display a progress report
368 * @param string $label
369 * @param int $current
370 * @param int $end
371 */
372 function report( $label, $current, $end ) {
373 $this->numBatches++;
374 if ( $current == $end || $this->numBatches >= $this->reportingInterval ) {
375 $this->numBatches = 0;
376 $this->info( "$label: $current / $end" );
377 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
378 }
379 }
380
381 /**
382 * Move all orphan text to the new clusters
383 */
384 function doAllOrphans() {
385 $dbr = wfGetDB( DB_REPLICA );
386 $startId = 0;
387 $i = 0;
388 if ( $this->noCount ) {
389 $numOrphans = '[unknown]';
390 } else {
391 $numOrphans = $dbr->selectField( 'blob_tracking',
392 'COUNT(DISTINCT bt_text_id)',
393 [ 'bt_moved' => 0, 'bt_page' => 0 ],
394 __METHOD__ );
395 if ( !$numOrphans ) {
396 return;
397 }
398 }
399 if ( $this->copyOnly ) {
400 $this->info( "Copying orphans..." );
401 } else {
402 $this->info( "Moving orphans..." );
403 }
404
405 while ( true ) {
406 $res = $dbr->select( 'blob_tracking',
407 [ 'bt_text_id' ],
408 [
409 'bt_moved' => 0,
410 'bt_page' => 0,
411 'bt_text_id > ' . $dbr->addQuotes( $startId )
412 ],
413 __METHOD__,
414 [
415 'DISTINCT',
416 'ORDER BY' => 'bt_text_id',
417 'LIMIT' => $this->batchSize
418 ]
419 );
420 if ( !$res->numRows() ) {
421 break;
422 }
423 $ids = [];
424 foreach ( $res as $row ) {
425 $startId = $row->bt_text_id;
426 $ids[] = $row->bt_text_id;
427 $i++;
428 }
429 // Need to send enough orphan IDs to the child at a time to fill a blob,
430 // so orphanBatchSize needs to be at least ~100.
431 // batchSize can be smaller or larger.
432 while ( count( $ids ) > $this->orphanBatchSize ) {
433 $args = array_slice( $ids, 0, $this->orphanBatchSize );
434 $ids = array_slice( $ids, $this->orphanBatchSize );
435 array_unshift( $args, 'doOrphanList' );
436 $this->dispatch( ...$args );
437 }
438 if ( count( $ids ) ) {
439 $args = $ids;
440 array_unshift( $args, 'doOrphanList' );
441 $this->dispatch( ...$args );
442 }
443
444 $this->report( 'orphans', $i, $numOrphans );
445 }
446 $this->report( 'orphans', $i, $numOrphans );
447 $this->info( "All orphans queued." );
448 }
449
450 /**
451 * Main entry point for worker processes
452 */
453 function executeChild() {
454 $this->debug( 'starting' );
455 $this->syncDBs();
456
457 while ( !feof( STDIN ) ) {
458 $line = rtrim( fgets( STDIN ) );
459 if ( $line == '' ) {
460 continue;
461 }
462 $this->debug( $line );
463 $args = explode( ' ', $line );
464 $cmd = array_shift( $args );
465 switch ( $cmd ) {
466 case 'doPage':
467 $this->doPage( intval( $args[0] ) );
468 break;
469 case 'doOrphanList':
470 $this->doOrphanList( array_map( 'intval', $args ) );
471 break;
472 case 'quit':
473 return;
474 }
475 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
476 }
477 }
478
479 /**
480 * Move tracked text in a given page
481 *
482 * @param int $pageId
483 */
484 function doPage( $pageId ) {
485 $title = Title::newFromID( $pageId );
486 if ( $title ) {
487 $titleText = $title->getPrefixedText();
488 } else {
489 $titleText = '[deleted]';
490 }
491 $dbr = wfGetDB( DB_REPLICA );
492
493 // Finish any incomplete transactions
494 if ( !$this->copyOnly ) {
495 $this->finishIncompleteMoves( [ 'bt_page' => $pageId ] );
496 $this->syncDBs();
497 }
498
499 $startId = 0;
500 $trx = new CgzCopyTransaction( $this, $this->pageBlobClass );
501
502 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
503 while ( true ) {
504 $res = $dbr->select(
505 [ 'blob_tracking', 'text' ],
506 '*',
507 [
508 'bt_page' => $pageId,
509 'bt_text_id > ' . $dbr->addQuotes( $startId ),
510 'bt_moved' => 0,
511 'bt_new_url IS NULL',
512 'bt_text_id=old_id',
513 ],
514 __METHOD__,
515 [
516 'ORDER BY' => 'bt_text_id',
517 'LIMIT' => $this->batchSize
518 ]
519 );
520 if ( !$res->numRows() ) {
521 break;
522 }
523
524 $lastTextId = 0;
525 foreach ( $res as $row ) {
526 $startId = $row->bt_text_id;
527 if ( $lastTextId == $row->bt_text_id ) {
528 // Duplicate (null edit)
529 continue;
530 }
531 $lastTextId = $row->bt_text_id;
532 // Load the text
533 $text = Revision::getRevisionText( $row );
534 if ( $text === false ) {
535 $this->critical( "Error loading {$row->bt_rev_id}/{$row->bt_text_id}" );
536 continue;
537 }
538
539 // Queue it
540 if ( !$trx->addItem( $text, $row->bt_text_id ) ) {
541 $this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
542 $trx->commit();
543 $trx = new CgzCopyTransaction( $this, $this->pageBlobClass );
544 $lbFactory->waitForReplication();
545 }
546 }
547 }
548
549 $this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
550 $trx->commit();
551 }
552
553 /**
554 * Atomic move operation.
555 *
556 * Write the new URL to the text table and set the bt_moved flag.
557 *
558 * This is done in a single transaction to provide restartable behavior
559 * without data loss.
560 *
561 * The transaction is kept short to reduce locking.
562 *
563 * @param int $textId
564 * @param string $url
565 */
566 function moveTextRow( $textId, $url ) {
567 if ( $this->copyOnly ) {
568 $this->critical( "Internal error: can't call moveTextRow() in --copy-only mode" );
569 exit( 1 );
570 }
571 $dbw = wfGetDB( DB_MASTER );
572 $dbw->begin( __METHOD__ );
573 $dbw->update( 'text',
574 [ // set
575 'old_text' => $url,
576 'old_flags' => 'external,utf-8',
577 ],
578 [ // where
579 'old_id' => $textId
580 ],
581 __METHOD__
582 );
583 $dbw->update( 'blob_tracking',
584 [ 'bt_moved' => 1 ],
585 [ 'bt_text_id' => $textId ],
586 __METHOD__
587 );
588 $dbw->commit( __METHOD__ );
589 }
590
591 /**
592 * Moves are done in two phases: bt_new_url and then bt_moved.
593 * - bt_new_url indicates that the text has been copied to the new cluster.
594 * - bt_moved indicates that the text table has been updated.
595 *
596 * This function completes any moves that only have done bt_new_url. This
597 * can happen when the script is interrupted, or when --copy-only is used.
598 *
599 * @param array $conds
600 */
601 function finishIncompleteMoves( $conds ) {
602 $dbr = wfGetDB( DB_REPLICA );
603 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
604
605 $startId = 0;
606 $conds = array_merge( $conds, [
607 'bt_moved' => 0,
608 'bt_new_url IS NOT NULL'
609 ] );
610 while ( true ) {
611 $res = $dbr->select( 'blob_tracking',
612 '*',
613 array_merge( $conds, [ 'bt_text_id > ' . $dbr->addQuotes( $startId ) ] ),
614 __METHOD__,
615 [
616 'ORDER BY' => 'bt_text_id',
617 'LIMIT' => $this->batchSize,
618 ]
619 );
620 if ( !$res->numRows() ) {
621 break;
622 }
623 $this->debug( 'Incomplete: ' . $res->numRows() . ' rows' );
624 foreach ( $res as $row ) {
625 $startId = $row->bt_text_id;
626 $this->moveTextRow( $row->bt_text_id, $row->bt_new_url );
627 if ( $row->bt_text_id % 10 == 0 ) {
628 $lbFactory->waitForReplication();
629 }
630 }
631 }
632 }
633
634 /**
635 * Returns the name of the next target cluster
636 * @return string
637 */
638 function getTargetCluster() {
639 $cluster = next( $this->destClusters );
640 if ( $cluster === false ) {
641 $cluster = reset( $this->destClusters );
642 }
643
644 return $cluster;
645 }
646
647 /**
648 * Gets a DB master connection for the given external cluster name
649 * @param string $cluster
650 * @return IDatabase
651 */
652 function getExtDB( $cluster ) {
653 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
654 $lb = $lbFactory->getExternalLB( $cluster );
655
656 return $lb->getConnection( DB_MASTER );
657 }
658
659 /**
660 * Move an orphan text_id to the new cluster
661 *
662 * @param array $textIds
663 */
664 function doOrphanList( $textIds ) {
665 // Finish incomplete moves
666 if ( !$this->copyOnly ) {
667 $this->finishIncompleteMoves( [ 'bt_text_id' => $textIds ] );
668 $this->syncDBs();
669 }
670
671 $trx = new CgzCopyTransaction( $this, $this->orphanBlobClass );
672
673 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
674 $res = wfGetDB( DB_REPLICA )->select(
675 [ 'text', 'blob_tracking' ],
676 [ 'old_id', 'old_text', 'old_flags' ],
677 [
678 'old_id' => $textIds,
679 'bt_text_id=old_id',
680 'bt_moved' => 0,
681 ],
682 __METHOD__,
683 [ 'DISTINCT' ]
684 );
685
686 foreach ( $res as $row ) {
687 $text = Revision::getRevisionText( $row );
688 if ( $text === false ) {
689 $this->critical( "Error: cannot load revision text for old_id={$row->old_id}" );
690 continue;
691 }
692
693 if ( !$trx->addItem( $text, $row->old_id ) ) {
694 $this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
695 $trx->commit();
696 $trx = new CgzCopyTransaction( $this, $this->orphanBlobClass );
697 $lbFactory->waitForReplication();
698 }
699 }
700 $this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
701 $trx->commit();
702 }
703 }
704
705 /**
706 * Class to represent a recompression operation for a single CGZ blob
707 */
708 class CgzCopyTransaction {
709 /** @var RecompressTracked */
710 public $parent;
711 public $blobClass;
712 /** @var ConcatenatedGzipHistoryBlob */
713 public $cgz;
714 public $referrers;
715
716 /**
717 * Create a transaction from a RecompressTracked object
718 * @param RecompressTracked $parent
719 * @param string $blobClass
720 */
721 function __construct( $parent, $blobClass ) {
722 $this->blobClass = $blobClass;
723 $this->cgz = false;
724 $this->texts = [];
725 $this->parent = $parent;
726 }
727
728 /**
729 * Add text.
730 * Returns false if it's ready to commit.
731 * @param string $text
732 * @param int $textId
733 * @return bool
734 */
735 function addItem( $text, $textId ) {
736 if ( !$this->cgz ) {
737 $class = $this->blobClass;
738 $this->cgz = new $class;
739 }
740 $hash = $this->cgz->addItem( $text );
741 $this->referrers[$textId] = $hash;
742 $this->texts[$textId] = $text;
743
744 return $this->cgz->isHappy();
745 }
746
747 function getSize() {
748 return count( $this->texts );
749 }
750
751 /**
752 * Recompress text after some aberrant modification
753 */
754 function recompress() {
755 $class = $this->blobClass;
756 $this->cgz = new $class;
757 $this->referrers = [];
758 foreach ( $this->texts as $textId => $text ) {
759 $hash = $this->cgz->addItem( $text );
760 $this->referrers[$textId] = $hash;
761 }
762 }
763
764 /**
765 * Commit the blob.
766 * Does nothing if no text items have been added.
767 * May skip the move if --copy-only is set.
768 */
769 function commit() {
770 $originalCount = count( $this->texts );
771 if ( !$originalCount ) {
772 return;
773 }
774
775 /* Check to see if the target text_ids have been moved already.
776 *
777 * We originally read from the replica DB, so this can happen when a single
778 * text_id is shared between multiple pages. It's rare, but possible
779 * if a delete/move/undelete cycle splits up a null edit.
780 *
781 * We do a locking read to prevent closer-run race conditions.
782 */
783 $dbw = wfGetDB( DB_MASTER );
784 $dbw->begin( __METHOD__ );
785 $res = $dbw->select( 'blob_tracking',
786 [ 'bt_text_id', 'bt_moved' ],
787 [ 'bt_text_id' => array_keys( $this->referrers ) ],
788 __METHOD__, [ 'FOR UPDATE' ] );
789 $dirty = false;
790 foreach ( $res as $row ) {
791 if ( $row->bt_moved ) {
792 # This row has already been moved, remove it
793 $this->parent->debug( "TRX: conflict detected in old_id={$row->bt_text_id}" );
794 unset( $this->texts[$row->bt_text_id] );
795 $dirty = true;
796 }
797 }
798
799 // Recompress the blob if necessary
800 if ( $dirty ) {
801 if ( !count( $this->texts ) ) {
802 // All have been moved already
803 if ( $originalCount > 1 ) {
804 // This is suspcious, make noise
805 $this->parent->critical(
806 "Warning: concurrent operation detected, are there two conflicting " .
807 "processes running, doing the same job?" );
808 }
809
810 return;
811 }
812 $this->recompress();
813 }
814
815 // Insert the data into the destination cluster
816 $targetCluster = $this->parent->getTargetCluster();
817 $store = $this->parent->store;
818 $targetDB = $store->getMaster( $targetCluster );
819 $targetDB->clearFlag( DBO_TRX ); // we manage the transactions
820 $targetDB->begin( __METHOD__ );
821 $baseUrl = $this->parent->store->store( $targetCluster, serialize( $this->cgz ) );
822
823 // Write the new URLs to the blob_tracking table
824 foreach ( $this->referrers as $textId => $hash ) {
825 $url = $baseUrl . '/' . $hash;
826 $dbw->update( 'blob_tracking',
827 [ 'bt_new_url' => $url ],
828 [
829 'bt_text_id' => $textId,
830 'bt_moved' => 0, # Check for concurrent conflicting update
831 ],
832 __METHOD__
833 );
834 }
835
836 $targetDB->commit( __METHOD__ );
837 // Critical section here: interruption at this point causes blob duplication
838 // Reversing the order of the commits would cause data loss instead
839 $dbw->commit( __METHOD__ );
840
841 // Write the new URLs to the text table and set the moved flag
842 if ( !$this->parent->copyOnly ) {
843 foreach ( $this->referrers as $textId => $hash ) {
844 $url = $baseUrl . '/' . $hash;
845 $this->parent->moveTextRow( $textId, $url );
846 }
847 }
848 }
849 }