| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/../Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class OrphanStats extends Maintenance { |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
$this->addDescription( |
| 38 |
"Show some statistics on the blob_orphans table, created with trackBlobs.php" ); |
| 39 |
} |
| 40 |
|
| 41 |
protected function getExternalDB( $db, $cluster ) { |
| 42 |
$lbFactory = $this->getServiceContainer()->getDBLoadBalancerFactory(); |
| 43 |
$lb = $lbFactory->getExternalLB( $cluster ); |
| 44 |
|
| 45 |
return $lb->getMaintenanceConnectionRef( $db ); |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
if ( !$this->getDB( DB_PRIMARY )->tableExists( 'blob_orphans', __METHOD__ ) ) { |
| 50 |
$this->fatalError( "blob_orphans doesn't seem to exist, need to run trackBlobs.php first" ); |
| 51 |
} |
| 52 |
$dbr = $this->getReplicaDB(); |
| 53 |
$res = $dbr->newSelectQueryBuilder() |
| 54 |
->select( '*' ) |
| 55 |
->from( 'blob_orphans' ) |
| 56 |
->caller( __METHOD__ )->fetchResultSet(); |
| 57 |
|
| 58 |
$num = 0; |
| 59 |
$totalSize = 0; |
| 60 |
$hashes = []; |
| 61 |
$maxSize = 0; |
| 62 |
|
| 63 |
foreach ( $res as $row ) { |
| 64 |
$extDB = $this->getExternalDB( DB_REPLICA, $row->bo_cluster ); |
| 65 |
$blobRow = $extDB->newSelectQueryBuilder() |
| 66 |
->select( '*' ) |
| 67 |
->from( 'blobs' ) |
| 68 |
->where( [ 'blob_id' => $row->bo_blob_id ] ) |
| 69 |
->caller( __METHOD__ )->fetchRow(); |
| 70 |
|
| 71 |
$num++; |
| 72 |
$size = strlen( $blobRow->blob_text ); |
| 73 |
$totalSize += $size; |
| 74 |
$hashes[sha1( $blobRow->blob_text )] = true; |
| 75 |
$maxSize = max( $size, $maxSize ); |
| 76 |
} |
| 77 |
unset( $res ); |
| 78 |
|
| 79 |
$this->output( "Number of orphans: $num\n" ); |
| 80 |
if ( $num > 0 ) { |
| 81 |
$this->output( "Average size: " . round( $totalSize / $num, 0 ) . " bytes\n" . |
| 82 |
"Max size: $maxSize\n" . |
| 83 |
"Number of unique texts: " . count( $hashes ) . "\n" ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
$maintClass = OrphanStats::class; |
| 90 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 91 |
|