3 * Holds tests for LBFactory abstract MediaWiki class.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Antoine Musso
22 * @copyright © 2013 Antoine Musso
23 * @copyright © 2013 Wikimedia Foundation Inc.
26 use Wikimedia\Rdbms\LBFactory
;
27 use Wikimedia\Rdbms\LBFactorySimple
;
28 use Wikimedia\Rdbms\LBFactoryMulti
;
29 use Wikimedia\Rdbms\LoadBalancer
;
30 use Wikimedia\Rdbms\ChronologyProtector
;
31 use Wikimedia\Rdbms\DatabaseMysqli
;
32 use Wikimedia\Rdbms\MySQLMasterPos
;
33 use Wikimedia\Rdbms\DatabaseDomain
;
37 * @covers \Wikimedia\Rdbms\LBFactory
38 * @covers \Wikimedia\Rdbms\LBFactorySimple
39 * @covers \Wikimedia\Rdbms\LBFactoryMulti
41 class LBFactoryTest
extends MediaWikiTestCase
{
44 * @covers MWLBFactory::getLBFactoryClass()
45 * @dataProvider getLBFactoryClassProvider
47 public function testGetLBFactoryClass( $expected, $deprecated ) {
48 $mockDB = $this->getMockBuilder( DatabaseMysqli
::class )
49 ->disableOriginalConstructor()
53 'class' => $deprecated,
54 'connection' => $mockDB,
55 # Various other parameters required:
58 'serverTemplate' => [],
61 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
62 $result = MWLBFactory
::getLBFactoryClass( $config );
64 $this->assertEquals( $expected, $result );
67 public function getLBFactoryClassProvider() {
69 # Format: new class, old class
70 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactory_Simple' ],
71 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactory_Single' ],
72 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactory_Multi' ],
73 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactorySimple' ],
74 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactorySingle' ],
75 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactoryMulti' ],
80 * @covers \Wikimedia\Rdbms\LBFactory::getLocalDomainID()
81 * @covers \Wikimedia\Rdbms\LBFactory::resolveDomainID()
83 public function testLBFactorySimpleServer() {
84 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
88 'host' => $wgDBserver,
89 'dbname' => $wgDBname,
91 'password' => $wgDBpassword,
93 'dbDirectory' => $wgSQLiteDataDir,
95 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
99 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
100 $lb = $factory->getMainLB();
102 $dbw = $lb->getConnection( DB_MASTER
);
103 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
105 $dbr = $lb->getConnection( DB_REPLICA
);
106 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
108 $this->assertSame( 'my_test_wiki', $factory->resolveDomainID( 'my_test_wiki' ) );
109 $this->assertSame( $factory->getLocalDomainID(), $factory->resolveDomainID( false ) );
111 $factory->shutdown();
115 public function testLBFactorySimpleServers() {
116 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
120 'host' => $wgDBserver,
121 'dbname' => $wgDBname,
123 'password' => $wgDBpassword,
125 'dbDirectory' => $wgSQLiteDataDir,
127 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
129 [ // emulated replica
130 'host' => $wgDBserver,
131 'dbname' => $wgDBname,
133 'password' => $wgDBpassword,
135 'dbDirectory' => $wgSQLiteDataDir,
137 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
141 $factory = new LBFactorySimple( [
142 'servers' => $servers,
143 'loadMonitorClass' => LoadMonitorNull
::class
145 $lb = $factory->getMainLB();
147 $dbw = $lb->getConnection( DB_MASTER
);
148 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
150 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
151 $dbw->getLBInfo( 'clusterMasterHost' ),
152 'cluster master set' );
154 $dbr = $lb->getConnection( DB_REPLICA
);
155 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
157 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
158 $dbr->getLBInfo( 'clusterMasterHost' ),
159 'cluster master set' );
161 $factory->shutdown();
165 public function testLBFactoryMultiConns() {
166 $factory = $this->newLBFactoryMultiLBs();
168 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
169 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
171 $dbr = $factory->getMainLB()->getConnection( DB_REPLICA
);
172 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
174 // Destructor should trigger without round stage errors
178 public function testLBFactoryMultiRoundCallbacks() {
180 $countLBsFunc = function ( LBFactoryMulti
$factory ) {
182 $factory->forEachLB( function () use ( &$count ) {
189 $factory = $this->newLBFactoryMultiLBs();
190 $this->assertEquals( 0, $countLBsFunc( $factory ) );
191 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
192 $this->assertEquals( 1, $countLBsFunc( $factory ) );
193 // Test that LoadBalancer instances made during pre-commit callbacks in do not
194 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.
195 $factory->beginMasterChanges( __METHOD__
);
196 $dbw->onTransactionPreCommitOrIdle( function () use ( $factory, &$called ) {
198 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
199 // There is no s1wiki DB to select so it is not in getConnection(),
200 // but this fools getMainLB() at least.
201 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
203 $factory->commitMasterChanges( __METHOD__
);
204 $this->assertEquals( 1, $called );
205 $this->assertEquals( 2, $countLBsFunc( $factory ) );
206 $factory->shutdown();
207 $factory->closeAll();
210 $factory = $this->newLBFactoryMultiLBs();
211 $this->assertEquals( 0, $countLBsFunc( $factory ) );
212 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
213 $this->assertEquals( 1, $countLBsFunc( $factory ) );
214 // Test that LoadBalancer instances made during pre-commit callbacks in do not
215 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.hrow
216 // DBTransactionError due to transaction ROUND_* stages being mismatched.
217 $factory->beginMasterChanges( __METHOD__
);
218 $dbw->query( "SELECT 1 as t", __METHOD__
);
219 $dbw->onTransactionResolution( function () use ( $factory, &$called ) {
221 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
222 // There is no s1wiki DB to select so it is not in getConnection(),
223 // but this fools getMainLB() at least.
224 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
226 $factory->commitMasterChanges( __METHOD__
);
227 $this->assertEquals( 1, $called );
228 $this->assertEquals( 2, $countLBsFunc( $factory ) );
229 $factory->shutdown();
230 $factory->closeAll();
232 $factory = $this->newLBFactoryMultiLBs();
233 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
234 // DBTransactionError should not be thrown
236 $dbw->onTransactionPreCommitOrIdle( function () use ( &$ran ) {
239 $factory->commitAll( __METHOD__
);
240 $this->assertEquals( 1, $ran );
242 $factory->shutdown();
243 $factory->closeAll();
246 private function newLBFactoryMultiLBs() {
247 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
249 return new LBFactoryMulti( [
263 'serverTemplate' => [
264 'dbname' => $wgDBname,
266 'password' => $wgDBpassword,
268 'dbDirectory' => $wgSQLiteDataDir,
269 'flags' => DBO_DEFAULT
272 'test-db1' => $wgDBserver,
273 'test-db2' => $wgDBserver,
274 'test-db3' => $wgDBserver,
275 'test-db4' => $wgDBserver
277 'loadMonitorClass' => LoadMonitorNull
::class
282 * @covers \Wikimedia\Rdbms\ChronologyProtector
284 public function testChronologyProtector() {
285 $now = microtime( true );
287 // (a) First HTTP request
288 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
289 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
292 $mockDB1 = $this->getMockBuilder( DatabaseMysqli
::class )
293 ->disableOriginalConstructor()
295 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
296 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
297 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
298 // Load balancer for master DB 1
299 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
300 ->disableOriginalConstructor()
302 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
303 $lb1->method( 'getServerCount' )->willReturn( 2 );
304 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
305 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
306 function () use ( $mockDB1 ) {
308 $p |
= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
309 $p |
= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
314 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
315 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
317 $mockDB2 = $this->getMockBuilder( DatabaseMysqli
::class )
318 ->disableOriginalConstructor()
320 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
321 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
322 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
323 // Load balancer for master DB 2
324 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
325 ->disableOriginalConstructor()
327 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
328 $lb2->method( 'getServerCount' )->willReturn( 2 );
329 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
330 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
331 function () use ( $mockDB2 ) {
333 $p |
= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
334 $p |
= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
339 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
340 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
342 $bag = new HashBagOStuff();
343 $cp = new ChronologyProtector(
347 'agent' => "Totally-Not-FireFox"
351 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
352 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
353 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
354 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
356 // Nothing to wait for on first HTTP request start
359 // Record positions in stash on first HTTP request end
360 $cp->shutdownLB( $lb1 );
361 $cp->shutdownLB( $lb2 );
363 $cp->shutdown( null, 'sync', $cpIndex );
365 $this->assertEquals( 1, $cpIndex, "CP write index set" );
367 // (b) Second HTTP request
369 // Load balancer for master DB 1
370 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
371 ->disableOriginalConstructor()
373 $lb1->method( 'getServerCount' )->willReturn( 2 );
374 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
375 $lb1->expects( $this->once() )
376 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
377 // Load balancer for master DB 2
378 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
379 ->disableOriginalConstructor()
381 $lb2->method( 'getServerCount' )->willReturn( 2 );
382 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
383 $lb2->expects( $this->once() )
384 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
386 $cp = new ChronologyProtector(
390 'agent' => "Totally-Not-FireFox"
395 // Wait for last positions to be reached on second HTTP request start
398 // Shutdown (nothing to record)
399 $cp->shutdownLB( $lb1 );
400 $cp->shutdownLB( $lb2 );
402 $cp->shutdown( null, 'sync', $cpIndex );
404 $this->assertEquals( null, $cpIndex, "CP write index retained" );
406 $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
409 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
410 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
411 global $wgSQLiteDataDir;
413 return new LBFactoryMulti( $baseOverride +
[
414 'sectionsByDB' => [],
420 'serverTemplate' => $serverOverride +
[
421 'dbname' => $wgDBname,
422 'tablePrefix' => $wgDBprefix,
424 'password' => $wgDBpassword,
426 'dbDirectory' => $wgSQLiteDataDir,
427 'flags' => DBO_DEFAULT
430 'test-db1' => $wgDBserver,
432 'loadMonitorClass' => LoadMonitorNull
::class,
433 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
434 'agent' => 'MW-UNIT-TESTS'
438 public function testNiceDomains() {
441 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
442 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
446 $factory = $this->newLBFactoryMulti(
450 $lb = $factory->getMainLB();
452 $db = $lb->getConnectionRef( DB_MASTER
);
459 /** @var Database $db */
460 $db = $lb->getConnection( DB_MASTER
, [], '' );
465 'Null domain ID handle used'
470 'Null domain ID handle used'
475 'Main domain ID handle used; prefix is empty though'
478 $this->quoteTable( $db, 'page' ),
479 $db->tableName( 'page' ),
480 "Correct full table name"
483 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
484 $db->tableName( "$wgDBname.page" ),
485 "Correct full table name"
488 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
489 $db->tableName( 'nice_db.page' ),
490 "Correct full table name"
493 $lb->reuseConnection( $db ); // don't care
495 $db = $lb->getConnection( DB_MASTER
); // local domain connection
496 $factory->setDomainPrefix( 'my_' );
498 $this->assertEquals( $wgDBname, $db->getDBname() );
504 $this->quoteTable( $db, 'my_page' ),
505 $db->tableName( 'page' ),
506 "Correct full table name"
509 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
510 $db->tableName( 'other_nice_db.page' ),
511 "Correct full table name"
514 $factory->closeAll();
518 public function testTrickyDomain() {
521 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
522 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
526 $dbname = 'unittest-domain'; // explodes if DB is selected
527 $factory = $this->newLBFactoryMulti(
528 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
530 'dbName' => 'do_not_select_me' // explodes if DB is selected
533 $lb = $factory->getMainLB();
534 /** @var Database $db */
535 $db = $lb->getConnection( DB_MASTER
, [], '' );
537 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
540 $this->quoteTable( $db, 'page' ),
541 $db->tableName( 'page' ),
542 "Correct full table name"
546 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
547 $db->tableName( "$dbname.page" ),
548 "Correct full table name"
552 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
553 $db->tableName( 'nice_db.page' ),
554 "Correct full table name"
557 $lb->reuseConnection( $db ); // don't care
559 $factory->setDomainPrefix( 'my_' );
560 $db = $lb->getConnection( DB_MASTER
, [], "$wgDBname-my_" );
563 $this->quoteTable( $db, 'my_page' ),
564 $db->tableName( 'page' ),
565 "Correct full table name"
568 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
569 $db->tableName( 'other_nice_db.page' ),
570 "Correct full table name"
573 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
574 $db->tableName( 'garbage-db.page' ),
575 "Correct full table name"
578 $lb->reuseConnection( $db ); // don't care
580 $factory->closeAll();
584 public function testInvalidSelectDB() {
585 // FIXME: fails under sqlite
586 $this->markTestSkippedIfDbType( 'sqlite' );
587 $dbname = 'unittest-domain'; // explodes if DB is selected
588 $factory = $this->newLBFactoryMulti(
589 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
591 'dbName' => 'do_not_select_me' // explodes if DB is selected
594 $lb = $factory->getMainLB();
595 /** @var Database $db */
596 $db = $lb->getConnection( DB_MASTER
, [], '' );
598 if ( $db->getType() === 'sqlite' ) {
599 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
600 } elseif ( $db->databasesAreIndependent() ) {
603 $db->selectDB( 'garbage-db' );
604 } catch ( \Wikimedia\Rdbms\DBConnectionError
$e ) {
607 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError
::class, $e );
608 $this->assertFalse( $db->isOpen() );
610 \Wikimedia\
suppressWarnings();
612 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
613 $this->fail( "No error thrown." );
614 } catch ( \Wikimedia\Rdbms\DBExpectedError
$e ) {
616 "Could not select database 'garbage-db'.",
620 \Wikimedia\restoreWarnings
();
624 public function testRedefineLocalDomain() {
627 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
628 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
632 $factory = $this->newLBFactoryMulti(
636 $lb = $factory->getMainLB();
638 $conn1 = $lb->getConnectionRef( DB_MASTER
);
641 $conn1->getDomainID()
645 $factory->redefineLocalDomain( 'somedb-prefix' );
646 $this->assertEquals( 'somedb-prefix', $factory->getLocalDomainID() );
648 $domain = new DatabaseDomain( $wgDBname, null, 'pref' );
649 $factory->redefineLocalDomain( $domain );
652 $lb->forEachOpenConnection( function () use ( &$n ) {
655 $this->assertEquals( 0, $n, "Connections closed" );
657 $conn2 = $lb->getConnectionRef( DB_MASTER
);
660 $conn2->getDomainID()
664 $factory->closeAll();
668 private function quoteTable( Database
$db, $table ) {
669 if ( $db->getType() === 'sqlite' ) {
672 return $db->addIdentifierQuotes( $table );
677 * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
678 * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
680 public function testCPPosIndexCookieValues() {
682 $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
685 '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
686 LBFactory
::makeCookieValueFromCPIndex( 3, 542, $agentId )
689 $lbFactory = $this->newLBFactoryMulti();
690 $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
692 '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
693 LBFactory
::makeCookieValueFromCPIndex( 1, 542, $agentId )
698 LBFactory
::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
703 LBFactory
::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
708 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
714 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
719 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
9 - 10 )['index'],
724 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time +
9 - 10 )['index'],
725 'Almost stale; bad index'
729 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
11 - 10 )['index'],
735 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
740 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time +
11 - 10 )['clientId'],