| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use Psr\Log\LoggerInterface; |
| 23 |
use Psr\Log\NullLogger; |
| 24 |
use RuntimeException; |
| 25 |
use Throwable; |
| 26 |
use UnexpectedValueException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class TransactionManager { |
| 33 |
|
| 34 |
public const STATUS_TRX_ERROR = 1; |
| 35 |
|
| 36 |
public const STATUS_TRX_OK = 2; |
| 37 |
|
| 38 |
public const STATUS_TRX_NONE = 3; |
| 39 |
|
| 40 |
|
| 41 |
public const STATUS_SESS_ERROR = 1; |
| 42 |
|
| 43 |
public const STATUS_SESS_OK = 2; |
| 44 |
|
| 45 |
|
| 46 |
private const TINY_WRITE_SEC = 0.010; |
| 47 |
|
| 48 |
private const SLOW_WRITE_SEC = 0.500; |
| 49 |
|
| 50 |
private const SMALL_WRITE_ROWS = 100; |
| 51 |
|
| 52 |
|
| 53 |
private const SAVEPOINT_PREFIX = 'wikimedia_rdbms_atomic'; |
| 54 |
|
| 55 |
|
| 56 |
private $trxId; |
| 57 |
|
| 58 |
private $trxTimestamp = null; |
| 59 |
|
| 60 |
private $trxRoundTripDelay = null; |
| 61 |
|
| 62 |
private $trxStatus = self::STATUS_TRX_NONE; |
| 63 |
|
| 64 |
private $trxStatusCause; |
| 65 |
|
| 66 |
private $trxStatusIgnoredCause; |
| 67 |
|
| 68 |
|
| 69 |
private $sessionError; |
| 70 |
|
| 71 |
|
| 72 |
private $trxWriteCallers = []; |
| 73 |
|
| 74 |
private $trxWriteDuration = 0.0; |
| 75 |
|
| 76 |
private $trxWriteQueryCount = 0; |
| 77 |
|
| 78 |
private $trxWriteAffectedRows = 0; |
| 79 |
|
| 80 |
private $trxWriteAdjDuration = 0.0; |
| 81 |
|
| 82 |
private $trxWriteAdjQueryCount = 0; |
| 83 |
|
| 84 |
|
| 85 |
private $trxAtomicLevels = []; |
| 86 |
|
| 87 |
private $trxAutomatic = false; |
| 88 |
|
| 89 |
private $trxAutomaticAtomic = false; |
| 90 |
|
| 91 |
|
| 92 |
private $trxFname = null; |
| 93 |
|
| 94 |
private $trxAtomicCounter = 0; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private $trxPostCommitOrIdleCallbacks = []; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private $trxPreCommitOrIdleCallbacks = []; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
private $trxEndCallbacks = []; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
private $trxSectionCancelCallbacks = []; |
| 116 |
|
| 117 |
private $trxRecurringCallbacks = []; |
| 118 |
|
| 119 |
private $trxEndCallbacksSuppressed = false; |
| 120 |
|
| 121 |
|
| 122 |
private $logger; |
| 123 |
|
| 124 |
private $profiler; |
| 125 |
|
| 126 |
public function __construct( ?LoggerInterface $logger = null, $profiler = null ) { |
| 127 |
$this->logger = $logger ?? new NullLogger(); |
| 128 |
$this->profiler = $profiler ?? new TransactionProfiler(); |
| 129 |
} |
| 130 |
|
| 131 |
public function trxLevel() { |
| 132 |
return $this->trxId ? 1 : 0; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function getTrxId() { |
| 141 |
return $this->trxId; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private function consumeTrxId() { |
| 150 |
$old = $this->trxId; |
| 151 |
$this->trxId = null; |
| 152 |
|
| 153 |
return $old; |
| 154 |
} |
| 155 |
|
| 156 |
public function trxTimestamp(): ?float { |
| 157 |
return $this->trxLevel() ? $this->trxTimestamp : null; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function trxStatus(): int { |
| 164 |
return $this->trxStatus; |
| 165 |
} |
| 166 |
|
| 167 |
public function setTrxStatusToOk() { |
| 168 |
$this->trxStatus = self::STATUS_TRX_OK; |
| 169 |
$this->trxStatusCause = null; |
| 170 |
$this->trxStatusIgnoredCause = null; |
| 171 |
} |
| 172 |
|
| 173 |
public function setTrxStatusToNone() { |
| 174 |
$this->trxStatus = self::STATUS_TRX_NONE; |
| 175 |
$this->trxStatusCause = null; |
| 176 |
$this->trxStatusIgnoredCause = null; |
| 177 |
} |
| 178 |
|
| 179 |
public function assertTransactionStatus( IDatabase $db, $deprecationLogger, $fname ) { |
| 180 |
if ( $this->trxStatus === self::STATUS_TRX_ERROR ) { |
| 181 |
throw new DBTransactionStateError( |
| 182 |
$db, |
| 183 |
"Cannot execute query from $fname while transaction status is ERROR", |
| 184 |
[], |
| 185 |
$this->trxStatusCause |
| 186 |
); |
| 187 |
} elseif ( $this->trxStatus === self::STATUS_TRX_OK && $this->trxStatusIgnoredCause ) { |
| 188 |
[ $iLastError, $iLastErrno, $iFname ] = $this->trxStatusIgnoredCause; |
| 189 |
call_user_func( $deprecationLogger, |
| 190 |
"Caller from $fname ignored an error originally raised from $iFname: " . |
| 191 |
"[$iLastErrno] $iLastError" |
| 192 |
); |
| 193 |
$this->trxStatusIgnoredCause = null; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public function assertSessionStatus( IDatabase $db, $fname ) { |
| 198 |
if ( $this->sessionError ) { |
| 199 |
throw new DBSessionStateError( |
| 200 |
$db, |
| 201 |
"Cannot execute query from $fname while session status is ERROR", |
| 202 |
[], |
| 203 |
$this->sessionError |
| 204 |
); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function setTransactionError( Throwable $trxError ) { |
| 214 |
if ( $this->trxStatus !== self::STATUS_TRX_ERROR ) { |
| 215 |
$this->trxStatus = self::STATUS_TRX_ERROR; |
| 216 |
$this->trxStatusCause = $trxError; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function setTrxStatusIgnoredCause( ?array $trxStatusIgnoredCause ): void { |
| 224 |
$this->trxStatusIgnoredCause = $trxStatusIgnoredCause; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function sessionStatus() { |
| 233 |
|
| 234 |
return ( $this->sessionError ) ? self::STATUS_SESS_ERROR : self::STATUS_SESS_OK; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function setSessionError( Throwable $sessionError ) { |
| 243 |
$this->sessionError ??= $sessionError; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function clearSessionError() { |
| 250 |
$this->sessionError = null; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function calculateLastTrxApplyTime( float $rtt ) { |
| 258 |
$rttAdjTotal = $this->trxWriteAdjQueryCount * $rtt; |
| 259 |
$applyTime = max( $this->trxWriteAdjDuration - $rttAdjTotal, 0.0 ); |
| 260 |
|
| 261 |
$omitted = $this->trxWriteQueryCount - $this->trxWriteAdjQueryCount; |
| 262 |
$applyTime += self::TINY_WRITE_SEC * $omitted; |
| 263 |
|
| 264 |
return $applyTime; |
| 265 |
} |
| 266 |
|
| 267 |
public function pendingWriteCallers() { |
| 268 |
return $this->trxLevel() ? $this->trxWriteCallers : []; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function updateTrxWriteQueryReport( $queryVerb, $runtime, $affected, $fname ) { |
| 285 |
|
| 286 |
$indicativeOfReplicaRuntime = true; |
| 287 |
if ( $runtime > self::SLOW_WRITE_SEC ) { |
| 288 |
|
| 289 |
if ( $queryVerb === 'INSERT' ) { |
| 290 |
$indicativeOfReplicaRuntime = $affected > self::SMALL_WRITE_ROWS; |
| 291 |
} elseif ( $queryVerb === 'REPLACE' ) { |
| 292 |
$indicativeOfReplicaRuntime = $affected > self::SMALL_WRITE_ROWS / 2; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
$this->trxWriteDuration += $runtime; |
| 297 |
$this->trxWriteQueryCount += 1; |
| 298 |
$this->trxWriteAffectedRows += $affected; |
| 299 |
if ( $indicativeOfReplicaRuntime ) { |
| 300 |
$this->trxWriteAdjDuration += $runtime; |
| 301 |
$this->trxWriteAdjQueryCount += 1; |
| 302 |
} |
| 303 |
|
| 304 |
$this->trxWriteCallers[] = $fname; |
| 305 |
} |
| 306 |
|
| 307 |
public function pendingWriteQueryDuration( $type = IDatabase::ESTIMATE_TOTAL ) { |
| 308 |
if ( !$this->trxLevel() ) { |
| 309 |
return false; |
| 310 |
} elseif ( !$this->trxWriteCallers ) { |
| 311 |
return 0.0; |
| 312 |
} |
| 313 |
|
| 314 |
if ( $type === IDatabase::ESTIMATE_DB_APPLY ) { |
| 315 |
return $this->calculateLastTrxApplyTime( $this->trxRoundTripDelay ); |
| 316 |
} |
| 317 |
|
| 318 |
return $this->trxWriteDuration; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
private function flatAtomicSectionList() { |
| 325 |
return array_reduce( $this->trxAtomicLevels, static function ( $accum, $v ) { |
| 326 |
return $accum === null ? $v[0] : "$accum, " . $v[0]; |
| 327 |
} ); |
| 328 |
} |
| 329 |
|
| 330 |
public function resetTrxAtomicLevels() { |
| 331 |
$this->trxAtomicLevels = []; |
| 332 |
$this->trxAtomicCounter = 0; |
| 333 |
} |
| 334 |
|
| 335 |
public function explicitTrxActive() { |
| 336 |
return $this->trxLevel() && ( $this->trxAtomicLevels || !$this->trxAutomatic ); |
| 337 |
} |
| 338 |
|
| 339 |
public function trxCheckBeforeClose( IDatabaseForOwner $db, $fname ) { |
| 340 |
$error = null; |
| 341 |
if ( $this->trxAtomicLevels ) { |
| 342 |
|
| 343 |
$levels = $this->flatAtomicSectionList(); |
| 344 |
$error = "$fname: atomic sections $levels are still open"; |
| 345 |
} elseif ( $this->trxAutomatic ) { |
| 346 |
|
| 347 |
|
| 348 |
if ( $db->writesOrCallbacksPending() ) { |
| 349 |
$error = "$fname: " . |
| 350 |
"expected mass rollback of all peer transactions (DBO_TRX set)"; |
| 351 |
} |
| 352 |
} else { |
| 353 |
|
| 354 |
|
| 355 |
$error = "$fname: transaction is still open (from {$this->trxFname})"; |
| 356 |
} |
| 357 |
|
| 358 |
if ( $this->trxEndCallbacksSuppressed && $error === null ) { |
| 359 |
$error = "$fname: callbacks are suppressed; cannot properly commit"; |
| 360 |
} |
| 361 |
|
| 362 |
return $error; |
| 363 |
} |
| 364 |
|
| 365 |
public function onAtomicSectionCancel( IDatabase $db, $callback, $fname ): void { |
| 366 |
if ( !$this->trxLevel() || !$this->trxAtomicLevels ) { |
| 367 |
throw new DBUnexpectedError( $db, "No atomic section is open (got $fname)" ); |
| 368 |
} |
| 369 |
$this->trxSectionCancelCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ]; |
| 370 |
} |
| 371 |
|
| 372 |
public function onCancelAtomicBeforeCriticalSection( IDatabase $db, $fname ): void { |
| 373 |
if ( !$this->trxLevel() || !$this->trxAtomicLevels ) { |
| 374 |
throw new DBUnexpectedError( $db, "No atomic section is open (got $fname)" ); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function currentAtomicSectionId(): ?AtomicSectionIdentifier { |
| 382 |
if ( $this->trxLevel() && $this->trxAtomicLevels ) { |
| 383 |
$levelInfo = end( $this->trxAtomicLevels ); |
| 384 |
|
| 385 |
return $levelInfo[1]; |
| 386 |
} |
| 387 |
|
| 388 |
return null; |
| 389 |
} |
| 390 |
|
| 391 |
public function addToAtomicLevels( $fname, AtomicSectionIdentifier $sectionId, $savepointId ) { |
| 392 |
$this->trxAtomicLevels[] = [ $fname, $sectionId, $savepointId ]; |
| 393 |
$this->logger->debug( 'startAtomic: entering level ' . |
| 394 |
( count( $this->trxAtomicLevels ) - 1 ) . " ($fname)", [ 'db_log_category' => 'trx' ] ); |
| 395 |
} |
| 396 |
|
| 397 |
public function onBegin( IDatabase $db, $fname ): void { |
| 398 |
|
| 399 |
if ( $this->trxLevel() ) { |
| 400 |
if ( $this->trxAtomicLevels ) { |
| 401 |
$levels = $this->flatAtomicSectionList(); |
| 402 |
$msg = "$fname: got explicit BEGIN while atomic section(s) $levels are open"; |
| 403 |
throw new DBUnexpectedError( $db, $msg ); |
| 404 |
} elseif ( !$this->trxAutomatic ) { |
| 405 |
$msg = "$fname: explicit transaction already active (from {$this->trxFname})"; |
| 406 |
throw new DBUnexpectedError( $db, $msg ); |
| 407 |
} else { |
| 408 |
$msg = "$fname: implicit transaction already active (from {$this->trxFname})"; |
| 409 |
throw new DBUnexpectedError( $db, $msg ); |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public function onCommit( IDatabase $db, $fname, $flush ): bool { |
| 421 |
if ( $this->trxLevel() && $this->trxAtomicLevels ) { |
| 422 |
|
| 423 |
$levels = $this->flatAtomicSectionList(); |
| 424 |
throw new DBUnexpectedError( |
| 425 |
$db, |
| 426 |
"$fname: got COMMIT while atomic sections $levels are still open" |
| 427 |
); |
| 428 |
} |
| 429 |
|
| 430 |
if ( $flush === IDatabase::FLUSHING_INTERNAL || $flush === IDatabase::FLUSHING_ALL_PEERS ) { |
| 431 |
if ( !$this->trxLevel() ) { |
| 432 |
return false; |
| 433 |
} elseif ( !$this->trxAutomatic ) { |
| 434 |
throw new DBUnexpectedError( |
| 435 |
$db, |
| 436 |
"$fname: flushing an explicit transaction, getting out of sync" |
| 437 |
); |
| 438 |
} |
| 439 |
} elseif ( !$this->trxLevel() ) { |
| 440 |
$this->logger->error( |
| 441 |
"$fname: no transaction to commit, something got out of sync", |
| 442 |
[ |
| 443 |
'exception' => new RuntimeException(), |
| 444 |
'db_log_category' => 'trx' |
| 445 |
] |
| 446 |
); |
| 447 |
|
| 448 |
return false; |
| 449 |
} elseif ( $this->trxAutomatic ) { |
| 450 |
throw new DBUnexpectedError( |
| 451 |
$db, |
| 452 |
"$fname: expected mass commit of all peer transactions (DBO_TRX set)" |
| 453 |
); |
| 454 |
} |
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
public function onEndAtomic( IDatabase $db, $fname ): array { |
| 459 |
if ( !$this->trxLevel() || !$this->trxAtomicLevels ) { |
| 460 |
throw new DBUnexpectedError( $db, "No atomic section is open (got $fname)" ); |
| 461 |
} |
| 462 |
|
| 463 |
$pos = count( $this->trxAtomicLevels ) - 1; |
| 464 |
[ $savedFname, $sectionId, $savepointId ] = $this->trxAtomicLevels[$pos]; |
| 465 |
$this->logger->debug( "endAtomic: leaving level $pos ($fname)", [ 'db_log_category' => 'trx' ] ); |
| 466 |
|
| 467 |
if ( $savedFname !== $fname ) { |
| 468 |
throw new DBUnexpectedError( |
| 469 |
$db, |
| 470 |
"Invalid atomic section ended (got $fname but expected $savedFname)" |
| 471 |
); |
| 472 |
} |
| 473 |
|
| 474 |
return [ $savepointId, $sectionId ]; |
| 475 |
} |
| 476 |
|
| 477 |
public function getPositionFromSectionId( ?AtomicSectionIdentifier $sectionId = null ): ?int { |
| 478 |
if ( $sectionId !== null ) { |
| 479 |
|
| 480 |
$pos = -1; |
| 481 |
foreach ( $this->trxAtomicLevels as $i => [ , $asId, ] ) { |
| 482 |
if ( $asId === $sectionId ) { |
| 483 |
$pos = $i; |
| 484 |
} |
| 485 |
} |
| 486 |
} else { |
| 487 |
$pos = null; |
| 488 |
} |
| 489 |
|
| 490 |
return $pos; |
| 491 |
} |
| 492 |
|
| 493 |
public function cancelAtomic( $pos ) { |
| 494 |
$excisedIds = []; |
| 495 |
$excisedFnames = []; |
| 496 |
$newTopSection = $this->currentAtomicSectionId(); |
| 497 |
if ( $pos !== null ) { |
| 498 |
|
| 499 |
$len = count( $this->trxAtomicLevels ); |
| 500 |
for ( $i = $pos + 1; $i < $len; ++$i ) { |
| 501 |
$excisedFnames[] = $this->trxAtomicLevels[$i][0]; |
| 502 |
$excisedIds[] = $this->trxAtomicLevels[$i][1]; |
| 503 |
} |
| 504 |
$this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos + 1 ); |
| 505 |
$newTopSection = $this->currentAtomicSectionId(); |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
$pos = count( $this->trxAtomicLevels ) - 1; |
| 510 |
[ $savedFname, $savedSectionId, $savepointId ] = $this->trxAtomicLevels[$pos]; |
| 511 |
|
| 512 |
if ( $excisedFnames ) { |
| 513 |
$this->logger->debug( "cancelAtomic: canceling level $pos ($savedFname) " . |
| 514 |
"and descendants " . implode( ', ', $excisedFnames ), |
| 515 |
[ 'db_log_category' => 'trx' ] |
| 516 |
); |
| 517 |
} else { |
| 518 |
$this->logger->debug( "cancelAtomic: canceling level $pos ($savedFname)", |
| 519 |
[ 'db_log_category' => 'trx' ] |
| 520 |
); |
| 521 |
} |
| 522 |
|
| 523 |
return [ $savedFname, $excisedIds, $newTopSection, $savedSectionId, $savepointId ]; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public function popAtomicLevel() { |
| 530 |
array_pop( $this->trxAtomicLevels ); |
| 531 |
|
| 532 |
return !$this->trxAtomicLevels && $this->trxAutomaticAtomic; |
| 533 |
} |
| 534 |
|
| 535 |
public function setAutomaticAtomic( $value ) { |
| 536 |
$this->trxAutomaticAtomic = $value; |
| 537 |
} |
| 538 |
|
| 539 |
public function turnOnAutomatic() { |
| 540 |
$this->trxAutomatic = true; |
| 541 |
} |
| 542 |
|
| 543 |
public function nextSavePointId( IDatabase $db, $fname ) { |
| 544 |
$savepointId = self::SAVEPOINT_PREFIX . ++$this->trxAtomicCounter; |
| 545 |
if ( strlen( $savepointId ) > 30 ) { |
| 546 |
|
| 547 |
|
| 548 |
throw new DBUnexpectedError( |
| 549 |
$db, |
| 550 |
'There have been an excessively large number of atomic sections in a transaction' |
| 551 |
. " started by $this->trxFname (at $fname)" |
| 552 |
); |
| 553 |
} |
| 554 |
|
| 555 |
return $savepointId; |
| 556 |
} |
| 557 |
|
| 558 |
public function writesPending() { |
| 559 |
return $this->trxLevel() && $this->trxWriteCallers; |
| 560 |
} |
| 561 |
|
| 562 |
public function onDestruct() { |
| 563 |
if ( $this->trxLevel() && $this->trxWriteCallers ) { |
| 564 |
trigger_error( "Uncommitted DB writes (transaction from {$this->trxFname})" ); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
public function transactionWritingIn( $serverName, $domainId, float $startTime ) { |
| 569 |
if ( !$this->trxWriteCallers ) { |
| 570 |
$this->profiler->transactionWritingIn( |
| 571 |
$serverName, |
| 572 |
$domainId, |
| 573 |
(string)$this->trxId, |
| 574 |
$startTime |
| 575 |
); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
public function transactionWritingOut( IDatabase $db, $oldId ) { |
| 580 |
if ( $this->trxWriteCallers ) { |
| 581 |
$this->profiler->transactionWritingOut( |
| 582 |
$db->getServerName(), |
| 583 |
$db->getDomainID(), |
| 584 |
$oldId, |
| 585 |
$this->pendingWriteQueryDuration( IDatabase::ESTIMATE_TOTAL ), |
| 586 |
$this->trxWriteAffectedRows |
| 587 |
); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
public function recordQueryCompletion( $sql, $startTime, $isPermWrite, $rowCount, $serverName ) { |
| 592 |
$this->profiler->recordQueryCompletion( |
| 593 |
$sql, |
| 594 |
$startTime, |
| 595 |
$isPermWrite, |
| 596 |
$rowCount, |
| 597 |
(string)$this->trxId, |
| 598 |
$serverName |
| 599 |
); |
| 600 |
} |
| 601 |
|
| 602 |
public function onTransactionResolution( IDatabase $db, callable $callback, $fname ) { |
| 603 |
if ( !$this->trxLevel() ) { |
| 604 |
throw new DBUnexpectedError( $db, "No transaction is active" ); |
| 605 |
} |
| 606 |
$this->trxEndCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ]; |
| 607 |
} |
| 608 |
|
| 609 |
public function addPostCommitOrIdleCallback( callable $callback, $fname ) { |
| 610 |
$this->trxPostCommitOrIdleCallbacks[] = [ |
| 611 |
$callback, |
| 612 |
$fname, |
| 613 |
$this->currentAtomicSectionId() |
| 614 |
]; |
| 615 |
} |
| 616 |
|
| 617 |
final public function addPreCommitOrIdleCallback( callable $callback, $fname ) { |
| 618 |
$this->trxPreCommitOrIdleCallbacks[] = [ |
| 619 |
$callback, |
| 620 |
$fname, |
| 621 |
$this->currentAtomicSectionId() |
| 622 |
]; |
| 623 |
} |
| 624 |
|
| 625 |
public function setTransactionListener( $name, ?callable $callback = null ) { |
| 626 |
if ( $callback ) { |
| 627 |
$this->trxRecurringCallbacks[$name] = $callback; |
| 628 |
} else { |
| 629 |
unset( $this->trxRecurringCallbacks[$name] ); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public function setTrxEndCallbackSuppression( bool $suppress ) { |
| 638 |
$this->trxEndCallbacksSuppressed = $suppress; |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
public function reassignCallbacksForSection( |
| 648 |
AtomicSectionIdentifier $old, |
| 649 |
AtomicSectionIdentifier $new |
| 650 |
) { |
| 651 |
foreach ( $this->trxPreCommitOrIdleCallbacks as $key => $info ) { |
| 652 |
if ( $info[2] === $old ) { |
| 653 |
$this->trxPreCommitOrIdleCallbacks[$key][2] = $new; |
| 654 |
} |
| 655 |
} |
| 656 |
foreach ( $this->trxPostCommitOrIdleCallbacks as $key => $info ) { |
| 657 |
if ( $info[2] === $old ) { |
| 658 |
$this->trxPostCommitOrIdleCallbacks[$key][2] = $new; |
| 659 |
} |
| 660 |
} |
| 661 |
foreach ( $this->trxEndCallbacks as $key => $info ) { |
| 662 |
if ( $info[2] === $old ) { |
| 663 |
$this->trxEndCallbacks[$key][2] = $new; |
| 664 |
} |
| 665 |
} |
| 666 |
foreach ( $this->trxSectionCancelCallbacks as $key => $info ) { |
| 667 |
if ( $info[2] === $old ) { |
| 668 |
$this->trxSectionCancelCallbacks[$key][2] = $new; |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
public function modifyCallbacksForCancel( |
| 693 |
array $excisedSectionsId, |
| 694 |
?AtomicSectionIdentifier $newSectionId = null |
| 695 |
) { |
| 696 |
|
| 697 |
$this->trxPostCommitOrIdleCallbacks = array_filter( |
| 698 |
$this->trxPostCommitOrIdleCallbacks, |
| 699 |
static function ( $entry ) use ( $excisedSectionsId ) { |
| 700 |
return !in_array( $entry[2], $excisedSectionsId, true ); |
| 701 |
} |
| 702 |
); |
| 703 |
$this->trxPreCommitOrIdleCallbacks = array_filter( |
| 704 |
$this->trxPreCommitOrIdleCallbacks, |
| 705 |
static function ( $entry ) use ( $excisedSectionsId ) { |
| 706 |
return !in_array( $entry[2], $excisedSectionsId, true ); |
| 707 |
} |
| 708 |
); |
| 709 |
|
| 710 |
foreach ( $this->trxEndCallbacks as $key => $entry ) { |
| 711 |
if ( in_array( $entry[2], $excisedSectionsId, true ) ) { |
| 712 |
$callback = $entry[0]; |
| 713 |
$this->trxEndCallbacks[$key][0] = static function ( $t, $db ) use ( $callback ) { |
| 714 |
return $callback( IDatabase::TRIGGER_ROLLBACK, $db ); |
| 715 |
}; |
| 716 |
|
| 717 |
$this->trxEndCallbacks[$key][2] = null; |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
foreach ( $this->trxSectionCancelCallbacks as $key => $entry ) { |
| 722 |
if ( in_array( $entry[2], $excisedSectionsId, true ) ) { |
| 723 |
$this->trxSectionCancelCallbacks[$key][2] = $newSectionId; |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
public function consumeEndCallbacks( $trigger ): array { |
| 729 |
$callbackEntries = array_merge( |
| 730 |
$this->trxPostCommitOrIdleCallbacks, |
| 731 |
$this->trxEndCallbacks, |
| 732 |
( $trigger === IDatabase::TRIGGER_ROLLBACK ) |
| 733 |
? $this->trxSectionCancelCallbacks |
| 734 |
: [] |
| 735 |
); |
| 736 |
$this->trxPostCommitOrIdleCallbacks = []; |
| 737 |
$this->trxEndCallbacks = []; |
| 738 |
$this->trxSectionCancelCallbacks = []; |
| 739 |
|
| 740 |
return $callbackEntries; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
public function runOnAtomicSectionCancelCallbacks( IDatabase $db, int $trigger, array $sectionIds ) { |
| 752 |
|
| 753 |
$unrelatedCallbackEntries = []; |
| 754 |
do { |
| 755 |
$callbackEntries = $this->trxSectionCancelCallbacks; |
| 756 |
$this->trxSectionCancelCallbacks = []; |
| 757 |
foreach ( $callbackEntries as $entry ) { |
| 758 |
if ( in_array( $entry[2], $sectionIds, true ) ) { |
| 759 |
try { |
| 760 |
$entry[0]( $trigger, $db ); |
| 761 |
} catch ( Throwable $trxError ) { |
| 762 |
$this->setTransactionError( $trxError ); |
| 763 |
throw $trxError; |
| 764 |
} |
| 765 |
} else { |
| 766 |
$unrelatedCallbackEntries[] = $entry; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
} while ( $this->trxSectionCancelCallbacks ); |
| 771 |
|
| 772 |
$this->trxSectionCancelCallbacks = $unrelatedCallbackEntries; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
public function runOnTransactionPreCommitCallbacks( IDatabase $db ): int { |
| 783 |
$count = 0; |
| 784 |
|
| 785 |
|
| 786 |
do { |
| 787 |
$callbackEntries = $this->trxPreCommitOrIdleCallbacks; |
| 788 |
$this->trxPreCommitOrIdleCallbacks = []; |
| 789 |
$count += count( $callbackEntries ); |
| 790 |
foreach ( $callbackEntries as $entry ) { |
| 791 |
try { |
| 792 |
$entry[0]( $db ); |
| 793 |
} catch ( Throwable $trxError ) { |
| 794 |
$this->setTransactionError( $trxError ); |
| 795 |
throw $trxError; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
} while ( $this->trxPreCommitOrIdleCallbacks ); |
| 800 |
|
| 801 |
return $count; |
| 802 |
} |
| 803 |
|
| 804 |
public function clearPreEndCallbacks() { |
| 805 |
$this->trxPostCommitOrIdleCallbacks = []; |
| 806 |
$this->trxPreCommitOrIdleCallbacks = []; |
| 807 |
} |
| 808 |
|
| 809 |
public function clearEndCallbacks() { |
| 810 |
$this->trxEndCallbacks = []; |
| 811 |
$this->trxSectionCancelCallbacks = []; |
| 812 |
} |
| 813 |
|
| 814 |
public function writesOrCallbacksPending(): bool { |
| 815 |
return $this->trxLevel() && ( |
| 816 |
$this->trxWriteCallers || |
| 817 |
$this->trxPostCommitOrIdleCallbacks || |
| 818 |
$this->trxPreCommitOrIdleCallbacks || |
| 819 |
$this->trxEndCallbacks || |
| 820 |
$this->trxSectionCancelCallbacks |
| 821 |
); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
public function pendingWriteAndCallbackCallers(): array { |
| 830 |
$fnames = $this->pendingWriteCallers(); |
| 831 |
foreach ( [ |
| 832 |
$this->trxPostCommitOrIdleCallbacks, |
| 833 |
$this->trxPreCommitOrIdleCallbacks, |
| 834 |
$this->trxEndCallbacks, |
| 835 |
$this->trxSectionCancelCallbacks |
| 836 |
] as $callbacks ) { |
| 837 |
foreach ( $callbacks as $callback ) { |
| 838 |
$fnames[] = $callback[1]; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
return $fnames; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
public function pendingPreCommitCallbackCallers(): array { |
| 851 |
$fnames = $this->pendingWriteCallers(); |
| 852 |
foreach ( $this->trxPreCommitOrIdleCallbacks as $callback ) { |
| 853 |
$fnames[] = $callback[1]; |
| 854 |
} |
| 855 |
|
| 856 |
return $fnames; |
| 857 |
} |
| 858 |
|
| 859 |
public function isEndCallbacksSuppressed(): bool { |
| 860 |
return $this->trxEndCallbacksSuppressed; |
| 861 |
} |
| 862 |
|
| 863 |
public function getRecurringCallbacks() { |
| 864 |
return $this->trxRecurringCallbacks; |
| 865 |
} |
| 866 |
|
| 867 |
public function countPostCommitOrIdleCallbacks() { |
| 868 |
return count( $this->trxPostCommitOrIdleCallbacks ); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
public function onBeginInCriticalSection( $mode, $fname, $rtt ) { |
| 877 |
$this->trxId = new TransactionIdentifier(); |
| 878 |
$this->setTrxStatusToOk(); |
| 879 |
$this->resetTrxAtomicLevels(); |
| 880 |
$this->trxWriteCallers = []; |
| 881 |
$this->trxWriteDuration = 0.0; |
| 882 |
$this->trxWriteQueryCount = 0; |
| 883 |
$this->trxWriteAffectedRows = 0; |
| 884 |
$this->trxWriteAdjDuration = 0.0; |
| 885 |
$this->trxWriteAdjQueryCount = 0; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
$this->trxAutomatic = ( $mode === IDatabase::TRANSACTION_INTERNAL ); |
| 891 |
$this->trxAutomaticAtomic = false; |
| 892 |
$this->trxFname = $fname; |
| 893 |
$this->trxTimestamp = microtime( true ); |
| 894 |
$this->trxRoundTripDelay = $rtt; |
| 895 |
} |
| 896 |
|
| 897 |
public function onRollbackInCriticalSection( IDatabase $db ) { |
| 898 |
$oldTrxId = $this->consumeTrxId(); |
| 899 |
$this->setTrxStatusToNone(); |
| 900 |
$this->resetTrxAtomicLevels(); |
| 901 |
$this->clearPreEndCallbacks(); |
| 902 |
$this->transactionWritingOut( $db, (string)$oldTrxId ); |
| 903 |
} |
| 904 |
|
| 905 |
public function onCommitInCriticalSection( IDatabase $db ) { |
| 906 |
$lastWriteTime = null; |
| 907 |
|
| 908 |
$oldTrxId = $this->consumeTrxId(); |
| 909 |
$this->setTrxStatusToNone(); |
| 910 |
if ( $this->trxWriteCallers ) { |
| 911 |
$lastWriteTime = microtime( true ); |
| 912 |
$this->transactionWritingOut( $db, (string)$oldTrxId ); |
| 913 |
} |
| 914 |
|
| 915 |
return $lastWriteTime; |
| 916 |
} |
| 917 |
|
| 918 |
public function onSessionLoss( IDatabase $db ) { |
| 919 |
$oldTrxId = $this->consumeTrxId(); |
| 920 |
$this->clearPreEndCallbacks(); |
| 921 |
$this->transactionWritingOut( $db, (string)$oldTrxId ); |
| 922 |
} |
| 923 |
|
| 924 |
public function onEndAtomicInCriticalSection( $sectionId ) { |
| 925 |
|
| 926 |
|
| 927 |
$currentSectionId = $this->currentAtomicSectionId(); |
| 928 |
if ( $currentSectionId ) { |
| 929 |
$this->reassignCallbacksForSection( $sectionId, $currentSectionId ); |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
public function onFlushSnapshot( IDatabase $db, $fname, $flush, $trxRoundId ) { |
| 934 |
if ( $this->explicitTrxActive() ) { |
| 935 |
|
| 936 |
throw new DBUnexpectedError( |
| 937 |
$db, |
| 938 |
"$fname: Cannot flush snapshot; " . |
| 939 |
"explicit transaction '{$this->trxFname}' is still open" |
| 940 |
); |
| 941 |
} elseif ( $this->writesOrCallbacksPending() ) { |
| 942 |
|
| 943 |
$fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() ); |
| 944 |
throw new DBUnexpectedError( |
| 945 |
$db, |
| 946 |
"$fname: Cannot flush snapshot; " . |
| 947 |
"writes from transaction {$this->trxFname} are still pending ($fnames)" |
| 948 |
); |
| 949 |
} elseif ( |
| 950 |
$this->trxLevel() && |
| 951 |
$trxRoundId && |
| 952 |
$flush !== IDatabase::FLUSHING_INTERNAL && |
| 953 |
$flush !== IDatabase::FLUSHING_ALL_PEERS |
| 954 |
) { |
| 955 |
$this->logger->warning( |
| 956 |
"$fname: Expected mass snapshot flush of all peer transactions " . |
| 957 |
"in the explicit transactions round '{$trxRoundId}'", |
| 958 |
[ |
| 959 |
'exception' => new RuntimeException(), |
| 960 |
'db_log_category' => 'trx' |
| 961 |
] |
| 962 |
); |
| 963 |
} |
| 964 |
} |
| 965 |
|
| 966 |
public function onGetScopedLockAndFlush( IDatabase $db, $fname ) { |
| 967 |
if ( $this->writesOrCallbacksPending() ) { |
| 968 |
|
| 969 |
$fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() ); |
| 970 |
throw new DBUnexpectedError( |
| 971 |
$db, |
| 972 |
"$fname: Cannot flush pre-lock snapshot; " . |
| 973 |
"writes from transaction {$this->trxFname} are still pending ($fnames)" |
| 974 |
); |
| 975 |
} |
| 976 |
} |
| 977 |
} |