2017-07-20 03:51:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
2017-12-20 18:37:13 +00:00
|
|
|
use Wikimedia\Rdbms\DBError;
|
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-07-20 03:51:54 +00:00
|
|
|
use Wikimedia\Rdbms\LoadBalancer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Holds tests for LoadBalancer MediaWiki class.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @group Database
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
class LoadBalancerTest extends MediaWikiTestCase {
|
2017-12-20 18:37:13 +00:00
|
|
|
public function testWithoutReplica() {
|
2017-07-20 03:51:54 +00:00
|
|
|
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
|
|
|
|
|
|
|
|
|
|
$servers = [
|
|
|
|
|
[
|
|
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 0,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$lb = new LoadBalancer( [
|
|
|
|
|
'servers' => $servers,
|
|
|
|
|
'localDomain' => wfWikiID()
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
|
|
|
|
|
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
|
2017-12-20 18:37:13 +00:00
|
|
|
$this->assertWriteAllowed( $dbw );
|
2017-07-20 03:51:54 +00:00
|
|
|
|
|
|
|
|
$dbr = $lb->getConnection( DB_REPLICA );
|
|
|
|
|
$this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
|
2017-12-20 18:37:13 +00:00
|
|
|
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
|
2017-07-20 03:51:54 +00:00
|
|
|
|
|
|
|
|
$dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
|
|
|
|
|
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
|
|
|
|
|
$this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
|
|
|
|
|
|
|
|
|
|
$dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
|
|
|
|
|
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
|
|
|
|
|
$this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
|
|
|
|
|
|
|
|
|
|
$dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
|
|
|
|
|
|
|
|
|
|
$lb->closeAll();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 18:37:13 +00:00
|
|
|
public function testWithReplica() {
|
2017-07-20 03:51:54 +00:00
|
|
|
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
|
|
|
|
|
|
|
|
|
|
$servers = [
|
|
|
|
|
[ // master
|
|
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 0,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
|
|
|
|
],
|
2017-12-20 18:37:13 +00:00
|
|
|
[ // emulated replica
|
2017-07-20 03:51:54 +00:00
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'dbDirectory' => $wgSQLiteDataDir,
|
|
|
|
|
'load' => 100,
|
|
|
|
|
'flags' => DBO_TRX // REPEATABLE-READ for consistency
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$lb = new LoadBalancer( [
|
|
|
|
|
'servers' => $servers,
|
|
|
|
|
'localDomain' => wfWikiID(),
|
|
|
|
|
'loadMonitorClass' => 'LoadMonitorNull'
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
|
|
|
|
|
$dbw->getLBInfo( 'clusterMasterHost' ),
|
|
|
|
|
'cluster master set' );
|
|
|
|
|
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
|
2017-12-20 18:37:13 +00:00
|
|
|
$this->assertWriteAllowed( $dbw );
|
2017-07-20 03:51:54 +00:00
|
|
|
|
|
|
|
|
$dbr = $lb->getConnection( DB_REPLICA );
|
2017-12-20 18:37:13 +00:00
|
|
|
$this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
|
2017-07-20 03:51:54 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
|
|
|
|
|
$dbr->getLBInfo( 'clusterMasterHost' ),
|
|
|
|
|
'cluster master set' );
|
2017-12-20 18:37:13 +00:00
|
|
|
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
|
|
|
|
|
$this->assertWriteForbidden( $dbr );
|
2017-07-20 03:51:54 +00:00
|
|
|
|
|
|
|
|
$dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
|
|
|
|
|
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
|
|
|
|
|
$this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
|
|
|
|
|
|
|
|
|
|
$dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
|
|
|
|
|
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
|
|
|
|
|
$this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
|
|
|
|
|
|
|
|
|
|
$dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
|
|
|
|
|
$this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
|
|
|
|
|
|
|
|
|
|
$lb->closeAll();
|
|
|
|
|
}
|
2017-12-20 18:37:13 +00:00
|
|
|
|
|
|
|
|
private function assertWriteForbidden( IDatabase $db ) {
|
|
|
|
|
try {
|
|
|
|
|
$db->delete( 'user', [ 'user_id' => 57634126 ], 'TEST' );
|
|
|
|
|
$this->fail( 'Write operation should have failed!' );
|
|
|
|
|
} catch ( DBError $ex ) {
|
|
|
|
|
// check that the exception message contains "Write operation"
|
2017-12-21 11:59:53 +00:00
|
|
|
$constraint = new PHPUnit_Framework_Constraint_StringContains( 'Write operation' );
|
2017-12-20 18:37:13 +00:00
|
|
|
|
2017-12-21 11:59:53 +00:00
|
|
|
if ( !$constraint->evaluate( $ex->getMessage(), '', true ) ) {
|
2017-12-20 18:37:13 +00:00
|
|
|
// re-throw original error, to preserve stack trace
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
$db->rollback( __METHOD__, 'flush' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function assertWriteAllowed( IDatabase $db ) {
|
|
|
|
|
try {
|
|
|
|
|
$this->assertNotSame( false, $db->delete( 'user', [ 'user_id' => 57634126 ] ) );
|
|
|
|
|
} finally {
|
|
|
|
|
$db->rollback( __METHOD__, 'flush' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 03:51:54 +00:00
|
|
|
}
|