94 $this->initPlaceholderExpectations();
96 $this->dbTrxHoldingLocks = [];
97 $this->dbTrxMethodTimes = [];
99 $this->silenced = array_fill_keys( self::EVENT_NAMES, 0 );
102 $this->statsFactory = StatsFactory::newNull();
140 public function silenceForScope(
string $type = self::EXPECTATION_ANY ): ScopedCallback {
141 if ( $type === self::EXPECTATION_REPLICAS_ONLY ) {
143 foreach ( [
'writes',
'masterConns' ] as $event ) {
144 if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) {
149 $events = self::EVENT_NAMES;
152 foreach ( $events as $event ) {
153 ++$this->silenced[$event];
156 return new ScopedCallback(
function () use ( $events ) {
157 foreach ( $events as $event ) {
158 --$this->silenced[$event];
200 if ( !isset( $this->expect[$event] ) ) {
204 if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) {
206 $this->expect[$event] = [
207 self::FLD_LIMIT => $limit,
208 self::FLD_FNAME => $fname
355 ?
string $serverName =
null
357 $eTime = $this->getCurrentTime();
358 $elapsed = ( $eTime - $sTime );
360 if ( $isWrite && $this->isAboveThreshold( $rowCount,
'maxAffected' ) ) {
361 $this->reportExpectationViolated(
'maxAffected', $query, $rowCount, $trxId, $serverName );
362 } elseif ( !$isWrite && $this->isAboveThreshold( $rowCount,
'readQueryRows' ) ) {
363 $this->reportExpectationViolated(
'readQueryRows', $query, $rowCount, $trxId, $serverName );
367 if ( $this->pingAndCheckThreshold(
'queries' ) ) {
368 $this->reportExpectationViolated(
'queries', $query, $this->hits[
'queries'], $trxId, $serverName );
370 if ( $isWrite && $this->pingAndCheckThreshold(
'writes' ) ) {
371 $this->reportExpectationViolated(
'writes', $query, $this->hits[
'writes'], $trxId, $serverName );
374 if ( !$isWrite && $this->isAboveThreshold( $elapsed,
'readQueryTime' ) ) {
375 $this->reportExpectationViolated(
'readQueryTime', $query, $elapsed, $trxId, $serverName );
377 if ( $isWrite && $this->isAboveThreshold( $elapsed,
'writeQueryTime' ) ) {
378 $this->reportExpectationViolated(
'writeQueryTime', $query, $elapsed, $trxId, $serverName );
381 if ( !$this->dbTrxHoldingLocks ) {
384 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
389 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
390 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
393 $lastEnd = $lastQuery[2];
394 if ( $sTime >= $lastEnd ) {
395 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
397 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $sTime ];
399 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
403 if ( $sTime >= $info[
'start'] ) {
404 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
431 $name =
"{$db} {$server} TRX#$id";
432 if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
433 $this->logger->warning(
"Detected no transaction for '$name' - out of sync." );
440 if ( $this->isAboveThreshold( $writeTime,
'writeQueryTime' ) ) {
441 $this->reportExpectationViolated(
443 "[transaction writes to {$db} at {$server}]",
450 if ( $this->isAboveThreshold( $affected,
'maxAffected' ) ) {
451 $this->reportExpectationViolated(
453 "[transaction writes to {$db} at {$server}]",
459 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
461 $now = $this->getCurrentTime();
462 $lastEnd = $lastQuery[2];
463 if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
464 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $now ];
468 foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
469 $elapsed = ( $info[2] - $info[1] );
470 if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) {
477 foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
479 "%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) );
481 $this->logger->warning(
"Suboptimal transaction [{dbs}]:\n{trace}", [
482 'dbs' => implode(
', ', array_keys( $this->dbTrxHoldingLocks[$name][
'conns'] ) ),
483 'trace' => mb_substr( $trace, 0, 2000 )
486 unset( $this->dbTrxHoldingLocks[$name] );
487 unset( $this->dbTrxMethodTimes[$name] );