summaryrefslogtreecommitdiff
path: root/maintenance/storage/recompressTracked.php
blob: 4098077f1501711afd5ddf303200d5e2d610174e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
<?php
/**
 * Moves blobs indexed by trackBlobs.php to a specified list of destination
 * clusters, and recompresses them in the process.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance ExternalStorage
 */

$optionsWithArgs = RecompressTracked::getOptionsWithArgs();
require( __DIR__ . '/../commandLine.inc' );

if ( count( $args ) < 1 ) {
	echo "Usage: php recompressTracked.php [options] <cluster> [... <cluster>...]
Moves blobs indexed by trackBlobs.php to a specified list of destination clusters, and recompresses them in the process. Restartable.

Options:
	--procs <procs>         Set the number of child processes (default 1)
	--copy-only             Copy only, do not update the text table. Restart without this option to complete.
	--debug-log <file>      Log debugging data to the specified file
	--info-log <file>       Log progress messages to the specified file
	--critical-log <file>   Log error messages to the specified file
";
	exit( 1 );
}

$job = RecompressTracked::newFromCommandLine( $args, $options );
$job->execute();

class RecompressTracked {
	var $destClusters;
	var $batchSize = 1000;
	var $orphanBatchSize = 1000;
	var $reportingInterval = 10;
	var $numProcs = 1;
	var $useDiff, $pageBlobClass, $orphanBlobClass;
	var $slavePipes, $slaveProcs, $prevSlaveId;
	var $copyOnly = false;
	var $isChild = false;
	var $slaveId = false;
	var $noCount = false;
	var $debugLog, $infoLog, $criticalLog;
	var $store;

	static $optionsWithArgs = array( 'procs', 'slave-id', 'debug-log', 'info-log', 'critical-log' );
	static $cmdLineOptionMap = array(
		'no-count' => 'noCount',
		'procs' => 'numProcs',
		'copy-only' => 'copyOnly',
		'child' => 'isChild',
		'slave-id' => 'slaveId',
		'debug-log' => 'debugLog',
		'info-log' => 'infoLog',
		'critical-log' => 'criticalLog',
	);

	static function getOptionsWithArgs() {
		return self::$optionsWithArgs;
	}

	static function newFromCommandLine( $args, $options ) {
		$jobOptions = array( 'destClusters' => $args );
		foreach ( self::$cmdLineOptionMap as $cmdOption => $classOption ) {
			if ( isset( $options[$cmdOption] ) ) {
				$jobOptions[$classOption] = $options[$cmdOption];
			}
		}
		return new self( $jobOptions );
	}

	function __construct( $options ) {
		foreach ( $options as $name => $value ) {
			$this->$name = $value;
		}
		$this->store = new ExternalStoreDB;
		if ( !$this->isChild ) {
			$GLOBALS['wgDebugLogPrefix'] = "RCT M: ";
		} elseif ( $this->slaveId !== false ) {
			$GLOBALS['wgDebugLogPrefix'] = "RCT {$this->slaveId}: ";
		}
		$this->useDiff = function_exists( 'xdiff_string_bdiff' );
		$this->pageBlobClass = $this->useDiff ? 'DiffHistoryBlob' : 'ConcatenatedGzipHistoryBlob';
		$this->orphanBlobClass = 'ConcatenatedGzipHistoryBlob';
	}

	function debug( $msg ) {
		wfDebug( "$msg\n" );
		if ( $this->debugLog ) {
			$this->logToFile( $msg, $this->debugLog );
		}

	}

	function info( $msg ) {
		echo "$msg\n";
		if ( $this->infoLog ) {
			$this->logToFile( $msg, $this->infoLog );
		}
	}

	function critical( $msg ) {
		echo "$msg\n";
		if ( $this->criticalLog ) {
			$this->logToFile( $msg, $this->criticalLog );
		}
	}

	function logToFile( $msg, $file ) {
		$header = '[' . date( 'd\TH:i:s' ) . '] ' . wfHostname() . ' ' . posix_getpid();
		if ( $this->slaveId !== false ) {
			$header .= "({$this->slaveId})";
		}
		$header .= ' ' . wfWikiID();
		wfErrorLog( sprintf( "%-50s %s\n", $header, $msg ), $file );
	}

	/**
	 * Wait until the selected slave has caught up to the master.
	 * This allows us to use the slave for things that were committed in a
	 * previous part of this batch process.
	 */
	function syncDBs() {
		$dbw = wfGetDB( DB_MASTER );
		$dbr = wfGetDB( DB_SLAVE );
		$pos = $dbw->getMasterPos();
		$dbr->masterPosWait( $pos, 100000 );
	}

	/**
	 * Execute parent or child depending on the isChild option
	 */
	function execute() {
		if ( $this->isChild ) {
			$this->executeChild();
		} else {
			$this->executeParent();
		}
	}

	/**
	 * Execute the parent process
	 */
	function executeParent() {
		if ( !$this->checkTrackingTable() ) {
			return;
		}

		$this->syncDBs();
		$this->startSlaveProcs();
		$this->doAllPages();
		$this->doAllOrphans();
		$this->killSlaveProcs();
	}

	/**
	 * Make sure the tracking table exists and isn't empty
	 * @return bool
	 */
	function checkTrackingTable() {
		$dbr = wfGetDB( DB_SLAVE );
		if ( !$dbr->tableExists( 'blob_tracking' ) ) {
			$this->critical( "Error: blob_tracking table does not exist" );
			return false;
		}
		$row = $dbr->selectRow( 'blob_tracking', '*', false, __METHOD__ );
		if ( !$row ) {
			$this->info( "Warning: blob_tracking table contains no rows, skipping this wiki." );
			return false;
		}
		return true;
	}

	/**
	 * Start the worker processes.
	 * These processes will listen on stdin for commands.
	 * This necessary because text recompression is slow: loading, compressing and
	 * writing are all slow.
	 */
	function startSlaveProcs() {
		$cmd = 'php ' . wfEscapeShellArg( __FILE__ );
		foreach ( self::$cmdLineOptionMap as $cmdOption => $classOption ) {
			if ( $cmdOption == 'slave-id' ) {
				continue;
			} elseif ( in_array( $cmdOption, self::$optionsWithArgs ) && isset( $this->$classOption ) ) {
				$cmd .= " --$cmdOption " . wfEscapeShellArg( $this->$classOption );
			} elseif ( $this->$classOption ) {
				$cmd .= " --$cmdOption";
			}
		}
		$cmd .= ' --child' .
			' --wiki ' . wfEscapeShellArg( wfWikiID() ) .
			' ' . call_user_func_array( 'wfEscapeShellArg', $this->destClusters );

		$this->slavePipes = $this->slaveProcs = array();
		for ( $i = 0; $i < $this->numProcs; $i++ ) {
			$pipes = false;
			$spec = array(
				array( 'pipe', 'r' ),
				array( 'file', 'php://stdout', 'w' ),
				array( 'file', 'php://stderr', 'w' )
			);
			wfSuppressWarnings();
			$proc = proc_open( "$cmd --slave-id $i", $spec, $pipes );
			wfRestoreWarnings();
			if ( !$proc ) {
				$this->critical( "Error opening slave process: $cmd" );
				exit( 1 );
			}
			$this->slaveProcs[$i] = $proc;
			$this->slavePipes[$i] = $pipes[0];
		}
		$this->prevSlaveId = -1;
	}

	/**
	 * Gracefully terminate the child processes
	 */
	function killSlaveProcs() {
		$this->info( "Waiting for slave processes to finish..." );
		for ( $i = 0; $i < $this->numProcs; $i++ ) {
			$this->dispatchToSlave( $i, 'quit' );
		}
		for ( $i = 0; $i < $this->numProcs; $i++ ) {
			$status = proc_close( $this->slaveProcs[$i] );
			if ( $status ) {
				$this->critical( "Warning: child #$i exited with status $status" );
			}
		}
		$this->info( "Done." );
	}

	/**
	 * Dispatch a command to the next available slave.
	 * This may block until a slave finishes its work and becomes available.
	 */
	function dispatch( /*...*/ ) {
		$args = func_get_args();
		$pipes = $this->slavePipes;
		$numPipes = stream_select( $x = array(), $pipes, $y = array(), 3600 );
		if ( !$numPipes ) {
			$this->critical( "Error waiting to write to slaves. Aborting" );
			exit( 1 );
		}
		for ( $i = 0; $i < $this->numProcs; $i++ ) {
			$slaveId = ( $i + $this->prevSlaveId + 1 ) % $this->numProcs;
			if ( isset( $pipes[$slaveId] ) ) {
				$this->prevSlaveId = $slaveId;
				$this->dispatchToSlave( $slaveId, $args );
				return;
			}
		}
		$this->critical( "Unreachable" );
		exit( 1 );
	}

	/**
	 * Dispatch a command to a specified slave
	 */
	function dispatchToSlave( $slaveId, $args ) {
		$args = (array)$args;
		$cmd = implode( ' ',  $args );
		fwrite( $this->slavePipes[$slaveId], "$cmd\n" );
	}

	/**
	 * Move all tracked pages to the new clusters
	 */
	function doAllPages() {
		$dbr = wfGetDB( DB_SLAVE );
		$i = 0;
		$startId = 0;
		if ( $this->noCount ) {
			$numPages = '[unknown]';
		} else {
			$numPages = $dbr->selectField( 'blob_tracking',
				'COUNT(DISTINCT bt_page)',
				# A condition is required so that this query uses the index
				array( 'bt_moved' => 0 ),
				__METHOD__
			);
		}
		if ( $this->copyOnly ) {
			$this->info( "Copying pages..." );
		} else {
			$this->info( "Moving pages..." );
		}
		while ( true ) {
			$res = $dbr->select( 'blob_tracking',
				array( 'bt_page' ),
				array(
					'bt_moved' => 0,
					'bt_page > ' . $dbr->addQuotes( $startId )
				),
				__METHOD__,
				array(
					'DISTINCT',
					'ORDER BY' => 'bt_page',
					'LIMIT' => $this->batchSize,
				)
			);
			if ( !$res->numRows() ) {
				break;
			}
			foreach ( $res as $row ) {
				$this->dispatch( 'doPage', $row->bt_page );
				$i++;
			}
			$startId = $row->bt_page;
			$this->report( 'pages', $i, $numPages );
		}
		$this->report( 'pages', $i, $numPages );
		if ( $this->copyOnly ) {
			$this->info( "All page copies queued." );
		} else {
			$this->info( "All page moves queued." );
		}
	}

	/**
	 * Display a progress report
	 */
	function report( $label, $current, $end ) {
		$this->numBatches++;
		if ( $current == $end || $this->numBatches >= $this->reportingInterval ) {
			$this->numBatches = 0;
			$this->info( "$label: $current / $end" );
			$this->waitForSlaves();
		}
	}

	/**
	 * Move all orphan text to the new clusters
	 */
	function doAllOrphans() {
		$dbr = wfGetDB( DB_SLAVE );
		$startId = 0;
		$i = 0;
		if ( $this->noCount ) {
			$numOrphans = '[unknown]';
		} else {
			$numOrphans = $dbr->selectField( 'blob_tracking',
				'COUNT(DISTINCT bt_text_id)',
				array( 'bt_moved' => 0, 'bt_page' => 0 ),
				__METHOD__ );
			if ( !$numOrphans ) {
				return;
			}
		}
		if ( $this->copyOnly ) {
			$this->info( "Copying orphans..." );
		} else {
			$this->info( "Moving orphans..." );
		}

		while ( true ) {
			$res = $dbr->select( 'blob_tracking',
				array( 'bt_text_id' ),
				array(
					'bt_moved' => 0,
					'bt_page' => 0,
					'bt_text_id > ' . $dbr->addQuotes( $startId )
				),
				__METHOD__,
				array(
					'DISTINCT',
					'ORDER BY' => 'bt_text_id',
					'LIMIT' => $this->batchSize
				)
			);
			if ( !$res->numRows() ) {
				break;
			}
			$ids = array();
			foreach ( $res as $row ) {
				$ids[] = $row->bt_text_id;
				$i++;
			}
			// Need to send enough orphan IDs to the child at a time to fill a blob,
			// so orphanBatchSize needs to be at least ~100.
			// batchSize can be smaller or larger.
			while ( count( $ids ) > $this->orphanBatchSize ) {
				$args = array_slice( $ids, 0, $this->orphanBatchSize );
				$ids = array_slice( $ids, $this->orphanBatchSize );
				array_unshift( $args, 'doOrphanList' );
				call_user_func_array( array( $this, 'dispatch' ), $args );
			}
			if ( count( $ids ) ) {
				$args = $ids;
				array_unshift( $args, 'doOrphanList' );
				call_user_func_array( array( $this, 'dispatch' ), $args );
			}

			$startId = $row->bt_text_id;
			$this->report( 'orphans', $i, $numOrphans );
		}
		$this->report( 'orphans', $i, $numOrphans );
		$this->info( "All orphans queued." );
	}

	/**
	 * Main entry point for worker processes
	 */
	function executeChild() {
		$this->debug( 'starting' );
		$this->syncDBs();

		while ( !feof( STDIN ) ) {
			$line = rtrim( fgets( STDIN ) );
			if ( $line == '' ) {
				continue;
			}
			$this->debug( $line );
			$args = explode( ' ', $line );
			$cmd = array_shift( $args );
			switch ( $cmd ) {
			case 'doPage':
				$this->doPage( intval( $args[0] ) );
				break;
			case 'doOrphanList':
				$this->doOrphanList( array_map( 'intval', $args ) );
				break;
			case 'quit':
				return;
			}
			$this->waitForSlaves();
		}
	}

	/**
	 * Move tracked text in a given page
	 */
	function doPage( $pageId ) {
		$title = Title::newFromId( $pageId );
		if ( $title ) {
			$titleText = $title->getPrefixedText();
		} else {
			$titleText = '[deleted]';
		}
		$dbr = wfGetDB( DB_SLAVE );

		// Finish any incomplete transactions
		if ( !$this->copyOnly ) {
			$this->finishIncompleteMoves( array( 'bt_page' => $pageId ) );
			$this->syncDBs();
		}

		$startId = 0;
		$trx = new CgzCopyTransaction( $this, $this->pageBlobClass );

		while ( true ) {
			$res = $dbr->select(
				array( 'blob_tracking', 'text' ),
				'*',
				array(
					'bt_page' => $pageId,
					'bt_text_id > ' . $dbr->addQuotes( $startId ),
					'bt_moved' => 0,
					'bt_new_url IS NULL',
					'bt_text_id=old_id',
				),
				__METHOD__,
				array(
					'ORDER BY' => 'bt_text_id',
					'LIMIT' => $this->batchSize
				)
			);
			if ( !$res->numRows() ) {
				break;
			}

			$lastTextId = 0;
			foreach ( $res as $row ) {
				if ( $lastTextId == $row->bt_text_id ) {
					// Duplicate (null edit)
					continue;
				}
				$lastTextId = $row->bt_text_id;
				// Load the text
				$text = Revision::getRevisionText( $row );
				if ( $text === false ) {
					$this->critical( "Error loading {$row->bt_rev_id}/{$row->bt_text_id}" );
					continue;
				}

				// Queue it
				if ( !$trx->addItem( $text, $row->bt_text_id ) ) {
					$this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
					$trx->commit();
					$trx = new CgzCopyTransaction( $this, $this->pageBlobClass );
					$this->waitForSlaves();
				}
			}
			$startId = $row->bt_text_id;
		}

		$this->debug( "$titleText: committing blob with " . $trx->getSize() . " items" );
		$trx->commit();
	}

	/**
	 * Atomic move operation.
	 *
	 * Write the new URL to the text table and set the bt_moved flag.
	 *
	 * This is done in a single transaction to provide restartable behaviour
	 * without data loss.
	 *
	 * The transaction is kept short to reduce locking.
	 */
	function moveTextRow( $textId, $url ) {
		if ( $this->copyOnly ) {
			$this->critical( "Internal error: can't call moveTextRow() in --copy-only mode" );
			exit( 1 );
		}
		$dbw = wfGetDB( DB_MASTER );
		$dbw->begin( __METHOD__ );
		$dbw->update( 'text',
			array( // set
				'old_text' => $url,
				'old_flags' => 'external,utf-8',
			),
			array( // where
				'old_id' => $textId
			),
			__METHOD__
		);
		$dbw->update( 'blob_tracking',
			array( 'bt_moved' => 1 ),
			array( 'bt_text_id' => $textId ),
			__METHOD__
		);
		$dbw->commit( __METHOD__ );
	}

	/**
	 * Moves are done in two phases: bt_new_url and then bt_moved.
	 *  - bt_new_url indicates that the text has been copied to the new cluster.
	 *  - bt_moved indicates that the text table has been updated.
	 *
	 * This function completes any moves that only have done bt_new_url. This
	 * can happen when the script is interrupted, or when --copy-only is used.
	 */
	function finishIncompleteMoves( $conds ) {
		$dbr = wfGetDB( DB_SLAVE );

		$startId = 0;
		$conds = array_merge( $conds, array(
			'bt_moved' => 0,
			'bt_new_url IS NOT NULL'
		) );
		while ( true ) {
			$res = $dbr->select( 'blob_tracking',
				'*',
				array_merge( $conds, array( 'bt_text_id > ' . $dbr->addQuotes( $startId ) ) ),
				__METHOD__,
				array(
					'ORDER BY' => 'bt_text_id',
					'LIMIT' => $this->batchSize,
				)
			);
			if ( !$res->numRows() ) {
				break;
			}
			$this->debug( 'Incomplete: ' . $res->numRows() . ' rows' );
			foreach ( $res as $row ) {
				$this->moveTextRow( $row->bt_text_id, $row->bt_new_url );
				if ( $row->bt_text_id % 10 == 0 ) {
					$this->waitForSlaves();
				}
			}
			$startId = $row->bt_text_id;
		}
	}

	/**
	 * Returns the name of the next target cluster
	 * @return string
	 */
	function getTargetCluster() {
		$cluster = next( $this->destClusters );
		if ( $cluster === false ) {
			$cluster = reset( $this->destClusters );
		}
		return $cluster;
	}

	/**
	 * Gets a DB master connection for the given external cluster name
	 * @param $cluster string
	 * @return DatabaseBase
	 */
	function getExtDB( $cluster ) {
		$lb = wfGetLBFactory()->getExternalLB( $cluster );
		return $lb->getConnection( DB_MASTER );
	}

	/**
	 * Move an orphan text_id to the new cluster
	 */
	function doOrphanList( $textIds ) {
		// Finish incomplete moves
		if ( !$this->copyOnly ) {
			$this->finishIncompleteMoves( array( 'bt_text_id' => $textIds ) );
			$this->syncDBs();
		}

		$trx = new CgzCopyTransaction( $this, $this->orphanBlobClass );

		$res = wfGetDB( DB_SLAVE )->select(
			array( 'text', 'blob_tracking' ),
			array( 'old_id', 'old_text', 'old_flags' ),
			array(
				'old_id' => $textIds,
				'bt_text_id=old_id',
				'bt_moved' => 0,
			),
			__METHOD__,
			array( 'DISTINCT' )
		);

		foreach ( $res as $row ) {
			$text = Revision::getRevisionText( $row );
			if ( $text === false ) {
				$this->critical( "Error: cannot load revision text for old_id={$row->old_id}" );
				continue;
			}

			if ( !$trx->addItem( $text, $row->old_id ) ) {
				$this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
				$trx->commit();
				$trx = new CgzCopyTransaction( $this, $this->orphanBlobClass );
				$this->waitForSlaves();
			}
		}
		$this->debug( "[orphan]: committing blob with " . $trx->getSize() . " rows" );
		$trx->commit();
	}

	/**
	 * Wait for slaves (quietly)
	 */
	function waitForSlaves() {
		$lb = wfGetLB();
		while ( true ) {
			list( $host, $maxLag ) = $lb->getMaxLag();
			if ( $maxLag < 2 ) {
				break;
			}
			sleep( 5 );
		}
	}
}

/**
 * Class to represent a recompression operation for a single CGZ blob
 */
class CgzCopyTransaction {
	var $parent;
	var $blobClass;
	var $cgz;
	var $referrers;

	/**
	 * Create a transaction from a RecompressTracked object
	 */
	function __construct( $parent, $blobClass ) {
		$this->blobClass = $blobClass;
		$this->cgz = false;
		$this->texts = array();
		$this->parent = $parent;
	}

	/**
	 * Add text.
	 * Returns false if it's ready to commit.
	 * @param $text string
	 * @param $textId
	 * @return bool
	 */
	function addItem( $text, $textId ) {
		if ( !$this->cgz ) {
			$class = $this->blobClass;
			$this->cgz = new $class;
		}
		$hash = $this->cgz->addItem( $text );
		$this->referrers[$textId] = $hash;
		$this->texts[$textId] = $text;
		return $this->cgz->isHappy();
	}

	function getSize() {
		return count( $this->texts );
	}

	/**
	 * Recompress text after some aberrant modification
	 */
	function recompress() {
		$class = $this->blobClass;
		$this->cgz = new $class;
		$this->referrers = array();
		foreach ( $this->texts as $textId => $text ) {
			$hash = $this->cgz->addItem( $text );
			$this->referrers[$textId] = $hash;
		}
	}

	/**
	 * Commit the blob.
	 * Does nothing if no text items have been added.
	 * May skip the move if --copy-only is set.
	 */
	function commit() {
		$originalCount = count( $this->texts );
		if ( !$originalCount ) {
			return;
		}

		// Check to see if the target text_ids have been moved already.
		//
		// We originally read from the slave, so this can happen when a single
		// text_id is shared between multiple pages. It's rare, but possible
		// if a delete/move/undelete cycle splits up a null edit.
		//
		// We do a locking read to prevent closer-run race conditions.
		$dbw = wfGetDB( DB_MASTER );
		$dbw->begin( __METHOD__ );
		$res = $dbw->select( 'blob_tracking',
			array( 'bt_text_id', 'bt_moved' ),
			array( 'bt_text_id' => array_keys( $this->referrers ) ),
			__METHOD__, array( 'FOR UPDATE' ) );
		$dirty = false;
		foreach ( $res as $row ) {
			if ( $row->bt_moved ) {
				# This row has already been moved, remove it
				$this->parent->debug( "TRX: conflict detected in old_id={$row->bt_text_id}" );
				unset( $this->texts[$row->bt_text_id] );
				$dirty = true;
			}
		}

		// Recompress the blob if necessary
		if ( $dirty ) {
			if ( !count( $this->texts ) ) {
				// All have been moved already
				if ( $originalCount > 1 ) {
					// This is suspcious, make noise
					$this->critical( "Warning: concurrent operation detected, are there two conflicting " .
						"processes running, doing the same job?" );
				}
				return;
			}
			$this->recompress();
		}

		// Insert the data into the destination cluster
		$targetCluster = $this->parent->getTargetCluster();
		$store = $this->parent->store;
		$targetDB = $store->getMaster( $targetCluster );
		$targetDB->clearFlag( DBO_TRX ); // we manage the transactions
		$targetDB->begin( __METHOD__ );
		$baseUrl = $this->parent->store->store( $targetCluster, serialize( $this->cgz ) );

		// Write the new URLs to the blob_tracking table
		foreach ( $this->referrers as $textId => $hash ) {
			$url = $baseUrl . '/' . $hash;
			$dbw->update( 'blob_tracking',
				array( 'bt_new_url' => $url ),
				array(
					'bt_text_id' => $textId,
					'bt_moved' => 0, # Check for concurrent conflicting update
				),
				__METHOD__
			);
		}

		$targetDB->commit( __METHOD__ );
		// Critical section here: interruption at this point causes blob duplication
		// Reversing the order of the commits would cause data loss instead
		$dbw->commit( __METHOD__ );

		// Write the new URLs to the text table and set the moved flag
		if ( !$this->parent->copyOnly ) {
			foreach ( $this->referrers as $textId => $hash ) {
				$url = $baseUrl . '/' . $hash;
				$this->parent->moveTextRow( $textId, $url );
			}
		}
	}
}