MediaWiki master
TransactionProfiler.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\Rdbms;
21
22use Psr\Log\LoggerAwareInterface;
23use Psr\Log\LoggerInterface;
24use Psr\Log\NullLogger;
25use RuntimeException;
26use Wikimedia\ScopedCallback;
28
39class TransactionProfiler implements LoggerAwareInterface {
41 private $logger;
43 private $statsFactory;
45 private $expect;
47 private $hits;
49 private $violations;
51 private $silenced;
52
57 private $dbTrxHoldingLocks;
58
63 private $dbTrxMethodTimes;
64
66 private $method;
67
69 private $wallClockOverride;
70
72 private const DB_LOCK_THRESHOLD_SEC = 3.0;
74 private const EVENT_THRESHOLD_SEC = 0.25;
75
77 private const EVENT_NAMES = [
78 'writes',
79 'queries',
80 'conns',
81 'masterConns',
82 'maxAffected',
83 'readQueryRows',
84 'readQueryTime',
85 'writeQueryTime'
86 ];
87
89 private const COUNTER_EVENT_NAMES = [
90 'writes',
91 'queries',
92 'conns',
93 'masterConns'
94 ];
95
97 private const FLD_LIMIT = 0;
99 private const FLD_FNAME = 1;
100
102 public const EXPECTATION_ANY = 'any';
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
128 public function setStatsFactory( StatsFactory $statsFactory ) {
129 $this->statsFactory = $statsFactory;
130 }
131
136 public function setRequestMethod( ?string $method ) {
137 $this->method = $method;
138 }
139
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
186 public function setExpectation( string $event, $limit, string $fname ) {
187 if ( !isset( $this->expect[$event] ) ) {
188 return; // obsolete/bogus expectation
189 }
190
191 if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) {
192 // New limit is more restrictive
193 $this->expect[$event] = [
194 self::FLD_LIMIT => $limit,
195 self::FLD_FNAME => $fname
196 ];
197 }
198 }
199
211 public function setExpectations( array $expects, string $fname ) {
212 foreach ( $expects as $event => $value ) {
213 $this->setExpectation( $event, $value, $fname );
214 }
215 }
216
226 public function resetExpectations() {
227 $this->initPlaceholderExpectations();
228 }
229
240 public function redefineExpectations( array $expects, string $fname ) {
241 $this->initPlaceholderExpectations();
242 $this->setExpectations( $expects, $fname );
243 }
244
254 public function recordConnection( $server, $db, bool $isPrimaryWithReplicas ) {
255 // Report when too many connections happen...
256 if ( $this->pingAndCheckThreshold( 'conns' ) ) {
257 $this->reportExpectationViolated(
258 'conns',
259 "[connect to $server ($db)]",
260 $this->hits['conns']
261 );
262 }
263
264 // Report when too many primary connections happen...
265 if ( $isPrimaryWithReplicas && $this->pingAndCheckThreshold( 'masterConns' ) ) {
266 $this->reportExpectationViolated(
267 'masterConns',
268 "[connect to $server ($db)]",
269 $this->hits['masterConns']
270 );
271 }
272 }
273
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' => [], // all connections involved
292 ];
293 $this->dbTrxMethodTimes[$name] = [];
294
295 foreach ( $this->dbTrxHoldingLocks as $name => &$info ) {
296 // Track all DBs in transactions for this transaction
297 $info['conns'][$name] = 1;
298 }
299 }
300
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 // Report when too many writes/queries happen...
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 // Report slow queries...
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 // Short-circuit
347 return;
348 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
349 // Not an important query nor slow enough
350 return;
351 }
352
353 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
354 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
355 if ( $lastQuery ) {
356 // Additional query in the trx...
357 $lastEnd = $lastQuery[2];
358 if ( $sTime >= $lastEnd ) {
359 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
360 // Add an entry representing the time spent doing non-queries
361 $this->dbTrxMethodTimes[$name][] = [ '...delay...', $lastEnd, $sTime ];
362 }
363 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
364 }
365 } else {
366 // First query in the trx...
367 if ( $sTime >= $info['start'] ) {
368 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
369 }
370 }
371 }
372 }
373
387 public function transactionWritingOut(
388 $server,
389 $db,
390 string $id,
391 float $writeTime,
392 int $affected
393 ) {
394 // Must match $name in transactionWritingIn()
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 // Warn if too much time was spend writing...
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 // Warn if too many rows were changed...
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 // Fill in the last non-query period...
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 // Check for any slow queries or non-query periods...
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
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
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
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 // First violation; check if this is a web request
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 // Avoid truncated JSON in Logstash (T349140)
537 'fullQuery' => mb_substr( $this->getRawSql( $query ), 0, 2000 ),
538 'dbHost' => $serverName
539 ]
540 );
541 }
542
547 private function getGeneralizedSql( $query ) {
548 return $query instanceof GeneralizedSql ? $query->stringify() : $query;
549 }
550
555 private function getRawSql( $query ) {
556 return $query instanceof GeneralizedSql ? $query->getRawSql() : $query;
557 }
558
563 private function getCurrentTime() {
564 return $this->wallClockOverride ?: microtime( true );
565 }
566
571 public function setMockTime( &$time ) {
572 $this->wallClockOverride =& $time;
573 }
574}
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.
resetExpectations()
Reset all performance expectations and hit counters.
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.
transactionWritingIn( $server, $db, string $id, float $startTime)
Mark a DB as in a transaction with one or more writes pending.
recordConnection( $server, $db, bool $isPrimaryWithReplicas)
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.
setStatsFactory(StatsFactory $statsFactory)
Set statsFactory.
StatsFactory Implementation.