| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\Rdbms\ChronologyProtector; |
| 22 |
use Wikimedia\Rdbms\Database; |
| 23 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 24 |
use Wikimedia\Rdbms\DBConnRef; |
| 25 |
use Wikimedia\Rdbms\DBReadOnlyRoleError; |
| 26 |
use Wikimedia\Rdbms\IDatabase; |
| 27 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 28 |
use Wikimedia\Rdbms\LoadBalancer; |
| 29 |
use Wikimedia\Rdbms\LoadMonitorNull; |
| 30 |
use Wikimedia\Rdbms\ServerInfo; |
| 31 |
use Wikimedia\Rdbms\TransactionManager; |
| 32 |
use Wikimedia\TestingAccessWrapper; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class LoadBalancerTest extends MediaWikiIntegrationTestCase { |
| 41 |
private function makeServerConfig( $flags = DBO_DEFAULT ) { |
| 42 |
global $wgDBserver, $wgDBport, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; |
| 43 |
global $wgSQLiteDataDir; |
| 44 |
|
| 45 |
return [ |
| 46 |
'host' => $wgDBserver, |
| 47 |
'port' => $wgDBport, |
| 48 |
'serverName' => 'testhost', |
| 49 |
'dbname' => $wgDBname, |
| 50 |
'tablePrefix' => self::dbPrefix(), |
| 51 |
'user' => $wgDBuser, |
| 52 |
'password' => $wgDBpassword, |
| 53 |
'type' => $wgDBtype, |
| 54 |
'dbDirectory' => $wgSQLiteDataDir, |
| 55 |
'load' => 0, |
| 56 |
'flags' => $flags |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function testWithoutReplica() { |
| 61 |
global $wgDBname; |
| 62 |
|
| 63 |
$called = false; |
| 64 |
$chronologyProtector = $this->createMock( ChronologyProtector::class ); |
| 65 |
$chronologyProtector->method( 'getSessionPrimaryPos' ) |
| 66 |
->willReturnCallback( |
| 67 |
static function () use ( &$called ) { |
| 68 |
$called = true; |
| 69 |
} |
| 70 |
); |
| 71 |
$lb = new LoadBalancer( [ |
| 72 |
|
| 73 |
'servers' => [ $this->makeServerConfig( DBO_TRX ) ], |
| 74 |
'logger' => MediaWiki\Logger\LoggerFactory::getInstance( 'rdbms' ), |
| 75 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ), |
| 76 |
'chronologyProtector' => $chronologyProtector, |
| 77 |
'clusterName' => 'xyz' |
| 78 |
] ); |
| 79 |
|
| 80 |
$this->assertSame( 1, $lb->getServerCount() ); |
| 81 |
$this->assertFalse( $lb->hasReplicaServers() ); |
| 82 |
$this->assertFalse( $lb->hasStreamingReplicaServers() ); |
| 83 |
$this->assertSame( 'xyz', $lb->getClusterName() ); |
| 84 |
|
| 85 |
$ld = DatabaseDomain::newFromId( $lb->getLocalDomainID() ); |
| 86 |
$this->assertSame( $wgDBname, $ld->getDatabase(), 'local domain DB set' ); |
| 87 |
$this->assertSame( self::dbPrefix(), $ld->getTablePrefix(), 'local domain prefix set' ); |
| 88 |
$this->assertSame( 'my_test_wiki', $lb->resolveDomainID( 'my_test_wiki' ) ); |
| 89 |
$this->assertSame( $ld->getId(), $lb->resolveDomainID( false ) ); |
| 90 |
$this->assertSame( $ld->getId(), $lb->resolveDomainID( $ld ) ); |
| 91 |
$this->assertFalse( $called ); |
| 92 |
|
| 93 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 94 |
$dbw->getServerName(); |
| 95 |
$this->assertFalse( $called, "getServerName() optimized for DB_PRIMARY" ); |
| 96 |
|
| 97 |
$dbw->ensureConnection(); |
| 98 |
$this->assertFalse( $called, "Session replication pos not used with single server" ); |
| 99 |
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" ); |
| 100 |
$this->assertWriteAllowed( $dbw ); |
| 101 |
|
| 102 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 103 |
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" ); |
| 104 |
|
| 105 |
if ( !$lb->getServerAttributes( ServerInfo::WRITER_INDEX )[Database::ATTR_DB_LEVEL_LOCKING] ) { |
| 106 |
$dbwAC1 = $lb->getConnection( DB_PRIMARY, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 107 |
$this->assertFalse( |
| 108 |
$dbwAC1->getFlag( $dbw::DBO_TRX ), |
| 109 |
"No DBO_TRX with CONN_TRX_AUTOCOMMIT" |
| 110 |
); |
| 111 |
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" ); |
| 112 |
$this->assertUnsharedHandle( $dbw, $dbwAC1, "CONN_TRX_AUTOCOMMIT separate connection" ); |
| 113 |
|
| 114 |
$dbrAC1 = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 115 |
$this->assertFalse( |
| 116 |
$dbrAC1->getFlag( $dbw::DBO_TRX ), |
| 117 |
"No DBO_TRX with CONN_TRX_AUTOCOMMIT" |
| 118 |
); |
| 119 |
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" ); |
| 120 |
$this->assertUnsharedHandle( $dbr, $dbrAC1, "CONN_TRX_AUTOCOMMIT separate connection" ); |
| 121 |
|
| 122 |
$dbwAC2 = $lb->getConnection( DB_PRIMARY, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 123 |
$dbwAC2->ensureConnection(); |
| 124 |
$this->assertSharedHandle( $dbwAC2, $dbwAC1, "CONN_TRX_AUTOCOMMIT reuses connections" ); |
| 125 |
|
| 126 |
$dbrAC2 = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 127 |
$dbrAC2->ensureConnection(); |
| 128 |
$this->assertSharedHandle( $dbrAC2, $dbrAC1, "CONN_TRX_AUTOCOMMIT reuses connections" ); |
| 129 |
} |
| 130 |
|
| 131 |
$lb->closeAll( __METHOD__ ); |
| 132 |
} |
| 133 |
|
| 134 |
public function testWithReplica() { |
| 135 |
|
| 136 |
$lb = $this->newMultiServerLocalLoadBalancer( [], [ 'flags' => DBO_TRX ] ); |
| 137 |
|
| 138 |
$this->assertSame( 8, $lb->getServerCount() ); |
| 139 |
$this->assertTrue( $lb->hasReplicaServers() ); |
| 140 |
$this->assertTrue( $lb->hasStreamingReplicaServers() ); |
| 141 |
$this->assertSame( 'main-test-cluster', $lb->getClusterName() ); |
| 142 |
|
| 143 |
for ( $i = 0; $i < $lb->getServerCount(); ++$i ) { |
| 144 |
$this->assertIsString( $lb->getServerName( $i ) ); |
| 145 |
$this->assertIsArray( $lb->getServerInfo( $i ) ); |
| 146 |
$this->assertIsString( $lb->getServerType( $i ) ); |
| 147 |
$this->assertIsArray( $lb->getServerAttributes( $i ) ); |
| 148 |
} |
| 149 |
|
| 150 |
$dbw = $lb->getConnection( DB_PRIMARY ); |
| 151 |
$dbw->ensureConnection(); |
| 152 |
$wConn = TestingAccessWrapper::newFromObject( $dbw )->conn; |
| 153 |
$wConnWrap = TestingAccessWrapper::newFromObject( $wConn ); |
| 154 |
|
| 155 |
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on primary" ); |
| 156 |
$this->assertWriteAllowed( $dbw ); |
| 157 |
|
| 158 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 159 |
$dbr->ensureConnection(); |
| 160 |
$rConn = TestingAccessWrapper::newFromObject( $dbr )->conn; |
| 161 |
$rConnWrap = TestingAccessWrapper::newFromObject( $rConn ); |
| 162 |
|
| 163 |
$this->assertTrue( $dbr->isReadOnly(), 'replica shows as replica' ); |
| 164 |
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" ); |
| 165 |
$this->assertSame( $dbr->getLBInfo( 'serverIndex' ), $lb->getReaderIndex() ); |
| 166 |
|
| 167 |
if ( !$lb->getServerAttributes( ServerInfo::WRITER_INDEX )[Database::ATTR_DB_LEVEL_LOCKING] ) { |
| 168 |
$dbwAC1 = $lb->getConnection( DB_PRIMARY, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 169 |
$this->assertFalse( |
| 170 |
$dbwAC1->getFlag( $dbw::DBO_TRX ), |
| 171 |
"No DBO_TRX with CONN_TRX_AUTOCOMMIT" |
| 172 |
); |
| 173 |
$this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" ); |
| 174 |
$this->assertUnsharedHandle( $dbw, $dbwAC1, "CONN_TRX_AUTOCOMMIT separate connection" ); |
| 175 |
|
| 176 |
$dbrAC1 = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 177 |
$this->assertFalse( |
| 178 |
$dbrAC1->getFlag( $dbw::DBO_TRX ), |
| 179 |
"No DBO_TRX with CONN_TRX_AUTOCOMMIT" |
| 180 |
); |
| 181 |
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" ); |
| 182 |
$this->assertUnsharedHandle( $dbr, $dbrAC1, "CONN_TRX_AUTOCOMMIT separate connection" ); |
| 183 |
|
| 184 |
$dbwAC2 = $lb->getConnection( DB_PRIMARY, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 185 |
$dbwAC2->ensureConnection(); |
| 186 |
$this->assertSharedHandle( $dbwAC2, $dbwAC1, "CONN_TRX_AUTOCOMMIT reuses connections" ); |
| 187 |
|
| 188 |
$dbrAC2 = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 189 |
$dbrAC2->ensureConnection(); |
| 190 |
$this->assertSharedHandle( $dbrAC2, $dbrAC1, "CONN_TRX_AUTOCOMMIT reuses connections" ); |
| 191 |
} |
| 192 |
|
| 193 |
$lb->closeAll( __METHOD__ ); |
| 194 |
} |
| 195 |
|
| 196 |
private function assertSharedHandle( DBConnRef $connRef1, DBConnRef $connRef2, $msg ) { |
| 197 |
$connRef1Wrap = TestingAccessWrapper::newFromObject( $connRef1 ); |
| 198 |
$connRef2Wrap = TestingAccessWrapper::newFromObject( $connRef2 ); |
| 199 |
$this->assertSame( $connRef1Wrap->conn, $connRef2Wrap->conn, $msg ); |
| 200 |
} |
| 201 |
|
| 202 |
private function assertUnsharedHandle( DBConnRef $connRef1, DBConnRef $connRef2, $msg ) { |
| 203 |
$connRef1Wrap = TestingAccessWrapper::newFromObject( $connRef1 ); |
| 204 |
$connRef2Wrap = TestingAccessWrapper::newFromObject( $connRef2 ); |
| 205 |
$this->assertNotSame( $connRef1Wrap->conn, $connRef2Wrap->conn, $msg ); |
| 206 |
} |
| 207 |
|
| 208 |
private function newSingleServerLocalLoadBalancer() { |
| 209 |
global $wgDBname; |
| 210 |
|
| 211 |
return new LoadBalancer( [ |
| 212 |
'servers' => [ $this->makeServerConfig() ], |
| 213 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ), |
| 214 |
'cliMode' => false |
| 215 |
] ); |
| 216 |
} |
| 217 |
|
| 218 |
private function newMultiServerLocalLoadBalancer( |
| 219 |
$lbExtra = [], $srvExtra = [], $masterOnly = false |
| 220 |
) { |
| 221 |
global $wgDBserver, $wgDBport, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; |
| 222 |
global $wgSQLiteDataDir; |
| 223 |
|
| 224 |
$servers = [ |
| 225 |
|
| 226 |
0 => $srvExtra + [ |
| 227 |
'serverName' => 'db0', |
| 228 |
'host' => $wgDBserver, |
| 229 |
'port' => $wgDBport, |
| 230 |
'dbname' => $wgDBname, |
| 231 |
'tablePrefix' => self::dbPrefix(), |
| 232 |
'user' => $wgDBuser, |
| 233 |
'password' => $wgDBpassword, |
| 234 |
'type' => $wgDBtype, |
| 235 |
'dbDirectory' => $wgSQLiteDataDir, |
| 236 |
'load' => $masterOnly ? 100 : 0, |
| 237 |
], |
| 238 |
|
| 239 |
1 => $srvExtra + [ |
| 240 |
'serverName' => 'db1', |
| 241 |
'host' => $wgDBserver, |
| 242 |
'port' => $wgDBport, |
| 243 |
'dbname' => $wgDBname, |
| 244 |
'tablePrefix' => self::dbPrefix(), |
| 245 |
'user' => $wgDBuser, |
| 246 |
'password' => $wgDBpassword, |
| 247 |
'type' => $wgDBtype, |
| 248 |
'dbDirectory' => $wgSQLiteDataDir, |
| 249 |
'load' => $masterOnly ? 0 : 100, |
| 250 |
], |
| 251 |
2 => $srvExtra + [ |
| 252 |
'serverName' => 'db2', |
| 253 |
'host' => $wgDBserver, |
| 254 |
'port' => $wgDBport, |
| 255 |
'dbname' => $wgDBname, |
| 256 |
'tablePrefix' => self::dbPrefix(), |
| 257 |
'user' => $wgDBuser, |
| 258 |
'password' => $wgDBpassword, |
| 259 |
'type' => $wgDBtype, |
| 260 |
'dbDirectory' => $wgSQLiteDataDir, |
| 261 |
'load' => $masterOnly ? 0 : 100, |
| 262 |
], |
| 263 |
|
| 264 |
3 => $srvExtra + [ |
| 265 |
'serverName' => 'db3', |
| 266 |
'host' => $wgDBserver, |
| 267 |
'port' => $wgDBport, |
| 268 |
'dbname' => $wgDBname, |
| 269 |
'tablePrefix' => self::dbPrefix(), |
| 270 |
'user' => $wgDBuser, |
| 271 |
'password' => $wgDBpassword, |
| 272 |
'type' => $wgDBtype, |
| 273 |
'dbDirectory' => $wgSQLiteDataDir, |
| 274 |
'load' => 0, |
| 275 |
'groupLoads' => [ |
| 276 |
'foo' => 100, |
| 277 |
'bar' => 100 |
| 278 |
], |
| 279 |
], |
| 280 |
|
| 281 |
4 => $srvExtra + [ |
| 282 |
'serverName' => 'db4', |
| 283 |
'host' => $wgDBserver, |
| 284 |
'port' => $wgDBport, |
| 285 |
'dbname' => $wgDBname, |
| 286 |
'tablePrefix' => self::dbPrefix(), |
| 287 |
'user' => $wgDBuser, |
| 288 |
'password' => $wgDBpassword, |
| 289 |
'type' => $wgDBtype, |
| 290 |
'dbDirectory' => $wgSQLiteDataDir, |
| 291 |
'load' => 0, |
| 292 |
'groupLoads' => [ |
| 293 |
'baz' => 100 |
| 294 |
], |
| 295 |
], |
| 296 |
5 => $srvExtra + [ |
| 297 |
'serverName' => 'db5', |
| 298 |
'host' => $wgDBserver, |
| 299 |
'port' => $wgDBport, |
| 300 |
'dbname' => $wgDBname, |
| 301 |
'tablePrefix' => self::dbPrefix(), |
| 302 |
'user' => $wgDBuser, |
| 303 |
'password' => $wgDBpassword, |
| 304 |
'type' => $wgDBtype, |
| 305 |
'dbDirectory' => $wgSQLiteDataDir, |
| 306 |
'load' => 0, |
| 307 |
'groupLoads' => [ |
| 308 |
'baz' => 100 |
| 309 |
], |
| 310 |
], |
| 311 |
|
| 312 |
6 => $srvExtra + [ |
| 313 |
'serverName' => 'db6', |
| 314 |
'host' => $wgDBserver, |
| 315 |
'port' => $wgDBport, |
| 316 |
'dbname' => $wgDBname, |
| 317 |
'tablePrefix' => self::dbPrefix(), |
| 318 |
'user' => $wgDBuser, |
| 319 |
'password' => $wgDBpassword, |
| 320 |
'type' => $wgDBtype, |
| 321 |
'dbDirectory' => $wgSQLiteDataDir, |
| 322 |
'load' => 0, |
| 323 |
'groupLoads' => [ |
| 324 |
'vslow' => 100 |
| 325 |
], |
| 326 |
], |
| 327 |
|
| 328 |
7 => $srvExtra + [ |
| 329 |
'serverName' => 'db7', |
| 330 |
'host' => $wgDBserver, |
| 331 |
'port' => $wgDBport, |
| 332 |
'dbname' => $wgDBname, |
| 333 |
'tablePrefix' => self::dbPrefix(), |
| 334 |
'user' => $wgDBuser, |
| 335 |
'password' => $wgDBpassword, |
| 336 |
'type' => $wgDBtype, |
| 337 |
'dbDirectory' => $wgSQLiteDataDir, |
| 338 |
'load' => 0, |
| 339 |
'groupLoads' => [ |
| 340 |
'archive' => 100 |
| 341 |
], |
| 342 |
'is static' => true |
| 343 |
] |
| 344 |
]; |
| 345 |
|
| 346 |
return new LoadBalancer( $lbExtra + [ |
| 347 |
'servers' => $servers, |
| 348 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ), |
| 349 |
'logger' => MediaWiki\Logger\LoggerFactory::getInstance( 'rdbms' ), |
| 350 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ], |
| 351 |
'clusterName' => 'main-test-cluster' |
| 352 |
] ); |
| 353 |
} |
| 354 |
|
| 355 |
private function assertWriteAllowed( IMaintainableDatabase $db ) { |
| 356 |
$table = $db->tableName( 'some_table' ); |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
$useAtomicSection = in_array( $db->getType(), [ 'sqlite', 'postgres' ], true ); |
| 361 |
|
| 362 |
try { |
| 363 |
$db->dropTable( 'some_table' ); |
| 364 |
$this->assertNotEquals( TransactionManager::STATUS_TRX_ERROR, $db->trxStatus() ); |
| 365 |
|
| 366 |
if ( $useAtomicSection ) { |
| 367 |
$db->startAtomic( __METHOD__ ); |
| 368 |
} |
| 369 |
|
| 370 |
$this->assertNotFalse( |
| 371 |
$db->query( "CREATE TABLE $table (id INT, time INT)", __METHOD__ ), |
| 372 |
"table created" |
| 373 |
); |
| 374 |
$this->assertNotEquals( TransactionManager::STATUS_TRX_ERROR, $db->trxStatus() ); |
| 375 |
$this->assertNotFalse( |
| 376 |
$db->query( "DELETE FROM $table WHERE id=57634126", __METHOD__ ), |
| 377 |
"delete query" |
| 378 |
); |
| 379 |
$this->assertNotEquals( TransactionManager::STATUS_TRX_ERROR, $db->trxStatus() ); |
| 380 |
} finally { |
| 381 |
if ( !$useAtomicSection ) { |
| 382 |
|
| 383 |
$db->dropTable( 'some_table' ); |
| 384 |
} |
| 385 |
|
| 386 |
$db->rollback( __METHOD__, 'flush' ); |
| 387 |
$this->assertNotEquals( TransactionManager::STATUS_TRX_ERROR, $db->trxStatus() ); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
public function testServerAttributes() { |
| 392 |
$servers = [ |
| 393 |
[ |
| 394 |
'dbname' => 'my_unittest_wiki', |
| 395 |
'tablePrefix' => self::DB_PREFIX, |
| 396 |
'type' => 'sqlite', |
| 397 |
'dbDirectory' => "some_directory", |
| 398 |
'load' => 0 |
| 399 |
] |
| 400 |
]; |
| 401 |
|
| 402 |
$lb = new LoadBalancer( [ |
| 403 |
'servers' => $servers, |
| 404 |
'localDomain' => new DatabaseDomain( 'my_unittest_wiki', null, self::DB_PREFIX ), |
| 405 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ] |
| 406 |
] ); |
| 407 |
|
| 408 |
$this->assertTrue( $lb->getServerAttributes( 0 )[Database::ATTR_DB_LEVEL_LOCKING] ); |
| 409 |
|
| 410 |
$servers = [ |
| 411 |
[ |
| 412 |
'serverName' => 'db1', |
| 413 |
'host' => 'db1001', |
| 414 |
'user' => 'wikiuser', |
| 415 |
'password' => 'none', |
| 416 |
'dbname' => 'my_unittest_wiki', |
| 417 |
'tablePrefix' => self::DB_PREFIX, |
| 418 |
'type' => 'mysql', |
| 419 |
'load' => 100 |
| 420 |
], |
| 421 |
[ |
| 422 |
'serverName' => 'db2', |
| 423 |
'host' => 'db1002', |
| 424 |
'user' => 'wikiuser', |
| 425 |
'password' => 'none', |
| 426 |
'dbname' => 'my_unittest_wiki', |
| 427 |
'tablePrefix' => self::DB_PREFIX, |
| 428 |
'type' => 'mysql', |
| 429 |
'load' => 100 |
| 430 |
] |
| 431 |
]; |
| 432 |
|
| 433 |
$lb = new LoadBalancer( [ |
| 434 |
'servers' => $servers, |
| 435 |
'localDomain' => new DatabaseDomain( 'my_unittest_wiki', null, self::DB_PREFIX ), |
| 436 |
'loadMonitor' => [ 'class' => LoadMonitorNull::class ] |
| 437 |
] ); |
| 438 |
|
| 439 |
$this->assertFalse( $lb->getServerAttributes( 1 )[Database::ATTR_DB_LEVEL_LOCKING] ); |
| 440 |
} |
| 441 |
|
| 442 |
public function testOpenConnection() { |
| 443 |
$lb = $this->newSingleServerLocalLoadBalancer(); |
| 444 |
$i = ServerInfo::WRITER_INDEX; |
| 445 |
|
| 446 |
$this->assertFalse( $lb->getAnyOpenConnection( $i ) ); |
| 447 |
$this->assertFalse( $lb->getAnyOpenConnection( $i, $lb::CONN_TRX_AUTOCOMMIT ) ); |
| 448 |
|
| 449 |
|
| 450 |
$raConnRef1 = $lb->getConnection( $i ); |
| 451 |
$raConnRef1->ensureConnection(); |
| 452 |
$raConnRef1Wrapper = TestingAccessWrapper::newFromObject( $raConnRef1 ); |
| 453 |
$raConnRef2 = $lb->getConnection( $i ); |
| 454 |
$raConnRef2->ensureConnection(); |
| 455 |
$raConnRef2Wrapper = TestingAccessWrapper::newFromObject( $raConnRef2 ); |
| 456 |
|
| 457 |
$this->assertNotNull( $raConnRef1Wrapper->conn ); |
| 458 |
$this->assertSame( $raConnRef1Wrapper->conn, $raConnRef2Wrapper->conn ); |
| 459 |
$this->assertTrue( $raConnRef1Wrapper->conn->getFlag( DBO_TRX ) ); |
| 460 |
|
| 461 |
|
| 462 |
$acConnRef1 = $lb->getConnection( $i, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 463 |
$acConnRef1->ensureConnection(); |
| 464 |
$acConnRef1Wrapper = TestingAccessWrapper::newFromObject( $acConnRef1 ); |
| 465 |
$acConnRef2 = $lb->getConnection( $i, [], false, $lb::CONN_TRX_AUTOCOMMIT ); |
| 466 |
$acConnRef2->ensureConnection(); |
| 467 |
$acConnRef2Wrapper = TestingAccessWrapper::newFromObject( $acConnRef2 ); |
| 468 |
|
| 469 |
$this->assertNotNull( $acConnRef1Wrapper->conn ); |
| 470 |
$this->assertSame( $acConnRef1Wrapper->conn, $acConnRef2Wrapper->conn ); |
| 471 |
|
| 472 |
$this->assertNotFalse( $lb->getAnyOpenConnection( $i ) ); |
| 473 |
|
| 474 |
$lb->closeAll( __METHOD__ ); |
| 475 |
} |
| 476 |
|
| 477 |
public function testReconfigure() { |
| 478 |
$serverA = $this->makeServerConfig(); |
| 479 |
$serverA['serverName'] = 'test_one'; |
| 480 |
|
| 481 |
$serverB = $this->makeServerConfig(); |
| 482 |
$serverB['serverName'] = 'test_two'; |
| 483 |
$conf = [ |
| 484 |
'servers' => [ $serverA, $serverB ], |
| 485 |
'clusterName' => 'A', |
| 486 |
'localDomain' => $this->getDb()->getDomainID() |
| 487 |
]; |
| 488 |
|
| 489 |
$lb = new LoadBalancer( $conf ); |
| 490 |
$this->assertSame( 2, $lb->getServerCount() ); |
| 491 |
|
| 492 |
$con = $lb->getConnectionInternal( DB_PRIMARY ); |
| 493 |
$ref = $lb->getConnection( DB_PRIMARY ); |
| 494 |
|
| 495 |
$this->assertTrue( $con->isOpen() ); |
| 496 |
$this->assertTrue( $ref->isOpen() ); |
| 497 |
|
| 498 |
|
| 499 |
$conf['servers'] = [ $serverA ]; |
| 500 |
$lb->reconfigure( $conf ); |
| 501 |
$this->assertSame( 1, $lb->getServerCount() ); |
| 502 |
|
| 503 |
|
| 504 |
$this->assertTrue( $con->isOpen() ); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
$this->assertTrue( $ref->isOpen() ); |
| 509 |
|
| 510 |
|
| 511 |
$this->assertFalse( $con->isOpen() ); |
| 512 |
} |
| 513 |
|
| 514 |
public function testTransactionCallbackChains() { |
| 515 |
global $wgDBserver, $wgDBport, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype; |
| 516 |
global $wgSQLiteDataDir; |
| 517 |
|
| 518 |
$servers = [ |
| 519 |
[ |
| 520 |
'host' => $wgDBserver, |
| 521 |
'port' => $wgDBport, |
| 522 |
'dbname' => $wgDBname, |
| 523 |
'tablePrefix' => self::dbPrefix(), |
| 524 |
'user' => $wgDBuser, |
| 525 |
'password' => $wgDBpassword, |
| 526 |
'type' => $wgDBtype, |
| 527 |
'dbDirectory' => $wgSQLiteDataDir, |
| 528 |
'load' => 0, |
| 529 |
'flags' => DBO_TRX |
| 530 |
], |
| 531 |
]; |
| 532 |
|
| 533 |
$lb = new LoadBalancer( [ |
| 534 |
'servers' => $servers, |
| 535 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ) |
| 536 |
] ); |
| 537 |
|
| 538 |
$lbWrapper = TestingAccessWrapper::newFromObject( $lb ); |
| 539 |
|
| 540 |
$conn1 = $lb->getConnection( ServerInfo::WRITER_INDEX, [], false ); |
| 541 |
$count = iterator_count( $lbWrapper->getOpenPrimaryConnections() ); |
| 542 |
$this->assertSame( 0, $count, 'Connection handle count' ); |
| 543 |
$conn1->getServerName(); |
| 544 |
$count = iterator_count( $lbWrapper->getOpenPrimaryConnections() ); |
| 545 |
$this->assertSame( 0, $count, 'Connection handle count' ); |
| 546 |
$conn1->ensureConnection(); |
| 547 |
|
| 548 |
$conn2 = $lb->getConnection( ServerInfo::WRITER_INDEX, [], '' ); |
| 549 |
$count = iterator_count( $lbWrapper->getOpenPrimaryConnections() ); |
| 550 |
$this->assertSame( 1, $count, 'Connection handle count' ); |
| 551 |
$conn2->getServerName(); |
| 552 |
$count = iterator_count( $lbWrapper->getOpenPrimaryConnections() ); |
| 553 |
$this->assertSame( 1, $count, 'Connection handle count' ); |
| 554 |
$conn2->ensureConnection(); |
| 555 |
|
| 556 |
$count = iterator_count( $lbWrapper->getOpenPrimaryConnections() ); |
| 557 |
$this->assertSame( 1, $count, 'Connection handle count' ); |
| 558 |
|
| 559 |
$tlCalls = 0; |
| 560 |
$lb->setTransactionListener( 'test-listener', static function () use ( &$tlCalls ) { |
| 561 |
++$tlCalls; |
| 562 |
} ); |
| 563 |
|
| 564 |
$lb->beginPrimaryChanges( __METHOD__ ); |
| 565 |
$bc = array_fill_keys( [ 'a', 'b', 'c', 'd' ], 0 ); |
| 566 |
$conn1->onTransactionPreCommitOrIdle( static function () use ( &$bc, $conn1, $conn2 ) { |
| 567 |
$bc['a'] = 1; |
| 568 |
$conn2->onTransactionPreCommitOrIdle( static function () use ( &$bc, $conn1 ) { |
| 569 |
$bc['b'] = 1; |
| 570 |
$conn1->onTransactionPreCommitOrIdle( static function () use ( &$bc, $conn1 ) { |
| 571 |
$bc['c'] = 1; |
| 572 |
$conn1->onTransactionPreCommitOrIdle( static function () use ( &$bc ) { |
| 573 |
$bc['d'] = 1; |
| 574 |
} ); |
| 575 |
} ); |
| 576 |
} ); |
| 577 |
} ); |
| 578 |
$lb->finalizePrimaryChanges(); |
| 579 |
$lb->approvePrimaryChanges( 0 ); |
| 580 |
$lb->commitPrimaryChanges( __METHOD__ ); |
| 581 |
$lb->runPrimaryTransactionIdleCallbacks(); |
| 582 |
$lb->runPrimaryTransactionListenerCallbacks(); |
| 583 |
|
| 584 |
$this->assertSame( array_fill_keys( [ 'a', 'b', 'c', 'd' ], 1 ), $bc ); |
| 585 |
$this->assertSame( 1, $tlCalls ); |
| 586 |
|
| 587 |
$tlCalls = 0; |
| 588 |
$lb->beginPrimaryChanges( __METHOD__ ); |
| 589 |
$ac = array_fill_keys( [ 'a', 'b', 'c', 'd' ], 0 ); |
| 590 |
$conn1->onTransactionCommitOrIdle( static function () use ( &$ac, $conn1, $conn2 ) { |
| 591 |
$ac['a'] = 1; |
| 592 |
$conn2->onTransactionCommitOrIdle( static function () use ( &$ac, $conn1 ) { |
| 593 |
$ac['b'] = 1; |
| 594 |
$conn1->onTransactionCommitOrIdle( static function () use ( &$ac, $conn1 ) { |
| 595 |
$ac['c'] = 1; |
| 596 |
$conn1->onTransactionCommitOrIdle( static function () use ( &$ac ) { |
| 597 |
$ac['d'] = 1; |
| 598 |
} ); |
| 599 |
} ); |
| 600 |
} ); |
| 601 |
} ); |
| 602 |
$lb->finalizePrimaryChanges(); |
| 603 |
$lb->approvePrimaryChanges( 0 ); |
| 604 |
$lb->commitPrimaryChanges( __METHOD__ ); |
| 605 |
$lb->runPrimaryTransactionIdleCallbacks(); |
| 606 |
$lb->runPrimaryTransactionListenerCallbacks(); |
| 607 |
|
| 608 |
$this->assertSame( array_fill_keys( [ 'a', 'b', 'c', 'd' ], 1 ), $ac ); |
| 609 |
$this->assertSame( 1, $tlCalls ); |
| 610 |
|
| 611 |
$conn1->lock( 'test_lock_' . mt_rand(), __METHOD__, 0 ); |
| 612 |
$lb->flushPrimarySessions( __METHOD__ ); |
| 613 |
$this->assertSame( TransactionManager::STATUS_TRX_NONE, $conn1->trxStatus() ); |
| 614 |
$this->assertSame( TransactionManager::STATUS_TRX_NONE, $conn2->trxStatus() ); |
| 615 |
} |
| 616 |
|
| 617 |
public function testForbiddenWritesNoRef() { |
| 618 |
|
| 619 |
$lb = $this->newMultiServerLocalLoadBalancer( [], [ 'flags' => DBO_TRX ] ); |
| 620 |
|
| 621 |
$dbr = $lb->getConnection( DB_REPLICA ); |
| 622 |
$this->assertTrue( $dbr->isReadOnly(), 'replica shows as replica' ); |
| 623 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 624 |
$dbr->newDeleteQueryBuilder() |
| 625 |
->deleteFrom( 'some_table' ) |
| 626 |
->where( [ 'id' => 57634126 ] ) |
| 627 |
->caller( __METHOD__ ) |
| 628 |
->execute(); |
| 629 |
|
| 630 |
|
| 631 |
$lb->closeAll( __METHOD__ ); |
| 632 |
} |
| 633 |
|
| 634 |
public function testDBConnRefReadsMasterAndReplicaRoles() { |
| 635 |
$lb = $this->newSingleServerLocalLoadBalancer(); |
| 636 |
|
| 637 |
$rConn = $lb->getConnection( DB_REPLICA ); |
| 638 |
$wConn = $lb->getConnection( DB_PRIMARY ); |
| 639 |
$wConn2 = $lb->getConnection( 0 ); |
| 640 |
|
| 641 |
$v = [ 'value' => '1', '1' ]; |
| 642 |
$sql = 'SELECT MAX(1) AS value'; |
| 643 |
foreach ( [ $rConn, $wConn, $wConn2 ] as $conn ) { |
| 644 |
$conn->clearFlag( $conn::DBO_TRX ); |
| 645 |
|
| 646 |
$res = $conn->query( $sql, __METHOD__ ); |
| 647 |
$this->assertEquals( $v, $res->fetchRow() ); |
| 648 |
|
| 649 |
$res = $conn->query( $sql, __METHOD__, $conn::QUERY_REPLICA_ROLE ); |
| 650 |
$this->assertEquals( $v, $res->fetchRow() ); |
| 651 |
} |
| 652 |
|
| 653 |
$wConn->getScopedLockAndFlush( 'key', __METHOD__, 1 ); |
| 654 |
$wConn2->getScopedLockAndFlush( 'key2', __METHOD__, 1 ); |
| 655 |
} |
| 656 |
|
| 657 |
public function testDBConnRefWritesReplicaRole() { |
| 658 |
$lb = $this->newSingleServerLocalLoadBalancer(); |
| 659 |
|
| 660 |
$rConn = $lb->getConnection( DB_REPLICA ); |
| 661 |
|
| 662 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 663 |
$rConn->query( 'DELETE FROM sometesttable WHERE 1=0' ); |
| 664 |
} |
| 665 |
|
| 666 |
public function testDBConnRefWritesReplicaRoleIndex() { |
| 667 |
$lb = $this->newMultiServerLocalLoadBalancer(); |
| 668 |
|
| 669 |
$rConn = $lb->getConnection( 1 ); |
| 670 |
|
| 671 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 672 |
$rConn->query( 'DELETE FROM sometesttable WHERE 1=0' ); |
| 673 |
} |
| 674 |
|
| 675 |
public function testDBConnRefWritesReplicaRoleInsert() { |
| 676 |
$lb = $this->newMultiServerLocalLoadBalancer(); |
| 677 |
|
| 678 |
$rConn = $lb->getConnection( DB_REPLICA ); |
| 679 |
|
| 680 |
$this->expectException( DBReadOnlyRoleError::class ); |
| 681 |
$rConn->insert( 'test', [ 't' => 1 ], __METHOD__ ); |
| 682 |
} |
| 683 |
|
| 684 |
public function testGetConnectionRefDefaultGroup() { |
| 685 |
$lb = $this->newMultiServerLocalLoadBalancer( [ 'defaultGroup' => 'vslow' ] ); |
| 686 |
$lbWrapper = TestingAccessWrapper::newFromObject( $lb ); |
| 687 |
|
| 688 |
$rVslow = $lb->getConnection( DB_REPLICA ); |
| 689 |
$vslowIndexPicked = $rVslow->getLBInfo( 'serverIndex' ); |
| 690 |
|
| 691 |
$this->assertSame( $vslowIndexPicked, $lbWrapper->getExistingReaderIndex( 'vslow' ) ); |
| 692 |
} |
| 693 |
|
| 694 |
public function testGetConnectionRefUnknownDefaultGroup() { |
| 695 |
$lb = $this->newMultiServerLocalLoadBalancer( [ 'defaultGroup' => 'invalid' ] ); |
| 696 |
|
| 697 |
$this->assertInstanceOf( |
| 698 |
IDatabase::class, |
| 699 |
$lb->getConnection( DB_REPLICA ) |
| 700 |
); |
| 701 |
} |
| 702 |
|
| 703 |
public function testQueryGroupIndex() { |
| 704 |
$lb = $this->newMultiServerLocalLoadBalancer( [ 'defaultGroup' => false ] ); |
| 705 |
|
| 706 |
$lbWrapper = TestingAccessWrapper::newFromObject( $lb ); |
| 707 |
|
| 708 |
$rGeneric = $lb->getConnection( DB_REPLICA ); |
| 709 |
$mainIndexPicked = $rGeneric->getLBInfo( 'serverIndex' ); |
| 710 |
|
| 711 |
$this->assertSame( |
| 712 |
$mainIndexPicked, |
| 713 |
$lbWrapper->getExistingReaderIndex( $lb::GROUP_GENERIC ) |
| 714 |
); |
| 715 |
$this->assertContains( $mainIndexPicked, [ 1, 2 ] ); |
| 716 |
for ( $i = 0; $i < 300; ++$i ) { |
| 717 |
$rLog = $lb->getConnection( DB_REPLICA, [] ); |
| 718 |
$this->assertSame( |
| 719 |
$mainIndexPicked, |
| 720 |
$rLog->getLBInfo( 'serverIndex' ), |
| 721 |
"Main index unchanged" ); |
| 722 |
} |
| 723 |
|
| 724 |
$rRC = $lb->getConnection( DB_REPLICA, [ 'foo' ] ); |
| 725 |
$rWL = $lb->getConnection( DB_REPLICA, [ 'bar' ] ); |
| 726 |
$rRCMaint = $lb->getMaintenanceConnectionRef( DB_REPLICA, [ 'foo' ] ); |
| 727 |
$rWLMaint = $lb->getMaintenanceConnectionRef( DB_REPLICA, [ 'bar' ] ); |
| 728 |
|
| 729 |
$this->assertSame( 3, $rRC->getLBInfo( 'serverIndex' ) ); |
| 730 |
$this->assertSame( 3, $rWL->getLBInfo( 'serverIndex' ) ); |
| 731 |
$this->assertSame( 3, $rRCMaint->getLBInfo( 'serverIndex' ) ); |
| 732 |
$this->assertSame( 3, $rWLMaint->getLBInfo( 'serverIndex' ) ); |
| 733 |
|
| 734 |
$rLog = $lb->getConnection( DB_REPLICA, [ 'baz', 'bar' ] ); |
| 735 |
$logIndexPicked = $rLog->getLBInfo( 'serverIndex' ); |
| 736 |
|
| 737 |
$this->assertSame( $logIndexPicked, $lbWrapper->getExistingReaderIndex( 'baz' ) ); |
| 738 |
$this->assertContains( $logIndexPicked, [ 4, 5 ] ); |
| 739 |
|
| 740 |
for ( $i = 0; $i < 300; ++$i ) { |
| 741 |
$rLog = $lb->getConnection( DB_REPLICA, [ 'baz', 'bar' ] ); |
| 742 |
$this->assertSame( |
| 743 |
$logIndexPicked, $rLog->getLBInfo( 'serverIndex' ), "Index unchanged" ); |
| 744 |
} |
| 745 |
|
| 746 |
$rVslow = $lb->getConnection( DB_REPLICA, [ 'vslow', 'baz' ] ); |
| 747 |
$vslowIndexPicked = $rVslow->getLBInfo( 'serverIndex' ); |
| 748 |
|
| 749 |
$this->assertSame( $vslowIndexPicked, $lbWrapper->getExistingReaderIndex( 'vslow' ) ); |
| 750 |
$this->assertSame( 6, $vslowIndexPicked ); |
| 751 |
} |
| 752 |
|
| 753 |
public function testNonZeroMasterLoad() { |
| 754 |
$lb = $this->newMultiServerLocalLoadBalancer( [], [ 'flags' => DBO_DEFAULT ], true ); |
| 755 |
|
| 756 |
$rGeneric = $lb->getConnection( DB_REPLICA ); |
| 757 |
$this->assertSame( ServerInfo::WRITER_INDEX, $rGeneric->getLBInfo( 'serverIndex' ) ); |
| 758 |
} |
| 759 |
|
| 760 |
public function testSetDomainAliases() { |
| 761 |
$lb = $this->newMultiServerLocalLoadBalancer(); |
| 762 |
$origDomain = $lb->getLocalDomainID(); |
| 763 |
|
| 764 |
$this->assertSame( $origDomain, $lb->resolveDomainID( false ) ); |
| 765 |
$this->assertSame( "db-prefix_", $lb->resolveDomainID( "db-prefix_" ) ); |
| 766 |
|
| 767 |
$lb->setDomainAliases( [ |
| 768 |
'alias-db' => 'realdb', |
| 769 |
'alias-db-prefix_' => 'realdb-realprefix_' |
| 770 |
] ); |
| 771 |
|
| 772 |
$this->assertSame( 'realdb', $lb->resolveDomainID( 'alias-db' ) ); |
| 773 |
$this->assertSame( "realdb-realprefix_", $lb->resolveDomainID( "alias-db-prefix_" ) ); |
| 774 |
} |
| 775 |
|
| 776 |
public function testClusterName() { |
| 777 |
global $wgDBname; |
| 778 |
$chronologyProtector = $this->createMock( ChronologyProtector::class ); |
| 779 |
$lb1 = new LoadBalancer( [ |
| 780 |
'servers' => [ $this->makeServerConfig() ], |
| 781 |
'logger' => MediaWiki\Logger\LoggerFactory::getInstance( 'rdbms' ), |
| 782 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ), |
| 783 |
'chronologyProtector' => $chronologyProtector, |
| 784 |
'clusterName' => 'xx' |
| 785 |
] ); |
| 786 |
$this->assertSame( 'xx', $lb1->getClusterName() ); |
| 787 |
|
| 788 |
$lb2 = new LoadBalancer( [ |
| 789 |
'servers' => [ $this->makeServerConfig() ], |
| 790 |
'logger' => MediaWiki\Logger\LoggerFactory::getInstance( 'rdbms' ), |
| 791 |
'localDomain' => new DatabaseDomain( $wgDBname, null, self::dbPrefix() ), |
| 792 |
'chronologyProtector' => $chronologyProtector, |
| 793 |
'clusterName' => null |
| 794 |
] ); |
| 795 |
$this->assertSame( 'testhost', $lb2->getClusterName() ); |
| 796 |
} |
| 797 |
} |