/** @var bool */
protected $opened = false;
- /** @var array[] List of (callable, method name) */
+ /** @var array[] List of (callable, method name, atomic section id) */
protected $trxIdleCallbacks = [];
- /** @var array[] List of (callable, method name) */
+ /** @var array[] List of (callable, method name, atomic section id) */
protected $trxPreCommitCallbacks = [];
- /** @var array[] List of (callable, method name) */
+ /** @var array[] List of (callable, method name, atomic section id) */
protected $trxEndCallbacks = [];
/** @var callable[] Map of (name => callable) */
protected $trxRecurringCallbacks = [];
/** @var int */
protected $nonNativeInsertSelectBatchSize = 10000;
+ /** @var string Idiom used when a cancelable atomic section started the transaction */
+ private static $NOT_APPLICABLE = 'n/a';
+ /** @var string Prefix to the atomic section counter used to make savepoint IDs */
+ private static $SAVEPOINT_PREFIX = 'wikimedia_rdbms_atomic';
+
/** @var int Transaction is in a error state requiring a full or savepoint rollback */
const STATUS_TRX_ERROR = 1;
/** @var int Transaction is active and in a normal state */
}
/**
- * Get the list of method names that have pending write queries or callbacks
- * for this transaction
+ * List the methods that have write queries or callbacks for the current transaction
*
- * @return array
+ * This method should not be used outside of Database/LoadBalancer
+ *
+ * @return string[]
+ * @since 1.32
*/
- protected function pendingWriteAndCallbackCallers() {
- if ( !$this->trxLevel ) {
- return [];
- }
-
- $fnames = $this->trxWriteCallers;
+ public function pendingWriteAndCallbackCallers() {
+ $fnames = $this->pendingWriteCallers();
foreach ( [
$this->trxIdleCallbacks,
$this->trxPreCommitCallbacks,
}
// Sanity check that no callbacks are dangling
- if (
- $this->trxIdleCallbacks || $this->trxPreCommitCallbacks || $this->trxEndCallbacks
- ) {
+ $fnames = $this->pendingWriteAndCallbackCallers();
+ if ( $fnames ) {
throw new RuntimeException(
- "Transaction callbacks are still pending:\n" .
- implode( ', ', $this->pendingWriteAndCallbackCallers() )
+ "Transaction callbacks are still pending:\n" . implode( ', ', $fnames )
);
}
abstract protected function closeConnection();
/**
- * @param string $error Fallback error message, used if none is given by DB
+ * @deprecated since 1.32
+ * @param string $error Fallback message, if none is given by DB
* @throws DBConnectionError
*/
public function reportConnectionError( $error = 'Unknown error' ) {
- $myError = $this->lastError();
- if ( $myError ) {
- $error = $myError;
- }
-
- # New method
- throw new DBConnectionError( $this, $error );
+ call_user_func( $this->deprecationLogger, 'Use of ' . __METHOD__ . ' is deprecated.' );
+ throw new DBConnectionError( $this, $this->lastError() ?: $error );
}
/**
return '';
}
- public function select( $table, $vars, $conds = '', $fname = __METHOD__,
- $options = [], $join_conds = [] ) {
+ public function select(
+ $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
+ ) {
$sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
return $this->query( $sql, $fname );
$options = [], $join_conds = []
) {
if ( is_array( $vars ) ) {
- $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
+ $fields = implode( ',', $this->fieldNamesWithAlias( $vars ) );
+ } else {
+ $fields = $vars;
}
$options = (array)$options;
? $options['IGNORE INDEX']
: [];
+ if (
+ $this->selectOptionsIncludeLocking( $options ) &&
+ $this->selectFieldsOrOptionsAggregate( $vars, $options )
+ ) {
+ // Some DB types (postgres/oracle) disallow FOR UPDATE with aggregate
+ // functions. Discourage use of such queries to encourage compatibility.
+ call_user_func(
+ $this->deprecationLogger,
+ __METHOD__ . ": aggregation used with a locking SELECT ($fname)."
+ );
+ }
+
if ( is_array( $table ) ) {
$from = ' FROM ' .
$this->tableNamesWithIndexClauseOrJOIN(
}
if ( $conds === '' ) {
- $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
+ $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex $preLimitTail";
} elseif ( is_string( $conds ) ) {
- $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex " .
+ $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex " .
"WHERE $conds $preLimitTail";
} else {
throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0;
}
+ /**
+ * @param string|array $options
+ * @return bool
+ */
+ private function selectOptionsIncludeLocking( $options ) {
+ $options = (array)$options;
+ foreach ( [ 'FOR UPDATE', 'LOCK IN SHARE MODE' ] as $lock ) {
+ if ( in_array( $lock, $options, true ) ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @param array|string $fields
+ * @param array|string $options
+ * @return bool
+ */
+ private function selectFieldsOrOptionsAggregate( $fields, $options ) {
+ foreach ( (array)$options as $key => $value ) {
+ if ( is_string( $key ) ) {
+ if ( preg_match( '/^(?:GROUP BY|HAVING)$/i', $key ) ) {
+ return true;
+ }
+ } elseif ( is_string( $value ) ) {
+ if ( preg_match( '/^(?:DISTINCT|DISTINCTROW)$/i', $value ) ) {
+ return true;
+ }
+ }
+ }
+
+ $regex = '/^(?:COUNT|MIN|MAX|SUM|GROUP_CONCAT|LISTAGG|ARRAY_AGG)\s*\\(/i';
+ foreach ( (array)$fields as $field ) {
+ if ( is_string( $field ) && preg_match( $regex, $field ) ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
/**
* @param array|string $conds
* @param string $fname
if ( !$this->trxLevel ) {
throw new DBUnexpectedError( $this, "No transaction is active." );
}
- $this->trxEndCallbacks[] = [ $callback, $fname ];
+ $this->trxEndCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
}
final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
$this->trxAutomatic = true;
}
- $this->trxIdleCallbacks[] = [ $callback, $fname ];
+ $this->trxIdleCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
if ( !$this->trxLevel ) {
$this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
}
}
if ( $this->trxLevel ) {
- $this->trxPreCommitCallbacks[] = [ $callback, $fname ];
+ $this->trxPreCommitCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
} else {
// No transaction is active nor will start implicitly, so make one for this callback
$this->startAtomic( __METHOD__, self::ATOMIC_CANCELABLE );
try {
- call_user_func( $callback );
+ call_user_func( $callback, $this );
$this->endAtomic( __METHOD__ );
} catch ( Exception $e ) {
$this->cancelAtomic( __METHOD__ );
}
}
+ /**
+ * @return AtomicSectionIdentifier|null ID of the topmost atomic section level
+ */
+ private function currentAtomicSectionId() {
+ if ( $this->trxLevel && $this->trxAtomicLevels ) {
+ $levelInfo = end( $this->trxAtomicLevels );
+
+ return $levelInfo[1];
+ }
+
+ return null;
+ }
+
+ /**
+ * @param AtomicSectionIdentifier $old
+ * @param AtomicSectionIdentifier $new
+ */
+ private function reassignCallbacksForSection(
+ AtomicSectionIdentifier $old, AtomicSectionIdentifier $new
+ ) {
+ foreach ( $this->trxPreCommitCallbacks as $key => $info ) {
+ if ( $info[2] === $old ) {
+ $this->trxPreCommitCallbacks[$key][2] = $new;
+ }
+ }
+ foreach ( $this->trxIdleCallbacks as $key => $info ) {
+ if ( $info[2] === $old ) {
+ $this->trxIdleCallbacks[$key][2] = $new;
+ }
+ }
+ foreach ( $this->trxEndCallbacks as $key => $info ) {
+ if ( $info[2] === $old ) {
+ $this->trxEndCallbacks[$key][2] = $new;
+ }
+ }
+ }
+
+ /**
+ * @param AtomicSectionIdentifier[] $sectionIds ID of an actual savepoint
+ * @throws UnexpectedValueException
+ */
+ private function modifyCallbacksForCancel( array $sectionIds ) {
+ // Cancel the "on commit" callbacks owned by this savepoint
+ $this->trxIdleCallbacks = array_filter(
+ $this->trxIdleCallbacks,
+ function ( $entry ) use ( $sectionIds ) {
+ return !in_array( $entry[2], $sectionIds, true );
+ }
+ );
+ $this->trxPreCommitCallbacks = array_filter(
+ $this->trxPreCommitCallbacks,
+ function ( $entry ) use ( $sectionIds ) {
+ return !in_array( $entry[2], $sectionIds, true );
+ }
+ );
+ // Make "on resolution" callbacks owned by this savepoint to perceive a rollback
+ foreach ( $this->trxEndCallbacks as $key => $entry ) {
+ if ( in_array( $entry[2], $sectionIds, true ) ) {
+ $callback = $entry[0];
+ $this->trxEndCallbacks[$key][0] = function () use ( $callback ) {
+ return $callback( self::TRIGGER_ROLLBACK, $this );
+ };
+ }
+ }
+ }
+
final public function setTransactionListener( $name, callable $callback = null ) {
if ( $callback ) {
$this->trxRecurringCallbacks[$name] = $callback;
}
/**
- * Actually run and consume any "on transaction idle/resolution" callbacks.
+ * Actually consume and run any "on transaction idle/resolution" callbacks.
*
* This method should not be used outside of Database/LoadBalancer
*
* @param int $trigger IDatabase::TRIGGER_* constant
+ * @return int Number of callbacks attempted
* @since 1.20
* @throws Exception
*/
public function runOnTransactionIdleCallbacks( $trigger ) {
+ if ( $this->trxLevel ) { // sanity
+ throw new DBUnexpectedError( $this, __METHOD__ . ': a transaction is still open.' );
+ }
+
if ( $this->trxEndCallbacksSuppressed ) {
- return;
+ return 0;
}
+ $count = 0;
$autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
/** @var Exception $e */
$e = null; // first exception
$this->trxEndCallbacks = []; // consumed (recursion guard)
foreach ( $callbacks as $callback ) {
try {
+ ++$count;
list( $phpCallback ) = $callback;
$this->clearFlag( self::DBO_TRX ); // make each query its own transaction
- call_user_func_array( $phpCallback, [ $trigger ] );
+ call_user_func( $phpCallback, $trigger, $this );
if ( $autoTrx ) {
$this->setFlag( self::DBO_TRX ); // restore automatic begin()
} else {
if ( $e instanceof Exception ) {
throw $e; // re-throw any first exception
}
+
+ return $count;
}
/**
- * Actually run and consume any "on transaction pre-commit" callbacks.
+ * Actually consume and run any "on transaction pre-commit" callbacks.
*
* This method should not be used outside of Database/LoadBalancer
*
* @since 1.22
+ * @return int Number of callbacks attempted
* @throws Exception
*/
public function runOnTransactionPreCommitCallbacks() {
+ $count = 0;
+
$e = null; // first exception
do { // callbacks may add callbacks :)
$callbacks = $this->trxPreCommitCallbacks;
$this->trxPreCommitCallbacks = []; // consumed (and recursion guard)
foreach ( $callbacks as $callback ) {
try {
+ ++$count;
list( $phpCallback ) = $callback;
- call_user_func( $phpCallback );
+ call_user_func( $phpCallback, $this );
} catch ( Exception $ex ) {
call_user_func( $this->errorLogger, $ex );
$e = $e ?: $ex;
if ( $e instanceof Exception ) {
throw $e; // re-throw any first exception
}
+
+ return $count;
}
/**
$this->query( 'ROLLBACK TO SAVEPOINT ' . $this->addIdentifierQuotes( $identifier ), $fname );
}
+ /**
+ * @param string $fname
+ * @return string
+ */
+ private function nextSavepointId( $fname ) {
+ $savepointId = self::$SAVEPOINT_PREFIX . ++$this->trxAtomicCounter;
+ if ( strlen( $savepointId ) > 30 ) {
+ // 30 == Oracle's identifier length limit (pre 12c)
+ // With a 22 character prefix, that puts the highest number at 99999999.
+ throw new DBUnexpectedError(
+ $this,
+ 'There have been an excessively large number of atomic sections in a transaction'
+ . " started by $this->trxFname (at $fname)"
+ );
+ }
+
+ return $savepointId;
+ }
+
final public function startAtomic(
$fname = __METHOD__, $cancelable = self::ATOMIC_NOT_CANCELABLE
) {
- $savepointId = $cancelable === self::ATOMIC_CANCELABLE ? 'n/a' : null;
+ $savepointId = $cancelable === self::ATOMIC_CANCELABLE ? self::$NOT_APPLICABLE : null;
+
if ( !$this->trxLevel ) {
- $this->begin( $fname, self::TRANSACTION_INTERNAL );
+ $this->begin( $fname, self::TRANSACTION_INTERNAL ); // sets trxAutomatic
// If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
// in all changes being in one transaction to keep requests transactional.
- if ( !$this->getFlag( self::DBO_TRX ) ) {
+ if ( $this->getFlag( self::DBO_TRX ) ) {
+ // Since writes could happen in between the topmost atomic sections as part
+ // of the transaction, those sections will need savepoints.
+ $savepointId = $this->nextSavepointId( $fname );
+ $this->doSavepoint( $savepointId, $fname );
+ } else {
$this->trxAutomaticAtomic = true;
}
} elseif ( $cancelable === self::ATOMIC_CANCELABLE ) {
- $savepointId = 'wikimedia_rdbms_atomic' . ++$this->trxAtomicCounter;
- if ( strlen( $savepointId ) > 30 ) { // 30 == Oracle's identifier length limit (pre 12c)
- $this->queryLogger->warning(
- 'There have been an excessively large number of atomic sections in a transaction'
- . " started by $this->trxFname, reusing IDs (at $fname)",
- [ 'trace' => ( new RuntimeException() )->getTraceAsString() ]
- );
- $this->trxAtomicCounter = 0;
- $savepointId = 'wikimedia_rdbms_atomic' . ++$this->trxAtomicCounter;
- }
+ $savepointId = $this->nextSavepointId( $fname );
$this->doSavepoint( $savepointId, $fname );
}
// Check if the current section matches $fname
$pos = count( $this->trxAtomicLevels ) - 1;
- list( $savedFname, , $savepointId ) = $this->trxAtomicLevels[$pos];
+ list( $savedFname, $sectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
if ( $savedFname !== $fname ) {
throw new DBUnexpectedError(
);
}
- // Remove the last section and re-index the array
- $this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos );
+ // Remove the last section (no need to re-index the array)
+ array_pop( $this->trxAtomicLevels );
if ( !$this->trxAtomicLevels && $this->trxAutomaticAtomic ) {
$this->commit( $fname, self::FLUSHING_INTERNAL );
- } elseif ( $savepointId !== null && $savepointId !== 'n/a' ) {
+ } elseif ( $savepointId !== null && $savepointId !== self::$NOT_APPLICABLE ) {
$this->doReleaseSavepoint( $savepointId, $fname );
}
+
+ // Hoist callback ownership for callbacks in the section that just ended;
+ // all callbacks should have an owner that is present in trxAtomicLevels.
+ $currentSectionId = $this->currentAtomicSectionId();
+ if ( $currentSectionId ) {
+ $this->reassignCallbacksForSection( $sectionId, $currentSectionId );
+ }
}
final public function cancelAtomic(
throw new DBUnexpectedError( "Atomic section not found (for $fname)" );
}
// Remove all descendant sections and re-index the array
+ $excisedIds = [];
+ $len = count( $this->trxAtomicLevels );
+ for ( $i = $pos + 1; $i < $len; ++$i ) {
+ $excisedIds[] = $this->trxAtomicLevels[$i][1];
+ }
$this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos + 1 );
+ $this->modifyCallbacksForCancel( $excisedIds );
}
// Check if the current section matches $fname
$pos = count( $this->trxAtomicLevels ) - 1;
- list( $savedFname, , $savepointId ) = $this->trxAtomicLevels[$pos];
+ list( $savedFname, $savedSectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
if ( $savedFname !== $fname ) {
throw new DBUnexpectedError(
);
}
- // Remove the last section and re-index the array
- $this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos );
+ // Remove the last section (no need to re-index the array)
+ array_pop( $this->trxAtomicLevels );
+ $this->modifyCallbacksForCancel( [ $savedSectionId ] );
if ( $savepointId !== null ) {
// Rollback the transaction to the state just before this atomic section
- if ( $savepointId === 'n/a' ) {
+ if ( $savepointId === self::$NOT_APPLICABLE ) {
$this->rollback( $fname, self::FLUSHING_INTERNAL );
} else {
$this->doRollbackToSavepoint( $savepointId, $fname );
);
}
- $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
- $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
+ // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
+ if ( $flush !== self::FLUSHING_ALL_PEERS ) {
+ $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
+ $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
+ }
}
/**
$this->trxIdleCallbacks = [];
$this->trxPreCommitCallbacks = [];
- if ( $trxActive ) {
+ // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
+ if ( $trxActive && $flush !== self::FLUSHING_ALL_PEERS ) {
try {
$this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
} catch ( Exception $e ) {