| 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\LoggerAwareInterface; |
| 23 |
use Psr\Log\LoggerInterface; |
| 24 |
use Psr\Log\NullLogger; |
| 25 |
use RuntimeException; |
| 26 |
use Wikimedia\ScopedCallback; |
| 27 |
use Wikimedia\Stats\StatsFactory; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class TransactionProfiler implements LoggerAwareInterface { |
| 40 |
|
| 41 |
private $logger; |
| 42 |
|
| 43 |
private $statsFactory; |
| 44 |
|
| 45 |
private $expect; |
| 46 |
|
| 47 |
private $hits; |
| 48 |
|
| 49 |
private $violations; |
| 50 |
|
| 51 |
private $silenced; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $dbTrxHoldingLocks; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private $dbTrxMethodTimes; |
| 64 |
|
| 65 |
|
| 66 |
private $method; |
| 67 |
|
| 68 |
|
| 69 |
private $wallClockOverride; |
| 70 |
|
| 71 |
|
| 72 |
private const DB_LOCK_THRESHOLD_SEC = 3.0; |
| 73 |
|
| 74 |
private const EVENT_THRESHOLD_SEC = 0.25; |
| 75 |
|
| 76 |
|
| 77 |
private const EVENT_NAMES = [ |
| 78 |
'writes', |
| 79 |
'queries', |
| 80 |
'conns', |
| 81 |
'masterConns', |
| 82 |
'maxAffected', |
| 83 |
'readQueryRows', |
| 84 |
'readQueryTime', |
| 85 |
'writeQueryTime' |
| 86 |
]; |
| 87 |
|
| 88 |
|
| 89 |
private const COUNTER_EVENT_NAMES = [ |
| 90 |
'writes', |
| 91 |
'queries', |
| 92 |
'conns', |
| 93 |
'masterConns' |
| 94 |
]; |
| 95 |
|
| 96 |
|
| 97 |
private const FLD_LIMIT = 0; |
| 98 |
|
| 99 |
private const FLD_FNAME = 1; |
| 100 |
|
| 101 |
|
| 102 |
public const EXPECTATION_ANY = 'any'; |
| 103 |
|
| 104 |
public const EXPECTATION_REPLICAS_ONLY = 'replicas-only'; |
| 105 |
|
| 106 |
public function __construct() { |
| 107 |
$this->initPlaceholderExpectations(); |
| 108 |
|
| 109 |
$this->dbTrxHoldingLocks = []; |
| 110 |
$this->dbTrxMethodTimes = []; |
| 111 |
|
| 112 |
$this->silenced = array_fill_keys( self::EVENT_NAMES, 0 ); |
| 113 |
|
| 114 |
$this->setLogger( new NullLogger() ); |
| 115 |
$this->statsFactory = StatsFactory::newNull(); |
| 116 |
} |
| 117 |
|
| 118 |
public function setLogger( LoggerInterface $logger ) { |
| 119 |
$this->logger = $logger; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function setStatsFactory( StatsFactory $statsFactory ) { |
| 129 |
$this->statsFactory = $statsFactory; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function setRequestMethod( ?string $method ) { |
| 137 |
$this->method = $method; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function silenceForScope( string $type = self::EXPECTATION_ANY ) { |
| 154 |
if ( $type === self::EXPECTATION_REPLICAS_ONLY ) { |
| 155 |
$events = []; |
| 156 |
foreach ( [ 'writes', 'masterConns' ] as $event ) { |
| 157 |
if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) { |
| 158 |
$events[] = $event; |
| 159 |
} |
| 160 |
} |
| 161 |
} else { |
| 162 |
$events = self::EVENT_NAMES; |
| 163 |
} |
| 164 |
|
| 165 |
foreach ( $events as $event ) { |
| 166 |
++$this->silenced[$event]; |
| 167 |
} |
| 168 |
|
| 169 |
return new ScopedCallback( function () use ( $events ) { |
| 170 |
foreach ( $events as $event ) { |
| 171 |
--$this->silenced[$event]; |
| 172 |
} |
| 173 |
} ); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function setExpectation( string $event, $limit, string $fname ) { |
| 187 |
if ( !isset( $this->expect[$event] ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) { |
| 192 |
|
| 193 |
$this->expect[$event] = [ |
| 194 |
self::FLD_LIMIT => $limit, |
| 195 |
self::FLD_FNAME => $fname |
| 196 |
]; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function setExpectations( array $expects, string $fname ) { |
| 212 |
foreach ( $expects as $event => $value ) { |
| 213 |
$this->setExpectation( $event, $value, $fname ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function resetExpectations() { |
| 227 |
$this->initPlaceholderExpectations(); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function redefineExpectations( array $expects, string $fname ) { |
| 241 |
$this->initPlaceholderExpectations(); |
| 242 |
$this->setExpectations( $expects, $fname ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function recordConnection( $server, $db, bool $isPrimaryWithReplicas ) { |
| 255 |
|
| 256 |
if ( $this->pingAndCheckThreshold( 'conns' ) ) { |
| 257 |
$this->reportExpectationViolated( |
| 258 |
'conns', |
| 259 |
"[connect to $server ($db)]", |
| 260 |
$this->hits['conns'] |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if ( $isPrimaryWithReplicas && $this->pingAndCheckThreshold( 'masterConns' ) ) { |
| 266 |
$this->reportExpectationViolated( |
| 267 |
'masterConns', |
| 268 |
"[connect to $server ($db)]", |
| 269 |
$this->hits['masterConns'] |
| 270 |
); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function transactionWritingIn( $server, $db, string $id, float $startTime ) { |
| 285 |
$name = "{$db} {$server} TRX#$id"; |
| 286 |
if ( isset( $this->dbTrxHoldingLocks[$name] ) ) { |
| 287 |
$this->logger->warning( "Nested transaction for '$name' - out of sync." ); |
| 288 |
} |
| 289 |
$this->dbTrxHoldingLocks[$name] = [ |
| 290 |
'start' => $startTime, |
| 291 |
'conns' => [], |
| 292 |
]; |
| 293 |
$this->dbTrxMethodTimes[$name] = []; |
| 294 |
|
| 295 |
foreach ( $this->dbTrxHoldingLocks as $name => &$info ) { |
| 296 |
|
| 297 |
$info['conns'][$name] = 1; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public function recordQueryCompletion( |
| 314 |
$query, |
| 315 |
float $sTime, |
| 316 |
bool $isWrite, |
| 317 |
?int $rowCount, |
| 318 |
string $trxId, |
| 319 |
?string $serverName = null |
| 320 |
) { |
| 321 |
$eTime = $this->getCurrentTime(); |
| 322 |
$elapsed = ( $eTime - $sTime ); |
| 323 |
|
| 324 |
if ( $isWrite && $this->isAboveThreshold( $rowCount, 'maxAffected' ) ) { |
| 325 |
$this->reportExpectationViolated( 'maxAffected', $query, $rowCount, $trxId, $serverName ); |
| 326 |
} elseif ( !$isWrite && $this->isAboveThreshold( $rowCount, 'readQueryRows' ) ) { |
| 327 |
$this->reportExpectationViolated( 'readQueryRows', $query, $rowCount, $trxId, $serverName ); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
if ( $this->pingAndCheckThreshold( 'queries' ) ) { |
| 332 |
$this->reportExpectationViolated( 'queries', $query, $this->hits['queries'], $trxId, $serverName ); |
| 333 |
} |
| 334 |
if ( $isWrite && $this->pingAndCheckThreshold( 'writes' ) ) { |
| 335 |
$this->reportExpectationViolated( 'writes', $query, $this->hits['writes'], $trxId, $serverName ); |
| 336 |
} |
| 337 |
|
| 338 |
if ( !$isWrite && $this->isAboveThreshold( $elapsed, 'readQueryTime' ) ) { |
| 339 |
$this->reportExpectationViolated( 'readQueryTime', $query, $elapsed, $trxId, $serverName ); |
| 340 |
} |
| 341 |
if ( $isWrite && $this->isAboveThreshold( $elapsed, 'writeQueryTime' ) ) { |
| 342 |
$this->reportExpectationViolated( 'writeQueryTime', $query, $elapsed, $trxId, $serverName ); |
| 343 |
} |
| 344 |
|
| 345 |
if ( !$this->dbTrxHoldingLocks ) { |
| 346 |
|
| 347 |
return; |
| 348 |
} elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) { |
| 349 |
|
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
foreach ( $this->dbTrxHoldingLocks as $name => $info ) { |
| 354 |
$lastQuery = end( $this->dbTrxMethodTimes[$name] ); |
| 355 |
if ( $lastQuery ) { |
| 356 |
|
| 357 |
$lastEnd = $lastQuery[2]; |
| 358 |
if ( $sTime >= $lastEnd ) { |
| 359 |
if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) { |
| 360 |
|
| 361 |
$this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $sTime ]; |
| 362 |
} |
| 363 |
$this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ]; |
| 364 |
} |
| 365 |
} else { |
| 366 |
|
| 367 |
if ( $sTime >= $info['start'] ) { |
| 368 |
$this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ]; |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public function transactionWritingOut( |
| 388 |
$server, |
| 389 |
$db, |
| 390 |
string $id, |
| 391 |
float $writeTime, |
| 392 |
int $affected |
| 393 |
) { |
| 394 |
|
| 395 |
$name = "{$db} {$server} TRX#$id"; |
| 396 |
if ( !isset( $this->dbTrxMethodTimes[$name] ) ) { |
| 397 |
$this->logger->warning( "Detected no transaction for '$name' - out of sync." ); |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
$slow = false; |
| 402 |
|
| 403 |
|
| 404 |
if ( $this->isAboveThreshold( $writeTime, 'writeQueryTime' ) ) { |
| 405 |
$this->reportExpectationViolated( |
| 406 |
'writeQueryTime', |
| 407 |
"[transaction writes to {$db} at {$server}]", |
| 408 |
$writeTime, |
| 409 |
$id |
| 410 |
); |
| 411 |
$slow = true; |
| 412 |
} |
| 413 |
|
| 414 |
if ( $this->isAboveThreshold( $affected, 'maxAffected' ) ) { |
| 415 |
$this->reportExpectationViolated( |
| 416 |
'maxAffected', |
| 417 |
"[transaction writes to {$db} at {$server}]", |
| 418 |
$affected, |
| 419 |
$id |
| 420 |
); |
| 421 |
} |
| 422 |
|
| 423 |
$lastQuery = end( $this->dbTrxMethodTimes[$name] ); |
| 424 |
if ( $lastQuery ) { |
| 425 |
$now = $this->getCurrentTime(); |
| 426 |
$lastEnd = $lastQuery[2]; |
| 427 |
if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) { |
| 428 |
$this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $now ]; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
foreach ( $this->dbTrxMethodTimes[$name] as $info ) { |
| 433 |
$elapsed = ( $info[2] - $info[1] ); |
| 434 |
if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) { |
| 435 |
$slow = true; |
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
if ( $slow ) { |
| 440 |
$trace = ''; |
| 441 |
foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) { |
| 442 |
$trace .= sprintf( |
| 443 |
"%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) ); |
| 444 |
} |
| 445 |
$this->logger->warning( "Suboptimal transaction [{dbs}]:\n{trace}", [ |
| 446 |
'dbs' => implode( ', ', array_keys( $this->dbTrxHoldingLocks[$name]['conns'] ) ), |
| 447 |
'trace' => mb_substr( $trace, 0, 2000 ) |
| 448 |
] ); |
| 449 |
} |
| 450 |
unset( $this->dbTrxHoldingLocks[$name] ); |
| 451 |
unset( $this->dbTrxMethodTimes[$name] ); |
| 452 |
} |
| 453 |
|
| 454 |
private function initPlaceholderExpectations() { |
| 455 |
$this->expect = array_fill_keys( |
| 456 |
self::EVENT_NAMES, |
| 457 |
[ self::FLD_LIMIT => INF, self::FLD_FNAME => null ] |
| 458 |
); |
| 459 |
|
| 460 |
$this->hits = array_fill_keys( self::COUNTER_EVENT_NAMES, 0 ); |
| 461 |
$this->violations = array_fill_keys( self::EVENT_NAMES, 0 ); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
private function isAboveThreshold( $value, string $event ) { |
| 470 |
if ( $this->silenced[$event] > 0 ) { |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
return ( $value > $this->expect[$event][self::FLD_LIMIT] ); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
private function pingAndCheckThreshold( string $event ) { |
| 482 |
if ( $this->silenced[$event] > 0 ) { |
| 483 |
return false; |
| 484 |
} |
| 485 |
|
| 486 |
$newValue = ++$this->hits[$event]; |
| 487 |
$limit = $this->expect[$event][self::FLD_LIMIT]; |
| 488 |
|
| 489 |
return ( $newValue > $limit ); |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
private function reportExpectationViolated( |
| 500 |
$event, |
| 501 |
$query, |
| 502 |
$actual, |
| 503 |
?string $trxId = null, |
| 504 |
?string $serverName = null |
| 505 |
) { |
| 506 |
$violations = ++$this->violations[$event]; |
| 507 |
|
| 508 |
if ( $violations === 1 && $this->method !== null ) { |
| 509 |
$this->statsFactory->getCounter( 'rdbms_trxprofiler_warnings_total' ) |
| 510 |
->setLabel( 'event', $event ) |
| 511 |
->setLabel( 'method', $this->method ) |
| 512 |
->copyToStatsdAt( "rdbms_trxprofiler_warnings.$event.{$this->method}" ) |
| 513 |
->increment(); |
| 514 |
} |
| 515 |
|
| 516 |
$max = $this->expect[$event][self::FLD_LIMIT]; |
| 517 |
$by = $this->expect[$event][self::FLD_FNAME]; |
| 518 |
|
| 519 |
$message = "Expectation ($event <= $max) by $by not met (actual: {actualSeconds})"; |
| 520 |
if ( $trxId ) { |
| 521 |
$message .= ' in trx #{trxId}'; |
| 522 |
} |
| 523 |
$message .= ":\n{query}\n"; |
| 524 |
|
| 525 |
$this->logger->warning( |
| 526 |
$message, |
| 527 |
[ |
| 528 |
'db_log_category' => 'performance', |
| 529 |
'measure' => $event, |
| 530 |
'maxSeconds' => $max, |
| 531 |
'by' => $by, |
| 532 |
'actualSeconds' => $actual, |
| 533 |
'query' => $this->getGeneralizedSql( $query ), |
| 534 |
'exception' => new RuntimeException(), |
| 535 |
'trxId' => $trxId, |
| 536 |
|
| 537 |
'fullQuery' => mb_substr( $this->getRawSql( $query ), 0, 2000 ), |
| 538 |
'dbHost' => $serverName |
| 539 |
] |
| 540 |
); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
private function getGeneralizedSql( $query ) { |
| 548 |
if ( $query instanceof Query ) { |
| 549 |
return $query->getCleanedSql(); |
| 550 |
} |
| 551 |
return $query instanceof GeneralizedSql ? $query->stringify() : $query; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
private function getRawSql( $query ) { |
| 559 |
if ( $query instanceof Query ) { |
| 560 |
return $query->getSQL(); |
| 561 |
} |
| 562 |
return $query instanceof GeneralizedSql ? $query->getRawSql() : $query; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
private function getCurrentTime() { |
| 570 |
return $this->wallClockOverride ?: microtime( true ); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
public function setMockTime( &$time ) { |
| 578 |
$this->wallClockOverride =& $time; |
| 579 |
} |
| 580 |
} |