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\Database
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use Wikimedia\Rdbms\IMaintainableDatabase
;
29 use Wikimedia\Rdbms\LBFactory
;
30 use Wikimedia\Rdbms\LBFactorySimple
;
31 use Wikimedia\Rdbms\LBFactoryMulti
;
32 use Wikimedia\Rdbms\LoadBalancer
;
33 use Wikimedia\Rdbms\ChronologyProtector
;
34 use Wikimedia\Rdbms\DatabaseMysqli
;
35 use Wikimedia\Rdbms\MySQLMasterPos
;
36 use Wikimedia\Rdbms\DatabaseDomain
;
40 * @covers \Wikimedia\Rdbms\LBFactory
41 * @covers \Wikimedia\Rdbms\LBFactorySimple
42 * @covers \Wikimedia\Rdbms\LBFactoryMulti
44 class LBFactoryTest
extends MediaWikiTestCase
{
47 * @covers MWLBFactory::getLBFactoryClass()
48 * @dataProvider getLBFactoryClassProvider
50 public function testGetLBFactoryClass( $expected, $deprecated ) {
51 $mockDB = $this->getMockBuilder( DatabaseMysqli
::class )
52 ->disableOriginalConstructor()
56 'class' => $deprecated,
57 'connection' => $mockDB,
58 # Various other parameters required:
61 'serverTemplate' => [],
64 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
65 $result = MWLBFactory
::getLBFactoryClass( $config );
67 $this->assertEquals( $expected, $result );
70 public function getLBFactoryClassProvider() {
72 # Format: new class, old class
73 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactory_Simple' ],
74 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactory_Single' ],
75 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactory_Multi' ],
76 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactorySimple' ],
77 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactorySingle' ],
78 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactoryMulti' ],
83 * @covers \Wikimedia\Rdbms\LBFactory::getLocalDomainID()
84 * @covers \Wikimedia\Rdbms\LBFactory::resolveDomainID()
86 public function testLBFactorySimpleServer() {
87 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
91 'host' => $wgDBserver,
92 'dbname' => $wgDBname,
94 'password' => $wgDBpassword,
96 'dbDirectory' => $wgSQLiteDataDir,
98 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
102 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
103 $lb = $factory->getMainLB();
105 $dbw = $lb->getConnection( DB_MASTER
);
106 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
108 $dbr = $lb->getConnection( DB_REPLICA
);
109 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
111 $this->assertSame( 'my_test_wiki', $factory->resolveDomainID( 'my_test_wiki' ) );
112 $this->assertSame( $factory->getLocalDomainID(), $factory->resolveDomainID( false ) );
114 $factory->shutdown();
118 public function testLBFactorySimpleServers() {
119 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
123 'host' => $wgDBserver,
124 'dbname' => $wgDBname,
126 'password' => $wgDBpassword,
128 'dbDirectory' => $wgSQLiteDataDir,
130 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
132 [ // emulated replica
133 'host' => $wgDBserver,
134 'dbname' => $wgDBname,
136 'password' => $wgDBpassword,
138 'dbDirectory' => $wgSQLiteDataDir,
140 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
144 $factory = new LBFactorySimple( [
145 'servers' => $servers,
146 'loadMonitorClass' => LoadMonitorNull
::class
148 $lb = $factory->getMainLB();
150 $dbw = $lb->getConnection( DB_MASTER
);
151 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
153 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
154 $dbw->getLBInfo( 'clusterMasterHost' ),
155 'cluster master set' );
157 $dbr = $lb->getConnection( DB_REPLICA
);
158 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
160 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
161 $dbr->getLBInfo( 'clusterMasterHost' ),
162 'cluster master set' );
164 $factory->shutdown();
168 public function testLBFactoryMultiConns() {
169 $factory = $this->newLBFactoryMultiLBs();
171 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
172 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
174 $dbr = $factory->getMainLB()->getConnection( DB_REPLICA
);
175 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'replica shows as replica' );
177 // Destructor should trigger without round stage errors
181 public function testLBFactoryMultiRoundCallbacks() {
183 $countLBsFunc = function ( LBFactoryMulti
$factory ) {
185 $factory->forEachLB( function () use ( &$count ) {
192 $factory = $this->newLBFactoryMultiLBs();
193 $this->assertEquals( 0, $countLBsFunc( $factory ) );
194 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
195 $this->assertEquals( 1, $countLBsFunc( $factory ) );
196 // Test that LoadBalancer instances made during pre-commit callbacks in do not
197 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.
198 $factory->beginMasterChanges( __METHOD__
);
199 $dbw->onTransactionPreCommitOrIdle( function () use ( $factory, &$called ) {
201 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
202 // There is no s1wiki DB to select so it is not in getConnection(),
203 // but this fools getMainLB() at least.
204 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
206 $factory->commitMasterChanges( __METHOD__
);
207 $this->assertEquals( 1, $called );
208 $this->assertEquals( 2, $countLBsFunc( $factory ) );
209 $factory->shutdown();
210 $factory->closeAll();
213 $factory = $this->newLBFactoryMultiLBs();
214 $this->assertEquals( 0, $countLBsFunc( $factory ) );
215 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
216 $this->assertEquals( 1, $countLBsFunc( $factory ) );
217 // Test that LoadBalancer instances made during pre-commit callbacks in do not
218 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.hrow
219 // DBTransactionError due to transaction ROUND_* stages being mismatched.
220 $factory->beginMasterChanges( __METHOD__
);
221 $dbw->query( "SELECT 1 as t", __METHOD__
);
222 $dbw->onTransactionResolution( function () use ( $factory, &$called ) {
224 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
225 // There is no s1wiki DB to select so it is not in getConnection(),
226 // but this fools getMainLB() at least.
227 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
229 $factory->commitMasterChanges( __METHOD__
);
230 $this->assertEquals( 1, $called );
231 $this->assertEquals( 2, $countLBsFunc( $factory ) );
232 $factory->shutdown();
233 $factory->closeAll();
235 $factory = $this->newLBFactoryMultiLBs();
236 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
237 // DBTransactionError should not be thrown
239 $dbw->onTransactionPreCommitOrIdle( function () use ( &$ran ) {
242 $factory->commitAll( __METHOD__
);
243 $this->assertEquals( 1, $ran );
245 $factory->shutdown();
246 $factory->closeAll();
249 private function newLBFactoryMultiLBs() {
250 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
252 return new LBFactoryMulti( [
266 'serverTemplate' => [
267 'dbname' => $wgDBname,
269 'password' => $wgDBpassword,
271 'dbDirectory' => $wgSQLiteDataDir,
272 'flags' => DBO_DEFAULT
275 'test-db1' => $wgDBserver,
276 'test-db2' => $wgDBserver,
277 'test-db3' => $wgDBserver,
278 'test-db4' => $wgDBserver
280 'loadMonitorClass' => LoadMonitorNull
::class
285 * @covers \Wikimedia\Rdbms\ChronologyProtector
287 public function testChronologyProtector() {
288 $now = microtime( true );
290 // (a) First HTTP request
291 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
292 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
295 $mockDB1 = $this->getMockBuilder( DatabaseMysqli
::class )
296 ->disableOriginalConstructor()
298 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
299 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
300 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
301 // Load balancer for master DB 1
302 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
303 ->disableOriginalConstructor()
305 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
306 $lb1->method( 'getServerCount' )->willReturn( 2 );
307 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
308 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
309 function () use ( $mockDB1 ) {
311 $p |
= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
312 $p |
= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
317 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
318 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
320 $mockDB2 = $this->getMockBuilder( DatabaseMysqli
::class )
321 ->disableOriginalConstructor()
323 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
324 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
325 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
326 // Load balancer for master DB 2
327 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
328 ->disableOriginalConstructor()
330 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
331 $lb2->method( 'getServerCount' )->willReturn( 2 );
332 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
333 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
334 function () use ( $mockDB2 ) {
336 $p |
= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
337 $p |
= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
342 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
343 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
345 $bag = new HashBagOStuff();
346 $cp = new ChronologyProtector(
350 'agent' => "Totally-Not-FireFox"
354 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
355 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
356 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
357 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
359 // Nothing to wait for on first HTTP request start
362 // Record positions in stash on first HTTP request end
363 $cp->shutdownLB( $lb1 );
364 $cp->shutdownLB( $lb2 );
366 $cp->shutdown( null, 'sync', $cpIndex );
368 $this->assertEquals( 1, $cpIndex, "CP write index set" );
370 // (b) Second HTTP request
372 // Load balancer for master DB 1
373 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
374 ->disableOriginalConstructor()
376 $lb1->method( 'getServerCount' )->willReturn( 2 );
377 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
378 $lb1->expects( $this->once() )
379 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
380 // Load balancer for master DB 2
381 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
382 ->disableOriginalConstructor()
384 $lb2->method( 'getServerCount' )->willReturn( 2 );
385 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
386 $lb2->expects( $this->once() )
387 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
389 $cp = new ChronologyProtector(
393 'agent' => "Totally-Not-FireFox"
398 // Wait for last positions to be reached on second HTTP request start
401 // Shutdown (nothing to record)
402 $cp->shutdownLB( $lb1 );
403 $cp->shutdownLB( $lb2 );
405 $cp->shutdown( null, 'sync', $cpIndex );
407 $this->assertEquals( null, $cpIndex, "CP write index retained" );
409 $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
412 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
413 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
414 global $wgSQLiteDataDir;
416 return new LBFactoryMulti( $baseOverride +
[
417 'sectionsByDB' => [],
423 'serverTemplate' => $serverOverride +
[
424 'dbname' => $wgDBname,
425 'tablePrefix' => $wgDBprefix,
427 'password' => $wgDBpassword,
429 'dbDirectory' => $wgSQLiteDataDir,
430 'flags' => DBO_DEFAULT
433 'test-db1' => $wgDBserver,
435 'loadMonitorClass' => LoadMonitorNull
::class,
436 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
437 'agent' => 'MW-UNIT-TESTS'
441 public function testNiceDomains() {
444 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
445 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
449 $factory = $this->newLBFactoryMulti(
453 $lb = $factory->getMainLB();
455 $db = $lb->getConnectionRef( DB_MASTER
);
462 /** @var IMaintainableDatabase $db */
463 $db = $lb->getConnection( DB_MASTER
, [], '' );
468 'Null domain ID handle used'
473 'Null domain ID handle used'
478 'Main domain ID handle used; prefix is empty though'
481 $this->quoteTable( $db, 'page' ),
482 $db->tableName( 'page' ),
483 "Correct full table name"
486 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
487 $db->tableName( "$wgDBname.page" ),
488 "Correct full table name"
491 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
492 $db->tableName( 'nice_db.page' ),
493 "Correct full table name"
496 $lb->reuseConnection( $db ); // don't care
498 $db = $lb->getConnection( DB_MASTER
); // local domain connection
499 $factory->setLocalDomainPrefix( 'my_' );
501 $this->assertEquals( $wgDBname, $db->getDBname() );
507 $this->quoteTable( $db, 'my_page' ),
508 $db->tableName( 'page' ),
509 "Correct full table name"
512 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
513 $db->tableName( 'other_nice_db.page' ),
514 "Correct full table name"
517 $factory->closeAll();
521 public function testTrickyDomain() {
524 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
525 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
529 $dbname = 'unittest-domain'; // explodes if DB is selected
530 $factory = $this->newLBFactoryMulti(
531 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
533 'dbName' => 'do_not_select_me' // explodes if DB is selected
536 $lb = $factory->getMainLB();
537 /** @var IMaintainableDatabase $db */
538 $db = $lb->getConnection( DB_MASTER
, [], '' );
540 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
543 $this->quoteTable( $db, 'page' ),
544 $db->tableName( 'page' ),
545 "Correct full table name"
549 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
550 $db->tableName( "$dbname.page" ),
551 "Correct full table name"
555 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
556 $db->tableName( 'nice_db.page' ),
557 "Correct full table name"
560 $lb->reuseConnection( $db ); // don't care
562 $factory->setLocalDomainPrefix( 'my_' );
563 $db = $lb->getConnection( DB_MASTER
, [], "$wgDBname-my_" );
566 $this->quoteTable( $db, 'my_page' ),
567 $db->tableName( 'page' ),
568 "Correct full table name"
571 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
572 $db->tableName( 'other_nice_db.page' ),
573 "Correct full table name"
576 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
577 $db->tableName( 'garbage-db.page' ),
578 "Correct full table name"
581 $lb->reuseConnection( $db ); // don't care
583 $factory->closeAll();
587 public function testInvalidSelectDB() {
588 // FIXME: fails under sqlite
589 $this->markTestSkippedIfDbType( 'sqlite' );
590 $dbname = 'unittest-domain'; // explodes if DB is selected
591 $factory = $this->newLBFactoryMulti(
592 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
594 'dbName' => 'do_not_select_me' // explodes if DB is selected
597 $lb = $factory->getMainLB();
598 /** @var IDatabase $db */
599 $db = $lb->getConnection( DB_MASTER
, [], '' );
601 if ( $db->getType() === 'sqlite' ) {
602 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
603 } elseif ( $db->databasesAreIndependent() ) {
606 $db->selectDB( 'garbage-db' );
607 } catch ( \Wikimedia\Rdbms\DBConnectionError
$e ) {
610 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError
::class, $e );
611 $this->assertFalse( $db->isOpen() );
613 \Wikimedia\
suppressWarnings();
615 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
616 $this->fail( "No error thrown." );
617 } catch ( \Wikimedia\Rdbms\DBExpectedError
$e ) {
619 "Could not select database 'garbage-db'.",
623 \Wikimedia\restoreWarnings
();
627 public function testRedefineLocalDomain() {
630 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
631 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
635 $factory = $this->newLBFactoryMulti(
639 $lb = $factory->getMainLB();
641 $conn1 = $lb->getConnectionRef( DB_MASTER
);
644 $conn1->getDomainID()
648 $factory->redefineLocalDomain( 'somedb-prefix' );
649 $this->assertEquals( 'somedb-prefix', $factory->getLocalDomainID() );
651 $domain = new DatabaseDomain( $wgDBname, null, 'pref' );
652 $factory->redefineLocalDomain( $domain );
655 $lb->forEachOpenConnection( function () use ( &$n ) {
658 $this->assertEquals( 0, $n, "Connections closed" );
660 $conn2 = $lb->getConnectionRef( DB_MASTER
);
663 $conn2->getDomainID()
667 $factory->closeAll();
671 private function quoteTable( Database
$db, $table ) {
672 if ( $db->getType() === 'sqlite' ) {
675 return $db->addIdentifierQuotes( $table );
680 * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
681 * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
683 public function testCPPosIndexCookieValues() {
685 $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
688 '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
689 LBFactory
::makeCookieValueFromCPIndex( 3, 542, $agentId )
692 $lbFactory = $this->newLBFactoryMulti();
693 $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
695 '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
696 LBFactory
::makeCookieValueFromCPIndex( 1, 542, $agentId )
701 LBFactory
::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
706 LBFactory
::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
711 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
717 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
722 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
9 - 10 )['index'],
727 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time +
9 - 10 )['index'],
728 'Almost stale; bad index'
732 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
11 - 10 )['index'],
738 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
743 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time +
11 - 10 )['clientId'],