Aaron Schulz has uploaded a new change for review. https://gerrit.wikimedia.org/r/312554
Change subject: Add DBO_* class constants and defines.php alias the class constants ...................................................................... Add DBO_* class constants and defines.php alias the class constants Change-Id: If13b23ef849d4cf4c711b0ec2bf2e8a795f90738 --- M includes/libs/rdbms/database/Database.php M includes/libs/rdbms/database/DatabaseMysql.php M includes/libs/rdbms/database/DatabaseMysqli.php M includes/libs/rdbms/database/DatabasePostgres.php M includes/libs/rdbms/database/DatabaseSqlite.php M includes/libs/rdbms/database/IDatabase.php M includes/libs/rdbms/defines.php M includes/libs/rdbms/lbfactory/LBFactoryMulti.php M includes/libs/rdbms/loadbalancer/ILoadBalancer.php M includes/libs/rdbms/loadbalancer/LoadBalancer.php 10 files changed, 81 insertions(+), 55 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/54/312554/1 diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index f56f380..3cd2eaa 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -248,11 +248,11 @@ $this->agent = str_replace( '/', '-', $params['agent'] ); $this->mFlags = $params['flags']; - if ( $this->mFlags & DBO_DEFAULT ) { + if ( $this->mFlags & self::DBO_DEFAULT ) { if ( $this->cliMode ) { - $this->mFlags &= ~DBO_TRX; + $this->mFlags &= ~self::DBO_TRX; } else { - $this->mFlags |= DBO_TRX; + $this->mFlags |= self::DBO_TRX; } } @@ -407,9 +407,11 @@ } public function bufferResults( $buffer = null ) { - $res = !$this->getFlag( DBO_NOBUFFER ); + $res = !$this->getFlag( self::DBO_NOBUFFER ); if ( $buffer !== null ) { - $buffer ? $this->clearFlag( DBO_NOBUFFER ) : $this->setFlag( DBO_NOBUFFER ); + $buffer + ? $this->clearFlag( self::DBO_NOBUFFER ) + : $this->setFlag( self::DBO_NOBUFFER ); } return $res; @@ -428,9 +430,11 @@ * @return bool The previous value of the flag. */ protected function ignoreErrors( $ignoreErrors = null ) { - $res = $this->getFlag( DBO_IGNORE ); + $res = $this->getFlag( self::DBO_IGNORE ); if ( $ignoreErrors !== null ) { - $ignoreErrors ? $this->setFlag( DBO_IGNORE ) : $this->clearFlag( DBO_IGNORE ); + $ignoreErrors + ? $this->setFlag( self::DBO_IGNORE ) + : $this->clearFlag( self::DBO_IGNORE ); } return $res; @@ -828,7 +832,7 @@ $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 ); # Start implicit transactions that wrap the request if DBO_TRX is enabled - if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX ) + if ( !$this->mTrxLevel && $this->getFlag( self::DBO_TRX ) && $this->isTransactableQuery( $sql ) ) { $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL ); @@ -842,7 +846,7 @@ $this->mServer, $this->mDBname, $this->mTrxShortId ); } - if ( $this->getFlag( DBO_DEBUG ) ) { + if ( $this->getFlag( self::DBO_DEBUG ) ) { $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" ); } @@ -2586,7 +2590,7 @@ return; } - $autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled? + $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled? /** @var Exception $e */ $e = null; // first exception do { // callbacks may add callbacks :) @@ -2599,12 +2603,12 @@ foreach ( $callbacks as $callback ) { try { list( $phpCallback ) = $callback; - $this->clearFlag( DBO_TRX ); // make each query its own transaction + $this->clearFlag( self::DBO_TRX ); // make each query its own transaction call_user_func_array( $phpCallback, [ $trigger ] ); if ( $autoTrx ) { - $this->setFlag( DBO_TRX ); // restore automatic begin() + $this->setFlag( self::DBO_TRX ); // restore automatic begin() } else { - $this->clearFlag( DBO_TRX ); // restore auto-commit + $this->clearFlag( self::DBO_TRX ); // restore auto-commit } } catch ( Exception $ex ) { call_user_func( $this->errorLogger, $ex ); @@ -2688,7 +2692,7 @@ $this->begin( $fname, self::TRANSACTION_INTERNAL ); // 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( DBO_TRX ) ) { + if ( !$this->getFlag( self::DBO_TRX ) ) { $this->mTrxAutomaticAtomic = true; } } @@ -2740,7 +2744,7 @@ $this->queryLogger->error( $msg ); return; // join the main transaction set } - } elseif ( $this->getFlag( DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) { + } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) { // @TODO: make this an exception at some point $msg = "$fname: Implicit transaction expected (DBO_TRX set)."; $this->queryLogger->error( $msg ); @@ -2852,7 +2856,7 @@ $this->queryLogger->error( "$fname: No transaction to rollback, something got out of sync." ); return; // nothing to do - } elseif ( $this->getFlag( DBO_TRX ) ) { + } elseif ( $this->getFlag( self::DBO_TRX ) ) { throw new DBUnexpectedError( $this, "$fname: Expected mass rollback of all peer databases (DBO_TRX set)." @@ -3019,7 +3023,7 @@ } // This will reconnect if possible or return false if not - $this->clearFlag( DBO_TRX, self::REMEMBER_PRIOR ); + $this->clearFlag( self::DBO_TRX, self::REMEMBER_PRIOR ); $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false ); $this->restoreFlags( self::RESTORE_PRIOR ); diff --git a/includes/libs/rdbms/database/DatabaseMysql.php b/includes/libs/rdbms/database/DatabaseMysql.php index 87330b0..9ab7c64 100644 --- a/includes/libs/rdbms/database/DatabaseMysql.php +++ b/includes/libs/rdbms/database/DatabaseMysql.php @@ -59,10 +59,10 @@ } $connFlags = 0; - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connFlags |= MYSQL_CLIENT_SSL; } - if ( $this->mFlags & DBO_COMPRESS ) { + if ( $this->mFlags & self::DBO_COMPRESS ) { $connFlags |= MYSQL_CLIENT_COMPRESS; } @@ -81,7 +81,7 @@ if ( $i > 1 ) { usleep( 1000 ); } - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $conn = mysql_pconnect( $realServer, $this->mUser, $this->mPassword, $connFlags ); } else { # Create a new connection... diff --git a/includes/libs/rdbms/database/DatabaseMysqli.php b/includes/libs/rdbms/database/DatabaseMysqli.php index fb983bd..c34f901 100644 --- a/includes/libs/rdbms/database/DatabaseMysqli.php +++ b/includes/libs/rdbms/database/DatabaseMysqli.php @@ -82,7 +82,7 @@ $mysqli = mysqli_init(); $connFlags = 0; - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connFlags |= MYSQLI_CLIENT_SSL; $mysqli->ssl_set( $this->sslKeyPath, @@ -92,10 +92,10 @@ $this->sslCiphers ); } - if ( $this->mFlags & DBO_COMPRESS ) { + if ( $this->mFlags & self::DBO_COMPRESS ) { $connFlags |= MYSQLI_CLIENT_COMPRESS; } - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $realServer = 'p:' . $realServer; } diff --git a/includes/libs/rdbms/database/DatabasePostgres.php b/includes/libs/rdbms/database/DatabasePostgres.php index 84439f4..a69a5fa 100644 --- a/includes/libs/rdbms/database/DatabasePostgres.php +++ b/includes/libs/rdbms/database/DatabasePostgres.php @@ -109,7 +109,7 @@ if ( (int)$this->port > 0 ) { $connectVars['port'] = (int)$this->port; } - if ( $this->mFlags & DBO_SSL ) { + if ( $this->mFlags & self::DBO_SSL ) { $connectVars['sslmode'] = 1; } diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index f201dad..c33d3b3 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -171,7 +171,7 @@ $this->dbPath = $fileName; try { - if ( $this->mFlags & DBO_PERSISTENT ) { + if ( $this->mFlags & self::DBO_PERSISTENT ) { $this->mConn = new PDO( "sqlite:$fileName", '', '', [ PDO::ATTR_PERSISTENT => true ] ); } else { diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index 56eb002..711bdb0 100644 --- a/includes/libs/rdbms/database/IDatabase.php +++ b/includes/libs/rdbms/database/IDatabase.php @@ -1,5 +1,4 @@ <?php - /** * @defgroup Database Database * @@ -74,6 +73,27 @@ /** @var int Combine list with OR clauses */ const LIST_OR = 4; + /** @var int Enable debug logging */ + const DBO_DEBUG = 1; + /** @var int Disable query buffering (only one result set can be iterated at a time) */ + const DBO_NOBUFFER = 2; + /** @var int Ignore query errors (internal use only!) */ + const DBO_IGNORE = 4; + /** @var int Autoatically start transaction on first query (work with ILoadBalancer rounds) */ + const DBO_TRX = 8; + /** @var int Use DBO_TRX in non-CLI mode */ + const DBO_DEFAULT = 16; + /** @var int Use DB persistent connections if possible */ + const DBO_PERSISTENT = 32; + /** @var int DBA session mode; mostly for Oracle */ + const DBO_SYSDBA = 64; + /** @var int Schema file mode; mostly for Oracle */ + const DBO_DDLMODE = 128; + /** @var int Enable SSL/TLS in connection protocol */ + const DBO_SSL = 256; + /** @var int Enable compression in connection protocol */ + const DBO_COMPRESS = 512; + /** * A string describing the current software version, and possibly * other details in a user-friendly way. Will be listed on Special:Version, etc. diff --git a/includes/libs/rdbms/defines.php b/includes/libs/rdbms/defines.php index b420ca1..4b125b6 100644 --- a/includes/libs/rdbms/defines.php +++ b/includes/libs/rdbms/defines.php @@ -1,24 +1,27 @@ <?php +require_once __DIR__ . '/database/IDatabase.php'; +require_once __DIR__ . '/loadbalancer/ILoadBalancer.php'; + /**@{ * Database related constants */ -define( 'DBO_DEBUG', 1 ); -define( 'DBO_NOBUFFER', 2 ); -define( 'DBO_IGNORE', 4 ); -define( 'DBO_TRX', 8 ); // automatically start transaction on first query -define( 'DBO_DEFAULT', 16 ); -define( 'DBO_PERSISTENT', 32 ); -define( 'DBO_SYSDBA', 64 ); // for oracle maintenance -define( 'DBO_DDLMODE', 128 ); // when using schema files: mostly for Oracle -define( 'DBO_SSL', 256 ); -define( 'DBO_COMPRESS', 512 ); +define( 'DBO_DEBUG', IDatabase::DBO_DEBUG ); +define( 'DBO_NOBUFFER', IDatabase::DBO_NOBUFFER ); +define( 'DBO_IGNORE', IDatabase::DBO_IGNORE ); +define( 'DBO_TRX', IDatabase::DBO_TRX ); +define( 'DBO_DEFAULT', IDatabase::DBO_DEFAULT ); +define( 'DBO_PERSISTENT', IDatabase::DBO_PERSISTENT ); +define( 'DBO_SYSDBA', IDatabase::DBO_SYSDBA ); +define( 'DBO_DDLMODE', IDatabase::DBO_DDLMODE ); +define( 'DBO_SSL', IDatabase::DBO_SSL ); +define( 'DBO_COMPRESS', IDatabase::DBO_COMPRESS ); /**@}*/ /**@{ * Valid database indexes * Operation-based indexes */ -define( 'DB_REPLICA', -1 ); # Read from a replica (or only server) -define( 'DB_MASTER', -2 ); # Write to master (or only server) +define( 'DB_REPLICA', ILoadBalancer::DB_REPLICA ); +define( 'DB_MASTER', ILoadBalancer::DB_MASTER ); /**@}*/ diff --git a/includes/libs/rdbms/lbfactory/LBFactoryMulti.php b/includes/libs/rdbms/lbfactory/LBFactoryMulti.php index 6993dac..2fb8c4b 100644 --- a/includes/libs/rdbms/lbfactory/LBFactoryMulti.php +++ b/includes/libs/rdbms/lbfactory/LBFactoryMulti.php @@ -359,7 +359,7 @@ } $serverInfo['hostName'] = $serverName; $serverInfo['load'] = $load; - $serverInfo += [ 'flags' => DBO_DEFAULT ]; + $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ]; $servers[] = $serverInfo; } diff --git a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php index 3e1261e..cd51e66 100644 --- a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php @@ -21,7 +21,6 @@ * @ingroup Database * @author Aaron Schulz */ -require_once __DIR__ . '/../defines.php'; /** * Database cluster connection, tracking, load balancing, and transaction manager interface @@ -74,10 +73,10 @@ * @ingroup Database */ interface ILoadBalancer { - /** @var integer Request a master DB connection */ - const DB_MASTER = DB_MASTER; /** @var integer Request a replica DB connection */ - const DB_REPLICA = DB_REPLICA; + const DB_REPLICA = -1; + /** @var integer Request a master DB connection */ + const DB_MASTER = -2; /** * Construct a manager of IDatabase connection objects diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index bda185a..5a1a8ba 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -530,10 +530,10 @@ ? [ false ] // check one "group": the generic pool : (array)$groups; - $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() ); + $masterOnly = ( $i == self::DB_MASTER || $i == $this->getWriterIndex() ); $oldConnsOpened = $this->connsOpened; // connections open now - if ( $i == DB_MASTER ) { + if ( $i == self::DB_MASTER ) { $i = $this->getWriterIndex(); } else { # Try to find an available server in any the query groups (in order) @@ -547,7 +547,7 @@ } # Operation-based index - if ( $i == DB_REPLICA ) { + if ( $i == self::DB_REPLICA ) { $this->mLastError = 'Unknown error'; // reset error string # Try the general server pool if $groups are unavailable. $i = in_array( false, $groups, true ) @@ -589,7 +589,7 @@ /** * This can happen in code like: * foreach ( $dbs as $db ) { - * $conn = $lb->getConnection( DB_REPLICA, [], $db ); + * $conn = $lb->getConnection( $lb::DB_REPLICA, [], $db ); * ... * $lb->reuseConnection( $conn ); * } @@ -816,7 +816,7 @@ $server['agent'] = $this->agent; // Use DBO_DEFAULT flags by default for LoadBalancer managed databases. Assume that the // application calls LoadBalancer::commitMasterChanges() before the PHP script completes. - $server['flags'] = isset( $server['flags'] ) ? $server['flags'] : DBO_DEFAULT; + $server['flags'] = isset( $server['flags'] ) ? $server['flags'] : IDatabase::DBO_DEFAULT; // Create a live connection object try { @@ -829,7 +829,7 @@ $db->setLBInfo( $server ); $db->setLazyMasterHandle( - $this->getLazyConnectionRef( DB_MASTER, [], $db->getDomainID() ) + $this->getLazyConnectionRef( self::DB_MASTER, [], $db->getDomainID() ) ); $db->setTableAliases( $this->tableAliases ); @@ -1170,10 +1170,10 @@ * @param IDatabase $conn */ private function applyTransactionRoundFlags( IDatabase $conn ) { - if ( $conn->getFlag( DBO_DEFAULT ) ) { + if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) { // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT. // Force DBO_TRX even in CLI mode since a commit round is expected soon. - $conn->setFlag( DBO_TRX, $conn::REMEMBER_PRIOR ); + $conn->setFlag( $conn::DBO_TRX, $conn::REMEMBER_PRIOR ); // If config has explicitly requested DBO_TRX be either on or off by not // setting DBO_DEFAULT, then respect that. Forcing no transactions is useful // for things like blob stores (ExternalStore) which want auto-commit mode. @@ -1184,7 +1184,7 @@ * @param IDatabase $conn */ private function undoTransactionRoundFlags( IDatabase $conn ) { - if ( $conn->getFlag( DBO_DEFAULT ) ) { + if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) { $conn->restoreFlags( $conn::RESTORE_PRIOR ); } } @@ -1238,7 +1238,7 @@ if ( !$this->laggedReplicaMode && $this->getServerCount() > 1 ) { try { // See if laggedReplicaMode gets set - $conn = $this->getConnection( DB_REPLICA, false, $domain ); + $conn = $this->getConnection( self::DB_REPLICA, false, $domain ); $this->reuseConnection( $conn ); } catch ( DBConnectionError $e ) { // Avoid expensive re-connect attempts and failures @@ -1305,7 +1305,7 @@ function () use ( $domain, $conn ) { $this->trxProfiler->setSilenced( true ); try { - $dbw = $conn ?: $this->getConnection( DB_MASTER, [], $domain ); + $dbw = $conn ?: $this->getConnection( self::DB_MASTER, [], $domain ); $readOnly = (int)$dbw->serverIsReadOnly(); if ( !$conn ) { $this->reuseConnection( $dbw ); @@ -1432,7 +1432,7 @@ if ( !$pos ) { // Get the current master position - $dbw = $this->getConnection( DB_MASTER ); + $dbw = $this->getConnection( self::DB_MASTER ); $pos = $dbw->getMasterPos(); $this->reuseConnection( $dbw ); } -- To view, visit https://gerrit.wikimedia.org/r/312554 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: If13b23ef849d4cf4c711b0ec2bf2e8a795f90738 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits