108 $this->initPlaceholderExpectations();
110 $this->dbTrxHoldingLocks = [];
111 $this->dbTrxMethodTimes = [];
113 $this->silenced = array_fill_keys( self::EVENT_NAMES, 0 );
116 $this->statsFactory = StatsFactory::newNull();
155 if ( $type === self::EXPECTATION_REPLICAS_ONLY ) {
157 foreach ( [
'writes',
'masterConns' ] as $event ) {
158 if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) {
163 $events = self::EVENT_NAMES;
166 foreach ( $events as $event ) {
167 ++$this->silenced[$event];
170 return new ScopedCallback(
function () use ( $events ) {
171 foreach ( $events as $event ) {
172 --$this->silenced[$event];
188 if ( !isset( $this->expect[$event] ) ) {
192 if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) {
194 $this->expect[$event] = [
195 self::FLD_LIMIT => $limit,
196 self::FLD_FNAME => $fname
343 ?
string $serverName =
null
345 $eTime = $this->getCurrentTime();
346 $elapsed = ( $eTime - $sTime );
348 if ( $isWrite && $this->isAboveThreshold( $rowCount,
'maxAffected' ) ) {
349 $this->reportExpectationViolated(
'maxAffected', $query, $rowCount, $trxId, $serverName );
350 } elseif ( !$isWrite && $this->isAboveThreshold( $rowCount,
'readQueryRows' ) ) {
351 $this->reportExpectationViolated(
'readQueryRows', $query, $rowCount, $trxId, $serverName );
355 if ( $this->pingAndCheckThreshold(
'queries' ) ) {
356 $this->reportExpectationViolated(
'queries', $query, $this->hits[
'queries'], $trxId, $serverName );
358 if ( $isWrite && $this->pingAndCheckThreshold(
'writes' ) ) {
359 $this->reportExpectationViolated(
'writes', $query, $this->hits[
'writes'], $trxId, $serverName );
362 if ( !$isWrite && $this->isAboveThreshold( $elapsed,
'readQueryTime' ) ) {
363 $this->reportExpectationViolated(
'readQueryTime', $query, $elapsed, $trxId, $serverName );
365 if ( $isWrite && $this->isAboveThreshold( $elapsed,
'writeQueryTime' ) ) {
366 $this->reportExpectationViolated(
'writeQueryTime', $query, $elapsed, $trxId, $serverName );
369 if ( !$this->dbTrxHoldingLocks ) {
372 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
377 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
378 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
381 $lastEnd = $lastQuery[2];
382 if ( $sTime >= $lastEnd ) {
383 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
385 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $sTime ];
387 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
391 if ( $sTime >= $info[
'start'] ) {
392 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
419 $name =
"{$db} {$server} TRX#$id";
420 if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
421 $this->logger->warning(
"Detected no transaction for '$name' - out of sync." );
428 if ( $this->isAboveThreshold( $writeTime,
'writeQueryTime' ) ) {
429 $this->reportExpectationViolated(
431 "[transaction writes to {$db} at {$server}]",
438 if ( $this->isAboveThreshold( $affected,
'maxAffected' ) ) {
439 $this->reportExpectationViolated(
441 "[transaction writes to {$db} at {$server}]",
447 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
449 $now = $this->getCurrentTime();
450 $lastEnd = $lastQuery[2];
451 if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
452 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $now ];
456 foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
457 $elapsed = ( $info[2] - $info[1] );
458 if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) {
465 foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
467 "%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) );
469 $this->logger->warning(
"Suboptimal transaction [{dbs}]:\n{trace}", [
470 'dbs' => implode(
', ', array_keys( $this->dbTrxHoldingLocks[$name][
'conns'] ) ),
471 'trace' => mb_substr( $trace, 0, 2000 )
474 unset( $this->dbTrxHoldingLocks[$name] );
475 unset( $this->dbTrxMethodTimes[$name] );