42 parent::__construct();
44 $this->
addArg(
'cluster',
'cluster(s) to scan',
true,
true );
47 'Adds blobs from a given ES cluster to the blob_tracking table. ' .
48 'Automatically deletes the tracking table and starts from the start again when restarted.'
53 $this->clusters = $this->parameters->getArgs();
54 if ( extension_loaded(
'gmp' ) ) {
55 $this->doBlobOrphans =
true;
56 foreach ( $this->clusters as $cluster ) {
57 $this->trackedBlobs[$cluster] = gmp_init( 0 );
60 echo
"Warning: the gmp extension is needed to find orphan blobs\n";
63 $this->checkIntegrity();
64 $this->initTrackingTable();
65 $this->trackRevisions();
66 $this->trackOrphanText();
67 if ( $this->doBlobOrphans ) {
68 $this->findOrphanBlobs();
70 $this->
output(
"All done.\n" );
73 private function checkIntegrity() {
74 echo
"Doing integrity check...\n";
79 $exists = (bool)$dbr->newSelectQueryBuilder()
83 'old_flags LIKE \'%object%\' AND old_flags NOT LIKE \'%external%\' ' .
84 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'' )
85 ->caller( __METHOD__ )->fetchField();
88 echo
"Integrity check failed: found HistoryBlobStub objects in your text table.\n" .
89 "This script could destroy these objects if it continued. Run resolveStubs.php\n" .
94 echo
"Integrity check OK\n";
97 private function initTrackingTable() {
99 if ( $dbw->tableExists(
'blob_tracking', __METHOD__ ) ) {
100 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_tracking' ), __METHOD__ );
101 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_orphans' ), __METHOD__ );
103 $dbw->sourceFile( __DIR__ .
'/blob_tracking.sql' );
106 private function getTextClause() {
107 if ( !$this->textClause ) {
110 foreach ( $this->clusters as $cluster ) {
111 $conds[] = $dbr->expr(
114 new LikeValue(
"DB://$cluster/", $dbr->anyString() )
123 private function interpretPointer( $text ) {
124 if ( !preg_match(
'!^DB://(\w+)/(\d+)(?:/([0-9a-fA-F]+)|)$!', $text, $m ) ) {
130 'id' => intval( $m[2] ),
131 'hash' => $m[3] ?? null
138 private function trackRevisions() {
144 $endId = (int)$dbr->newSelectQueryBuilder()
145 ->select(
'MAX(rev_id)' )
147 ->caller( __METHOD__ )->fetchField();
151 echo
"Finding revisions...\n";
158 new LikeValue( $dbr->anyString(),
'external', $dbr->anyString() )
163 $conds = array_merge( [
164 'slot_role_id=' . $slotRoleStore->getId( SlotRecord::MAIN ),
165 'SUBSTRING(content_address, 1, 3)=' . $dbr->addQuotes(
'tt:' ),
169 $res = $dbr->newSelectQueryBuilder()
170 ->select( [
'rev_id',
'rev_page',
'old_id',
'old_flags',
'old_text' ] )
172 ->join(
'slots',
null,
'rev_id=slot_revision_id' )
173 ->join(
'content',
null,
'content_id=slot_content_id' )
174 ->join(
'text',
null,
'SUBSTRING(content_address, 4)=old_id' )
175 ->where( $dbr->expr(
'rev_id',
'>', $startId ) )
177 ->orderBy(
'rev_id' )
178 ->limit( $this->batchSize )
179 ->caller( __METHOD__ )->fetchResultSet();
180 if ( !$res->numRows() ) {
185 foreach ( $res as $row ) {
186 $startId = (int)$row->rev_id;
187 $info = $this->interpretPointer( $row->old_text );
189 echo
"Invalid DB:// URL in rev_id {$row->rev_id}\n";
192 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
193 echo
"Invalid cluster returned in SQL query: {$info['cluster']}\n";
197 'bt_page' => $row->rev_page,
198 'bt_rev_id' => $row->rev_id,
199 'bt_text_id' => $row->old_id,
200 'bt_cluster' => $info[
'cluster'],
201 'bt_blob_id' => $info[
'id'],
202 'bt_cgz_hash' => $info[
'hash']
204 if ( $this->doBlobOrphans ) {
205 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
208 $dbw->newInsertQueryBuilder()
209 ->insertInto(
'blob_tracking' )
210 ->rows( $insertBatch )
211 ->caller( __METHOD__ )->execute();
212 $rowsInserted += count( $insertBatch );
215 if ( $batchesDone >= $this->reportingInterval ) {
217 echo
"$startId / $endId\n";
221 echo
"Found $rowsInserted revisions\n";
229 private function trackOrphanText() {
230 # Wait until the blob_tracking table is available in the replica DB
233 $this->
getServiceContainer()->getDBLoadBalancerFactory()->waitForReplication( [
'timeout' => 100_000 ] );
237 $endId = (int)$dbr->newSelectQueryBuilder()
238 ->select(
'MAX(old_id)' )
240 ->caller( __METHOD__ )->fetchField();
244 echo
"Finding orphan text...\n";
246 # Scan the text table for orphan text
248 $res = $dbr->newSelectQueryBuilder()
249 ->select( [
'old_id',
'old_flags',
'old_text' ] )
251 ->leftJoin(
'blob_tracking',
null,
'bt_text_id=old_id' )
253 $dbr->expr(
'old_id',
'>', $startId ),
258 new LikeValue( $dbr->anyString(),
'external', $dbr->anyString() )
260 'bt_text_id' =>
null,
262 ->orderBy(
'old_id' )
263 ->limit( $this->batchSize )
264 ->caller( __METHOD__ )->fetchResultSet();
266 if ( !$res->numRows() ) {
271 foreach ( $res as $row ) {
272 $startId = (int)$row->old_id;
273 $info = $this->interpretPointer( $row->old_text );
275 echo
"Invalid DB:// URL in old_id {$row->old_id}\n";
278 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
279 echo
"Invalid cluster returned in SQL query\n";
286 'bt_text_id' => $row->old_id,
287 'bt_cluster' => $info[
'cluster'],
288 'bt_blob_id' => $info[
'id'],
289 'bt_cgz_hash' => $info[
'hash']
291 if ( $this->doBlobOrphans ) {
292 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
295 $dbw->newInsertQueryBuilder()
296 ->insertInto(
'blob_tracking' )
297 ->rows( $insertBatch )
298 ->caller( __METHOD__ )->execute();
300 $rowsInserted += count( $insertBatch );
302 if ( $batchesDone >= $this->reportingInterval ) {
304 echo
"$startId / $endId\n";
308 echo
"Found $rowsInserted orphan text rows\n";
318 private function findOrphanBlobs() {
319 if ( !extension_loaded(
'gmp' ) ) {
320 echo
"Can't find orphan blobs, need bitfield support provided by GMP.\n";
328 foreach ( $this->clusters as $cluster ) {
329 echo
"Searching for orphan blobs in $cluster...\n";
330 $lb = $lbFactory->getExternalLB( $cluster );
332 $extDB = $lb->getMaintenanceConnectionRef(
DB_REPLICA );
334 if ( strpos( $e->getMessage(),
'Unknown database' ) !==
false ) {
335 echo
"No database on $cluster\n";
337 echo
"Error on $cluster: " . $e->getMessage() .
"\n";
341 $table = $extDB->getLBInfo(
'blobs table' ) ??
'blobs';
342 if ( !$extDB->tableExists( $table, __METHOD__ ) ) {
343 echo
"No blobs table on cluster $cluster\n";
348 $actualBlobs = gmp_init( 0 );
349 $endId = (int)$extDB->newSelectQueryBuilder()
350 ->select(
'MAX(blob_id)' )
352 ->caller( __METHOD__ )->fetchField();
356 $res = $extDB->newSelectQueryBuilder()
357 ->select( [
'blob_id' ] )
359 ->where( $extDB->expr(
'blob_id',
'>', $startId ) )
360 ->orderBy(
'blob_id' )
361 ->limit( $this->batchSize )
362 ->caller( __METHOD__ )->fetchResultSet();
364 if ( !$res->numRows() ) {
368 foreach ( $res as $row ) {
369 gmp_setbit( $actualBlobs, $row->blob_id );
370 $startId = (int)$row->blob_id;
374 if ( $batchesDone >= $this->reportingInterval ) {
376 echo
"$startId / $endId\n";
382 $orphans = gmp_and( $actualBlobs, gmp_com( $this->trackedBlobs[$cluster] ) );
389 $id = gmp_scan1( $orphans, $id );
394 'bo_cluster' => $cluster,
397 if ( count( $insertBatch ) > $this->batchSize ) {
398 $dbw->newInsertQueryBuilder()
399 ->insertInto(
'blob_orphans' )
400 ->rows( $insertBatch )
401 ->caller( __METHOD__ )->execute();
408 if ( $insertBatch ) {
409 $dbw->newInsertQueryBuilder()
410 ->insertInto(
'blob_orphans' )
411 ->rows( $insertBatch )
412 ->caller( __METHOD__ )->execute();
414 echo
"Found $numOrphans orphan(s) in $cluster\n";
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.