Move remaining LoadBalancer classes to Rdbms

The old names are left as aliases.

Change-Id: I52a327f2463a2ba7437324047b5b00d28cd1d758
This commit is contained in:
Aaron Schulz 2017-02-17 16:26:47 -08:00
parent e476e2f666
commit 64df456b39
23 changed files with 76 additions and 51 deletions

View file

@ -1590,6 +1590,8 @@ $wgAutoloadLocalClasses = [
'Wikimedia\\Rdbms\\LBFactorySimple' => __DIR__ . '/includes/libs/rdbms/lbfactory/LBFactorySimple.php',
'Wikimedia\\Rdbms\\LBFactorySingle' => __DIR__ . '/includes/libs/rdbms/lbfactory/LBFactorySingle.php',
'Wikimedia\\Rdbms\\LikeMatch' => __DIR__ . '/includes/libs/rdbms/encasing/LikeMatch.php',
'Wikimedia\\Rdbms\\LoadBalancer' => __DIR__ . '/includes/libs/rdbms/loadbalancer/LoadBalancer.php',
'Wikimedia\\Rdbms\\LoadBalancerSingle' => __DIR__ . '/includes/libs/rdbms/loadbalancer/LoadBalancerSingle.php',
'Wikimedia\\Rdbms\\LoadMonitor' => __DIR__ . '/includes/libs/rdbms/loadmonitor/LoadMonitor.php',
'Wikimedia\\Rdbms\\LoadMonitorMySQL' => __DIR__ . '/includes/libs/rdbms/loadmonitor/LoadMonitorMySQL.php',
'Wikimedia\\Rdbms\\LoadMonitorNull' => __DIR__ . '/includes/libs/rdbms/loadmonitor/LoadMonitorNull.php',

View file

@ -28,7 +28,6 @@ use Liuggio\StatsdClient\Sender\SocketSender;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Session\SessionManager;
use Wikimedia\ScopedCallback;
use Wikimedia\Rdbms\LBFactory;
// Hide compatibility functions from Doxygen
/// @cond
@ -3085,7 +3084,7 @@ function wfGetDB( $db, $groups = [], $wiki = false ) {
* or MediaWikiServices::getDBLoadBalancerFactory() instead.
*
* @param string|bool $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
* @return \Wikimedia\Rdbms\LoadBalancer
*/
function wfGetLB( $wiki = false ) {
if ( $wiki === false ) {
@ -3101,7 +3100,7 @@ function wfGetLB( $wiki = false ) {
*
* @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.
*
* @return LBFactory
* @return \Wikimedia\Rdbms\LBFactory
*/
function wfGetLBFactory() {
return \MediaWiki\MediaWikiServices::getInstance()->getDBLoadBalancerFactory();

View file

@ -12,7 +12,7 @@ use Hooks;
use Wikimedia\Rdbms\LBFactory;
use LinkCache;
use Liuggio\StatsdClient\Factory\StatsdDataFactory;
use LoadBalancer;
use Wikimedia\Rdbms\LoadBalancer;
use MediaHandlerFactory;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Linker\LinkRendererFactory;

View file

@ -2,6 +2,7 @@
use MediaWiki\Linker\LinkTarget;
use Wikimedia\Assert\Assert;
use Wikimedia\Rdbms\LoadBalancer;
/**
* Class performing complex database queries related to WatchedItems.

View file

@ -5,6 +5,7 @@ use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use Wikimedia\Assert\Assert;
use Wikimedia\ScopedCallback;
use Wikimedia\Rdbms\LoadBalancer;
/**
* Storage layer class for WatchedItems.

View file

@ -1,5 +1,7 @@
<?php
use Wikimedia\Rdbms\LoadBalancer;
/**
* Base class for objects that allow access to other wiki's databases using
* the foreign database access mechanism implemented by LBFactoryMulti.

View file

@ -21,6 +21,7 @@
*/
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Rdbms\LoadBalancer;
/**
* Class for managing the deferred updates

View file

@ -20,6 +20,8 @@
* @file
*/
use Wikimedia\Rdbms\LoadBalancer;
/**
* DB accessable external objects.
*
@ -106,9 +108,7 @@ class ExternalStoreDB extends ExternalStoreMedium {
* @return LoadBalancer
*/
private function getLoadBalancer( $cluster ) {
$wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
return wfGetLBFactory()->getExternalLB( $cluster );
}
/**

View file

@ -6,7 +6,6 @@ use Database;
use DBConnRef;
use IDatabase;
use InvalidArgumentException;
use LoadBalancer;
/**
* Database connection manager.

View file

@ -24,7 +24,6 @@
namespace Wikimedia\Rdbms;
use InvalidArgumentException;
use LoadBalancer;
use DBTransactionError;
use DBReplicationWaitError;

View file

@ -31,7 +31,6 @@ use WANObjectCache;
use Exception;
use RuntimeException;
use IDatabase;
use LoadBalancer;
use DBTransactionError;
use DBReplicationWaitError;

View file

@ -23,7 +23,6 @@
namespace Wikimedia\Rdbms;
use LoadBalancer;
use IDatabase;
use InvalidArgumentException;

View file

@ -23,7 +23,6 @@
namespace Wikimedia\Rdbms;
use LoadBalancer;
use InvalidArgumentException;
/**

View file

@ -23,7 +23,6 @@
namespace Wikimedia\Rdbms;
use LoadBalancerSingle;
use IDatabase;
use InvalidArgumentException;
use BadMethodCallException;

View file

@ -240,9 +240,6 @@ interface ILoadBalancer {
* Index must be an actual index into the array.
* If the server is already open, returns it.
*
* On error, returns false, and the connection which caused the
* error will be available via $this->mErrorConnection.
*
* @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
*
* @param int $i Server index or DB_MASTER/DB_REPLICA

View file

@ -20,14 +20,29 @@
* @file
* @ingroup Database
*/
namespace Wikimedia\Rdbms;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\ScopedCallback;
use Wikimedia\Rdbms\TransactionProfiler;
use Wikimedia\Rdbms\ILoadMonitor;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\DBMasterPos;
use IDatabase;
use Database;
use DBConnRef;
use MaintainableDBConnRef;
use BagOStuff;
use EmptyBagOStuff;
use WANObjectCache;
use ArrayUtils;
use DBError;
use DBAccessError;
use DBExpectedError;
use DBUnexpectedError;
use DBTransactionError;
use DBTransactionSizeError;
use DBConnectionError;
use InvalidArgumentException;
use RuntimeException;
use Exception;
/**
* Database connection, tracking, load balancing, and transaction manager for a cluster
@ -37,7 +52,7 @@ use Wikimedia\Rdbms\DBMasterPos;
class LoadBalancer implements ILoadBalancer {
/** @var array[] Map of (server index => server config array) */
private $mServers;
/** @var IDatabase[][][] Map of local/foreignUsed/foreignFree => server index => IDatabase array */
/** @var \Database[][][] Map of local/foreignUsed/foreignFree => server index => IDatabase array */
private $mConns;
/** @var float[] Map of (server index => weight) */
private $mLoads;
@ -73,8 +88,8 @@ class LoadBalancer implements ILoadBalancer {
/** @var LoggerInterface */
protected $perfLogger;
/** @var bool|IDatabase Database connection that caused a problem */
private $mErrorConnection;
/** @var \Database Database connection that caused a problem */
private $errorConnection;
/** @var integer The generic (not query grouped) replica DB index (of $mServers) */
private $mReadIndex;
/** @var bool|DBMasterPos False if not set */
@ -146,7 +161,6 @@ class LoadBalancer implements ILoadBalancer {
];
$this->mLoads = [];
$this->mWaitForPos = false;
$this->mErrorConnection = false;
$this->mAllowLagged = false;
if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
@ -218,9 +232,9 @@ class LoadBalancer implements ILoadBalancer {
private function getLoadMonitor() {
if ( !isset( $this->loadMonitor ) ) {
$compat = [
'LoadMonitor' => Wikimedia\Rdbms\LoadMonitor::class,
'LoadMonitorNull' => Wikimedia\Rdbms\LoadMonitorNull::class,
'LoadMonitorMySQL' => Wikimedia\Rdbms\LoadMonitorMySQL::class,
'LoadMonitor' => LoadMonitor::class,
'LoadMonitorNull' => LoadMonitorNull::class,
'LoadMonitorMySQL' => LoadMonitorMySQL::class,
];
$class = $this->loadMonitorConfig['class'];
@ -722,17 +736,17 @@ class LoadBalancer implements ILoadBalancer {
$this->mConns['local'][$i][0] = $conn;
} else {
$this->connLogger->warning( "Failed to connect to database $i at '$serverName'." );
$this->mErrorConnection = $conn;
$this->errorConnection = $conn;
$conn = false;
}
}
if ( $conn && !$conn->isOpen() ) {
if ( $conn instanceof IDatabase && !$conn->isOpen() ) {
// Connection was made but later unrecoverably lost for some reason.
// Do not return a handle that will just throw exceptions on use,
// but let the calling code (e.g. getReaderIndex) try another server.
// See DatabaseMyslBase::ping() for how this can happen.
$this->mErrorConnection = $conn;
$this->errorConnection = $conn;
$conn = false;
}
@ -751,7 +765,7 @@ class LoadBalancer implements ILoadBalancer {
* it has been freed first with reuseConnection().
*
* On error, returns false, and the connection which caused the
* error will be available via $this->mErrorConnection.
* error will be available via $this->errorConnection.
*
* @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
*
@ -783,7 +797,7 @@ class LoadBalancer implements ILoadBalancer {
if ( strlen( $dbName ) && !$conn->selectDB( $dbName ) ) {
$this->mLastError = "Error selecting database '$dbName' on server " .
$conn->getServer() . " from client host {$this->host}";
$this->mErrorConnection = $conn;
$this->errorConnection = $conn;
$conn = false;
} else {
$conn->tablePrefix( $prefix );
@ -804,7 +818,7 @@ class LoadBalancer implements ILoadBalancer {
$conn = $this->reallyOpenConnection( $server, $dbName );
if ( !$conn->isOpen() ) {
$this->connLogger->warning( __METHOD__ . ": connection error for $i/$domain" );
$this->mErrorConnection = $conn;
$this->errorConnection = $conn;
$conn = false;
} else {
$conn->tablePrefix( $prefix );
@ -814,7 +828,7 @@ class LoadBalancer implements ILoadBalancer {
}
// Increment reference count
if ( $conn ) {
if ( $conn instanceof IDatabase ) {
$refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
$conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
}
@ -912,22 +926,13 @@ class LoadBalancer implements ILoadBalancer {
* @throws DBConnectionError
*/
private function reportConnectionError() {
$conn = $this->mErrorConnection; // the connection which caused the error
$conn = $this->errorConnection; // the connection which caused the error
$context = [
'method' => __METHOD__,
'last_error' => $this->mLastError,
];
if ( !is_object( $conn ) ) {
// No last connection, probably due to all servers being too busy
$this->connLogger->error(
"LB failure with no last connection. Connection error: {last_error}",
$context
);
// If all servers were busy, mLastError will contain something sensible
throw new DBConnectionError( null, $this->mLastError );
} else {
if ( $conn instanceof IDatabase ) {
$context['db_server'] = $conn->getServer();
$this->connLogger->warning(
"Connection error: {last_error} ({db_server})",
@ -936,6 +941,15 @@ class LoadBalancer implements ILoadBalancer {
// throws DBConnectionError
$conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
} else {
// No last connection, probably due to all servers being too busy
$this->connLogger->error(
"LB failure with no last connection. Connection error: {last_error}",
$context
);
// If all servers were busy, mLastError will contain something sensible
throw new DBConnectionError( null, $this->mLastError );
}
}
@ -1355,7 +1369,7 @@ class LoadBalancer implements ILoadBalancer {
/**
* @param string $domain Domain ID, or false for the current domain
* @param IDatabase|null DB master connectionl used to avoid loops [optional]
* @param IDatabase|null $conn DB master connectionl used to avoid loops [optional]
* @return bool
*/
private function masterRunningReadOnly( $domain, IDatabase $conn = null ) {
@ -1591,3 +1605,5 @@ class LoadBalancer implements ILoadBalancer {
$this->disable();
}
}
class_alias( LoadBalancer::class, 'LoadBalancer' );

View file

@ -21,6 +21,11 @@
* @ingroup Database
*/
namespace Wikimedia\Rdbms;
use IDatabase;
use InvalidArgumentException;
/**
* Trivial LoadBalancer that always returns an injected connection handle
*/
@ -72,3 +77,5 @@ class LoadBalancerSingle extends LoadBalancer {
return $this->db;
}
}
class_alias( 'Wikimedia\Rdbms\LoadBalancerSingle', 'LoadBalancerSingle' );

View file

@ -24,6 +24,7 @@
use \MediaWiki\MediaWikiServices;
use \Wikimedia\WaitConditionLoop;
use \Wikimedia\Rdbms\TransactionProfiler;
use Wikimedia\Rdbms\LoadBalancer;
/**
* Class to store objects in the database

View file

@ -1,5 +1,7 @@
<?php
use Wikimedia\Rdbms\LoadBalancer;
/**
* Represents the site configuration of a wiki.
* Holds a list of sites (ie SiteList), stored in the database.

View file

@ -27,6 +27,8 @@
require_once __DIR__ . '/Maintenance.php';
require_once __DIR__ . '/../includes/export/DumpFilter.php';
use Wikimedia\Rdbms\LoadBalancer;
/**
* @ingroup Dump Maintenance
*/

View file

@ -27,6 +27,8 @@
require_once __DIR__ . '/backup.inc';
require_once __DIR__ . '/../includes/export/WikiExporter.php';
use Wikimedia\Rdbms\LoadBalancer;
/**
* @ingroup Maintenance
*/
@ -212,7 +214,6 @@ TEXT
// We do /not/ retry upon failure, but delegate to encapsulating logic, to avoid
// individually retrying at different layers of code.
// 1. The LoadBalancer.
try {
$this->lb = wfGetLBFactory()->newMainLB();
} catch ( Exception $e ) {
@ -220,7 +221,6 @@ TEXT
. " rotating DB failed to obtain new load balancer (" . $e->getMessage() . ")" );
}
// 2. The Connection, through the load balancer.
try {
$this->db = $this->lb->getConnection( DB_REPLICA, 'dump' );
} catch ( Exception $e ) {

View file

@ -3,7 +3,7 @@
namespace Wikimedia\Tests\Rdbms;
use IDatabase;
use LoadBalancer;
use Wikimedia\Rdbms\LoadBalancer;
use PHPUnit_Framework_MockObject_MockObject;
use Wikimedia\Rdbms\ConnectionManager;

View file

@ -3,7 +3,7 @@
namespace Wikimedia\Tests\Rdbms;
use IDatabase;
use LoadBalancer;
use Wikimedia\Rdbms\LoadBalancer;
use PHPUnit_Framework_MockObject_MockObject;
use Wikimedia\Rdbms\SessionConsistentConnectionManager;