107 $this->initPlaceholderExpectations();
109 $this->dbTrxHoldingLocks = [];
110 $this->dbTrxMethodTimes = [];
112 $this->silenced = array_fill_keys( self::EVENT_NAMES, 0 );
115 $this->statsFactory = StatsFactory::newNull();
154 if ( $type === self::EXPECTATION_REPLICAS_ONLY ) {
156 foreach ( [
'writes',
'masterConns' ] as $event ) {
157 if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) {
162 $events = self::EVENT_NAMES;
165 foreach ( $events as $event ) {
166 ++$this->silenced[$event];
169 return new ScopedCallback(
function () use ( $events ) {
170 foreach ( $events as $event ) {
171 --$this->silenced[$event];
187 if ( !isset( $this->expect[$event] ) ) {
191 if ( $limit <= $this->expect[$event][self::FLD_LIMIT] ) {
193 $this->expect[$event] = [
194 self::FLD_LIMIT => $limit,
195 self::FLD_FNAME => $fname
319 ?
string $serverName =
null
321 $eTime = $this->getCurrentTime();
322 $elapsed = ( $eTime - $sTime );
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 );
331 if ( $this->pingAndCheckThreshold(
'queries' ) ) {
332 $this->reportExpectationViolated(
'queries', $query, $this->hits[
'queries'], $trxId, $serverName );
334 if ( $isWrite && $this->pingAndCheckThreshold(
'writes' ) ) {
335 $this->reportExpectationViolated(
'writes', $query, $this->hits[
'writes'], $trxId, $serverName );
338 if ( !$isWrite && $this->isAboveThreshold( $elapsed,
'readQueryTime' ) ) {
339 $this->reportExpectationViolated(
'readQueryTime', $query, $elapsed, $trxId, $serverName );
341 if ( $isWrite && $this->isAboveThreshold( $elapsed,
'writeQueryTime' ) ) {
342 $this->reportExpectationViolated(
'writeQueryTime', $query, $elapsed, $trxId, $serverName );
345 if ( !$this->dbTrxHoldingLocks ) {
348 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
353 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
354 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
357 $lastEnd = $lastQuery[2];
358 if ( $sTime >= $lastEnd ) {
359 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
361 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $sTime ];
363 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
367 if ( $sTime >= $info[
'start'] ) {
368 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
395 $name =
"{$db} {$server} TRX#$id";
396 if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
397 $this->logger->warning(
"Detected no transaction for '$name' - out of sync." );
404 if ( $this->isAboveThreshold( $writeTime,
'writeQueryTime' ) ) {
405 $this->reportExpectationViolated(
407 "[transaction writes to {$db} at {$server}]",
414 if ( $this->isAboveThreshold( $affected,
'maxAffected' ) ) {
415 $this->reportExpectationViolated(
417 "[transaction writes to {$db} at {$server}]",
423 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
425 $now = $this->getCurrentTime();
426 $lastEnd = $lastQuery[2];
427 if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
428 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $now ];
432 foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
433 $elapsed = ( $info[2] - $info[1] );
434 if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) {
441 foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
443 "%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) );
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 )
450 unset( $this->dbTrxHoldingLocks[$name] );
451 unset( $this->dbTrxMethodTimes[$name] );