MediaWiki master
TransactionProfiler.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\Rdbms;
21
22use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
24use Psr\Log\LoggerAwareInterface;
25use Psr\Log\LoggerInterface;
26use Psr\Log\NullLogger;
27use RuntimeException;
29use Wikimedia\ScopedCallback;
30
41class TransactionProfiler implements LoggerAwareInterface, StatsdAwareInterface {
43 private $logger;
45 private $stats;
47 private $expect;
49 private $hits;
51 private $violations;
53 private $silenced;
54
59 private $dbTrxHoldingLocks;
60
65 private $dbTrxMethodTimes;
66
68 private $method;
69
71 private $wallClockOverride;
72
74 private const DB_LOCK_THRESHOLD_SEC = 3.0;
76 private const EVENT_THRESHOLD_SEC = 0.25;
77
79 private const EVENT_NAMES = [
80 'writes',
81 'queries',
82 'conns',
83 'masterConns',
84 'maxAffected',
85 'readQueryRows',
86 'readQueryTime',
87 'writeQueryTime'
88 ];
89
91 private const COUNTER_EVENT_NAMES = [
92 'writes',
93 'queries',
94 'conns',
95 'masterConns'
96 ];
97
99 private const FLD_LIMIT = 0;
101 private const FLD_FNAME = 1;
102
104 public const EXPECTATION_ANY = 'any';
106 public const EXPECTATION_REPLICAS_ONLY = 'replicas-only';
107
108 public function __construct() {
109 $this->initPlaceholderExpectations();
110
111 $this->dbTrxHoldingLocks = [];
112 $this->dbTrxMethodTimes = [];
113
114 $this->silenced = array_fill_keys( self::EVENT_NAMES, 0 );
115
116 $this->setLogger( new NullLogger() );
117 $this->setStatsdDataFactory( new NullStatsdDataFactory() );
118 }
119
120 public function setLogger( LoggerInterface $logger ) {
121 $this->logger = $logger;
122 }
123
124 public function setStatsdDataFactory( StatsdDataFactoryInterface $statsFactory ) {
125 $this->stats = $statsFactory;
126 }
127
132 public function setRequestMethod( ?string $method ) {
133 $this->method = $method;
134 }
135
149 public function silenceForScope( string $type = self::EXPECTATION_ANY ) {
150 if ( $type === self::EXPECTATION_REPLICAS_ONLY ) {
151 $events = [];
152 foreach ( [ 'writes', 'masterConns' ] as $event ) {
153 if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) {
154 $events[] = $event;
155 }
156 }
157 } else {
158 $events = self::EVENT_NAMES;
159 }
160
161 foreach ( $events as $event ) {
162 ++$this->silenced[$event];
163 }
164
165 return new ScopedCallback( function () use ( $events ) {
166 foreach ( $events as $event ) {
167 --$this->silenced[$event];
168 }
169 } );
170 }
171
182 public function setExpectation( string $event, $limit, string $fname ) {
183 if ( !isset( $this->expect[$event] ) ) {
184 return; // obsolete/bogus expectation
185 }
186
187 if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) {
188 // New limit is more restrictive
189 $this->expect[$event] = [
190 self::FLD_LIMIT => $limit,
191 self::FLD_FNAME => $fname
192 ];
193 }
194 }
195
207 public function setExpectations( array $expects, string $fname ) {
208 foreach ( $expects as $event => $value ) {
209 $this->setExpectation( $event, $value, $fname );
210 }
211 }
212
222 public function resetExpectations() {
223 $this->initPlaceholderExpectations();
224 }
225
236 public function redefineExpectations( array $expects, string $fname ) {
237 $this->initPlaceholderExpectations();
238 $this->setExpectations( $expects, $fname );
239 }
240
250 public function recordConnection( $server, $db, bool $isPrimary ) {
251 // Report when too many connections happen...
252 if ( $this->pingAndCheckThreshold( 'conns' ) ) {
253 $this->reportExpectationViolated(
254 'conns',
255 "[connect to $server ($db)]",
256 $this->hits['conns']
257 );
258 }
259
260 // Report when too many primary connections happen...
261 if ( $isPrimary && $this->pingAndCheckThreshold( 'masterConns' ) ) {
262 $this->reportExpectationViolated(
263 'masterConns',
264 "[connect to $server ($db)]",
265 $this->hits['masterConns']
266 );
267 }
268 }
269
279 public function transactionWritingIn( $server, $db, string $id ) {
280 $name = "{$db} {$server} TRX#$id";
281 if ( isset( $this->dbTrxHoldingLocks[$name] ) ) {
282 $this->logger->warning( "Nested transaction for '$name' - out of sync." );
283 }
284 $this->dbTrxHoldingLocks[$name] = [
285 'start' => $this->getCurrentTime(),
286 'conns' => [], // all connections involved
287 ];
288 $this->dbTrxMethodTimes[$name] = [];
289
290 foreach ( $this->dbTrxHoldingLocks as $name => &$info ) {
291 // Track all DBs in transactions for this transaction
292 $info['conns'][$name] = 1;
293 }
294 }
295
308 public function recordQueryCompletion(
309 $query,
310 float $sTime,
311 bool $isWrite,
312 ?int $rowCount,
313 string $trxId,
314 ?string $serverName = null
315 ) {
316 $eTime = $this->getCurrentTime();
317 $elapsed = ( $eTime - $sTime );
318
319 if ( $isWrite && $this->isAboveThreshold( $rowCount, 'maxAffected' ) ) {
320 $this->reportExpectationViolated( 'maxAffected', $query, $rowCount, $trxId, $serverName );
321 } elseif ( !$isWrite && $this->isAboveThreshold( $rowCount, 'readQueryRows' ) ) {
322 $this->reportExpectationViolated( 'readQueryRows', $query, $rowCount, $trxId, $serverName );
323 }
324
325 // Report when too many writes/queries happen...
326 if ( $this->pingAndCheckThreshold( 'queries' ) ) {
327 $this->reportExpectationViolated( 'queries', $query, $this->hits['queries'], $trxId, $serverName );
328 }
329 if ( $isWrite && $this->pingAndCheckThreshold( 'writes' ) ) {
330 $this->reportExpectationViolated( 'writes', $query, $this->hits['writes'], $trxId, $serverName );
331 }
332 // Report slow queries...
333 if ( !$isWrite && $this->isAboveThreshold( $elapsed, 'readQueryTime' ) ) {
334 $this->reportExpectationViolated( 'readQueryTime', $query, $elapsed, $trxId, $serverName );
335 }
336 if ( $isWrite && $this->isAboveThreshold( $elapsed, 'writeQueryTime' ) ) {
337 $this->reportExpectationViolated( 'writeQueryTime', $query, $elapsed, $trxId, $serverName );
338 }
339
340 if ( !$this->dbTrxHoldingLocks ) {
341 // Short-circuit
342 return;
343 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
344 // Not an important query nor slow enough
345 return;
346 }
347
348 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
349 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
350 if ( $lastQuery ) {
351 // Additional query in the trx...
352 $lastEnd = $lastQuery[2];
353 if ( $sTime >= $lastEnd ) {
354 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
355 // Add an entry representing the time spent doing non-queries
356 $this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $sTime ];
357 }
358 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
359 }
360 } else {
361 // First query in the trx...
362 if ( $sTime >= $info['start'] ) {
363 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
364 }
365 }
366 }
367 }
368
382 public function transactionWritingOut(
383 $server,
384 $db,
385 string $id,
386 float $writeTime,
387 int $affected
388 ) {
389 // Must match $name in transactionWritingIn()
390 $name = "{$db} {$server} TRX#$id";
391 if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
392 $this->logger->warning( "Detected no transaction for '$name' - out of sync." );
393 return;
394 }
395
396 $slow = false;
397
398 // Warn if too much time was spend writing...
399 if ( $this->isAboveThreshold( $writeTime, 'writeQueryTime' ) ) {
400 $this->reportExpectationViolated(
401 'writeQueryTime',
402 "[transaction writes to {$db} at {$server}]",
403 $writeTime,
404 $id
405 );
406 $slow = true;
407 }
408 // Warn if too many rows were changed...
409 if ( $this->isAboveThreshold( $affected, 'maxAffected' ) ) {
410 $this->reportExpectationViolated(
411 'maxAffected',
412 "[transaction writes to {$db} at {$server}]",
413 $affected,
414 $id
415 );
416 }
417 // Fill in the last non-query period...
418 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
419 if ( $lastQuery ) {
420 $now = $this->getCurrentTime();
421 $lastEnd = $lastQuery[2];
422 if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
423 $this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $now ];
424 }
425 }
426 // Check for any slow queries or non-query periods...
427 foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
428 $elapsed = ( $info[2] - $info[1] );
429 if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) {
430 $slow = true;
431 break;
432 }
433 }
434 if ( $slow ) {
435 $trace = '';
436 foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
437 $trace .= sprintf(
438 "%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) );
439 }
440 $this->logger->warning( "Suboptimal transaction [{dbs}]:\n{trace}", [
441 'dbs' => implode( ', ', array_keys( $this->dbTrxHoldingLocks[$name]['conns'] ) ),
442 'trace' => mb_substr( $trace, 0, 2000 )
443 ] );
444 }
445 unset( $this->dbTrxHoldingLocks[$name] );
446 unset( $this->dbTrxMethodTimes[$name] );
447 }
448
449 private function initPlaceholderExpectations() {
450 $this->expect = array_fill_keys(
451 self::EVENT_NAMES,
452 [ self::FLD_LIMIT => INF, self::FLD_FNAME => null ]
453 );
454
455 $this->hits = array_fill_keys( self::COUNTER_EVENT_NAMES, 0 );
456 $this->violations = array_fill_keys( self::EVENT_NAMES, 0 );
457 }
458
464 private function isAboveThreshold( $value, string $event ) {
465 if ( $this->silenced[$event] > 0 ) {
466 return false;
467 }
468
469 return ( $value > $this->expect[$event][self::FLD_LIMIT] );
470 }
471
476 private function pingAndCheckThreshold( string $event ) {
477 if ( $this->silenced[$event] > 0 ) {
478 return false;
479 }
480
481 $newValue = ++$this->hits[$event];
482 $limit = $this->expect[$event][self::FLD_LIMIT];
483
484 return ( $newValue > $limit );
485 }
486
494 private function reportExpectationViolated(
495 $event,
496 $query,
497 $actual,
498 ?string $trxId = null,
499 ?string $serverName = null
500 ) {
501 $violations = ++$this->violations[$event];
502 // First violation; check if this is a web request
503 if ( $violations === 1 && $this->method !== null ) {
504 $this->stats->increment( "rdbms_trxprofiler_warnings.$event.{$this->method}" );
505 }
506
507 $max = $this->expect[$event][self::FLD_LIMIT];
508 $by = $this->expect[$event][self::FLD_FNAME];
509
510 $message = "Expectation ($event <= $max) by $by not met (actual: {actualSeconds})";
511 if ( $trxId ) {
512 $message .= ' in trx #{trxId}';
513 }
514 $message .= ":\n{query}\n";
515
516 $this->logger->warning(
517 $message,
518 [
519 'db_log_category' => 'performance',
520 'measure' => $event,
521 'maxSeconds' => $max,
522 'by' => $by,
523 'actualSeconds' => $actual,
524 'query' => $this->getGeneralizedSql( $query ),
525 'exception' => new RuntimeException(),
526 'trxId' => $trxId,
527 // Avoid truncated JSON in Logstash (T349140)
528 'fullQuery' => mb_substr( $this->getRawSql( $query ), 0, 2000 ),
529 'dbHost' => $serverName
530 ]
531 );
532 }
533
538 private function getGeneralizedSql( $query ) {
539 if ( $query instanceof Query ) {
540 return $query->getCleanedSql();
541 }
542 return $query instanceof GeneralizedSql ? $query->stringify() : $query;
543 }
544
549 private function getRawSql( $query ) {
550 if ( $query instanceof Query ) {
551 return $query->getSQL();
552 }
553 return $query instanceof GeneralizedSql ? $query->getRawSql() : $query;
554 }
555
560 private function getCurrentTime() {
561 return $this->wallClockOverride ?: microtime( true );
562 }
563
568 public function setMockTime( &$time ) {
569 $this->wallClockOverride =& $time;
570 }
571}
Detect high-contention DB queries via profiling calls.
transactionWritingOut( $server, $db, string $id, float $writeTime, int $affected)
Mark a DB as no longer in a transaction.
transactionWritingIn( $server, $db, string $id)
Mark a DB as in a transaction with one or more writes pending.
resetExpectations()
Reset all performance expectations and hit counters.
setStatsdDataFactory(StatsdDataFactoryInterface $statsFactory)
Sets a StatsdDataFactory instance on the object.
const EXPECTATION_ANY
Any type of expectation.
redefineExpectations(array $expects, string $fname)
Clear all expectations and hit counters and set new performance expectations.
setExpectations(array $expects, string $fname)
Set one or multiple performance expectations.
recordConnection( $server, $db, bool $isPrimary)
Mark a DB as having been connected to with a new handle.
const EXPECTATION_REPLICAS_ONLY
Any expectations about replica usage never occurring.
setExpectation(string $event, $limit, string $fname)
Set performance expectations.
silenceForScope(string $type=self::EXPECTATION_ANY)
Temporarily ignore expectations until the returned object goes out of scope.
recordQueryCompletion( $query, float $sTime, bool $isWrite, ?int $rowCount, string $trxId, ?string $serverName=null)
Register the name and time of a method for slow DB trx detection.
Describes a Statsd aware interface.