| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
o |
| 21 |
. |
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\WikiMap\WikiMap; |
| 25 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 26 |
use Wikimedia\Rdbms\ChronologyProtector; |
| 27 |
use Wikimedia\Rdbms\Database; |
| 28 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 29 |
use Wikimedia\Rdbms\IDatabase; |
| 30 |
use Wikimedia\Rdbms\IDatabaseForOwner; |
| 31 |
use Wikimedia\Rdbms\ILBFactory; |
| 32 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 33 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 34 |
use Wikimedia\Rdbms\LBFactoryMulti; |
| 35 |
use Wikimedia\Rdbms\LBFactorySimple; |
| 36 |
use Wikimedia\Rdbms\LoadBalancer; |
| 37 |
use Wikimedia\Rdbms\LoadMonitorNull; |
| 38 |
use Wikimedia\Rdbms\MySQLPrimaryPos; |
| 39 |
use Wikimedia\TestingAccessWrapper; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class LBFactoryTest extends MediaWikiIntegrationTestCase { |
| 54 |
|
| 55 |
private function getPrimaryServerConfig() { |
| 56 |
global $wgDBserver, $wgDBport, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; |
| 57 |
global $wgSQLiteDataDir; |
| 58 |
|
| 59 |
return [ |
| 60 |
'serverName' => 'db1', |
| 61 |
'host' => $wgDBserver, |
| 62 |
'port' => $wgDBport, |
| 63 |
'dbname' => $wgDBname, |
| 64 |
'user' => $wgDBuser, |
| 65 |
'password' => $wgDBpassword, |
| 66 |
'type' => $wgDBtype, |
| 67 |
'dbDirectory' => $wgSQLiteDataDir, |
| 68 |
'load' => 0, |
| 69 |
'flags' => DBO_TRX |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
public function testLBFactorySimpleServer() { |
| 74 |
$servers = [ $this->getPrimaryServerConfig() ]; |
| 75 |
$factory = new LBFactorySimple( [ 'servers' => $servers ] ); |
| 76 |
$lb = $factory->getMainLB(); |
| 77 |
|
| 78 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 79 |
$this->assertNotFalse( $dbw ); |
| 80 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 81 |
$this->assertNotFalse( $dbr ); |
| 82 |
|
| 83 |
$this->assertSame( 'DEFAULT', $lb->getClusterName() ); |
| 84 |
|
| 85 |
$factory->shutdown(); |
| 86 |
} |
| 87 |
|
| 88 |
public function testLBFactorySimpleServers() { |
| 89 |
$primaryConfig = $this->getPrimaryServerConfig(); |
| 90 |
$fakeReplica = [ 'serverName' => 'db2', 'load' => 100 ] + $primaryConfig; |
| 91 |
|
| 92 |
$servers = [ |
| 93 |
$primaryConfig, |
| 94 |
$fakeReplica |
| 95 |
]; |
| 96 |
|
| 97 |
$factory = new LBFactorySimple( [ |
| 98 |
'servers' => $servers, |
| 99 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ], |
| 100 |
] ); |
| 101 |
$lb = $factory->getMainLB(); |
| 102 |
|
| 103 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 104 |
$this->assertNotFalse( $dbw ); |
| 105 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 106 |
$this->assertNotFalse( $dbr ); |
| 107 |
|
| 108 |
$factory->shutdown(); |
| 109 |
} |
| 110 |
|
| 111 |
public function testLBFactoryMultiConns() { |
| 112 |
$factory = $this->newLBFactoryMultiLBs(); |
| 113 |
|
| 114 |
$this->assertSame( 's3', $factory->getMainLB()->getClusterName() ); |
| 115 |
|
| 116 |
$lb = $factory->getMainLB(); |
| 117 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 118 |
$this->assertNotFalse( $dbw ); |
| 119 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 120 |
$this->assertNotFalse( $dbr ); |
| 121 |
|
| 122 |
|
| 123 |
unset( $factory ); |
| 124 |
} |
| 125 |
|
| 126 |
public function testLBFactoryMultiRoundCallbacks() { |
| 127 |
$called = 0; |
| 128 |
$countLBsFunc = static function ( LBFactoryMulti $factory ) { |
| 129 |
$count = 0; |
| 130 |
foreach ( $factory->getAllLBs() as $lb ) { |
| 131 |
++$count; |
| 132 |
} |
| 133 |
|
| 134 |
return $count; |
| 135 |
}; |
| 136 |
|
| 137 |
$factory = $this->newLBFactoryMultiLBs(); |
| 138 |
$this->assertSame( 0, $countLBsFunc( $factory ) ); |
| 139 |
$dbw = $factory->getMainLB()->getConnection( DB_PRIMARY ); |
| 140 |
$this->assertSame( 1, $countLBsFunc( $factory ) ); |
| 141 |
|
| 142 |
|
| 143 |
$factory->beginPrimaryChanges( __METHOD__ ); |
| 144 |
$dbw->onTransactionPreCommitOrIdle( static function () use ( $factory, &$called ) { |
| 145 |
++$called; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
$factory->getMainLB( 's1wiki' )->getConnection( DB_PRIMARY ); |
| 150 |
} ); |
| 151 |
$factory->commitPrimaryChanges( __METHOD__ ); |
| 152 |
$this->assertSame( 1, $called ); |
| 153 |
$this->assertEquals( 2, $countLBsFunc( $factory ) ); |
| 154 |
$factory->shutdown(); |
| 155 |
$factory->closeAll( __METHOD__ ); |
| 156 |
|
| 157 |
$called = 0; |
| 158 |
$factory = $this->newLBFactoryMultiLBs(); |
| 159 |
$this->assertSame( 0, $countLBsFunc( $factory ) ); |
| 160 |
$dbw = $factory->getMainLB()->getConnection( DB_PRIMARY ); |
| 161 |
$this->assertSame( 1, $countLBsFunc( $factory ) ); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
$factory->beginPrimaryChanges( __METHOD__ ); |
| 166 |
$dbw->query( "SELECT 1 as t", __METHOD__ ); |
| 167 |
$dbw->onTransactionResolution( static function () use ( $factory, &$called ) { |
| 168 |
++$called; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$factory->getMainLB( 's1wiki' )->getConnection( DB_PRIMARY ); |
| 173 |
} ); |
| 174 |
$factory->commitPrimaryChanges( __METHOD__ ); |
| 175 |
$this->assertSame( 1, $called ); |
| 176 |
$this->assertEquals( 2, $countLBsFunc( $factory ) ); |
| 177 |
$factory->shutdown(); |
| 178 |
$factory->closeAll( __METHOD__ ); |
| 179 |
|
| 180 |
$factory = $this->newLBFactoryMultiLBs(); |
| 181 |
$dbw = $factory->getMainLB()->getConnection( DB_PRIMARY ); |
| 182 |
|
| 183 |
$ran = 0; |
| 184 |
$dbw->onTransactionPreCommitOrIdle( static function () use ( &$ran ) { |
| 185 |
++$ran; |
| 186 |
} ); |
| 187 |
$factory->commitPrimaryChanges( __METHOD__ ); |
| 188 |
$this->assertSame( 1, $ran ); |
| 189 |
|
| 190 |
$factory->shutdown(); |
| 191 |
$factory->closeAll( __METHOD__ ); |
| 192 |
} |
| 193 |
|
| 194 |
public function testLBFactoryMultiRoundTransactionSnapshots() { |
| 195 |
$factory = $this->newLBFactoryMultiLBs(); |
| 196 |
$dbr = $factory->getMainLB()->getConnection( DB_REPLICA ); |
| 197 |
$dbw = $factory->getMainLB()->getConnection( DB_PRIMARY ); |
| 198 |
|
| 199 |
$dbr->begin( __METHOD__, $dbr::TRANSACTION_INTERNAL ); |
| 200 |
$this->assertSame( 1, $dbr->trxLevel() ); |
| 201 |
$this->assertSame( 0, $dbw->trxLevel() ); |
| 202 |
|
| 203 |
$factory->beginPrimaryChanges( __METHOD__ ); |
| 204 |
$this->assertSame( 0, $dbr->trxLevel() ); |
| 205 |
$this->assertSame( 0, $dbw->trxLevel() ); |
| 206 |
|
| 207 |
$dbr->begin( __METHOD__, $dbr::TRANSACTION_INTERNAL ); |
| 208 |
$dbw->begin( __METHOD__, $dbw::TRANSACTION_INTERNAL ); |
| 209 |
$this->assertSame( 1, $dbr->trxLevel() ); |
| 210 |
$this->assertSame( 1, $dbw->trxLevel() ); |
| 211 |
|
| 212 |
$factory->commitPrimaryChanges( __METHOD__ ); |
| 213 |
$this->assertSame( 0, $dbr->trxLevel() ); |
| 214 |
$this->assertSame( 0, $dbw->trxLevel() ); |
| 215 |
} |
| 216 |
|
| 217 |
private function newLBFactoryMultiLBs() { |
| 218 |
global $wgDBserver, $wgDBport, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; |
| 219 |
global $wgSQLiteDataDir; |
| 220 |
|
| 221 |
return new LBFactoryMulti( [ |
| 222 |
'sectionsByDB' => [ |
| 223 |
's1wiki' => 's1', |
| 224 |
'DEFAULT' => 's3' |
| 225 |
], |
| 226 |
'sectionLoads' => [ |
| 227 |
's1' => [ |
| 228 |
'test-db3' => 0, |
| 229 |
'test-db4' => 100, |
| 230 |
], |
| 231 |
's3' => [ |
| 232 |
'test-db1' => 0, |
| 233 |
'test-db2' => 100, |
| 234 |
] |
| 235 |
], |
| 236 |
'serverTemplate' => [ |
| 237 |
'port' => $wgDBport, |
| 238 |
'dbname' => $wgDBname, |
| 239 |
'user' => $wgDBuser, |
| 240 |
'password' => $wgDBpassword, |
| 241 |
'type' => $wgDBtype, |
| 242 |
'dbDirectory' => $wgSQLiteDataDir, |
| 243 |
'flags' => DBO_DEFAULT |
| 244 |
], |
| 245 |
'hostsByName' => [ |
| 246 |
'test-db1' => $wgDBserver, |
| 247 |
'test-db2' => $wgDBserver, |
| 248 |
'test-db3' => $wgDBserver, |
| 249 |
'test-db4' => $wgDBserver |
| 250 |
], |
| 251 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ], |
| 252 |
] ); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public function testChronologyProtector() { |
| 259 |
$now = microtime( true ); |
| 260 |
|
| 261 |
$hasChangesFunc = static function ( $mockDB ) { |
| 262 |
$p = $mockDB->writesOrCallbacksPending(); |
| 263 |
$last = $mockDB->lastDoneWrites(); |
| 264 |
|
| 265 |
return is_float( $last ) || $p; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
$m1Pos = new MySQLPrimaryPos( 'db1034-bin.000976/843431247', $now ); |
| 270 |
$m2Pos = new MySQLPrimaryPos( 'db1064-bin.002400/794074907', $now ); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
$mockDB1 = $this->createMock( IDatabaseForOwner::class ); |
| 275 |
$mockDB1->method( 'writesOrCallbacksPending' )->willReturn( true ); |
| 276 |
$mockDB1->method( 'lastDoneWrites' )->willReturn( $now ); |
| 277 |
|
| 278 |
$lb1 = $this->createMock( LoadBalancer::class ); |
| 279 |
$lb1->method( 'getConnection' )->willReturn( $mockDB1 ); |
| 280 |
$lb1->method( 'getServerCount' )->willReturn( 2 ); |
| 281 |
$lb1->method( 'hasReplicaServers' )->willReturn( true ); |
| 282 |
$lb1->method( 'hasStreamingReplicaServers' )->willReturn( true ); |
| 283 |
$lb1->method( 'getAnyOpenConnection' )->willReturn( $mockDB1 ); |
| 284 |
$lb1->method( 'hasOrMadeRecentPrimaryChanges' )->willReturnCallback( |
| 285 |
static function () use ( $mockDB1, $hasChangesFunc ) { |
| 286 |
return $hasChangesFunc( $mockDB1 ); |
| 287 |
} |
| 288 |
); |
| 289 |
$lb1->method( 'getPrimaryPos' )->willReturn( $m1Pos ); |
| 290 |
$lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' ); |
| 291 |
|
| 292 |
|
| 293 |
$mockDB2 = $this->createMock( IDatabaseForOwner::class ); |
| 294 |
$mockDB2->method( 'writesOrCallbacksPending' )->willReturn( true ); |
| 295 |
$mockDB2->method( 'lastDoneWrites' )->willReturn( $now ); |
| 296 |
|
| 297 |
$lb2 = $this->createMock( LoadBalancer::class ); |
| 298 |
$lb2->method( 'getConnection' )->willReturn( $mockDB2 ); |
| 299 |
$lb2->method( 'getServerCount' )->willReturn( 2 ); |
| 300 |
$lb2->method( 'hasReplicaServers' )->willReturn( true ); |
| 301 |
$lb2->method( 'hasStreamingReplicaServers' )->willReturn( true ); |
| 302 |
$lb2->method( 'getAnyOpenConnection' )->willReturn( $mockDB2 ); |
| 303 |
$lb2->method( 'hasOrMadeRecentPrimaryChanges' )->willReturnCallback( |
| 304 |
static function () use ( $mockDB2, $hasChangesFunc ) { |
| 305 |
return $hasChangesFunc( $mockDB2 ); |
| 306 |
} |
| 307 |
); |
| 308 |
$lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' ); |
| 309 |
$lb2->method( 'getPrimaryPos' )->willReturn( $m2Pos ); |
| 310 |
|
| 311 |
$bag = new HashBagOStuff(); |
| 312 |
$cp = new ChronologyProtector( $bag, null, false ); |
| 313 |
$cp->setRequestInfo( [ |
| 314 |
'IPAddress' => '127.0.0.1', |
| 315 |
'UserAgent' => 'Totally-Not-Firefox', |
| 316 |
'ChronologyClientId' => 'random_id', |
| 317 |
] ); |
| 318 |
|
| 319 |
$mockDB1->expects( $this->once() )->method( 'writesOrCallbacksPending' ); |
| 320 |
$mockDB1->expects( $this->once() )->method( 'lastDoneWrites' ); |
| 321 |
$mockDB2->expects( $this->once() )->method( 'writesOrCallbacksPending' ); |
| 322 |
$mockDB2->expects( $this->once() )->method( 'lastDoneWrites' ); |
| 323 |
|
| 324 |
|
| 325 |
$sPos1 = $cp->getSessionPrimaryPos( $lb1 ); |
| 326 |
$sPos2 = $cp->getSessionPrimaryPos( $lb2 ); |
| 327 |
|
| 328 |
$cp->stageSessionPrimaryPos( $lb1 ); |
| 329 |
$cp->stageSessionPrimaryPos( $lb2 ); |
| 330 |
$cpIndex = null; |
| 331 |
$cp->persistSessionReplicationPositions( $cpIndex ); |
| 332 |
|
| 333 |
$this->assertNull( $sPos1 ); |
| 334 |
$this->assertNull( $sPos2 ); |
| 335 |
$this->assertSame( 1, $cpIndex, "CP write index set" ); |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
$lb1 = $this->createMock( LoadBalancer::class ); |
| 341 |
$lb1->method( 'getServerCount' )->willReturn( 2 ); |
| 342 |
$lb1->method( 'hasReplicaServers' )->willReturn( true ); |
| 343 |
$lb1->method( 'hasStreamingReplicaServers' )->willReturn( true ); |
| 344 |
$lb1->method( 'getServerName' )->with( 0 )->willReturn( 'master1' ); |
| 345 |
|
| 346 |
$lb2 = $this->createMock( LoadBalancer::class ); |
| 347 |
$lb2->method( 'getServerCount' )->willReturn( 2 ); |
| 348 |
$lb2->method( 'hasReplicaServers' )->willReturn( true ); |
| 349 |
$lb2->method( 'hasStreamingReplicaServers' )->willReturn( true ); |
| 350 |
$lb2->method( 'getServerName' )->with( 0 )->willReturn( 'master2' ); |
| 351 |
|
| 352 |
$cp = new ChronologyProtector( $bag, null, false ); |
| 353 |
$cp->setRequestInfo( |
| 354 |
[ |
| 355 |
'IPAddress' => '127.0.0.1', |
| 356 |
'UserAgent' => 'Totally-Not-Firefox', |
| 357 |
'ChronologyClientId' => 'random_id', |
| 358 |
'ChronologyPositionIndex' => $cpIndex |
| 359 |
] ); |
| 360 |
|
| 361 |
|
| 362 |
$sPos1 = $cp->getSessionPrimaryPos( $lb1 ); |
| 363 |
$sPos2 = $cp->getSessionPrimaryPos( $lb2 ); |
| 364 |
|
| 365 |
$cp->stageSessionPrimaryPos( $lb1 ); |
| 366 |
$cp->stageSessionPrimaryPos( $lb2 ); |
| 367 |
$cpIndex = null; |
| 368 |
$cp->persistSessionReplicationPositions( $cpIndex ); |
| 369 |
|
| 370 |
$this->assertNotNull( $sPos1 ); |
| 371 |
$this->assertNotNull( $sPos2 ); |
| 372 |
$this->assertSame( $m1Pos->__toString(), $sPos1->__toString() ); |
| 373 |
$this->assertSame( $m2Pos->__toString(), $sPos2->__toString() ); |
| 374 |
$this->assertNull( $cpIndex, "CP write index retained" ); |
| 375 |
|
| 376 |
$this->assertEquals( 'random_id', $cp->getClientId() ); |
| 377 |
} |
| 378 |
|
| 379 |
private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) { |
| 380 |
global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBprefix, $wgDBtype; |
| 381 |
global $wgSQLiteDataDir; |
| 382 |
|
| 383 |
return new LBFactoryMulti( $baseOverride + [ |
| 384 |
'sectionsByDB' => [], |
| 385 |
'sectionLoads' => [ |
| 386 |
'DEFAULT' => [ |
| 387 |
'test-db1' => 1, |
| 388 |
], |
| 389 |
], |
| 390 |
'serverTemplate' => $serverOverride + [ |
| 391 |
'dbname' => $wgDBname, |
| 392 |
'tablePrefix' => $wgDBprefix, |
| 393 |
'user' => $wgDBuser, |
| 394 |
'password' => $wgDBpassword, |
| 395 |
'type' => $wgDBtype, |
| 396 |
'dbDirectory' => $wgSQLiteDataDir, |
| 397 |
'flags' => DBO_DEFAULT |
| 398 |
], |
| 399 |
'hostsByName' => [ |
| 400 |
'test-db1' => $wgDBserver, |
| 401 |
], |
| 402 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ], |
| 403 |
'localDomain' => new DatabaseDomain( $wgDBname, null, $wgDBprefix ), |
| 404 |
'agent' => 'MW-UNIT-TESTS' |
| 405 |
] ); |
| 406 |
} |
| 407 |
|
| 408 |
public function testNiceDomains() { |
| 409 |
global $wgDBname; |
| 410 |
|
| 411 |
if ( $this->getDb()->databasesAreIndependent() ) { |
| 412 |
self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" ); |
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
$factory = $this->newLBFactoryMulti( |
| 417 |
[], |
| 418 |
[] |
| 419 |
); |
| 420 |
$lb = $factory->getMainLB(); |
| 421 |
|
| 422 |
$db = $lb->getConnection( DB_PRIMARY ); |
| 423 |
$this->assertEquals( |
| 424 |
WikiMap::getCurrentWikiId(), |
| 425 |
$db->getDomainID() |
| 426 |
); |
| 427 |
unset( $db ); |
| 428 |
|
| 429 |
|
| 430 |
$db = $lb->getConnection( DB_PRIMARY, [], $lb::DOMAIN_ANY ); |
| 431 |
|
| 432 |
$this->assertSame( |
| 433 |
'', |
| 434 |
$db->getDomainID(), |
| 435 |
'Null domain ID handle used' |
| 436 |
); |
| 437 |
$this->assertNull( |
| 438 |
$db->getDBname(), |
| 439 |
'Null domain ID handle used' |
| 440 |
); |
| 441 |
$this->assertSame( |
| 442 |
'', |
| 443 |
$db->tablePrefix(), |
| 444 |
'Main domain ID handle used; prefix is empty though' |
| 445 |
); |
| 446 |
$this->assertEquals( |
| 447 |
$this->quoteTable( $db, 'page' ), |
| 448 |
$db->tableName( 'page' ), |
| 449 |
"Correct full table name" |
| 450 |
); |
| 451 |
$this->assertEquals( |
| 452 |
$this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ), |
| 453 |
$db->tableName( "$wgDBname.page" ), |
| 454 |
"Correct full table name" |
| 455 |
); |
| 456 |
$this->assertEquals( |
| 457 |
$this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), |
| 458 |
$db->tableName( 'nice_db.page' ), |
| 459 |
"Correct full table name" |
| 460 |
); |
| 461 |
|
| 462 |
unset( $db ); |
| 463 |
|
| 464 |
$factory->setLocalDomainPrefix( 'my_' ); |
| 465 |
$db = $lb->getConnection( DB_PRIMARY ); |
| 466 |
|
| 467 |
$this->assertEquals( $wgDBname, $db->getDBname() ); |
| 468 |
$this->assertEquals( |
| 469 |
"$wgDBname-my_", |
| 470 |
$db->getDomainID() |
| 471 |
); |
| 472 |
$this->assertEquals( |
| 473 |
$this->quoteTable( $db, 'my_page' ), |
| 474 |
$db->tableName( 'page' ), |
| 475 |
"Correct full table name" |
| 476 |
); |
| 477 |
$this->assertEquals( |
| 478 |
$this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), |
| 479 |
$db->tableName( 'other_nice_db.page' ), |
| 480 |
"Correct full table name" |
| 481 |
); |
| 482 |
|
| 483 |
$factory->closeAll( __METHOD__ ); |
| 484 |
$factory->destroy(); |
| 485 |
} |
| 486 |
|
| 487 |
public function testTrickyDomain() { |
| 488 |
global $wgDBname; |
| 489 |
|
| 490 |
if ( $this->getDb()->databasesAreIndependent() ) { |
| 491 |
self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" ); |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
$dbname = 'unittest-domain'; |
| 496 |
$factory = $this->newLBFactoryMulti( |
| 497 |
[ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ], |
| 498 |
[ |
| 499 |
'dbname' => 'do_not_select_me' |
| 500 |
] |
| 501 |
); |
| 502 |
$lb = $factory->getMainLB(); |
| 503 |
|
| 504 |
$db = $lb->getConnection( DB_PRIMARY, [], $lb::DOMAIN_ANY ); |
| 505 |
|
| 506 |
$this->assertSame( '', $db->getDomainID(), "Null domain used" ); |
| 507 |
|
| 508 |
$this->assertEquals( |
| 509 |
$this->quoteTable( $db, 'page' ), |
| 510 |
$db->tableName( 'page' ), |
| 511 |
"Correct full table name" |
| 512 |
); |
| 513 |
|
| 514 |
$this->assertEquals( |
| 515 |
$this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ), |
| 516 |
$db->tableName( "$dbname.page" ), |
| 517 |
"Correct full table name" |
| 518 |
); |
| 519 |
|
| 520 |
$this->assertEquals( |
| 521 |
$this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), |
| 522 |
$db->tableName( 'nice_db.page' ), |
| 523 |
"Correct full table name" |
| 524 |
); |
| 525 |
|
| 526 |
unset( $db ); |
| 527 |
|
| 528 |
$factory->setLocalDomainPrefix( 'my_' ); |
| 529 |
$db = $lb->getConnection( DB_PRIMARY, [], "$wgDBname-my_" ); |
| 530 |
|
| 531 |
$this->assertEquals( |
| 532 |
$this->quoteTable( $db, 'my_page' ), |
| 533 |
$db->tableName( 'page' ), |
| 534 |
"Correct full table name" |
| 535 |
); |
| 536 |
$this->assertEquals( |
| 537 |
$this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ), |
| 538 |
$db->tableName( 'other_nice_db.page' ), |
| 539 |
"Correct full table name" |
| 540 |
); |
| 541 |
$this->assertEquals( |
| 542 |
$this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ), |
| 543 |
$db->tableName( 'garbage-db.page' ), |
| 544 |
"Correct full table name" |
| 545 |
); |
| 546 |
|
| 547 |
$factory->closeAll( __METHOD__ ); |
| 548 |
$factory->destroy(); |
| 549 |
} |
| 550 |
|
| 551 |
public function testInvalidSelectDB() { |
| 552 |
if ( $this->getDb()->databasesAreIndependent() ) { |
| 553 |
$this->markTestSkipped( "Not applicable per databasesAreIndependent()" ); |
| 554 |
} |
| 555 |
|
| 556 |
$dbname = 'unittest-domain'; |
| 557 |
$factory = $this->newLBFactoryMulti( |
| 558 |
[ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ], |
| 559 |
[ |
| 560 |
'dbname' => 'do_not_select_me' |
| 561 |
] |
| 562 |
); |
| 563 |
$lb = $factory->getMainLB(); |
| 564 |
|
| 565 |
$db = $lb->getConnection( DB_PRIMARY, [], $lb::DOMAIN_ANY ); |
| 566 |
|
| 567 |
$this->expectException( \Wikimedia\Rdbms\DBUnexpectedError::class ); |
| 568 |
$db->selectDomain( 'garbagedb' ); |
| 569 |
} |
| 570 |
|
| 571 |
public function testInvalidSelectDBIndependent() { |
| 572 |
$dbname = 'unittest-domain'; |
| 573 |
$factory = $this->newLBFactoryMulti( |
| 574 |
[ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ], |
| 575 |
[ |
| 576 |
|
| 577 |
'dbname' => 'bad_dir/do_not_select_me' |
| 578 |
] |
| 579 |
); |
| 580 |
$lb = $factory->getMainLB(); |
| 581 |
|
| 582 |
|
| 583 |
$this->expectException( \Wikimedia\Rdbms\DBConnectionError::class ); |
| 584 |
if ( !$factory->getMainLB()->getServerAttributes( 0 )[Database::ATTR_DB_IS_FILE] ) { |
| 585 |
$this->markTestSkipped( "Not applicable per ATTR_DB_IS_FILE" ); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
$this->assertNotNull( $lb->getConnectionInternal( DB_PRIMARY, [], $lb::DOMAIN_ANY ) ); |
| 590 |
} |
| 591 |
|
| 592 |
public function testInvalidSelectDBIndependent2() { |
| 593 |
$dbname = 'unittest-domain'; |
| 594 |
$factory = $this->newLBFactoryMulti( |
| 595 |
[ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ], |
| 596 |
[ |
| 597 |
|
| 598 |
'dbname' => 'bad_dir/do_not_select_me' |
| 599 |
] |
| 600 |
); |
| 601 |
$lb = $factory->getMainLB(); |
| 602 |
|
| 603 |
|
| 604 |
$this->expectException( \Wikimedia\Rdbms\DBExpectedError::class ); |
| 605 |
if ( !$lb->getConnection( DB_PRIMARY )->databasesAreIndependent() ) { |
| 606 |
$this->markTestSkipped( "Not applicable per databasesAreIndependent()" ); |
| 607 |
} |
| 608 |
|
| 609 |
$db = $lb->getConnectionInternal( DB_PRIMARY ); |
| 610 |
$db->selectDomain( 'garbage-db' ); |
| 611 |
} |
| 612 |
|
| 613 |
public function testRedefineLocalDomain() { |
| 614 |
global $wgDBname; |
| 615 |
|
| 616 |
if ( $this->getDb()->databasesAreIndependent() ) { |
| 617 |
self::markTestSkipped( "Skipping tests about selecting DBs: not applicable" ); |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
$factory = $this->newLBFactoryMulti( |
| 622 |
[], |
| 623 |
[] |
| 624 |
); |
| 625 |
$lb = $factory->getMainLB(); |
| 626 |
|
| 627 |
$conn1 = $lb->getConnection( DB_PRIMARY ); |
| 628 |
$this->assertEquals( |
| 629 |
WikiMap::getCurrentWikiId(), |
| 630 |
$conn1->getDomainID() |
| 631 |
); |
| 632 |
unset( $conn1 ); |
| 633 |
|
| 634 |
$factory->redefineLocalDomain( 'somedb-prefix_' ); |
| 635 |
$this->assertEquals( 'somedb-prefix_', $factory->getLocalDomainID() ); |
| 636 |
|
| 637 |
$domain = new DatabaseDomain( $wgDBname, null, 'pref_' ); |
| 638 |
$factory->redefineLocalDomain( $domain ); |
| 639 |
|
| 640 |
|
| 641 |
$lbWrapper = TestingAccessWrapper::newFromObject( $lb ); |
| 642 |
$n = iterator_count( $lbWrapper->getOpenConnections() ); |
| 643 |
$this->assertSame( 0, $n, "Connections closed" ); |
| 644 |
|
| 645 |
$conn2 = $lb->getConnection( DB_PRIMARY ); |
| 646 |
$this->assertEquals( |
| 647 |
$domain->getId(), |
| 648 |
$conn2->getDomainID() |
| 649 |
); |
| 650 |
unset( $conn2 ); |
| 651 |
|
| 652 |
$factory->closeAll( __METHOD__ ); |
| 653 |
$factory->destroy(); |
| 654 |
} |
| 655 |
|
| 656 |
public function testVirtualDomains() { |
| 657 |
$baseOverrides = [ |
| 658 |
'localDomain' => ( new DatabaseDomain( 'localdomain', null, '' ) )->getId(), |
| 659 |
'sectionLoads' => [ |
| 660 |
'DEFAULT' => [ |
| 661 |
'test-db1' => 1, |
| 662 |
], |
| 663 |
'shareddb' => [ |
| 664 |
'test-db1' => 1, |
| 665 |
], |
| 666 |
], |
| 667 |
'externalLoads' => [ |
| 668 |
'extension1' => [ |
| 669 |
'test-db1' => 1, |
| 670 |
], |
| 671 |
], |
| 672 |
'virtualDomains' => [ 'virtualdomain1', 'virtualdomain2', 'virtualdomain3', 'virtualdomain4' ], |
| 673 |
'virtualDomainsMapping' => [ |
| 674 |
'virtualdomain1' => [ 'db' => 'extdomain', 'cluster' => 'extension1' ], |
| 675 |
'virtualdomain2' => [ 'db' => false, 'cluster' => 'extension1' ], |
| 676 |
'virtualdomain3' => [ 'db' => 'shareddb' ], |
| 677 |
] |
| 678 |
]; |
| 679 |
$factory = $this->newLBFactoryMulti( $baseOverrides ); |
| 680 |
$db1 = $factory->getPrimaryDatabase( 'virtualdomain1' ); |
| 681 |
$this->assertEquals( |
| 682 |
'extdomain', |
| 683 |
$db1->getDomainID() |
| 684 |
); |
| 685 |
$this->assertEquals( |
| 686 |
'extension1', |
| 687 |
$factory->getLoadBalancer( 'virtualdomain1' )->getClusterName() |
| 688 |
); |
| 689 |
|
| 690 |
$db2 = $factory->getPrimaryDatabase( 'virtualdomain2' ); |
| 691 |
$this->assertEquals( |
| 692 |
'localdomain', |
| 693 |
$db2->getDomainID() |
| 694 |
); |
| 695 |
$this->assertEquals( |
| 696 |
'extension1', |
| 697 |
$factory->getLoadBalancer( 'virtualdomain2' )->getClusterName() |
| 698 |
); |
| 699 |
|
| 700 |
$db3 = $factory->getPrimaryDatabase( 'virtualdomain3' ); |
| 701 |
$this->assertEquals( |
| 702 |
'shareddb', |
| 703 |
$db3->getDomainID() |
| 704 |
); |
| 705 |
$this->assertEquals( |
| 706 |
'DEFAULT', |
| 707 |
$factory->getLoadBalancer( 'virtualdomain3' )->getClusterName() |
| 708 |
); |
| 709 |
|
| 710 |
$db3 = $factory->getPrimaryDatabase( 'virtualdomain4' ); |
| 711 |
$this->assertEquals( |
| 712 |
'localdomain', |
| 713 |
$db3->getDomainID() |
| 714 |
); |
| 715 |
$this->assertEquals( |
| 716 |
'DEFAULT', |
| 717 |
$factory->getLoadBalancer( 'virtualdomain4' )->getClusterName() |
| 718 |
); |
| 719 |
} |
| 720 |
|
| 721 |
private function quoteTable( IReadableDatabase $db, $table ) { |
| 722 |
if ( $db->getType() === 'sqlite' ) { |
| 723 |
return $table; |
| 724 |
} else { |
| 725 |
return $db->addIdentifierQuotes( $table ); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
public function testGetChronologyProtectorTouched() { |
| 730 |
$store = new HashBagOStuff; |
| 731 |
$chronologyProtector = new ChronologyProtector( $store, '', false ); |
| 732 |
$chronologyProtector->setRequestInfo( [ 'ChronologyClientId' => 'ii' ] ); |
| 733 |
|
| 734 |
|
| 735 |
$mockWallClock = 1549343000.0; |
| 736 |
$priorTime = $mockWallClock; |
| 737 |
$chronologyProtector->setMockTime( $mockWallClock ); |
| 738 |
|
| 739 |
$cpWrap = TestingAccessWrapper::newFromObject( $chronologyProtector ); |
| 740 |
$cpWrap->store->set( |
| 741 |
$cpWrap->key, |
| 742 |
$cpWrap->mergePositions( |
| 743 |
false, |
| 744 |
[], |
| 745 |
[ ILBFactory::CLUSTER_MAIN_DEFAULT => $priorTime ] |
| 746 |
), |
| 747 |
3600 |
| 748 |
); |
| 749 |
|
| 750 |
$lbFactory = $this->newLBFactoryMulti( [ 'chronologyProtector' => $chronologyProtector ] ); |
| 751 |
$mockWallClock += 1.0; |
| 752 |
$touched = $chronologyProtector->getTouched( $lbFactory->getMainLB() ); |
| 753 |
$this->assertEquals( $priorTime, $touched ); |
| 754 |
} |
| 755 |
|
| 756 |
public function testReconfigureWithOneReplica() { |
| 757 |
$primaryConfig = $this->getPrimaryServerConfig(); |
| 758 |
$fakeReplica = [ 'load' => 100, 'serverName' => 'replica' ] + $primaryConfig; |
| 759 |
|
| 760 |
$conf = [ 'servers' => [ |
| 761 |
$primaryConfig, |
| 762 |
$fakeReplica |
| 763 |
] ]; |
| 764 |
|
| 765 |
|
| 766 |
$factory = new LBFactorySimple( $conf ); |
| 767 |
$lb = $factory->getMainLB(); |
| 768 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 769 |
|
| 770 |
$con = $lb->getConnectionInternal( DB_REPLICA ); |
| 771 |
$ref = $lb->getConnection( DB_REPLICA ); |
| 772 |
|
| 773 |
|
| 774 |
$factory->reconfigure( $conf ); |
| 775 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 776 |
$this->assertTrue( $con->isOpen() ); |
| 777 |
$this->assertTrue( $ref->isOpen() ); |
| 778 |
|
| 779 |
|
| 780 |
$factory->reconfigure( [] ); |
| 781 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 782 |
$this->assertTrue( $con->isOpen() ); |
| 783 |
$this->assertTrue( $ref->isOpen() ); |
| 784 |
|
| 785 |
|
| 786 |
$conf['servers'] = [ $this->getPrimaryServerConfig() ]; |
| 787 |
$factory->reconfigure( $conf ); |
| 788 |
|
| 789 |
|
| 790 |
$this->assertSame( 1, $lb->getServerCount() ); |
| 791 |
|
| 792 |
|
| 793 |
$this->assertTrue( $con->isOpen() ); |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
$this->assertTrue( $ref->isOpen() ); |
| 798 |
|
| 799 |
|
| 800 |
$this->assertFalse( $con->isOpen() ); |
| 801 |
} |
| 802 |
|
| 803 |
public function testReconfigureWithThreeReplicas() { |
| 804 |
$primaryConfig = $this->getPrimaryServerConfig(); |
| 805 |
$replica1Config = [ 'serverName' => 'db2', 'load' => 0 ] + $primaryConfig; |
| 806 |
$replica2Config = [ 'serverName' => 'db3', 'load' => 1 ] + $primaryConfig; |
| 807 |
$replica3Config = [ 'serverName' => 'db4', 'load' => 1 ] + $primaryConfig; |
| 808 |
|
| 809 |
$conf = [ 'servers' => [ |
| 810 |
$primaryConfig, |
| 811 |
$replica1Config, |
| 812 |
$replica2Config, |
| 813 |
$replica3Config |
| 814 |
] ]; |
| 815 |
|
| 816 |
|
| 817 |
$factory = new LBFactorySimple( $conf ); |
| 818 |
$lb = $factory->getMainLB(); |
| 819 |
$this->assertSame( 4, $lb->getServerCount() ); |
| 820 |
$this->assertSame( 'db1', $lb->getServerName( 0 ) ); |
| 821 |
$this->assertSame( 'db2', $lb->getServerName( 1 ) ); |
| 822 |
$this->assertSame( 'db3', $lb->getServerName( 2 ) ); |
| 823 |
$this->assertSame( 'db4', $lb->getServerName( 3 ) ); |
| 824 |
|
| 825 |
$con = $lb->getConnectionInternal( DB_REPLICA ); |
| 826 |
$ref = $lb->getConnection( DB_REPLICA ); |
| 827 |
|
| 828 |
|
| 829 |
$factory->reconfigure( $conf ); |
| 830 |
$this->assertSame( 4, $lb->getServerCount() ); |
| 831 |
$this->assertSame( 'db1', $lb->getServerName( 0 ) ); |
| 832 |
$this->assertSame( 'db2', $lb->getServerName( 1 ) ); |
| 833 |
$this->assertSame( 'db3', $lb->getServerName( 2 ) ); |
| 834 |
$this->assertSame( 'db4', $lb->getServerName( 3 ) ); |
| 835 |
$this->assertTrue( $con->isOpen() ); |
| 836 |
$this->assertTrue( $ref->isOpen() ); |
| 837 |
|
| 838 |
|
| 839 |
$factory->reconfigure( [] ); |
| 840 |
$this->assertSame( 4, $lb->getServerCount() ); |
| 841 |
$this->assertSame( 'db1', $lb->getServerName( 0 ) ); |
| 842 |
$this->assertSame( 'db2', $lb->getServerName( 1 ) ); |
| 843 |
$this->assertSame( 'db3', $lb->getServerName( 2 ) ); |
| 844 |
$this->assertSame( 'db4', $lb->getServerName( 3 ) ); |
| 845 |
$this->assertTrue( $con->isOpen() ); |
| 846 |
$this->assertTrue( $ref->isOpen() ); |
| 847 |
|
| 848 |
|
| 849 |
$conf['servers'] = [ $primaryConfig, $replica2Config, $replica3Config ]; |
| 850 |
$factory->reconfigure( $conf ); |
| 851 |
|
| 852 |
$this->assertSame( 3, $lb->getServerCount() ); |
| 853 |
$this->assertSame( 'db1', $lb->getServerName( 0 ) ); |
| 854 |
$this->assertSame( false, $lb->getServerInfo( 1 ) ); |
| 855 |
$this->assertSame( 'db3', $lb->getServerName( 2 ) ); |
| 856 |
$this->assertSame( 'db4', $lb->getServerName( 3 ) ); |
| 857 |
|
| 858 |
$this->assertTrue( $con->isOpen() ); |
| 859 |
|
| 860 |
|
| 861 |
$this->assertTrue( $ref->isOpen() ); |
| 862 |
|
| 863 |
$this->assertFalse( $con->isOpen() ); |
| 864 |
} |
| 865 |
|
| 866 |
public function testAutoReconfigure() { |
| 867 |
$primaryConfig = $this->getPrimaryServerConfig(); |
| 868 |
$fakeReplica = [ 'load' => 100, 'serverName' => 'replica1' ] + $primaryConfig; |
| 869 |
|
| 870 |
$conf = [ |
| 871 |
'servers' => [ |
| 872 |
$primaryConfig, |
| 873 |
$fakeReplica |
| 874 |
], |
| 875 |
]; |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
$conf['configCallback'] = static function () use ( &$conf ) { |
| 880 |
static $calls = 0; |
| 881 |
$calls++; |
| 882 |
if ( $calls == 1 ) { |
| 883 |
return $conf; |
| 884 |
} else { |
| 885 |
unset( $conf['servers'][1] ); |
| 886 |
return $conf; |
| 887 |
} |
| 888 |
}; |
| 889 |
|
| 890 |
|
| 891 |
$factory = new LBFactorySimple( $conf ); |
| 892 |
|
| 893 |
$lb = $factory->getMainLB(); |
| 894 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 895 |
|
| 896 |
$con = $lb->getConnectionInternal( DB_REPLICA ); |
| 897 |
$ref = $lb->getConnection( DB_REPLICA ); |
| 898 |
|
| 899 |
|
| 900 |
$factory->autoReconfigure(); |
| 901 |
|
| 902 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 903 |
$this->assertTrue( $con->isOpen() ); |
| 904 |
$this->assertTrue( $ref->isOpen() ); |
| 905 |
|
| 906 |
|
| 907 |
$factory->autoReconfigure(); |
| 908 |
|
| 909 |
|
| 910 |
$this->assertSame( 1, $lb->getServerCount() ); |
| 911 |
|
| 912 |
|
| 913 |
$this->assertTrue( $con->isOpen() ); |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
$this->assertTrue( $ref->isOpen() ); |
| 918 |
|
| 919 |
|
| 920 |
$this->assertFalse( $con->isOpen() ); |
| 921 |
} |
| 922 |
|
| 923 |
public function testSetWaitForReplicationListener() { |
| 924 |
$factory = $this->newLBFactoryMultiLBs(); |
| 925 |
|
| 926 |
$allLBs = iterator_to_array( $factory->getAllLBs() ); |
| 927 |
$this->assertCount( 0, $allLBs ); |
| 928 |
|
| 929 |
$runs = 0; |
| 930 |
$callback = static function () use ( &$runs ) { |
| 931 |
++$runs; |
| 932 |
}; |
| 933 |
$factory->setWaitForReplicationListener( 'test', $callback ); |
| 934 |
|
| 935 |
$this->assertSame( 0, $runs ); |
| 936 |
$factory->waitForReplication(); |
| 937 |
$this->assertSame( 1, $runs ); |
| 938 |
|
| 939 |
$factory->getMainLB(); |
| 940 |
$allLBs = iterator_to_array( $factory->getAllLBs() ); |
| 941 |
$this->assertCount( 1, $allLBs ); |
| 942 |
$factory->waitForReplication(); |
| 943 |
$this->assertSame( 2, $runs ); |
| 944 |
} |
| 945 |
} |