MediaWiki REL1_31
LBFactoryTest.php
Go to the documentation of this file.
1<?php
33
40
45 public function testGetLBFactoryClass( $expected, $deprecated ) {
46 $mockDB = $this->getMockBuilder( DatabaseMysqli::class )
47 ->disableOriginalConstructor()
48 ->getMock();
49
50 $config = [
51 'class' => $deprecated,
52 'connection' => $mockDB,
53 # Various other parameters required:
54 'sectionsByDB' => [],
55 'sectionLoads' => [],
56 'serverTemplate' => [],
57 ];
58
59 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
60 $result = MWLBFactory::getLBFactoryClass( $config );
61
62 $this->assertEquals( $expected, $result );
63 }
64
65 public function getLBFactoryClassProvider() {
66 return [
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' ],
74 ];
75 }
76
77 public function testLBFactorySimpleServer() {
79
80 $servers = [
81 [
82 'host' => $wgDBserver,
83 'dbname' => $wgDBname,
84 'user' => $wgDBuser,
85 'password' => $wgDBpassword,
86 'type' => $wgDBtype,
87 'dbDirectory' => $wgSQLiteDataDir,
88 'load' => 0,
89 'flags' => DBO_TRX // REPEATABLE-READ for consistency
90 ],
91 ];
92
93 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
94 $lb = $factory->getMainLB();
95
96 $dbw = $lb->getConnection( DB_MASTER );
97 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
98
99 $dbr = $lb->getConnection( DB_REPLICA );
100 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
101
102 $factory->shutdown();
103 $lb->closeAll();
104 }
105
106 public function testLBFactorySimpleServers() {
108
109 $servers = [
110 [ // master
111 'host' => $wgDBserver,
112 'dbname' => $wgDBname,
113 'user' => $wgDBuser,
114 'password' => $wgDBpassword,
115 'type' => $wgDBtype,
116 'dbDirectory' => $wgSQLiteDataDir,
117 'load' => 0,
118 'flags' => DBO_TRX // REPEATABLE-READ for consistency
119 ],
120 [ // emulated slave
121 'host' => $wgDBserver,
122 'dbname' => $wgDBname,
123 'user' => $wgDBuser,
124 'password' => $wgDBpassword,
125 'type' => $wgDBtype,
126 'dbDirectory' => $wgSQLiteDataDir,
127 'load' => 100,
128 'flags' => DBO_TRX // REPEATABLE-READ for consistency
129 ]
130 ];
131
132 $factory = new LBFactorySimple( [
133 'servers' => $servers,
134 'loadMonitorClass' => LoadMonitorNull::class
135 ] );
136 $lb = $factory->getMainLB();
137
138 $dbw = $lb->getConnection( DB_MASTER );
139 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
140 $this->assertEquals(
141 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
142 $dbw->getLBInfo( 'clusterMasterHost' ),
143 'cluster master set' );
144
145 $dbr = $lb->getConnection( DB_REPLICA );
146 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
147 $this->assertEquals(
148 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
149 $dbr->getLBInfo( 'clusterMasterHost' ),
150 'cluster master set' );
151
152 $factory->shutdown();
153 $lb->closeAll();
154 }
155
156 public function testLBFactoryMulti() {
158
159 $factory = new LBFactoryMulti( [
160 'sectionsByDB' => [],
161 'sectionLoads' => [
162 'DEFAULT' => [
163 'test-db1' => 0,
164 'test-db2' => 100,
165 ],
166 ],
167 'serverTemplate' => [
168 'dbname' => $wgDBname,
169 'user' => $wgDBuser,
170 'password' => $wgDBpassword,
171 'type' => $wgDBtype,
172 'dbDirectory' => $wgSQLiteDataDir,
173 'flags' => DBO_DEFAULT
174 ],
175 'hostsByName' => [
176 'test-db1' => $wgDBserver,
177 'test-db2' => $wgDBserver
178 ],
179 'loadMonitorClass' => LoadMonitorNull::class
180 ] );
181 $lb = $factory->getMainLB();
182
183 $dbw = $lb->getConnection( DB_MASTER );
184 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
185
186 $dbr = $lb->getConnection( DB_REPLICA );
187 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
188
189 $factory->shutdown();
190 $lb->closeAll();
191 }
192
196 public function testChronologyProtector() {
197 $now = microtime( true );
198
199 // (a) First HTTP request
200 $m1Pos = new MySQLMasterPos( 'db1034-bin.000976/843431247', $now );
201 $m2Pos = new MySQLMasterPos( 'db1064-bin.002400/794074907', $now );
202
203 // Master DB 1
204 $mockDB1 = $this->getMockBuilder( DatabaseMysqli::class )
205 ->disableOriginalConstructor()
206 ->getMock();
207 $mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true );
208 $mockDB1->method( 'lastDoneWrites' )->willReturn( $now );
209 $mockDB1->method( 'getMasterPos' )->willReturn( $m1Pos );
210 // Load balancer for master DB 1
211 $lb1 = $this->getMockBuilder( LoadBalancer::class )
212 ->disableOriginalConstructor()
213 ->getMock();
214 $lb1->method( 'getConnection' )->willReturn( $mockDB1 );
215 $lb1->method( 'getServerCount' )->willReturn( 2 );
216 $lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 );
217 $lb1->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
218 function () use ( $mockDB1 ) {
219 $p = 0;
220 $p |= call_user_func( [ $mockDB1, 'writesOrCallbacksPending' ] );
221 $p |= call_user_func( [ $mockDB1, 'lastDoneWrites' ] );
222
223 return (bool)$p;
224 }
225 ) );
226 $lb1->method( 'getMasterPos' )->willReturn( $m1Pos );
227 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
228 // Master DB 2
229 $mockDB2 = $this->getMockBuilder( DatabaseMysqli::class )
230 ->disableOriginalConstructor()
231 ->getMock();
232 $mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true );
233 $mockDB2->method( 'lastDoneWrites' )->willReturn( $now );
234 $mockDB2->method( 'getMasterPos' )->willReturn( $m2Pos );
235 // Load balancer for master DB 2
236 $lb2 = $this->getMockBuilder( LoadBalancer::class )
237 ->disableOriginalConstructor()
238 ->getMock();
239 $lb2->method( 'getConnection' )->willReturn( $mockDB2 );
240 $lb2->method( 'getServerCount' )->willReturn( 2 );
241 $lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 );
242 $lb2->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
243 function () use ( $mockDB2 ) {
244 $p = 0;
245 $p |= call_user_func( [ $mockDB2, 'writesOrCallbacksPending' ] );
246 $p |= call_user_func( [ $mockDB2, 'lastDoneWrites' ] );
247
248 return (bool)$p;
249 }
250 ) );
251 $lb2->method( 'getMasterPos' )->willReturn( $m2Pos );
252 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
253
254 $bag = new HashBagOStuff();
255 $cp = new ChronologyProtector(
256 $bag,
257 [
258 'ip' => '127.0.0.1',
259 'agent' => "Totally-Not-FireFox"
260 ]
261 );
262
263 $mockDB1->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
264 $mockDB1->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
265 $mockDB2->expects( $this->exactly( 1 ) )->method( 'writesOrCallbacksPending' );
266 $mockDB2->expects( $this->exactly( 1 ) )->method( 'lastDoneWrites' );
267
268 // Nothing to wait for on first HTTP request start
269 $cp->initLB( $lb1 );
270 $cp->initLB( $lb2 );
271 // Record positions in stash on first HTTP request end
272 $cp->shutdownLB( $lb1 );
273 $cp->shutdownLB( $lb2 );
274 $cpIndex = null;
275 $cp->shutdown( null, 'sync', $cpIndex );
276
277 $this->assertEquals( 1, $cpIndex, "CP write index set" );
278
279 // (b) Second HTTP request
280
281 // Load balancer for master DB 1
282 $lb1 = $this->getMockBuilder( LoadBalancer::class )
283 ->disableOriginalConstructor()
284 ->getMock();
285 $lb1->method( 'getServerCount' )->willReturn( 2 );
286 $lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' );
287 $lb1->expects( $this->once() )
288 ->method( 'waitFor' )->with( $this->equalTo( $m1Pos ) );
289 // Load balancer for master DB 2
290 $lb2 = $this->getMockBuilder( LoadBalancer::class )
291 ->disableOriginalConstructor()
292 ->getMock();
293 $lb2->method( 'getServerCount' )->willReturn( 2 );
294 $lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' );
295 $lb2->expects( $this->once() )
296 ->method( 'waitFor' )->with( $this->equalTo( $m2Pos ) );
297
298 $cp = new ChronologyProtector(
299 $bag,
300 [
301 'ip' => '127.0.0.1',
302 'agent' => "Totally-Not-FireFox"
303 ],
304 $cpIndex
305 );
306
307 // Wait for last positions to be reached on second HTTP request start
308 $cp->initLB( $lb1 );
309 $cp->initLB( $lb2 );
310 // Shutdown (nothing to record)
311 $cp->shutdownLB( $lb1 );
312 $cp->shutdownLB( $lb2 );
313 $cpIndex = null;
314 $cp->shutdown( null, 'sync', $cpIndex );
315
316 $this->assertEquals( null, $cpIndex, "CP write index retained" );
317 }
318
319 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
321 global $wgSQLiteDataDir;
322
323 return new LBFactoryMulti( $baseOverride + [
324 'sectionsByDB' => [],
325 'sectionLoads' => [
326 'DEFAULT' => [
327 'test-db1' => 1,
328 ],
329 ],
330 'serverTemplate' => $serverOverride + [
331 'dbname' => $wgDBname,
332 'tablePrefix' => $wgDBprefix,
333 'user' => $wgDBuser,
334 'password' => $wgDBpassword,
335 'type' => $wgDBtype,
336 'dbDirectory' => $wgSQLiteDataDir,
337 'flags' => DBO_DEFAULT
338 ],
339 'hostsByName' => [
340 'test-db1' => $wgDBserver,
341 ],
342 'loadMonitorClass' => LoadMonitorNull::class,
343 'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix )
344 ] );
345 }
346
347 public function testNiceDomains() {
348 global $wgDBname;
349
350 if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
351 self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
352 return;
353 }
354
355 $factory = $this->newLBFactoryMulti(
356 [],
357 []
358 );
359 $lb = $factory->getMainLB();
360
361 $db = $lb->getConnectionRef( DB_MASTER );
362 $this->assertEquals(
363 wfWikiID(),
365 );
366 unset( $db );
367
369 $db = $lb->getConnection( DB_MASTER, [], '' );
370
371 $this->assertEquals(
372 '',
373 $db->getDomainId(),
374 'Null domain ID handle used'
375 );
376 $this->assertEquals(
377 '',
378 $db->getDBname(),
379 'Null domain ID handle used'
380 );
381 $this->assertEquals(
382 '',
383 $db->tablePrefix(),
384 'Main domain ID handle used; prefix is empty though'
385 );
386 $this->assertEquals(
387 $this->quoteTable( $db, 'page' ),
388 $db->tableName( 'page' ),
389 "Correct full table name"
390 );
391 $this->assertEquals(
392 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
393 $db->tableName( "$wgDBname.page" ),
394 "Correct full table name"
395 );
396 $this->assertEquals(
397 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
398 $db->tableName( 'nice_db.page' ),
399 "Correct full table name"
400 );
401
402 $lb->reuseConnection( $db ); // don't care
403
404 $db = $lb->getConnection( DB_MASTER ); // local domain connection
405 $factory->setDomainPrefix( 'my_' );
406
407 $this->assertEquals( $wgDBname, $db->getDBname() );
408 $this->assertEquals(
409 "$wgDBname-my_",
411 );
412 $this->assertEquals(
413 $this->quoteTable( $db, 'my_page' ),
414 $db->tableName( 'page' ),
415 "Correct full table name"
416 );
417 $this->assertEquals(
418 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
419 $db->tableName( 'other_nice_db.page' ),
420 "Correct full table name"
421 );
422
423 $factory->closeAll();
424 $factory->destroy();
425 }
426
427 public function testTrickyDomain() {
428 global $wgDBname;
429
430 if ( wfGetDB( DB_MASTER )->databasesAreIndependent() ) {
431 self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" );
432 return;
433 }
434
435 $dbname = 'unittest-domain'; // explodes if DB is selected
436 $factory = $this->newLBFactoryMulti(
437 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
438 [
439 'dbName' => 'do_not_select_me' // explodes if DB is selected
440 ]
441 );
442 $lb = $factory->getMainLB();
444 $db = $lb->getConnection( DB_MASTER, [], '' );
445
446 $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
447
448 $this->assertEquals(
449 $this->quoteTable( $db, 'page' ),
450 $db->tableName( 'page' ),
451 "Correct full table name"
452 );
453
454 $this->assertEquals(
455 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
456 $db->tableName( "$dbname.page" ),
457 "Correct full table name"
458 );
459
460 $this->assertEquals(
461 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
462 $db->tableName( 'nice_db.page' ),
463 "Correct full table name"
464 );
465
466 $lb->reuseConnection( $db ); // don't care
467
468 $factory->setDomainPrefix( 'my_' );
469 $db = $lb->getConnection( DB_MASTER, [], "$wgDBname-my_" );
470
471 $this->assertEquals(
472 $this->quoteTable( $db, 'my_page' ),
473 $db->tableName( 'page' ),
474 "Correct full table name"
475 );
476 $this->assertEquals(
477 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
478 $db->tableName( 'other_nice_db.page' ),
479 "Correct full table name"
480 );
481 $this->assertEquals(
482 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
483 $db->tableName( 'garbage-db.page' ),
484 "Correct full table name"
485 );
486
487 $lb->reuseConnection( $db ); // don't care
488
489 $factory->closeAll();
490 $factory->destroy();
491 }
492
493 public function testInvalidSelectDB() {
494 $dbname = 'unittest-domain'; // explodes if DB is selected
495 $factory = $this->newLBFactoryMulti(
496 [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
497 [
498 'dbName' => 'do_not_select_me' // explodes if DB is selected
499 ]
500 );
501 $lb = $factory->getMainLB();
503 $db = $lb->getConnection( DB_MASTER, [], '' );
504
505 if ( $db->getType() === 'sqlite' ) {
506 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
507 } elseif ( $db->databasesAreIndependent() ) {
508 try {
509 $e = null;
510 $db->selectDB( 'garbage-db' );
511 } catch ( \Wikimedia\Rdbms\DBConnectionError $e ) {
512 // expected
513 }
514 $this->assertInstanceOf( \Wikimedia\Rdbms\DBConnectionError::class, $e );
515 $this->assertFalse( $db->isOpen() );
516 } else {
517 \Wikimedia\suppressWarnings();
518 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
519 \Wikimedia\restoreWarnings();
520 }
521 }
522
523 private function quoteTable( Database $db, $table ) {
524 if ( $db->getType() === 'sqlite' ) {
525 return $table;
526 } else {
527 return $db->addIdentifierQuotes( $table );
528 }
529 }
530}
$wgDBprefix
Table name prefix.
$wgDBuser
Database username.
$wgSQLiteDataDir
To override default SQLite data directory ($docroot/../data)
$wgDBtype
Database type.
$wgDBserver
Database host name or IP address.
$wgDBpassword
Database user's password.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Simple store for keeping values in an associative array for the current process.
Database \Wikimedia\Rdbms\LBFactorySimple \Wikimedia\Rdbms\LBFactoryMulti.
quoteTable(Database $db, $table)
testChronologyProtector()
\Wikimedia\Rdbms\ChronologyProtector
newLBFactoryMulti(array $baseOverride=[], array $serverOverride=[])
testGetLBFactoryClass( $expected, $deprecated)
MWLBFactory::getLBFactoryClass getLBFactoryClassProvider.
static getLBFactoryClass(array $config)
Returns the LBFactory class to use and the load balancer configuration.
Database $db
Primary database.
hideDeprecated( $function)
Don't throw a warning if $function is deprecated and called later.
Class for ensuring a consistent ordering of events as seen by the user, despite replication.
Class to handle database/prefix specification for IDatabase domains.
Database abstraction object for PHP extension mysqli.
selectDB( $db)
Change the current database.
tablePrefix( $prefix=null)
Get/set the table prefix.
Definition Database.php:593
databasesAreIndependent()
Returns true if DBs are assumed to be on potentially different servers.
tableName( $name, $format='quoted')
Format a table name ready for use in constructing an SQL query.
addIdentifierQuotes( $s)
Quotes an identifier using backticks or "double quotes" depending on the database type.
isOpen()
Is a connection to the database open?
Definition Database.php:758
getDBname()
Get the current DB name.
A multi-database, multi-master factory for Wikimedia and similar installations.
A simple single-master LBFactory that gets its configuration from the b/c globals.
Database connection, tracking, load balancing, and transaction manager for a cluster.
DBMasterPos class for MySQL/MariaDB.
returning false will NOT prevent logging $e
Definition hooks.txt:2176
getType()
Get the type of the DBMS, as it appears in $wgDBtype.
controlled by $wgMainCacheType controlled by $wgParserCacheType controlled by $wgMessageCacheType If you set CACHE_NONE to one of the three control default value for MediaWiki still create a but requests to it are no ops and we always fall through to the database If the cache daemon can t be it should also disable itself fairly smoothly By $wgMemc is used but when it is $parserMemc or $messageMemc this is mentioned $wgDBname
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29
const DBO_DEFAULT
Definition defines.php:13
const DBO_TRX
Definition defines.php:12