167 if (
$type === self::EXPECTATION_REPLICAS_ONLY ) {
169 foreach ( [
'writes',
'masterConns' ] as $event ) {
170 if ( $this->expect[$event][self::FLD_LIMIT] === 0 ) {
175 $events = self::EVENT_NAMES;
178 foreach ( $events as $event ) {
179 ++$this->silenced[$event];
182 return new ScopedCallback(
function () use ( $events ) {
183 foreach ( $events as $event ) {
184 --$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
297 $name =
"{$db} {$server} TRX#$id";
298 if ( isset( $this->dbTrxHoldingLocks[$name] ) ) {
299 $this->logger->warning(
"Nested transaction for '$name' - out of sync." );
301 $this->dbTrxHoldingLocks[$name] = [
302 'start' => $this->getCurrentTime(),
305 $this->dbTrxMethodTimes[$name] = [];
307 foreach ( $this->dbTrxHoldingLocks as $name => &$info ) {
309 $info[
'conns'][$name] = 1;
331 ?
string $serverName =
null
333 $eTime = $this->getCurrentTime();
334 $elapsed = ( $eTime - $sTime );
336 if ( $isWrite && $this->isAboveThreshold( $rowCount,
'maxAffected' ) ) {
337 $this->reportExpectationViolated(
'maxAffected', $query, $rowCount, $trxId, $serverName );
338 } elseif ( !$isWrite && $this->isAboveThreshold( $rowCount,
'readQueryRows' ) ) {
339 $this->reportExpectationViolated(
'readQueryRows', $query, $rowCount, $trxId, $serverName );
343 if ( $this->pingAndCheckThreshold(
'queries' ) ) {
344 $this->reportExpectationViolated(
'queries', $query, $this->hits[
'queries'], $trxId, $serverName );
346 if ( $isWrite && $this->pingAndCheckThreshold(
'writes' ) ) {
347 $this->reportExpectationViolated(
'writes', $query, $this->hits[
'writes'], $trxId, $serverName );
350 if ( !$isWrite && $this->isAboveThreshold( $elapsed,
'readQueryTime' ) ) {
351 $this->reportExpectationViolated(
'readQueryTime', $query, $elapsed, $trxId, $serverName );
353 if ( $isWrite && $this->isAboveThreshold( $elapsed,
'writeQueryTime' ) ) {
354 $this->reportExpectationViolated(
'writeQueryTime', $query, $elapsed, $trxId, $serverName );
357 if ( !$this->dbTrxHoldingLocks ) {
360 } elseif ( !$isWrite && $elapsed < self::EVENT_THRESHOLD_SEC ) {
365 foreach ( $this->dbTrxHoldingLocks as $name => $info ) {
366 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
369 $lastEnd = $lastQuery[2];
370 if ( $sTime >= $lastEnd ) {
371 if ( ( $sTime - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
373 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $sTime ];
375 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
379 if ( $sTime >= $info[
'start'] ) {
380 $this->dbTrxMethodTimes[$name][] = [ $query, $sTime, $eTime ];
407 $name =
"{$db} {$server} TRX#$id";
408 if ( !isset( $this->dbTrxMethodTimes[$name] ) ) {
409 $this->logger->warning(
"Detected no transaction for '$name' - out of sync." );
416 if ( $this->isAboveThreshold( $writeTime,
'writeQueryTime' ) ) {
417 $this->reportExpectationViolated(
419 "[transaction writes to {$db} at {$server}]",
426 if ( $this->isAboveThreshold( $affected,
'maxAffected' ) ) {
427 $this->reportExpectationViolated(
429 "[transaction writes to {$db} at {$server}]",
435 $lastQuery = end( $this->dbTrxMethodTimes[$name] );
437 $now = $this->getCurrentTime();
438 $lastEnd = $lastQuery[2];
439 if ( ( $now - $lastEnd ) > self::EVENT_THRESHOLD_SEC ) {
440 $this->dbTrxMethodTimes[$name][] = [
'...delay...', $lastEnd, $now ];
444 foreach ( $this->dbTrxMethodTimes[$name] as $info ) {
445 $elapsed = ( $info[2] - $info[1] );
446 if ( $elapsed >= self::DB_LOCK_THRESHOLD_SEC ) {
453 foreach ( $this->dbTrxMethodTimes[$name] as $i => [ $query, $sTime, $end ] ) {
455 "%-2d %.3fs %s\n", $i, ( $end - $sTime ), $this->getGeneralizedSql( $query ) );
457 $this->logger->warning(
"Sub-optimal transaction [{dbs}]:\n{trace}", [
458 'dbs' => implode(
', ', array_keys( $this->dbTrxHoldingLocks[$name][
'conns'] ) ),
462 unset( $this->dbTrxHoldingLocks[$name] );
463 unset( $this->dbTrxMethodTimes[$name] );