MediaWiki REL1_30
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}
$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.
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Database connection, tracking, load balancing, and transaction manager for a cluster.
if(! $regexes) $dbr
Definition cleanup.php:94
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:26
const DBO_TRX
Definition defines.php:12