36 parent::__construct();
38 $this->
addArg(
'cluster',
'cluster(s) to scan',
true,
true );
41 'Adds blobs from a given ES cluster to the blob_tracking table. ' .
42 'Automatically deletes the tracking table and starts from the start again when restarted.'
47 $this->clusters = $this->parameters->getArgs();
48 if ( extension_loaded(
'gmp' ) ) {
49 $this->doBlobOrphans =
true;
50 foreach ( $this->clusters as $cluster ) {
51 $this->trackedBlobs[$cluster] = gmp_init( 0 );
54 echo
"Warning: the gmp extension is needed to find orphan blobs\n";
57 $this->checkIntegrity();
58 $this->initTrackingTable();
59 $this->trackRevisions();
60 $this->trackOrphanText();
61 if ( $this->doBlobOrphans ) {
62 $this->findOrphanBlobs();
64 $this->
output(
"All done.\n" );
67 private function checkIntegrity() {
68 echo
"Doing integrity check...\n";
73 $exists = (bool)$dbr->newSelectQueryBuilder()
77 'old_flags LIKE \'%object%\' AND old_flags NOT LIKE \'%external%\' ' .
78 'AND LOWER(CONVERT(LEFT(old_text,22) USING latin1)) = \'o:15:"historyblobstub"\'' )
79 ->caller( __METHOD__ )->fetchField();
82 echo
"Integrity check failed: found HistoryBlobStub objects in your text table.\n" .
83 "This script could destroy these objects if it continued. Run resolveStubs.php\n" .
88 echo
"Integrity check OK\n";
91 private function initTrackingTable() {
93 if ( $dbw->tableExists(
'blob_tracking', __METHOD__ ) ) {
94 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_tracking' ), __METHOD__ );
95 $dbw->query(
'DROP TABLE ' . $dbw->tableName(
'blob_orphans' ), __METHOD__ );
97 $dbw->sourceFile( __DIR__ .
'/blob_tracking.sql' );
101 if ( !$this->textClause ) {
104 foreach ( $this->clusters as $cluster ) {
105 $conds[] = $dbr->expr(
108 new LikeValue(
"DB://$cluster/", $dbr->anyString() )
111 $this->textClause = $dbr->orExpr( $conds );
118 private function interpretPointer(
string $text ) {
119 if ( !preg_match(
'!^DB://(\w+)/(\d+)(?:/([0-9a-fA-F]+)|)$!', $text, $m ) ) {
125 'id' => intval( $m[2] ),
126 'hash' => $m[3] ?? null
133 private function trackRevisions() {
134 $dbw = $this->getPrimaryDB();
135 $dbr = $this->getReplicaDB();
137 $textClause = $this->getTextClause();
139 $endId = (int)$dbr->newSelectQueryBuilder()
140 ->select(
'MAX(rev_id)' )
142 ->caller( __METHOD__ )->fetchField();
146 echo
"Finding revisions...\n";
153 new LikeValue( $dbr->anyString(),
'external', $dbr->anyString() )
156 $slotRoleStore = $this->getServiceContainer()->getSlotRoleStore();
158 $conds = array_merge( [
159 'slot_role_id' => $slotRoleStore->getId( SlotRecord::MAIN ),
160 'SUBSTRING(content_address, 1, 3)=' . $dbr->addQuotes(
'tt:' ),
164 $res = $dbr->newSelectQueryBuilder()
165 ->select( [
'rev_id',
'rev_page',
'old_id',
'old_flags',
'old_text' ] )
167 ->join(
'slots',
null,
'rev_id=slot_revision_id' )
168 ->join(
'content',
null,
'content_id=slot_content_id' )
169 ->join(
'text',
null,
'SUBSTRING(content_address, 4)=old_id' )
170 ->where( $dbr->expr(
'rev_id',
'>', $startId ) )
172 ->orderBy(
'rev_id' )
173 ->limit( $this->batchSize )
174 ->caller( __METHOD__ )->fetchResultSet();
175 if ( !$res->numRows() ) {
180 foreach ( $res as $row ) {
181 $startId = (int)$row->rev_id;
182 $info = $this->interpretPointer( $row->old_text );
184 echo
"Invalid DB:// URL in rev_id {$row->rev_id}\n";
187 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
188 echo
"Invalid cluster returned in SQL query: {$info['cluster']}\n";
192 'bt_page' => $row->rev_page,
193 'bt_rev_id' => $row->rev_id,
194 'bt_text_id' => $row->old_id,
195 'bt_cluster' => $info[
'cluster'],
196 'bt_blob_id' => $info[
'id'],
197 'bt_cgz_hash' => $info[
'hash']
199 if ( $this->doBlobOrphans ) {
200 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
203 $dbw->newInsertQueryBuilder()
204 ->insertInto(
'blob_tracking' )
205 ->rows( $insertBatch )
206 ->caller( __METHOD__ )->execute();
207 $rowsInserted += count( $insertBatch );
210 if ( $batchesDone >= $this->reportingInterval ) {
212 echo
"$startId / $endId\n";
213 $this->waitForReplication();
216 echo
"Found $rowsInserted revisions\n";
224 private function trackOrphanText() {
225 # Wait until the blob_tracking table is available in the replica DB
226 $dbw = $this->getPrimaryDB();
227 $dbr = $this->getReplicaDB();
228 $this->getServiceContainer()->getDBLoadBalancerFactory()->waitForReplication( [
'timeout' => 100_000 ] );
230 $textClause = $this->getTextClause();
232 $endId = (int)$dbr->newSelectQueryBuilder()
233 ->select(
'MAX(old_id)' )
235 ->caller( __METHOD__ )->fetchField();
239 echo
"Finding orphan text...\n";
241 # Scan the text table for orphan text
243 $res = $dbr->newSelectQueryBuilder()
244 ->select( [
'old_id',
'old_flags',
'old_text' ] )
246 ->leftJoin(
'blob_tracking',
null,
'bt_text_id=old_id' )
248 $dbr->expr(
'old_id',
'>', $startId ),
253 new LikeValue( $dbr->anyString(),
'external', $dbr->anyString() )
255 'bt_text_id' =>
null,
257 ->orderBy(
'old_id' )
258 ->limit( $this->batchSize )
259 ->caller( __METHOD__ )->fetchResultSet();
261 if ( !$res->numRows() ) {
266 foreach ( $res as $row ) {
267 $startId = (int)$row->old_id;
268 $info = $this->interpretPointer( $row->old_text );
270 echo
"Invalid DB:// URL in old_id {$row->old_id}\n";
273 if ( !in_array( $info[
'cluster'], $this->clusters ) ) {
274 echo
"Invalid cluster returned in SQL query\n";
281 'bt_text_id' => $row->old_id,
282 'bt_cluster' => $info[
'cluster'],
283 'bt_blob_id' => $info[
'id'],
284 'bt_cgz_hash' => $info[
'hash']
286 if ( $this->doBlobOrphans ) {
287 gmp_setbit( $this->trackedBlobs[$info[
'cluster']], $info[
'id'] );
290 $dbw->newInsertQueryBuilder()
291 ->insertInto(
'blob_tracking' )
292 ->rows( $insertBatch )
293 ->caller( __METHOD__ )->execute();
295 $rowsInserted += count( $insertBatch );
297 if ( $batchesDone >= $this->reportingInterval ) {
299 echo
"$startId / $endId\n";
300 $this->waitForReplication();
303 echo
"Found $rowsInserted orphan text rows\n";
313 private function findOrphanBlobs() {
314 if ( !extension_loaded(
'gmp' ) ) {
315 echo
"Can't find orphan blobs, need bitfield support provided by GMP.\n";
320 $dbw = $this->getPrimaryDB();
321 $lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory();
322 $dbStore = $this->getServiceContainer()->getExternalStoreFactory()->getDatabaseStore();
324 foreach ( $this->clusters as $cluster ) {
325 echo
"Searching for orphan blobs in $cluster...\n";
326 $lb = $lbFactory->getExternalLB( $cluster );
328 $extDB = $lb->getMaintenanceConnectionRef(
DB_REPLICA );
330 if ( str_contains( $e->getMessage(),
'Unknown database' ) ) {
331 echo
"No database on $cluster\n";
333 echo
"Error on $cluster: " . $e->getMessage() .
"\n";
337 $table = $dbStore->getTable( $cluster );
338 if ( !$extDB->tableExists( $table, __METHOD__ ) ) {
339 echo
"No blobs table on cluster $cluster\n";
344 $actualBlobs = gmp_init( 0 );
345 $endId = (int)$extDB->newSelectQueryBuilder()
346 ->select(
'MAX(blob_id)' )
348 ->caller( __METHOD__ )->fetchField();
352 $res = $extDB->newSelectQueryBuilder()
353 ->select( [
'blob_id' ] )
355 ->where( $extDB->expr(
'blob_id',
'>', $startId ) )
356 ->orderBy(
'blob_id' )
357 ->limit( $this->batchSize )
358 ->caller( __METHOD__ )->fetchResultSet();
360 if ( !$res->numRows() ) {
364 foreach ( $res as $row ) {
365 gmp_setbit( $actualBlobs, $row->blob_id );
366 $startId = (int)$row->blob_id;
370 if ( $batchesDone >= $this->reportingInterval ) {
372 echo
"$startId / $endId\n";
378 $orphans = gmp_and( $actualBlobs, gmp_com( $this->trackedBlobs[$cluster] ) );
385 $id = gmp_scan1( $orphans, $id );
390 'bo_cluster' => $cluster,
393 if ( count( $insertBatch ) > $this->batchSize ) {
394 $dbw->newInsertQueryBuilder()
395 ->insertInto(
'blob_orphans' )
396 ->rows( $insertBatch )
397 ->caller( __METHOD__ )->execute();
404 if ( $insertBatch ) {
405 $dbw->newInsertQueryBuilder()
406 ->insertInto(
'blob_orphans' )
407 ->rows( $insertBatch )
408 ->caller( __METHOD__ )->execute();
410 echo
"Found $numOrphans orphan(s) in $cluster\n";