3 require( dirname( __FILE__
) .'/../commandLine.inc' );
6 if ( count( $args ) < 1 ) {
7 echo "Usage: php trackBlobs.php <cluster> [... <cluster>]\n";
8 echo "Adds blobs from a given ES cluster to the blob_tracking table\n";
9 echo "Automatically deletes the tracking table and starts from the start again when restarted.\n";
13 $tracker = new TrackBlobs( $args );
18 var $clusters, $textClause;
20 var $trackedBlobs = array();
22 var $batchSize = 1000;
23 var $reportingInterval = 10;
25 function __construct( $clusters ) {
26 $this->clusters
= $clusters;
27 if ( extension_loaded( 'gmp' ) ) {
28 $this->doBlobOrphans
= true;
29 foreach ( $clusters as $cluster ) {
30 $this->trackedBlobs
[$cluster] = gmp_init( 0 );
33 echo "Warning: the gmp extension is needed to find orphan blobs\n";
38 $this->initTrackingTable();
39 $this->trackRevisions();
40 $this->trackOrphanText();
41 if ( $this->doBlobOrphans
) {
42 $this->findOrphanBlobs();
46 function initTrackingTable() {
47 $dbw = wfGetDB( DB_MASTER
);
48 if ( $dbw->tableExists( 'blob_tracking' ) ) {
49 $dbw->query( 'DROP TABLE ' . $dbw->tableName( 'blob_tracking' ) );
50 $dbw->query( 'DROP TABLE ' . $dbw->tableName( 'blob_orphans' ) );
52 $dbw->sourceFile( dirname( __FILE__
) . '/blob_tracking.sql' );
55 function getTextClause() {
56 if ( !$this->textClause
) {
57 $dbr = wfGetDB( DB_SLAVE
);
58 $this->textClause
= '';
59 foreach ( $this->clusters
as $cluster ) {
60 if ( $this->textClause
!= '' ) {
61 $this->textClause
.= ' OR ';
63 $this->textClause
.= 'old_text' . $dbr->buildLike( "DB://$cluster/", $dbr->anyString() );
66 return $this->textClause
;
69 function interpretPointer( $text ) {
70 if ( !preg_match( '!^DB://(\w+)/(\d+)(?:/([0-9a-fA-F]+)|)$!', $text, $m ) ) {
75 'id' => intval( $m[2] ),
76 'hash' => isset( $m[3] ) ?
$m[2] : null
81 * Scan the revision table for rows stored in the specified clusters
83 function trackRevisions() {
84 $dbw = wfGetDB( DB_MASTER
);
85 $dbr = wfGetDB( DB_SLAVE
);
87 $textClause = $this->getTextClause();
89 $endId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__
);
93 echo "Finding revisions...\n";
96 $res = $dbr->select( array( 'revision', 'text' ),
97 array( 'rev_id', 'rev_page', 'old_id', 'old_flags', 'old_text' ),
99 'rev_id > ' . $dbr->addQuotes( $startId ),
100 'rev_text_id=old_id',
102 'old_flags ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
106 'ORDER BY' => 'rev_id',
107 'LIMIT' => $this->batchSize
110 if ( !$res->numRows() ) {
114 $insertBatch = array();
115 foreach ( $res as $row ) {
116 $startId = $row->rev_id
;
117 $info = $this->interpretPointer( $row->old_text
);
119 echo "Invalid DB:// URL in rev_id {$row->rev_id}\n";
122 if ( !in_array( $info['cluster'], $this->clusters
) ) {
123 echo "Invalid cluster returned in SQL query: {$info['cluster']}\n";
126 $insertBatch[] = array(
127 'bt_page' => $row->rev_page
,
128 'bt_rev_id' => $row->rev_id
,
129 'bt_text_id' => $row->old_id
,
130 'bt_cluster' => $info['cluster'],
131 'bt_blob_id' => $info['id'],
132 'bt_cgz_hash' => $info['hash']
134 if ( $this->doBlobOrphans
) {
135 gmp_setbit( $this->trackedBlobs
[$info['cluster']], $info['id'] );
138 $dbw->insert( 'blob_tracking', $insertBatch, __METHOD__
);
139 $rowsInserted +
= count( $insertBatch );
142 if ( $batchesDone >= $this->reportingInterval
) {
144 echo "$startId / $endId\n";
145 wfWaitForSlaves( 5 );
148 echo "Found $rowsInserted revisions\n";
152 * Scan the text table for orphan text
153 * Orphan text here does not imply DB corruption -- deleted text tracked by the
154 * archive table counts as orphan for our purposes.
156 function trackOrphanText() {
157 # Wait until the blob_tracking table is available in the slave
158 $dbw = wfGetDB( DB_MASTER
);
159 $dbr = wfGetDB( DB_SLAVE
);
160 $pos = $dbw->getMasterPos();
161 $dbr->masterPosWait( $pos, 100000 );
163 $textClause = $this->getTextClause( $this->clusters
);
165 $endId = $dbr->selectField( 'text', 'MAX(old_id)', false, __METHOD__
);
169 echo "Finding orphan text...\n";
171 # Scan the text table for orphan text
173 $res = $dbr->select( array( 'text', 'blob_tracking' ),
174 array( 'old_id', 'old_flags', 'old_text' ),
176 'old_id>' . $dbr->addQuotes( $startId ),
178 'old_flags ' . $dbr->buildLike( $dbr->anyString(), 'external', $dbr->anyString() ),
183 'ORDER BY' => 'old_id',
184 'LIMIT' => $this->batchSize
186 array( 'blob_tracking' => array( 'LEFT JOIN', 'bt_text_id=old_id' ) )
189 foreach ( $res as $row ) {
190 $ids[] = $row->old_id
;
193 if ( !$res->numRows() ) {
197 $insertBatch = array();
198 foreach ( $res as $row ) {
199 $startId = $row->old_id
;
200 $info = $this->interpretPointer( $row->old_text
);
202 echo "Invalid DB:// URL in old_id {$row->old_id}\n";
205 if ( !in_array( $info['cluster'], $this->clusters
) ) {
206 echo "Invalid cluster returned in SQL query\n";
210 $insertBatch[] = array(
213 'bt_text_id' => $row->old_id
,
214 'bt_cluster' => $info['cluster'],
215 'bt_blob_id' => $info['id'],
216 'bt_cgz_hash' => $info['hash']
218 if ( $this->doBlobOrphans
) {
219 gmp_setbit( $this->trackedBlobs
[$info['cluster']], $info['id'] );
222 $dbw->insert( 'blob_tracking', $insertBatch, __METHOD__
);
224 $rowsInserted +
= count( $insertBatch );
226 if ( $batchesDone >= $this->reportingInterval
) {
228 echo "$startId / $endId\n";
229 wfWaitForSlaves( 5 );
232 echo "Found $rowsInserted orphan text rows\n";
236 * Scan the blobs table for rows not registered in blob_tracking (and thus not
237 * registered in the text table).
239 * Orphan blobs are indicative of DB corruption. They are inaccessible and
240 * should probably be deleted.
242 function findOrphanBlobs() {
243 if ( !extension_loaded( 'gmp' ) ) {
244 echo "Can't find orphan blobs, need bitfield support provided by GMP.\n";
248 $dbw = wfGetDB( DB_MASTER
);
250 foreach ( $this->clusters
as $cluster ) {
251 echo "Searching for orphan blobs in $cluster...\n";
252 $lb = wfGetLBFactory()->getExternalLB( $cluster );
254 $extDB = $lb->getConnection( DB_SLAVE
);
255 } catch ( DBConnectionError
$e ) {
256 if ( strpos( $e->error
, 'Unknown database' ) !== false ) {
257 echo "No database on $cluster\n";
259 echo "Error on $cluster: " . $e->getMessage() . "\n";
263 $table = $extDB->getLBInfo( 'blobs table' );
264 if ( is_null( $table ) ) {
267 if ( !$extDB->tableExists( $table ) ) {
268 echo "No blobs table on cluster $cluster\n";
273 $actualBlobs = gmp_init( 0 );
274 $endId = $extDB->selectField( $table, 'MAX(blob_id)', false, __METHOD__
);
276 // Build a bitmap of actual blob rows
278 $res = $extDB->select( $table,
280 array( 'blob_id > ' . $extDB->addQuotes( $startId ) ),
282 array( 'LIMIT' => $this->batchSize
, 'ORDER BY' => 'blob_id' )
285 if ( !$res->numRows() ) {
289 foreach ( $res as $row ) {
290 gmp_setbit( $actualBlobs, $row->blob_id
);
292 $startId = $row->blob_id
;
295 if ( $batchesDone >= $this->reportingInterval
) {
297 echo "$startId / $endId\n";
301 // Find actual blobs that weren't tracked by the previous passes
302 // This is a set-theoretic difference A \ B, or in bitwise terms, A & ~B
303 $orphans = gmp_and( $actualBlobs, gmp_com( $this->trackedBlobs
[$cluster] ) );
305 // Traverse the orphan list
306 $insertBatch = array();
310 $id = gmp_scan1( $orphans, $id );
314 $insertBatch[] = array(
315 'bo_cluster' => $cluster,
318 if ( count( $insertBatch ) > $this->batchSize
) {
319 $dbw->insert( 'blob_orphans', $insertBatch, __METHOD__
);
320 $insertBatch = array();
326 if ( $insertBatch ) {
327 $dbw->insert( 'blob_orphans', $insertBatch, __METHOD__
);
329 echo "Found $numOrphans orphan(s) in $cluster\n";