| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 22 |
use Wikimedia\Rdbms\DBUnexpectedError; |
| 23 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 24 |
use Wikimedia\Rdbms\LBFactory; |
| 25 |
use Wikimedia\Rdbms\Query; |
| 26 |
use Wikimedia\Rdbms\ServerInfo; |
| 27 |
use Wikimedia\ScopedCallback; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class ExternalStoreDB extends ExternalStoreMedium { |
| 39 |
|
| 40 |
private $lbFactory; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( array $params ) { |
| 48 |
parent::__construct( $params ); |
| 49 |
if ( !isset( $params['lbFactory'] ) || !( $params['lbFactory'] instanceof LBFactory ) ) { |
| 50 |
throw new InvalidArgumentException( "LBFactory required in 'lbFactory' field." ); |
| 51 |
} |
| 52 |
$this->lbFactory = $params['lbFactory']; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function fetchFromURL( $url ) { |
| 66 |
[ $cluster, $id, $itemID ] = $this->parseURL( $url ); |
| 67 |
$ret = $this->fetchBlob( $cluster, $id, $itemID ); |
| 68 |
|
| 69 |
if ( $itemID !== false && $ret !== false ) { |
| 70 |
return $ret->getItem( $itemID ); |
| 71 |
} |
| 72 |
|
| 73 |
return $ret; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function batchFetchFromURLs( array $urls ) { |
| 87 |
$batched = $inverseUrlMap = []; |
| 88 |
foreach ( $urls as $url ) { |
| 89 |
[ $cluster, $id, $itemID ] = $this->parseURL( $url ); |
| 90 |
$batched[$cluster][$id][] = $itemID; |
| 91 |
|
| 92 |
|
| 93 |
$inverseUrlMap[$cluster][$id][$itemID] = $url; |
| 94 |
} |
| 95 |
$ret = []; |
| 96 |
foreach ( $batched as $cluster => $batchByCluster ) { |
| 97 |
$res = $this->batchFetchBlobs( $cluster, $batchByCluster ); |
| 98 |
|
| 99 |
foreach ( $res as $id => $blob ) { |
| 100 |
foreach ( $batchByCluster[$id] as $itemID ) { |
| 101 |
$url = $inverseUrlMap[$cluster][$id][$itemID]; |
| 102 |
if ( $itemID === false ) { |
| 103 |
$ret[$url] = $blob; |
| 104 |
} else { |
| 105 |
$ret[$url] = $blob->getItem( $itemID ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $ret; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function store( $location, $data ) { |
| 118 |
$blobsTable = $this->getTable( $location ); |
| 119 |
|
| 120 |
$dbw = $this->getPrimary( $location ); |
| 121 |
$dbw->newInsertQueryBuilder() |
| 122 |
->insertInto( $blobsTable ) |
| 123 |
->row( [ 'blob_text' => $data ] ) |
| 124 |
->caller( __METHOD__ )->execute(); |
| 125 |
|
| 126 |
$id = $dbw->insertId(); |
| 127 |
if ( !$id ) { |
| 128 |
throw new ExternalStoreException( __METHOD__ . ': no insert ID' ); |
| 129 |
} |
| 130 |
|
| 131 |
return "DB://$location/$id"; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function isReadOnly( $location ) { |
| 138 |
if ( parent::isReadOnly( $location ) ) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !== false ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private function getLoadBalancer( $cluster ) { |
| 152 |
return $this->lbFactory->getExternalLB( $cluster ); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function getReplica( $cluster ) { |
| 163 |
$lb = $this->getLoadBalancer( $cluster ); |
| 164 |
|
| 165 |
return $lb->getConnection( |
| 166 |
DB_REPLICA, |
| 167 |
[], |
| 168 |
$this->getDomainId( $lb->getServerInfo( ServerInfo::WRITER_INDEX ) ), |
| 169 |
$lb::CONN_TRX_AUTOCOMMIT |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function getPrimary( $cluster ) { |
| 181 |
$lb = $this->getLoadBalancer( $cluster ); |
| 182 |
|
| 183 |
return $lb->getMaintenanceConnectionRef( |
| 184 |
DB_PRIMARY, |
| 185 |
[], |
| 186 |
$this->getDomainId( $lb->getServerInfo( ServerInfo::WRITER_INDEX ) ), |
| 187 |
$lb::CONN_TRX_AUTOCOMMIT |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function getDomainId( array $server ) { |
| 196 |
if ( $this->isDbDomainExplicit ) { |
| 197 |
return $this->dbDomain; |
| 198 |
} |
| 199 |
|
| 200 |
if ( isset( $server['dbname'] ) ) { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
$domain = new DatabaseDomain( |
| 207 |
$server['dbname'], |
| 208 |
$server['schema'] ?? null, |
| 209 |
$server['tablePrefix'] ?? '' |
| 210 |
); |
| 211 |
|
| 212 |
return $domain->getId(); |
| 213 |
} |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function getTable( string $cluster ) { |
| 229 |
$lb = $this->getLoadBalancer( $cluster ); |
| 230 |
$info = $lb->getServerInfo( ServerInfo::WRITER_INDEX ); |
| 231 |
|
| 232 |
return $info['blobs table'] ?? 'blobs'; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public function initializeTable( $cluster ) { |
| 242 |
global $IP; |
| 243 |
|
| 244 |
static $supportedTypes = [ 'mysql', 'sqlite' ]; |
| 245 |
|
| 246 |
$dbw = $this->getPrimary( $cluster ); |
| 247 |
if ( !in_array( $dbw->getType(), $supportedTypes, true ) ) { |
| 248 |
throw new DBUnexpectedError( $dbw, "RDBMS type '{$dbw->getType()}' not supported." ); |
| 249 |
} |
| 250 |
|
| 251 |
$sqlFilePath = "$IP/maintenance/storage/blobs.sql"; |
| 252 |
$sql = file_get_contents( $sqlFilePath ); |
| 253 |
if ( $sql === false ) { |
| 254 |
throw new RuntimeException( "Failed to read '$sqlFilePath'." ); |
| 255 |
} |
| 256 |
|
| 257 |
$blobsTable = $this->getTable( $cluster ); |
| 258 |
$encTable = $dbw->tableName( $blobsTable ); |
| 259 |
$sqlWithReplacedVars = str_replace( |
| 260 |
[ '/*$wgDBprefix*/blobs', '/*_*/blobs' ], |
| 261 |
[ $encTable, $encTable ], |
| 262 |
$sql |
| 263 |
); |
| 264 |
|
| 265 |
$dbw->query( |
| 266 |
new Query( |
| 267 |
$sqlWithReplacedVars, |
| 268 |
$dbw::QUERY_CHANGE_SCHEMA, |
| 269 |
'CREATE', |
| 270 |
$blobsTable, |
| 271 |
$sqlWithReplacedVars |
| 272 |
), |
| 273 |
__METHOD__ |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private function fetchBlob( $cluster, $id, $itemID ) { |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
static $externalBlobCache = []; |
| 294 |
|
| 295 |
$cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/"; |
| 296 |
$cacheID = "$cacheID@{$this->dbDomain}"; |
| 297 |
|
| 298 |
if ( isset( $externalBlobCache[$cacheID] ) ) { |
| 299 |
$this->logger->debug( __METHOD__ . ": cache hit on $cacheID" ); |
| 300 |
|
| 301 |
return $externalBlobCache[$cacheID]; |
| 302 |
} |
| 303 |
|
| 304 |
$this->logger->debug( __METHOD__ . ": cache miss on $cacheID" ); |
| 305 |
|
| 306 |
$blobsTable = $this->getTable( $cluster ); |
| 307 |
|
| 308 |
$dbr = $this->getReplica( $cluster ); |
| 309 |
$ret = $dbr->newSelectQueryBuilder() |
| 310 |
->select( 'blob_text' ) |
| 311 |
->from( $blobsTable ) |
| 312 |
->where( [ 'blob_id' => $id ] ) |
| 313 |
->caller( __METHOD__ )->fetchField(); |
| 314 |
|
| 315 |
if ( $ret === false ) { |
| 316 |
|
| 317 |
$this->logger->warning( __METHOD__ . ": primary DB fallback on $cacheID" ); |
| 318 |
$trxProfiler = $this->lbFactory->getTransactionProfiler(); |
| 319 |
$scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY ); |
| 320 |
$dbw = $this->getPrimary( $cluster ); |
| 321 |
$ret = $dbw->newSelectQueryBuilder() |
| 322 |
->select( 'blob_text' ) |
| 323 |
->from( $blobsTable ) |
| 324 |
->where( [ 'blob_id' => $id ] ) |
| 325 |
->caller( __METHOD__ )->fetchField(); |
| 326 |
ScopedCallback::consume( $scope ); |
| 327 |
if ( $ret === false ) { |
| 328 |
$this->logger->warning( __METHOD__ . ": primary DB failed to find $cacheID" ); |
| 329 |
} |
| 330 |
} |
| 331 |
if ( $itemID !== false && $ret !== false ) { |
| 332 |
|
| 333 |
$ret = HistoryBlobUtils::unserialize( $ret ); |
| 334 |
} |
| 335 |
|
| 336 |
$externalBlobCache = [ $cacheID => $ret ]; |
| 337 |
|
| 338 |
return $ret; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private function batchFetchBlobs( $cluster, array $ids ) { |
| 350 |
$blobsTable = $this->getTable( $cluster ); |
| 351 |
|
| 352 |
$dbr = $this->getReplica( $cluster ); |
| 353 |
$res = $dbr->newSelectQueryBuilder() |
| 354 |
->select( [ 'blob_id', 'blob_text' ] ) |
| 355 |
->from( $blobsTable ) |
| 356 |
->where( [ 'blob_id' => array_keys( $ids ) ] ) |
| 357 |
->caller( __METHOD__ ) |
| 358 |
->fetchResultSet(); |
| 359 |
|
| 360 |
$ret = []; |
| 361 |
$this->mergeBatchResult( $ret, $ids, $res ); |
| 362 |
if ( $ids ) { |
| 363 |
|
| 364 |
$this->logger->info( |
| 365 |
__METHOD__ . ": primary fallback on '$cluster' for: " . |
| 366 |
implode( ',', array_keys( $ids ) ) |
| 367 |
); |
| 368 |
$trxProfiler = $this->lbFactory->getTransactionProfiler(); |
| 369 |
$scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY ); |
| 370 |
$dbw = $this->getPrimary( $cluster ); |
| 371 |
$res = $dbw->newSelectQueryBuilder() |
| 372 |
->select( [ 'blob_id', 'blob_text' ] ) |
| 373 |
->from( $blobsTable ) |
| 374 |
->where( [ 'blob_id' => array_keys( $ids ) ] ) |
| 375 |
->caller( __METHOD__ ) |
| 376 |
->fetchResultSet(); |
| 377 |
ScopedCallback::consume( $scope ); |
| 378 |
$this->mergeBatchResult( $ret, $ids, $res ); |
| 379 |
} |
| 380 |
if ( $ids ) { |
| 381 |
$this->logger->error( |
| 382 |
__METHOD__ . ": primary on '$cluster' failed locating items: " . |
| 383 |
implode( ',', array_keys( $ids ) ) |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
return $ret; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
private function mergeBatchResult( array &$ret, array &$ids, $res ) { |
| 397 |
foreach ( $res as $row ) { |
| 398 |
$id = $row->blob_id; |
| 399 |
$itemIDs = $ids[$id]; |
| 400 |
unset( $ids[$id] ); |
| 401 |
if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) { |
| 402 |
|
| 403 |
$ret[$id] = $row->blob_text; |
| 404 |
} else { |
| 405 |
|
| 406 |
$ret[$id] = HistoryBlobUtils::unserialize( $row->blob_text ); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
protected function parseURL( $url ) { |
| 416 |
$path = explode( '/', $url ); |
| 417 |
|
| 418 |
return [ |
| 419 |
$path[2], |
| 420 |
$path[3], |
| 421 |
$path[4] ?? false |
| 422 |
]; |
| 423 |
} |
| 424 |
} |