Rename canRecoverFromDisconnect() in order to better describe its function. Make it use the transaction ID and query walltime as arguments and return an ERR_* class constant instead of a bool. Avoid retries of slow queries that yield lost connection errors. Add methods and class constants to track session state errors caused by the loss of named locks or temp tables. Such errors can be resolved by a "session flush" method. Make assertQueryIsCurrentlyAllowed() better distinguish ROLLBACK queries from ROLLBACK TO SAVEPOINT queries. For some scenarios, only full tranasction ROLLBACK queries should be allowed. Add flushSession() method to Database and flushPrimarySessions() methods to LBFactory/LoadBalancer. Also: * Rename wasKnownStatementRollbackError() and make it take the error number as an argument, similar to wasConnectionError(). Add mysql error codes for query timeouts since they only cause statement rollbacks. * Rename wasConnectionError() and mark it as protected. This is an internal method with no outside callers. * Rename wasQueryTimeout(), remove some HHVM-specific code, and simplify the arguments. * Make executeQuery() use a for loop for the query retry logic to reduce code duplication. * Move the error state setting logic in executeQueryAttempt() up in order to reduce code duplication. * Move the beginIfImplied() call in executeQueryAttempt() up to the retry loop in executeQuery(). This narrows the executeQueryAttempt() concerns to sending a single query and updating tracking fields. * Make closeConnection() and doHandleSessionLossPreconnect() in DatabaseSqlite more consistent with the base class by releasing named locks. * Mark trxStatus() as @internal. Bug: T281451 Bug: T293859 Change-Id: I200f90e413b8a725828745f81925b54985c72180
289 lines
6.9 KiB
PHP
289 lines
6.9 KiB
PHP
<?php
|
|
|
|
use Psr\Log\NullLogger;
|
|
use Wikimedia\Rdbms\Database;
|
|
use Wikimedia\Rdbms\DatabaseDomain;
|
|
use Wikimedia\Rdbms\TransactionProfiler;
|
|
use Wikimedia\RequestTimeout\RequestTimeout;
|
|
|
|
/**
|
|
* Helper for testing the methods from the Database class
|
|
* @since 1.22
|
|
*/
|
|
class DatabaseTestHelper extends Database {
|
|
|
|
/**
|
|
* @var string[] __CLASS__ of the test suite,
|
|
* used to determine, if the function name is passed every time to query()
|
|
*/
|
|
protected $testName = [];
|
|
|
|
/**
|
|
* @var string[] Array of lastSqls passed to query(),
|
|
* This is an array since some methods in Database can do more than one
|
|
* query. Cleared when calling getLastSqls().
|
|
*/
|
|
protected $lastSqls = [];
|
|
|
|
/** @var array List of row arrays */
|
|
protected $nextResult = [];
|
|
|
|
/** @var array|null */
|
|
protected $nextError = null;
|
|
/** @var array|null */
|
|
protected $lastError = null;
|
|
|
|
/**
|
|
* @var string[] Array of tables to be considered as existing by tableExist()
|
|
* Use setExistingTables() to alter.
|
|
*/
|
|
protected $tablesExists;
|
|
|
|
/**
|
|
* @var bool Value to return from unionSupportsOrderAndLimit()
|
|
*/
|
|
protected $unionSupportsOrderAndLimit = true;
|
|
|
|
/** @var int[] */
|
|
protected $forcedAffectedCountQueue = [];
|
|
|
|
public function __construct( $testName, array $opts = [] ) {
|
|
parent::__construct( $opts + [
|
|
'host' => null,
|
|
'user' => null,
|
|
'password' => null,
|
|
'dbname' => null,
|
|
'schema' => null,
|
|
'tablePrefix' => '',
|
|
'flags' => 0,
|
|
'cliMode' => true,
|
|
'agent' => '',
|
|
'serverName' => null,
|
|
'topologyRole' => null,
|
|
'topologicalMaster' => null,
|
|
'srvCache' => new HashBagOStuff(),
|
|
'profiler' => null,
|
|
'trxProfiler' => new TransactionProfiler(),
|
|
'connLogger' => new NullLogger(),
|
|
'queryLogger' => new NullLogger(),
|
|
'replLogger' => new NullLogger(),
|
|
'errorLogger' => static function ( Exception $e ) {
|
|
wfWarn( get_class( $e ) . ": {$e->getMessage()}" );
|
|
},
|
|
'deprecationLogger' => static function ( $msg ) {
|
|
wfWarn( $msg );
|
|
},
|
|
'criticalSectionProvider' =>
|
|
RequestTimeout::singleton()->createCriticalSectionProvider( 120 )
|
|
] );
|
|
|
|
$this->testName = $testName;
|
|
|
|
$this->currentDomain = DatabaseDomain::newUnspecified();
|
|
$this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
|
|
}
|
|
|
|
/**
|
|
* Returns SQL queries grouped by '; '
|
|
* Clear the list of queries that have been done so far.
|
|
* @return string
|
|
*/
|
|
public function getLastSqls() {
|
|
$lastSqls = implode( '; ', $this->lastSqls );
|
|
$this->lastSqls = [];
|
|
|
|
return $lastSqls;
|
|
}
|
|
|
|
public function setExistingTables( $tablesExists ) {
|
|
$this->tablesExists = (array)$tablesExists;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $res Use an array of row arrays to set row result
|
|
*/
|
|
public function forceNextResult( $res ) {
|
|
$this->nextResult = $res;
|
|
}
|
|
|
|
/**
|
|
* @param int $errno Error number
|
|
* @param string $error Error text
|
|
* @param array $options
|
|
* - wasKnownStatementRollbackError: Return value for wasKnownStatementRollbackError()
|
|
*/
|
|
public function forceNextQueryError( $errno, $error, $options = [] ) {
|
|
$this->nextError = [ 'errno' => $errno, 'error' => $error ] + $options;
|
|
}
|
|
|
|
protected function addSql( $sql ) {
|
|
// clean up spaces before and after some words and the whole string
|
|
$this->lastSqls[] = trim( preg_replace(
|
|
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
|
|
' ', $sql
|
|
) );
|
|
}
|
|
|
|
protected function checkFunctionName( $fname ) {
|
|
if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
|
|
return; // no $fname parameter
|
|
}
|
|
|
|
// Handle some internal calls from the Database class
|
|
$check = $fname;
|
|
if ( preg_match(
|
|
'/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
|
|
$fname,
|
|
$m
|
|
) ) {
|
|
$check = $m[1];
|
|
}
|
|
|
|
if ( substr( $check, 0, strlen( $this->testName ) ) !== $this->testName ) {
|
|
throw new MWException( 'function name does not start with test class. ' .
|
|
$fname . ' vs. ' . $this->testName . '. ' .
|
|
'Please provide __METHOD__ to database methods.' );
|
|
}
|
|
}
|
|
|
|
public function strencode( $s ) {
|
|
// Choose apos to avoid handling of escaping double quotes in quoted text
|
|
return str_replace( "'", "\'", $s );
|
|
}
|
|
|
|
public function addIdentifierQuotes( $s ) {
|
|
// no escaping to avoid handling of double quotes in quoted text
|
|
return $s;
|
|
}
|
|
|
|
public function query( $sql, $fname = '', $flags = 0 ) {
|
|
$this->checkFunctionName( $fname );
|
|
|
|
return parent::query( $sql, $fname, $flags );
|
|
}
|
|
|
|
public function tableExists( $table, $fname = __METHOD__ ) {
|
|
$tableRaw = $this->tableName( $table, 'raw' );
|
|
if ( isset( $this->sessionTempTables[$tableRaw] ) ) {
|
|
return true; // already known to exist
|
|
}
|
|
|
|
$this->checkFunctionName( $fname );
|
|
|
|
return in_array( $table, (array)$this->tablesExists );
|
|
}
|
|
|
|
public function getType() {
|
|
return 'test';
|
|
}
|
|
|
|
public function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
|
|
$this->conn = (object)[ 'test' ];
|
|
|
|
return true;
|
|
}
|
|
|
|
public function fetchObject( $res ) {
|
|
return false;
|
|
}
|
|
|
|
public function fetchRow( $res ) {
|
|
return false;
|
|
}
|
|
|
|
public function numRows( $res ) {
|
|
return -1;
|
|
}
|
|
|
|
public function insertId() {
|
|
return -1;
|
|
}
|
|
|
|
public function lastErrno() {
|
|
return $this->lastError ? $this->lastError['errno'] : -1;
|
|
}
|
|
|
|
public function lastError() {
|
|
return $this->lastError ? $this->lastError['error'] : 'test';
|
|
}
|
|
|
|
protected function isKnownStatementRollbackError( $errno ) {
|
|
return ( $this->lastError['errno'] ?? 0 ) === $errno
|
|
? ( $this->lastError['wasKnownStatementRollbackError'] ?? false )
|
|
: false;
|
|
}
|
|
|
|
public function fieldInfo( $table, $field ) {
|
|
return false;
|
|
}
|
|
|
|
public function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
|
|
return false;
|
|
}
|
|
|
|
public function fetchAffectedRowCount() {
|
|
return -1;
|
|
}
|
|
|
|
public function getSoftwareLink() {
|
|
return 'test';
|
|
}
|
|
|
|
public function getServerVersion() {
|
|
return 'test';
|
|
}
|
|
|
|
public function getServerInfo() {
|
|
return 'test';
|
|
}
|
|
|
|
public function ping( &$rtt = null ) {
|
|
$rtt = 0.0;
|
|
return true;
|
|
}
|
|
|
|
protected function closeConnection() {
|
|
return true;
|
|
}
|
|
|
|
public function setNextQueryAffectedRowCounts( array $counts ) {
|
|
$this->forcedAffectedCountQueue = $counts;
|
|
}
|
|
|
|
protected function doQuery( $sql ) {
|
|
$sql = preg_replace( '< /\* .+? \*/>', '', $sql );
|
|
$this->addSql( $sql );
|
|
|
|
if ( $this->nextError ) {
|
|
$this->lastError = $this->nextError;
|
|
$this->nextError = null;
|
|
return false;
|
|
}
|
|
|
|
$res = $this->nextResult;
|
|
$this->nextResult = [];
|
|
$this->lastError = null;
|
|
|
|
if ( $this->forcedAffectedCountQueue ) {
|
|
$this->affectedRowCount = array_shift( $this->forcedAffectedCountQueue );
|
|
}
|
|
|
|
return new FakeResultWrapper( $res );
|
|
}
|
|
|
|
public function unionSupportsOrderAndLimit() {
|
|
return $this->unionSupportsOrderAndLimit;
|
|
}
|
|
|
|
public function setUnionSupportsOrderAndLimit( $v ) {
|
|
$this->unionSupportsOrderAndLimit = (bool)$v;
|
|
}
|
|
|
|
public function useIndexClause( $index ) {
|
|
return "FORCE INDEX (" . $this->indexName( $index ) . ")";
|
|
}
|
|
|
|
public function ignoreIndexClause( $index ) {
|
|
return "IGNORE INDEX (" . $this->indexName( $index ) . ")";
|
|
}
|
|
}
|