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\LBFactorySimple
;
27 use Wikimedia\Rdbms\LBFactoryMulti
;
28 use Wikimedia\Rdbms\LoadBalancer
;
29 use Wikimedia\Rdbms\ChronologyProtector
;
30 use Wikimedia\Rdbms\DatabaseMysqli
;
31 use Wikimedia\Rdbms\MySQLMasterPos
;
32 use Wikimedia\Rdbms\DatabaseDomain
;
36 * @covers \Wikimedia\Rdbms\LBFactorySimple
37 * @covers \Wikimedia\Rdbms\LBFactoryMulti
39 class LBFactoryTest
extends MediaWikiTestCase
{
42 * @covers MWLBFactory::getLBFactoryClass
43 * @dataProvider getLBFactoryClassProvider
45 public function testGetLBFactoryClass( $expected, $deprecated ) {
46 $mockDB = $this->getMockBuilder( DatabaseMysqli
::class )
47 ->disableOriginalConstructor()
51 'class' => $deprecated,
52 'connection' => $mockDB,
53 # Various other parameters required:
56 'serverTemplate' => [],
59 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
60 $result = MWLBFactory
::getLBFactoryClass( $config );
62 $this->assertEquals( $expected, $result );
65 public function getLBFactoryClassProvider() {
67 # Format: new class, old class
68 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactory_Simple' ],
69 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactory_Single' ],
70 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactory_Multi' ],
71 [ Wikimedia\Rdbms\LBFactorySimple
::class, 'LBFactorySimple' ],
72 [ Wikimedia\Rdbms\LBFactorySingle
::class, 'LBFactorySingle' ],
73 [ Wikimedia\Rdbms\LBFactoryMulti
::class, 'LBFactoryMulti' ],
77 public function testLBFactorySimpleServer() {
78 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
82 'host' => $wgDBserver,
83 'dbname' => $wgDBname,
85 'password' => $wgDBpassword,
87 'dbDirectory' => $wgSQLiteDataDir,
89 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
93 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
94 $lb = $factory->getMainLB();
96 $dbw = $lb->getConnection( DB_MASTER
);
97 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
99 $dbr = $lb->getConnection( DB_REPLICA
);
100 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
102 $factory->shutdown();
106 public function testLBFactorySimpleServers() {
107 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
111 'host' => $wgDBserver,
112 'dbname' => $wgDBname,
114 'password' => $wgDBpassword,
116 'dbDirectory' => $wgSQLiteDataDir,
118 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
121 'host' => $wgDBserver,
122 'dbname' => $wgDBname,
124 'password' => $wgDBpassword,
126 'dbDirectory' => $wgSQLiteDataDir,
128 'flags' => DBO_TRX
// REPEATABLE-READ for consistency
132 $factory = new LBFactorySimple( [
133 'servers' => $servers,
134 'loadMonitorClass' => LoadMonitorNull
::class
136 $lb = $factory->getMainLB();
138 $dbw = $lb->getConnection( DB_MASTER
);
139 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
141 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
142 $dbw->getLBInfo( 'clusterMasterHost' ),
143 'cluster master set' );
145 $dbr = $lb->getConnection( DB_REPLICA
);
146 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
148 ( $wgDBserver != '' ) ?
$wgDBserver : 'localhost',
149 $dbr->getLBInfo( 'clusterMasterHost' ),
150 'cluster master set' );
152 $factory->shutdown();
156 public function testLBFactoryMultiConns() {
157 $factory = $this->newLBFactoryMultiLBs();
159 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
160 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
162 $dbr = $factory->getMainLB()->getConnection( DB_REPLICA
);
163 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
165 // Destructor should trigger without round stage errors
169 public function testLBFactoryMultiRoundCallbacks() {
171 $countLBsFunc = function ( LBFactoryMulti
$factory ) {
173 $factory->forEachLB( function () use ( &$count ) {
180 $factory = $this->newLBFactoryMultiLBs();
181 $this->assertEquals( 0, $countLBsFunc( $factory ) );
182 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
183 $this->assertEquals( 1, $countLBsFunc( $factory ) );
184 // Test that LoadBalancer instances made during pre-commit callbacks in do not
185 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.
186 $factory->beginMasterChanges( __METHOD__
);
187 $dbw->onTransactionPreCommitOrIdle( function () use ( $factory, &$called ) {
189 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
190 // There is no s1wiki DB to select so it is not in getConnection(),
191 // but this fools getMainLB() at least.
192 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
194 $factory->commitMasterChanges( __METHOD__
);
195 $this->assertEquals( 1, $called );
196 $this->assertEquals( 2, $countLBsFunc( $factory ) );
197 $factory->shutdown();
198 $factory->closeAll();
201 $factory = $this->newLBFactoryMultiLBs();
202 $this->assertEquals( 0, $countLBsFunc( $factory ) );
203 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
204 $this->assertEquals( 1, $countLBsFunc( $factory ) );
205 // Test that LoadBalancer instances made during pre-commit callbacks in do not
206 // throw DBTransactionError due to transaction ROUND_* stages being mismatched.hrow
207 // DBTransactionError due to transaction ROUND_* stages being mismatched.
208 $factory->beginMasterChanges( __METHOD__
);
209 $dbw->query( "SELECT 1 as t", __METHOD__
);
210 $dbw->onTransactionResolution( function () use ( $factory, &$called ) {
212 // Trigger s1 LoadBalancer instantiation during "finalize" stage.
213 // There is no s1wiki DB to select so it is not in getConnection(),
214 // but this fools getMainLB() at least.
215 $factory->getMainLB( 's1wiki' )->getConnection( DB_MASTER
);
217 $factory->commitMasterChanges( __METHOD__
);
218 $this->assertEquals( 1, $called );
219 $this->assertEquals( 2, $countLBsFunc( $factory ) );
220 $factory->shutdown();
221 $factory->closeAll();
223 $factory = $this->newLBFactoryMultiLBs();
224 $dbw = $factory->getMainLB()->getConnection( DB_MASTER
);
225 // DBTransactionError should not be thrown
227 $dbw->onTransactionPreCommitOrIdle( function () use ( &$ran ) {
230 $factory->commitAll( __METHOD__
);
231 $this->assertEquals( 1, $ran );
233 $factory->shutdown();
234 $factory->closeAll();
237 private function newLBFactoryMultiLBs() {
238 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
240 return new LBFactoryMulti( [
254 'serverTemplate' => [
255 'dbname' => $wgDBname,
257 'password' => $wgDBpassword,
259 'dbDirectory' => $wgSQLiteDataDir,
260 'flags' => DBO_DEFAULT
263 'test-db1' => $wgDBserver,
264 'test-db2' => $wgDBserver,
265 'test-db3' => $wgDBserver,
266 'test-db4' => $wgDBserver
268 'loadMonitorClass' => LoadMonitorNull
::class
273 * @covers \Wikimedia\Rdbms\ChronologyProtector
275 public function testChronologyProtector() {
276 $now = microtime( true );
278 // (a) First HTTP request
279 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
280 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
283 $mockDB1 = $this->getMockBuilder( DatabaseMysqli
::class )
284 ->disableOriginalConstructor()
286 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
287 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
288 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
289 // Load balancer for master DB 1
290 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
291 ->disableOriginalConstructor()
293 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
294 $lb1->method( 'getServerCount' )->willReturn( 2 );
295 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
296 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
297 function () use ( $mockDB1 ) {
299 $p |
= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
300 $p |
= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
305 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
306 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
308 $mockDB2 = $this->getMockBuilder( DatabaseMysqli
::class )
309 ->disableOriginalConstructor()
311 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
312 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
313 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
314 // Load balancer for master DB 2
315 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
316 ->disableOriginalConstructor()
318 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
319 $lb2->method( 'getServerCount' )->willReturn( 2 );
320 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
321 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
322 function () use ( $mockDB2 ) {
324 $p |
= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
325 $p |
= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
330 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
331 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
333 $bag = new HashBagOStuff();
334 $cp = new ChronologyProtector(
338 'agent' => "Totally-Not-FireFox"
342 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
343 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
344 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
345 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
347 // Nothing to wait for on first HTTP request start
350 // Record positions in stash on first HTTP request end
351 $cp->shutdownLB( $lb1 );
352 $cp->shutdownLB( $lb2 );
354 $cp->shutdown( null, 'sync', $cpIndex );
356 $this->assertEquals( 1, $cpIndex, "CP write index set" );
358 // (b) Second HTTP request
360 // Load balancer for master DB 1
361 $lb1 = $this->getMockBuilder( LoadBalancer
::class )
362 ->disableOriginalConstructor()
364 $lb1->method( 'getServerCount' )->willReturn( 2 );
365 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
366 $lb1->expects( $this->once() )
367 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
368 // Load balancer for master DB 2
369 $lb2 = $this->getMockBuilder( LoadBalancer
::class )
370 ->disableOriginalConstructor()
372 $lb2->method( 'getServerCount' )->willReturn( 2 );
373 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
374 $lb2->expects( $this->once() )
375 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
377 $cp = new ChronologyProtector(
381 'agent' => "Totally-Not-FireFox"
386 // Wait for last positions to be reached on second HTTP request start
389 // Shutdown (nothing to record)
390 $cp->shutdownLB( $lb1 );
391 $cp->shutdownLB( $lb2 );
393 $cp->shutdown( null, 'sync', $cpIndex );
395 $this->assertEquals( null, $cpIndex, "CP write index retained" );
397 $this->assertEquals( '45e93a9c215c031d38b7c42d8e4700ca', $cp->getClientId() );
400 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
401 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype;
402 global $wgSQLiteDataDir;
404 return new LBFactoryMulti( $baseOverride +
[
405 'sectionsByDB' => [],
411 'serverTemplate' => $serverOverride +
[
412 'dbname' => $wgDBname,
413 'tablePrefix' => $wgDBprefix,
415 'password' => $wgDBpassword,
417 'dbDirectory' => $wgSQLiteDataDir,
418 'flags' => DBO_DEFAULT
421 'test-db1' => $wgDBserver,
423 'loadMonitorClass' => LoadMonitorNull
::class,
424 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ),
425 'agent' => 'MW-UNIT-TESTS'
429 public function testNiceDomains() {
432 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
433 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
437 $factory = $this->newLBFactoryMulti(
441 $lb = $factory->getMainLB();
443 $db = $lb->getConnectionRef( DB_MASTER
);
450 /** @var Database $db */
451 $db = $lb->getConnection( DB_MASTER
, [], '' );
456 'Null domain ID handle used'
461 'Null domain ID handle used'
466 'Main domain ID handle used; prefix is empty though'
469 $this->quoteTable( $db, 'page' ),
470 $db->tableName( 'page' ),
471 "Correct full table name"
474 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
475 $db->tableName( "$wgDBname.page" ),
476 "Correct full table name"
479 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
480 $db->tableName( 'nice_db.page' ),
481 "Correct full table name"
484 $lb->reuseConnection( $db ); // don't care
486 $db = $lb->getConnection( DB_MASTER
); // local domain connection
487 $factory->setDomainPrefix( 'my_' );
489 $this->assertEquals( $wgDBname, $db->getDBname() );
495 $this->quoteTable( $db, 'my_page' ),
496 $db->tableName( 'page' ),
497 "Correct full table name"
500 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
501 $db->tableName( 'other_nice_db.page' ),
502 "Correct full table name"
505 $factory->closeAll();
509 public function testTrickyDomain() {
512 if ( wfGetDB( DB_MASTER
)->databasesAreIndependent() ) {
513 self
::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
517 $dbname = 'unittest-domain'; // explodes if DB is selected
518 $factory = $this->newLBFactoryMulti(
519 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
521 'dbName' => 'do_not_select_me' // explodes if DB is selected
524 $lb = $factory->getMainLB();
525 /** @var Database $db */
526 $db = $lb->getConnection( DB_MASTER
, [], '' );
528 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
531 $this->quoteTable( $db, 'page' ),
532 $db->tableName( 'page' ),
533 "Correct full table name"
537 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
538 $db->tableName( "$dbname.page" ),
539 "Correct full table name"
543 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
544 $db->tableName( 'nice_db.page' ),
545 "Correct full table name"
548 $lb->reuseConnection( $db ); // don't care
550 $factory->setDomainPrefix( 'my_' );
551 $db = $lb->getConnection( DB_MASTER
, [], "$wgDBname-my_" );
554 $this->quoteTable( $db, 'my_page' ),
555 $db->tableName( 'page' ),
556 "Correct full table name"
559 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
560 $db->tableName( 'other_nice_db.page' ),
561 "Correct full table name"
564 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
565 $db->tableName( 'garbage-db.page' ),
566 "Correct full table name"
569 $lb->reuseConnection( $db ); // don't care
571 $factory->closeAll();
575 public function testInvalidSelectDB() {
576 $dbname = 'unittest-domain'; // explodes if DB is selected
577 $factory = $this->newLBFactoryMulti(
578 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
580 'dbName' => 'do_not_select_me' // explodes if DB is selected
583 $lb = $factory->getMainLB();
584 /** @var Database $db */
585 $db = $lb->getConnection( DB_MASTER
, [], '' );
587 if ( $db->getType() === 'sqlite' ) {
588 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
589 } elseif ( $db->databasesAreIndependent() ) {
592 $db->selectDB( 'garbage-db' );
593 } catch ( \Wikimedia\Rdbms\DBConnectionError
$e ) {
596 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError
::class, $e );
597 $this->assertFalse( $db->isOpen() );
599 \Wikimedia\
suppressWarnings();
600 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
601 \Wikimedia\restoreWarnings
();
605 private function quoteTable( Database
$db, $table ) {
606 if ( $db->getType() === 'sqlite' ) {
609 return $db->addIdentifierQuotes( $table );
614 * @covers \Wikimedia\Rdbms\LBFactory::makeCookieValueFromCPIndex()
615 * @covers \Wikimedia\Rdbms\LBFactory::getCPInfoFromCookieValue()
617 public function testCPPosIndexCookieValues() {
619 $agentId = md5( 'Ramsey\'s Loyal Presa Canario' );
621 $lbFactory = $this->newLBFactoryMulti();
623 '3@542#c47dcfb0566e7d7bc110a6128a45c93a',
624 LBFactory
::makeCookieValueFromCPIndex( 3, 542, $agentId )
627 $lbFactory = $this->newLBFactoryMulti();
628 $lbFactory->setRequestInfo( [ 'IPAddress' => '10.64.24.52', 'UserAgent' => 'meow' ] );
630 '1@542#c47dcfb0566e7d7bc110a6128a45c93a',
631 LBFactory
::makeCookieValueFromCPIndex( 1, 542, $agentId )
636 LBFactory
::getCPInfoFromCookieValue( "5#$agentId", $time - 10 )['index'],
641 LBFactory
::getCPInfoFromCookieValue( "5@$time", $time - 10 )['index'],
646 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time - 10 )['index'],
652 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time - 10 )['index'],
657 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
9 - 10 )['index'],
662 LBFactory
::getCPInfoFromCookieValue( "0@$time#$agentId", $time +
9 - 10 )['index'],
663 'Almost stale; bad index'
667 LBFactory
::getCPInfoFromCookieValue( "2@$time#$agentId", $time +
11 - 10 )['index'],
673 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time - 10 )['clientId'],
678 LBFactory
::getCPInfoFromCookieValue( "5@$time#$agentId", $time +
11 - 10 )['clientId'],