27 require __DIR__ .
'/../commandLine.inc';
30 echo
"Usage: php trackBlobs.php <cluster> [... <cluster>]\n";
31 echo
"Adds blobs from a given ES cluster to the blob_tracking table\n";
32 echo
"Automatically deletes the tracking table and starts from the start again when restarted.\n";
50 if ( extension_loaded(
'gmp' ) ) {
51 $this->doBlobOrphans =
true;
53 $this->trackedBlobs[$cluster] = gmp_init( 0 );
56 echo
"Warning: the gmp extension is needed to find orphan blobs\n";
65 if ( $this->doBlobOrphans ) {
71 echo
"Doing integrity check...\n";
76 $exists =
$dbr->selectField(
'text', 1,
77 'old_flags LIKE \'%object%\' AND old_flags NOT LIKE \'%external%\' ' .
78 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'',
83 echo
"Integrity check failed: found HistoryBlobStub objects in your text table.\n" .
84 "This script could destroy these objects if it continued. Run resolveStubs.php\n" .
90 $flags =
$dbr->selectField(
'archive',
'ar_flags',
91 'ar_flags LIKE \'%external%\' OR (' .
92 'ar_flags LIKE \'%object%\' ' .
93 'AND LOWER(CONVERT(LEFT(ar_text,22) USING latin1)) = \'o:15:"historyblobstub"\' )',
97 if ( strpos(
$flags,
'external' ) !==
false ) {
98 echo
"Integrity check failed: found external storage pointers in your archive table.\n" .
99 "Run normaliseArchiveTable.php to fix this.\n";
102 echo
"Integrity check failed: found HistoryBlobStub objects in your archive table.\n" .
103 "These objects are probably already broken, continuing would make them\n" .
104 "unrecoverable. Run \"normaliseArchiveTable.php --fix-cgz-bug\" to fix this.\n";
108 echo
"Integrity check OK\n";
113 if ( $dbw->tableExists(
'blob_tracking' ) ) {
114 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_tracking' ) );
115 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_orphans' ) );
117 $dbw->sourceFile( __DIR__ .
'/blob_tracking.sql' );
121 if ( !$this->textClause ) {
123 $this->textClause =
'';
124 foreach ( $this->clusters
as $cluster ) {
125 if ( $this->textClause !=
'' ) {
126 $this->textClause .=
' OR ';
128 $this->textClause .=
'old_text' .
$dbr->buildLike(
"DB://$cluster/",
$dbr->anyString() );
136 if ( !preg_match(
'!^DB://(\w+)/(\d+)(?:/([0-9a-fA-F]+)|)$!', $text, $m ) ) {
142 'id' => intval( $m[2] ),
143 'hash' => isset( $m[3] ) ? $m[3] : null
156 $endId =
$dbr->selectField(
'revision',
'MAX(rev_id)',
false, __METHOD__ );
160 echo
"Finding revisions...\n";
163 $res =
$dbr->select( [
'revision',
'text' ],
164 [
'rev_id',
'rev_page',
'old_id',
'old_flags',
'old_text' ],
166 'rev_id > ' .
$dbr->addQuotes( $startId ),
167 'rev_text_id=old_id',
169 'old_flags ' .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() ),
173 'ORDER BY' =>
'rev_id',
174 'LIMIT' => $this->batchSize
177 if ( !
$res->numRows() ) {
182 foreach (
$res as $row ) {
183 $startId = $row->rev_id;
186 echo
"Invalid DB:// URL in rev_id {$row->rev_id}\n";
189 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
190 echo
"Invalid cluster returned in SQL query: {$info['cluster']}\n";
194 'bt_page' => $row->rev_page,
195 'bt_rev_id' => $row->rev_id,
196 'bt_text_id' => $row->old_id,
197 'bt_cluster' => $info[
'cluster'],
198 'bt_blob_id' => $info[
'id'],
199 'bt_cgz_hash' => $info[
'hash']
201 if ( $this->doBlobOrphans ) {
202 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
205 $dbw->insert(
'blob_tracking', $insertBatch, __METHOD__ );
206 $rowsInserted +=
count( $insertBatch );
209 if ( $batchesDone >= $this->reportingInterval ) {
211 echo
"$startId / $endId\n";
215 echo
"Found $rowsInserted revisions\n";
224 # Wait until the blob_tracking table is available in the replica DB
227 $pos = $dbw->getMasterPos();
228 $dbr->masterPosWait( $pos, 100000 );
232 $endId =
$dbr->selectField(
'text',
'MAX(old_id)',
false, __METHOD__ );
236 echo
"Finding orphan text...\n";
238 # Scan the text table for orphan text
240 $res =
$dbr->select( [
'text',
'blob_tracking' ],
241 [
'old_id',
'old_flags',
'old_text' ],
243 'old_id>' .
$dbr->addQuotes( $startId ),
245 'old_flags ' .
$dbr->buildLike(
$dbr->anyString(),
'external',
$dbr->anyString() ),
250 'ORDER BY' =>
'old_id',
251 'LIMIT' => $this->batchSize
253 [
'blob_tracking' => [
'LEFT JOIN',
'bt_text_id=old_id' ] ]
256 foreach (
$res as $row ) {
257 $ids[] = $row->old_id;
260 if ( !
$res->numRows() ) {
265 foreach (
$res as $row ) {
266 $startId = $row->old_id;
269 echo
"Invalid DB:// URL in old_id {$row->old_id}\n";
272 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
273 echo
"Invalid cluster returned in SQL query\n";
280 'bt_text_id' => $row->old_id,
281 'bt_cluster' => $info[
'cluster'],
282 'bt_blob_id' => $info[
'id'],
283 'bt_cgz_hash' => $info[
'hash']
285 if ( $this->doBlobOrphans ) {
286 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
289 $dbw->insert(
'blob_tracking', $insertBatch, __METHOD__ );
291 $rowsInserted +=
count( $insertBatch );
293 if ( $batchesDone >= $this->reportingInterval ) {
295 echo
"$startId / $endId\n";
299 echo
"Found $rowsInserted orphan text rows\n";
310 if ( !extension_loaded(
'gmp' ) ) {
311 echo
"Can't find orphan blobs, need bitfield support provided by GMP.\n";
318 foreach ( $this->clusters
as $cluster ) {
319 echo
"Searching for orphan blobs in $cluster...\n";
324 if ( strpos(
$e->error,
'Unknown database' ) !==
false ) {
325 echo
"No database on $cluster\n";
327 echo
"Error on $cluster: " .
$e->getMessage() .
"\n";
331 $table = $extDB->getLBInfo(
'blobs table' );
332 if ( is_null( $table ) ) {
335 if ( !$extDB->tableExists( $table ) ) {
336 echo
"No blobs table on cluster $cluster\n";
341 $actualBlobs = gmp_init( 0 );
342 $endId = $extDB->selectField( $table,
'MAX(blob_id)',
false, __METHOD__ );
346 $res = $extDB->select( $table,
348 [
'blob_id > ' . $extDB->addQuotes( $startId ) ],
353 if ( !
$res->numRows() ) {
357 foreach (
$res as $row ) {
358 gmp_setbit( $actualBlobs, $row->blob_id );
360 $startId = $row->blob_id;
363 if ( $batchesDone >= $this->reportingInterval ) {
365 echo
"$startId / $endId\n";
371 $orphans = gmp_and( $actualBlobs, gmp_com( $this->trackedBlobs[$cluster] ) );
378 $id = gmp_scan1( $orphans, $id );
383 'bo_cluster' => $cluster,
386 if (
count( $insertBatch ) > $this->batchSize ) {
387 $dbw->insert(
'blob_orphans', $insertBatch, __METHOD__ );
394 if ( $insertBatch ) {
395 $dbw->insert(
'blob_orphans', $insertBatch, __METHOD__ );
397 echo
"Found $numOrphans orphan(s) in $cluster\n";