MediaWiki  1.30.2
LoadBalancerTest.php
Go to the documentation of this file.
1 <?php
2 
4 
27  public function testLBSimpleServer() {
29 
30  $servers = [
31  [
32  'host' => $wgDBserver,
33  'dbname' => $wgDBname,
34  'user' => $wgDBuser,
35  'password' => $wgDBpassword,
36  'type' => $wgDBtype,
37  'dbDirectory' => $wgSQLiteDataDir,
38  'load' => 0,
39  'flags' => DBO_TRX // REPEATABLE-READ for consistency
40  ],
41  ];
42 
43  $lb = new LoadBalancer( [
44  'servers' => $servers,
45  'localDomain' => wfWikiID()
46  ] );
47 
48  $dbw = $lb->getConnection( DB_MASTER );
49  $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
50  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
51 
52  $dbr = $lb->getConnection( DB_REPLICA );
53  $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
54  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
55 
56  $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
57  $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
58  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
59  $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
60 
61  $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
62  $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
63  $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
64  $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
65 
66  $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
67  $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
68 
69  $lb->closeAll();
70  }
71 
72  public function testLBSimpleServers() {
74 
75  $servers = [
76  [ // master
77  'host' => $wgDBserver,
78  'dbname' => $wgDBname,
79  'user' => $wgDBuser,
80  'password' => $wgDBpassword,
81  'type' => $wgDBtype,
82  'dbDirectory' => $wgSQLiteDataDir,
83  'load' => 0,
84  'flags' => DBO_TRX // REPEATABLE-READ for consistency
85  ],
86  [ // emulated slave
87  'host' => $wgDBserver,
88  'dbname' => $wgDBname,
89  'user' => $wgDBuser,
90  'password' => $wgDBpassword,
91  'type' => $wgDBtype,
92  'dbDirectory' => $wgSQLiteDataDir,
93  'load' => 100,
94  'flags' => DBO_TRX // REPEATABLE-READ for consistency
95  ]
96  ];
97 
98  $lb = new LoadBalancer( [
99  'servers' => $servers,
100  'localDomain' => wfWikiID(),
101  'loadMonitorClass' => 'LoadMonitorNull'
102  ] );
103 
104  $dbw = $lb->getConnection( DB_MASTER );
105  $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
106  $this->assertEquals(
107  ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
108  $dbw->getLBInfo( 'clusterMasterHost' ),
109  'cluster master set' );
110  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on master" );
111 
112  $dbr = $lb->getConnection( DB_REPLICA );
113  $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
114  $this->assertEquals(
115  ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
116  $dbr->getLBInfo( 'clusterMasterHost' ),
117  'cluster master set' );
118  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
119 
120  $dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
121  $this->assertFalse( $dbwAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
122  $this->assertTrue( $dbw->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on master" );
123  $this->assertNotEquals( $dbw, $dbwAuto, "CONN_TRX_AUTO uses separate connection" );
124 
125  $dbrAuto = $lb->getConnection( DB_REPLICA, [], false, $lb::CONN_TRX_AUTO );
126  $this->assertFalse( $dbrAuto->getFlag( $dbw::DBO_TRX ), "No DBO_TRX with CONN_TRX_AUTO" );
127  $this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX still set on replica" );
128  $this->assertNotEquals( $dbr, $dbrAuto, "CONN_TRX_AUTO uses separate connection" );
129 
130  $dbwAuto2 = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTO );
131  $this->assertEquals( $dbwAuto2, $dbwAuto, "CONN_TRX_AUTO reuses connections" );
132 
133  $lb->closeAll();
134  }
135 }
$wgDBserver
$wgDBserver
Database host name or IP address.
Definition: DefaultSettings.php:1772
$wgDBtype
$wgDBtype
Database type.
Definition: DefaultSettings.php:1797
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$wgDBpassword
$wgDBpassword
Database user's password.
Definition: DefaultSettings.php:1792
DBO_TRX
const DBO_TRX
Definition: defines.php:12
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$wgSQLiteDataDir
$wgSQLiteDataDir
To override default SQLite data directory ($docroot/../data)
Definition: DefaultSettings.php:1872
$wgDBname
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
Definition: memcached.txt:96
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
LoadBalancerTest\testLBSimpleServers
testLBSimpleServers()
Definition: LoadBalancerTest.php:72
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2807
LoadBalancerTest\testLBSimpleServer
testLBSimpleServer()
Definition: LoadBalancerTest.php:27
LoadBalancerTest
Definition: LoadBalancerTest.php:26
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$wgDBuser
$wgDBuser
Database username.
Definition: DefaultSettings.php:1787