MediaWiki  master
Wikimedia\Rdbms\TransactionProfiler Class Reference

Detect high-contention DB queries via profiling calls. More...

Inheritance diagram for Wikimedia\Rdbms\TransactionProfiler:
Collaboration diagram for Wikimedia\Rdbms\TransactionProfiler:

Public Member Functions

 __construct ()
 
 recordConnection ( $server, $db, bool $isPrimary)
 Mark a DB as having been connected to with a new handle. More...
 
 recordQueryCompletion ( $query, float $sTime, bool $isWrite, ?int $rowCount)
 Register the name and time of a method for slow DB trx detection. More...
 
 redefineExpectations (array $expects, string $fname)
 Clear all expectations and hit counters and set new performance expectations. More...
 
 resetExpectations ()
 Reset all performance expectations and hit counters. More...
 
 setExpectation (string $event, $limit, string $fname)
 Set performance expectations. More...
 
 setExpectations (array $expects, string $fname)
 Set one or multiple performance expectations. More...
 
 setLogger (LoggerInterface $logger)
 
 setSilenced (bool $value)
 
 silenceForScope ()
 Disable the logging of warnings until the returned object goes out of scope or is consumed. More...
 
 transactionWritingIn ( $server, $db, string $id)
 Mark a DB as in a transaction with one or more writes pending. More...
 
 transactionWritingOut ( $server, $db, string $id, float $writeTime, int $affected)
 Mark a DB as no longer in a transaction. More...
 

Private Member Functions

 initPlaceholderExpectations ()
 
 isAboveThreshold ( $value, string $expectation)
 
 pingAndCheckThreshold (string $expectation)
 
 reportExpectationViolated ( $expectation, $query, $actual)
 

Static Private Member Functions

static queryString ( $query)
 

Private Attributes

array< string, array > $dbTrxHoldingLocks
 Map of (trx ID => (write start time, list of DBs involved)) More...
 
array[][] $dbTrxMethodTimes
 Map of (trx ID => list of (query name, start time, end time)) More...
 
array< string, array > $expect
 Map of (name => threshold value) More...
 
array< string, int > $hits
 Map of (name => current hits) More...
 
LoggerInterface $logger
 
bool $silenced
 Whether logging is disabled. More...
 
const COUNTER_EVENT_NAMES
 List of event names with hit counters. More...
 
const DB_LOCK_THRESHOLD_SEC = 3.0
 Treat locks as long-running if they last longer than this many seconds. More...
 
const EVENT_NAMES
 List of event names. More...
 
const EVENT_THRESHOLD_SEC = 0.25
 Include events in any violation logs if they last longer than this many seconds. More...
 
const FLD_FNAME = 1
 Key to the function that set the max expected value. More...
 
const FLD_LIMIT = 0
 Key to max expected value. More...
 

Detailed Description

Detect high-contention DB queries via profiling calls.

This class is meant to work with an IDatabase object, which manages queries.

Access: internal
For use by Database only
Since
1.24

Definition at line 40 of file TransactionProfiler.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Rdbms\TransactionProfiler::__construct ( )

Member Function Documentation

◆ initPlaceholderExpectations()

Wikimedia\Rdbms\TransactionProfiler::initPlaceholderExpectations ( )
private

◆ isAboveThreshold()

Wikimedia\Rdbms\TransactionProfiler::isAboveThreshold (   $value,
string  $expectation 
)
private
Parameters
float | int$value
string$expectation
Returns
bool

Definition at line 410 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\recordQueryCompletion(), and Wikimedia\Rdbms\TransactionProfiler\transactionWritingOut().

◆ pingAndCheckThreshold()

Wikimedia\Rdbms\TransactionProfiler::pingAndCheckThreshold ( string  $expectation)
private
Parameters
string$expectation
Returns
bool

Definition at line 418 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\recordConnection(), and Wikimedia\Rdbms\TransactionProfiler\recordQueryCompletion().

◆ queryString()

static Wikimedia\Rdbms\TransactionProfiler::queryString (   $query)
staticprivate
Parameters
GeneralizedSql | string$query
Returns
string

Definition at line 453 of file TransactionProfiler.php.

References Wikimedia\Rdbms\GeneralizedSql\stringify().

◆ recordConnection()

Wikimedia\Rdbms\TransactionProfiler::recordConnection (   $server,
  $db,
bool  $isPrimary 
)

Mark a DB as having been connected to with a new handle.

Note that there can be multiple connections to a single DB.

Parameters
string$serverDB server
string | null$dbDB name
bool$isPrimary

Definition at line 209 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\pingAndCheckThreshold(), and Wikimedia\Rdbms\TransactionProfiler\reportExpectationViolated().

◆ recordQueryCompletion()

Wikimedia\Rdbms\TransactionProfiler::recordQueryCompletion (   $query,
float  $sTime,
bool  $isWrite,
?int  $rowCount 
)

Register the name and time of a method for slow DB trx detection.

This assumes that all queries are synchronous (non-overlapping)

Parameters
string | GeneralizedSql$queryFunction name or generalized SQL
float$sTimeStarting UNIX wall time
bool$isWriteWhether this is a write query
int | null$rowCountNumber of affected/read rows

Definition at line 265 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\isAboveThreshold(), Wikimedia\Rdbms\TransactionProfiler\pingAndCheckThreshold(), and Wikimedia\Rdbms\TransactionProfiler\reportExpectationViolated().

◆ redefineExpectations()

Wikimedia\Rdbms\TransactionProfiler::redefineExpectations ( array  $expects,
string  $fname 
)

Clear all expectations and hit counters and set new performance expectations.

Use this to apply a totally different set of expectations for a different part of the request, such as during "post-send" (execution after HTTP response completion)

Parameters
array$expectsMap of (event => limit)
string$fname
Since
1.33

Definition at line 195 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\initPlaceholderExpectations(), and Wikimedia\Rdbms\TransactionProfiler\setExpectations().

◆ reportExpectationViolated()

Wikimedia\Rdbms\TransactionProfiler::reportExpectationViolated (   $expectation,
  $query,
  $actual 
)
private

◆ resetExpectations()

Wikimedia\Rdbms\TransactionProfiler::resetExpectations ( )

Reset all performance expectations and hit counters.

Use this for unit testing or before applying a totally different set of expectations for a different part of the request, such as during "post-send" (execution after HTTP response completion)

Since
1.25

Definition at line 181 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\initPlaceholderExpectations().

◆ setExpectation()

Wikimedia\Rdbms\TransactionProfiler::setExpectation ( string  $event,
  $limit,
string  $fname 
)

Set performance expectations.

With conflicting expectations, the most narrow ones will be used

Parameters
string$eventOne of the names in TransactionProfiler::EVENT_NAMES
float | int$limitMaximum event count, event value, or total event value
string$fnameCaller
Since
1.25

Definition at line 141 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\setExpectations().

◆ setExpectations()

Wikimedia\Rdbms\TransactionProfiler::setExpectations ( array  $expects,
string  $fname 
)

Set one or multiple performance expectations.

With conflicting expectations, the most narrow ones will be used

Use this to initialize expectations or make them stricter mid-request

Parameters
array$expectsMap of (event => limit)
string$fname
Since
1.26

Definition at line 166 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\setExpectation().

Referenced by Wikimedia\Rdbms\TransactionProfiler\redefineExpectations().

◆ setLogger()

Wikimedia\Rdbms\TransactionProfiler::setLogger ( LoggerInterface  $logger)

◆ setSilenced()

Wikimedia\Rdbms\TransactionProfiler::setSilenced ( bool  $value)
Parameters
bool$value
Returns
bool Old value
Since
1.28

Definition at line 113 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\$silenced.

Referenced by Wikimedia\Rdbms\TransactionProfiler\silenceForScope().

◆ silenceForScope()

Wikimedia\Rdbms\TransactionProfiler::silenceForScope ( )

Disable the logging of warnings until the returned object goes out of scope or is consumed.

Returns
ScopedCallback

Definition at line 124 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\setSilenced().

◆ transactionWritingIn()

Wikimedia\Rdbms\TransactionProfiler::transactionWritingIn (   $server,
  $db,
string  $id 
)

Mark a DB as in a transaction with one or more writes pending.

Note that there can be multiple connections to a single DB.

Parameters
string$serverDB server
string | null$dbDB name
string$idID string of transaction

Definition at line 238 of file TransactionProfiler.php.

◆ transactionWritingOut()

Wikimedia\Rdbms\TransactionProfiler::transactionWritingOut (   $server,
  $db,
string  $id,
float  $writeTime,
int  $affected 
)

Mark a DB as no longer in a transaction.

This will check if locks are possibly held for longer than needed and log any affected transactions to a special DB log. Note that there can be multiple connections to a single DB.

Parameters
string$serverDB server
string | null$dbDB name
string$idID string of transaction
float$writeTimeTime spent in write queries
int$affectedNumber of rows affected by writes

Definition at line 332 of file TransactionProfiler.php.

References Wikimedia\Rdbms\TransactionProfiler\isAboveThreshold(), and Wikimedia\Rdbms\TransactionProfiler\reportExpectationViolated().

Member Data Documentation

◆ $dbTrxHoldingLocks

array<string,array> Wikimedia\Rdbms\TransactionProfiler::$dbTrxHoldingLocks
private

Map of (trx ID => (write start time, list of DBs involved))

Definition at line 52 of file TransactionProfiler.php.

◆ $dbTrxMethodTimes

array [][] Wikimedia\Rdbms\TransactionProfiler::$dbTrxMethodTimes
private

Map of (trx ID => list of (query name, start time, end time))

Definition at line 58 of file TransactionProfiler.php.

◆ $expect

array<string,array> Wikimedia\Rdbms\TransactionProfiler::$expect
private

Map of (name => threshold value)

Definition at line 44 of file TransactionProfiler.php.

◆ $hits

array<string,int> Wikimedia\Rdbms\TransactionProfiler::$hits
private

Map of (name => current hits)

Definition at line 46 of file TransactionProfiler.php.

◆ $logger

LoggerInterface Wikimedia\Rdbms\TransactionProfiler::$logger
private

◆ $silenced

bool Wikimedia\Rdbms\TransactionProfiler::$silenced
private

Whether logging is disabled.

Definition at line 61 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\setSilenced().

◆ COUNTER_EVENT_NAMES

const Wikimedia\Rdbms\TransactionProfiler::COUNTER_EVENT_NAMES
private
Initial value:
= [
'writes',
'queries',
'conns',
'masterConns'
]

List of event names with hit counters.

Definition at line 81 of file TransactionProfiler.php.

◆ DB_LOCK_THRESHOLD_SEC

const Wikimedia\Rdbms\TransactionProfiler::DB_LOCK_THRESHOLD_SEC = 3.0
private

Treat locks as long-running if they last longer than this many seconds.

Definition at line 64 of file TransactionProfiler.php.

◆ EVENT_NAMES

const Wikimedia\Rdbms\TransactionProfiler::EVENT_NAMES
private
Initial value:
= [
'writes',
'queries',
'conns',
'masterConns',
'maxAffected',
'readQueryRows',
'readQueryTime',
'writeQueryTime'
]

List of event names.

Definition at line 69 of file TransactionProfiler.php.

◆ EVENT_THRESHOLD_SEC

const Wikimedia\Rdbms\TransactionProfiler::EVENT_THRESHOLD_SEC = 0.25
private

Include events in any violation logs if they last longer than this many seconds.

Definition at line 66 of file TransactionProfiler.php.

◆ FLD_FNAME

const Wikimedia\Rdbms\TransactionProfiler::FLD_FNAME = 1
private

Key to the function that set the max expected value.

Definition at line 91 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\reportExpectationViolated().

◆ FLD_LIMIT

const Wikimedia\Rdbms\TransactionProfiler::FLD_LIMIT = 0
private

Key to max expected value.

Definition at line 89 of file TransactionProfiler.php.

Referenced by Wikimedia\Rdbms\TransactionProfiler\reportExpectationViolated().


The documentation for this class was generated from the following file: