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\LBFactorySimple
38 * @covers \Wikimedia\Rdbms\LBFactoryMulti
40 class LBFactoryTest
extends MediaWikiTestCase
{
43 * @covers MWLBFactory::getLBFactoryClass
44 * @dataProvider getLBFactoryClassProvider
46 public function testGetLBFactoryClass( $expected, $deprecated ) {
47 $mockDB = $this->getMockBuilder( DatabaseMysqli
::class )
48 ->disableOriginalConstructor()
52 'class' => $deprecated,
53 'connection' => $mockDB,
54 # Various other parameters required:
57 'serverTemplate' => [],
60 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
61 $result = MWLBFactory
::getLBFactoryClass( $config );
63 $this->assertEquals( $expected, $result );
66 public function getLBFactoryClassProvider() {
68 # Format: new class, old class
69 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactory_Simple' ],
70 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactory_Single' ],
71 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactory_Multi' ],
72 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactorySimple' ],
73 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactorySingle' ],
74 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactoryMulti' ],
79 * @covers LBFactory::getLocalDomainID()
80 * @covers LBFactory::resolveDomainID()
82 public function testLBFactorySimpleServer() {
83 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
87 'host' => $wgDBserver,
88 'dbname' => $wgDBname,
90 'password' => $wgDBpassword,
92 'dbDirectory' => $wgSQLiteDataDir,
94 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
98 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
99 $lb = $factory->getMainLB();
101 $dbw = $lb->getConnection( DB_MASTER
);
102 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
104 $dbr = $lb->getConnection( DB_REPLICA
);
105 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
107 $this->assertSame( 'my_test_wiki', $factory->resolveDomainID( 'my_test_wiki' ) );
108 $this->assertSame( $factory->getLocalDomainID(), $factory->resolveDomainID( false ) );
110 $factory->shutdown();
114 public function testLBFactorySimpleServers() {
115 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
119 'host' => $wgDBserver,
120 'dbname' => $wgDBname,
122 'password' => $wgDBpassword,
124 'dbDirectory' => $wgSQLiteDataDir,
126 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
129 'host' => $wgDBserver,
130 'dbname' => $wgDBname,
132 'password' => $wgDBpassword,
134 'dbDirectory' => $wgSQLiteDataDir,
136 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
140 $factory = new LBFactorySimple( [
141 'servers' => $servers,
142 'loadMonitorClass' => LoadMonitorNull
::class
144 $lb = $factory->getMainLB();
146 $dbw = $lb->getConnection( DB_MASTER
);
147 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
149 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
150 $dbw->getLBInfo( 'clusterMasterHost' ),
151 'cluster master set' );
153 $dbr = $lb->getConnection( DB_REPLICA
);
154 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
156 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
157 $dbr->getLBInfo( 'clusterMasterHost' ),
158 'cluster master set' );
160 $factory->shutdown();
164 public function testLBFactoryMultiConns() {
165 $factory = $this->newLBFactoryMultiLBs();
167 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
168 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
170 $dbr = $factory->getMainLB()->getConnection( DB_REPLICA
);
171 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
173 // Destructor should trigger without round stage errors
177 public function testLBFactoryMultiRoundCallbacks() {
179 $countLBsFunc = function ( LBFactoryMulti
$factory ) {
181 $factory->forEachLB( function () use ( &$count ) {
188 $factory = $this->newLBFactoryMultiLBs();
189 $this->assertEquals( 0, $countLBsFunc( $factory ) );
190 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
191 $this->assertEquals( 1, $countLBsFunc( $factory ) );
192 // Test that LoadBalancer instances made during pre-commit callbacks in do not
193 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.
194 $factory->beginMasterChanges( __METHOD__
);
195 $dbw->onTransactionPreCommitOrIdle( function () use ( $factory, &$called ) {
197 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
198 // There is no s1wiki DB to select so it is not in getConnection(),
199 // but this fools getMainLB() at least.
200 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
202 $factory->commitMasterChanges( __METHOD__
);
203 $this->assertEquals( 1, $called );
204 $this->assertEquals( 2, $countLBsFunc( $factory ) );
205 $factory->shutdown();
206 $factory->closeAll();
209 $factory = $this->newLBFactoryMultiLBs();
210 $this->assertEquals( 0, $countLBsFunc( $factory ) );
211 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
212 $this->assertEquals( 1, $countLBsFunc( $factory ) );
213 // Test that LoadBalancer instances made during pre-commit callbacks in do not
214 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.hrow
215 // DBTransactionError due to transaction ROUND_* stages being mismatched.
216 $factory->beginMasterChanges( __METHOD__
);
217 $dbw->query( "SELECT 1 as t", __METHOD__
);
218 $dbw->onTransactionResolution( function () use ( $factory, &$called ) {
220 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
221 // There is no s1wiki DB to select so it is not in getConnection(),
222 // but this fools getMainLB() at least.
223 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
225 $factory->commitMasterChanges( __METHOD__
);
226 $this->assertEquals( 1, $called );
227 $this->assertEquals( 2, $countLBsFunc( $factory ) );
228 $factory->shutdown();
229 $factory->closeAll();
231 $factory = $this->newLBFactoryMultiLBs();
232 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
233 // DBTransactionError should not be thrown
235 $dbw->onTransactionPreCommitOrIdle( function () use ( &$ran ) {
238 $factory->commitAll( __METHOD__
);
239 $this->assertEquals( 1, $ran );
241 $factory->shutdown();
242 $factory->closeAll();
245 private function newLBFactoryMultiLBs() {
246 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
248 return new LBFactoryMulti( [
262 'serverTemplate' => [
263 'dbname' => $wgDBname,
265 'password' => $wgDBpassword,
267 'dbDirectory' => $wgSQLiteDataDir,
268 'flags' => DBO_DEFAULT
271 'test-db1' => $wgDBserver,
272 'test-db2' => $wgDBserver,
273 'test-db3' => $wgDBserver,
274 'test-db4' => $wgDBserver
276 'loadMonitorClass' => LoadMonitorNull
::class
281 * @covers \Wikimedia\Rdbms\ChronologyProtector
283 public function testChronologyProtector() {
284 $now = microtime( true );
286 // (a) First HTTP request
287 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
288 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
291 $mockDB1 = $this->getMockBuilder( DatabaseMysqli
::class )
292 ->disableOriginalConstructor()
294 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
295 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
296 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
297 // Load balancer for master DB 1
298 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
299 ->disableOriginalConstructor()
301 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
302 $lb1->method( 'getServerCount' )->willReturn( 2 );
303 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
304 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
305 function () use ( $mockDB1 ) {
307 $p |
= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
308 $p |
= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
313 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
314 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
316 $mockDB2 = $this->getMockBuilder( DatabaseMysqli
::class )
317 ->disableOriginalConstructor()
319 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
320 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
321 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
322 // Load balancer for master DB 2
323 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
324 ->disableOriginalConstructor()
326 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
327 $lb2->method( 'getServerCount' )->willReturn( 2 );
328 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
329 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
330 function () use ( $mockDB2 ) {
332 $p |
= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
333 $p |
= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
338 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
339 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
341 $bag = new HashBagOStuff();
342 $cp = new ChronologyProtector(
346 'agent' => "Totally-Not-FireFox"
350 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
351 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
352 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
353 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
355 // Nothing to wait for on first HTTP request start
358 // Record positions in stash on first HTTP request end
359 $cp->shutdownLB( $lb1 );
360 $cp->shutdownLB( $lb2 );
362 $cp->shutdown( null, 'sync', $cpIndex );
364 $this->assertEquals( 1, $cpIndex, "CP write index set" );
366 // (b) Second HTTP request
368 // Load balancer for master DB 1
369 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
370 ->disableOriginalConstructor()
372 $lb1->method( 'getServerCount' )->willReturn( 2 );
373 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
374 $lb1->expects( $this->once() )
375 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
376 // Load balancer for master DB 2
377 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
378 ->disableOriginalConstructor()
380 $lb2->method( 'getServerCount' )->willReturn( 2 );
381 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
382 $lb2->expects( $this->once() )
383 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
385 $cp = new ChronologyProtector(
389 'agent' => "Totally-Not-FireFox"
394 // Wait for last positions to be reached on second HTTP request start
397 // Shutdown (nothing to record)
398 $cp->shutdownLB( $lb1 );
399 $cp->shutdownLB( $lb2 );
401 $cp->shutdown( null, 'sync', $cpIndex );
403 $this->assertEquals( null, $cpIndex, "CP write index retained" );
405 $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
408 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
409 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
410 global $wgSQLiteDataDir;
412 return new LBFactoryMulti( $baseOverride +
[
413 'sectionsByDB' => [],
419 'serverTemplate' => $serverOverride +
[
420 'dbname' => $wgDBname,
421 'tablePrefix' => $wgDBprefix,
423 'password' => $wgDBpassword,
425 'dbDirectory' => $wgSQLiteDataDir,
426 'flags' => DBO_DEFAULT
429 'test-db1' => $wgDBserver,
431 'loadMonitorClass' => LoadMonitorNull
::class,
432 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
433 'agent' => 'MW-UNIT-TESTS'
437 public function testNiceDomains() {
440 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
441 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
445 $factory = $this->newLBFactoryMulti(
449 $lb = $factory->getMainLB();
451 $db = $lb->getConnectionRef( DB_MASTER
);
458 /** @var Database $db */
459 $db = $lb->getConnection( DB_MASTER
, [], '' );
464 'Null domain ID handle used'
469 'Null domain ID handle used'
474 'Main domain ID handle used; prefix is empty though'
477 $this->quoteTable( $db, 'page' ),
478 $db->tableName( 'page' ),
479 "Correct full table name"
482 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
483 $db->tableName( "$wgDBname.page" ),
484 "Correct full table name"
487 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
488 $db->tableName( 'nice_db.page' ),
489 "Correct full table name"
492 $lb->reuseConnection( $db ); // don't care
494 $db = $lb->getConnection( DB_MASTER
); // local domain connection
495 $factory->setDomainPrefix( 'my_' );
497 $this->assertEquals( $wgDBname, $db->getDBname() );
503 $this->quoteTable( $db, 'my_page' ),
504 $db->tableName( 'page' ),
505 "Correct full table name"
508 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
509 $db->tableName( 'other_nice_db.page' ),
510 "Correct full table name"
513 $factory->closeAll();
517 public function testTrickyDomain() {
520 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
521 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
525 $dbname = 'unittest-domain'; // explodes if DB is selected
526 $factory = $this->newLBFactoryMulti(
527 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
529 'dbName' => 'do_not_select_me' // explodes if DB is selected
532 $lb = $factory->getMainLB();
533 /** @var Database $db */
534 $db = $lb->getConnection( DB_MASTER
, [], '' );
536 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
539 $this->quoteTable( $db, 'page' ),
540 $db->tableName( 'page' ),
541 "Correct full table name"
545 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
546 $db->tableName( "$dbname.page" ),
547 "Correct full table name"
551 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
552 $db->tableName( 'nice_db.page' ),
553 "Correct full table name"
556 $lb->reuseConnection( $db ); // don't care
558 $factory->setDomainPrefix( 'my_' );
559 $db = $lb->getConnection( DB_MASTER
, [], "$wgDBname-my_" );
562 $this->quoteTable( $db, 'my_page' ),
563 $db->tableName( 'page' ),
564 "Correct full table name"
567 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
568 $db->tableName( 'other_nice_db.page' ),
569 "Correct full table name"
572 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
573 $db->tableName( 'garbage-db.page' ),
574 "Correct full table name"
577 $lb->reuseConnection( $db ); // don't care
579 $factory->closeAll();
583 public function testInvalidSelectDB() {
584 // FIXME: fails under sqlite
585 $this->markTestSkippedIfDbType( 'sqlite' );
586 $dbname = 'unittest-domain'; // explodes if DB is selected
587 $factory = $this->newLBFactoryMulti(
588 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
590 'dbName' => 'do_not_select_me' // explodes if DB is selected
593 $lb = $factory->getMainLB();
594 /** @var Database $db */
595 $db = $lb->getConnection( DB_MASTER
, [], '' );
597 if ( $db->getType() === 'sqlite' ) {
598 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
599 } elseif ( $db->databasesAreIndependent() ) {
602 $db->selectDB( 'garbage-db' );
603 } catch ( \Wikimedia\Rdbms\DBConnectionError
$e ) {
606 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError
::class, $e );
607 $this->assertFalse( $db->isOpen() );
609 \Wikimedia\
suppressWarnings();
610 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
611 \Wikimedia\restoreWarnings
();
615 private function quoteTable( Database
$db, $table ) {
616 if ( $db->getType() === 'sqlite' ) {
619 return $db->addIdentifierQuotes( $table );
624 * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
625 * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
627 public function testCPPosIndexCookieValues() {
629 $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
632 '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
633 LBFactory
::makeCookieValueFromCPIndex( 3, 542, $agentId )
636 $lbFactory = $this->newLBFactoryMulti();
637 $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
639 '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
640 LBFactory
::makeCookieValueFromCPIndex( 1, 542, $agentId )
645 LBFactory
::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
650 LBFactory
::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
655 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
661 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
666 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
9 - 10 )['index'],
671 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time +
9 - 10 )['index'],
672 'Almost stale; bad index'
676 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
11 - 10 )['index'],
682 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
687 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time +
11 - 10 )['clientId'],