Commit graph

51 commits

Author SHA1 Message Date
James D. Forrester
8eb07a8ef4 ILoadBalancer: Rename finalizeMasterChanged() to finalizePrimaryChanges()
Bug: T282894
Change-Id: Ic863ee6ec1d6e6488db77dd697ac7422d105f3ca
2021-08-02 18:15:12 +00:00
James D. Forrester
719cf161f2 More master -> primary documentation and internal var renaming
Bug: T254646
Change-Id: I63cc8895033714bdfbf09aee933a8f0a43b387f3
2021-07-15 11:20:20 +01:00
DannyS712
ae4efe66c6 Don't pass unneeded variables into anonymous functions
Change-Id: Iad4d1dc4727828f9e0120c8bf99dc245a29ee14d
2021-07-13 19:48:26 +00:00
Aaron Schulz
99d5d2e8cc rdbms: cleanup getServer() and connection parameter fields in Database
Make getServer() always return a string, as documented, even with new
Database::NEW_UNCONNECTED handles that have yet to call open(). If the
'host' parameter to __construct() is ''/null, getServer() now returns
'localhost' instead of null. This avoids problems like fatal errors in
calls to TransactionProfiler::recordConnection().

Use Database constants for "connectionParams" field keys for better
static analysis.

Also:
* Add Database::getServerName() method that returns "readable" server
  names in the style of LoadBalancer::getServerName(). Note that the
  "hostName" field is already passed in from LoadBalancer.
* Migrate most getServer() callers to getServerName() for easier
  debugging and more readable logging.
* Also, normalize Database/LoadBalancer SPI logging context to use
  "db_server" and reduce logging code duplication in LoadBalancer.

Bug: T277056
Change-Id: I00ed4049ebb45edab1ea07561c47e226a423ea3b
2021-05-05 19:44:02 +00:00
James D. Forrester
df5eb22f83 Replace uses of DB_MASTER with DB_PRIMARY
Just an auto-replace from codesniffer for now.

Change-Id: I5240dc9ac5929d291b0ef1c743ea2bfd3f428266
2021-04-29 09:24:31 -07:00
Aaron Schulz
5b6782b1ff rdbms: add LoadBalancer::getClusterName() method
Also:
* Add LBFactoryMulti sanity check to avoid section/cluster collisions.
  Since sections are just a type of cluster, using the same name can
  cause confusion. WMF config already treats them as overall unique.
* Rename 'externalLoads' field in LoadBalancer for clarity.

Change-Id: I748db691b4918b797b82d75cfd8722453ccd9d7f
2021-03-01 20:34:03 +00:00
Umherirrender
a1de8b8700 Tests: Mark more more closures as static
Result of a new sniff I25a17fb22b6b669e817317a0f45051ae9c608208

Bug: T274036
Change-Id: I695873737167a75f0d94901fa40383a33984ca55
2021-02-09 02:55:57 +00:00
Umherirrender
62002cdcf1 build: Update mediawiki/mediawiki-codesniffer to 35.0.0
Change-Id: Idb413be4b8cba8611afdc022af59810ce1a4531e
2021-01-31 13:34:38 +00:00
Umherirrender
bdf8cefaf8 Make unit tests assertion about assertNotEquals more strict
Originally an autofix of PHPUnitAssertEquals sniff, but manually
updated.

Change-Id: I78efc6f7997f0b7227b40d7eab1495cc7a1d7da5
2020-11-01 23:12:06 +00:00
addshore
959bc315f2 MediaWikiTestCase to MediaWikiIntegrationTestCase
The name change happened some time ago, and I think its
about time to start using the name name!
(Done with a find and replace)

My personal motivation for doing this is that I have started
trying out vscode as an IDE for mediawiki development, and
right now it doesn't appear to handle php aliases very well
or at all.

Change-Id: I412235d91ae26e4c1c6a62e0dbb7e7cf3c5ed4a6
2020-06-30 17:02:22 +01:00
Daimona Eaytoy
2b37cfaf18 build: Bump mediawiki-codesniffer to 31.0.0
Done with `composer fix` and suppressing the rest (i.e. sniffs for
global variables, which for core should be suppressed anyway).

Additionally, add `-p` to `phpcbf`, as otherwise it just seems stuck.

Change-Id: Ide8d6cdd083655891b6d654e78440fbda81ab2bc
2020-05-30 14:56:28 +00:00
Aaron Schulz
fcfea80fa3 rdbms: add "loadMonitor" parameter to LBFactoryMulti/LBFactorySimple
This allows full configuration of the LoadMonitor, not just setting
the class name like loadMonitorClass allows. Deprecate the later.

Also fix obsolete LoadBalancer constructor documentation and
standardize the style of LBFactoryMulti::__construct() comments.

Change-Id: Icfdc0d6bbf227dce56e65ad679b93ce3f604e9f7
2020-03-27 20:43:00 -07:00
Aaron Schulz
160370afcd rdbms: dependency inject domain aliases into LBFactory
Add new ILBFactory::setDomainAliases() method for injection database
domain aliases and call it in MWLBFactory::setDomainAliases().

Also:
* Remove overkill "last db/section" caching in LBFactoryMulti
* Clean up some LBFactoryMulti code comments
* Split out separate MWLBFactoryTest test file

Change-Id: If180a58c61178969ca7587c4a06b8786574c7254
2020-02-15 22:34:38 +00:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
Daimona Eaytoy
726f10bf5d Deprecate and stop using assertType(OrValue)
*assertType is marked as deprecated, and should ideally be removed soon
(i.e. no hard deprecation to follow)
*Most usages of assertType in core were autofixed by using I8ef556b630812aeea77c5606713f53d9af609f1b
*assertTypeOrValue was removed because only used in SiteTest
(codesearch: https://codesearch.wmflabs.org/search/?q=assertTypeOrValue&i=nope&files=&repos=)
*SiteTest::assertTypeOrFalse was removed because unused

Bug: T192167
Change-Id: Icb3014b8fe7d1c43e64a37e0bdaaffec18bb482f
2019-12-15 00:07:59 +00:00
James D. Forrester
83d76f4cb5 phpcs: Enable MediaWiki.Commenting.PhpunitAnnotations.ForbiddenExpectedException* and make pass
Change-Id: I63f97497714a32236268be6965c5e181dade6c58
2019-10-14 12:48:48 -07:00
Aaron Schulz
fb621c26a3 rdbms: various cleanups to LoadBalancer::reallyOpenConnection()
Move the DBO_TRX init logic out of Database::__construct() and into
LoadBalancer since the later already handles setting and clearing this
flag based on transaction rounds starting and ending.

Add 'lazyMasterHandle', 'topologyRole', and 'topologicalMaster' parameters
to Database::factory() and inject them via LoadBalancer all at once in order
to avoid worrying about call order. Move some type casting code to
Database::__construct().

Add IDatabase::getTopologyRole()/getTopologicalMaster().

Use constants for getLBInfo()/setLBInfo() for better usage tracking and
typo resistance.

Change-Id: I437ce434326601e6ba36d9aedc55db396dfe4452
2019-10-11 11:35:02 -07:00
Umherirrender
5bd311b1a2 Add public as visibility in tests folder
Add public, protected or private to function missing a visibility
Enable the tests folder for the phpcs sniff

Change-Id: Ibefce76ea9984c47e08c94889ea2eafca7565e2c
2019-10-10 21:55:37 +02:00
Max Semenik
fb06e4cd85 Update tests to use PHPUnit 6 class names
Bug: T192167
Change-Id: I42b0c8908b4968b95b08f861a40af18dc79fa0a1
2019-10-06 01:01:28 -07:00
Thiemo Kreuz
c2211946f7 tests: Replace PHPUnit's loose assertEquals(null) with assertNull()
assertEquals( null, … ) still succeeds when the actual value is 0, false,
an empty string, even an empty array. All these should be reported as a
failure, I would argue.

Note this patch previously also touched assertSame( null ). I reverted
these. The only benefit would have been consistency within this codebase,
but there is no strict reason to prefer one over the other. assertNull()
and assertSame( null ) are functionally identical.

Change-Id: I92102e833a8bc6af90b9516826abf111e2b79aac
2019-09-27 19:15:38 +00:00
Amir Sarabadani
a97171eaff Add drop of mssql/oracle to RELEASE-NOTES
Bug: T230418
Change-Id: Ie7771853048a27a8d8ee75764229e6e34477cd04
2019-08-14 22:14:20 +02:00
Amir Sarabadani
807d793ab9 More clean up of oracle/mssql
Bug: T230418
Change-Id: I1ff01548e55f01f9419668e31d5aa7dd03d23258
2019-08-14 17:04:50 +02:00
Aaron Schulz
32df79f3d1 rdbms: better handle a non-existing "defaultGroup" in LoadBalancer
If the specified default group does not have a corresponding server load map
defined, then ignore it and use GROUP_GENERIC.

Also, consolidate the group load and group reader index code for simplicity

Change-Id: Ic8bf9a3ebcbffb81fb14d7b1787a2adb97ac525d
2019-07-16 18:22:41 +00:00
Aaron Schulz
318306ed17 rdbms: add ILoadBalancer::getReplicaResumePos method
This does what ChronologyProtector wants more rigorously and is better
named. Not all replica servers will have the same position, so they
should be compared to get the highest one.

Simplify the getMasterPos() method to only return master positions
as the other current callers do not need anything else. It will now
connect if needed as well. This should make the method naming better.

Reducing the use of replica derived replication postitions (instead
of those from the master) makes certain GTID issues less likely,
such as the matter of obsolete domain IDs.

Increase general test coverage of LoadBalancer.

Bug: T224422
Change-Id: I5420721ee339a24d09c26c38709500c7bbe797c2
2019-07-12 18:16:15 +01:00
Aaron Schulz
da5f1240b0 rdbms: make IDatabase::isReadOnly() return true for replica DBs managed by LoadBalancer
Change-Id: I25b70fe0ea113c6741601cf0366a0fe9dc5cc3ef
2019-07-09 23:51:24 -07:00
Aaron Schulz
79d1881ede rdbms: avoid recursion in LoadBalancer when the master has non-zero load
Add and use IDatabase::getServerConnection() method to avoid loops caused
caused by pickReaderIndex() calling getConnection() for the master server.
That lead to getReadOnlyReason() triggering pickReaderIndex() again.

Make getLaggedReplicaMode() apply when the master has non-zero load and
the replicas are all lagged.

Remove "allReplicasDownMode" in favor of checking getExistingReaderIndex()
instead. This reduces the amount of state to keep track of a bit.

Follow-up to 95e2c99094

Bug: T226678
Bug: T226770
Change-Id: Id932c3fcc00625e3960f76d054d38d9679d25ecc
2019-07-09 19:26:46 +00:00
Aaron Schulz
ab09cf2fa5 rdbms: clean up $groups logic in LoadBalancer and expand comments
Split out private resolveGroups() method for normalizing query
groups. Move some server index validation to getConnectionIndex()
and make it stricter.

Make getConnectionIndex() group fallback logic aware of the generic
group. The main use case for custom default groups is heavy scripts
or jobs that run in the background. It is probably better to have
good DB server redundancy for the query group and rely on it rather
than possibly fallback onto all of the main servers used for normal
requests. The later behavior could spread slowness or outages.

Also make getAnyOpenConnection() more robust and readable by
splitting out a private pickAnyOpenConnection() method that
checks IDatabase::trxLevel().

In addition, remove redundant is_int() check from isOpen()
method as it will return false in that case even without it.

Remove bogus getConnection() parameter in testCopyTestData().

Change-Id: Ica619c5487c761c724791d151db7388e4b41b0aa
2019-07-04 22:26:55 +00:00
Aaron Schulz
69c503148f rdbms: add replica server counting methods to ILoadBalancer
This is slightly more robust and makes the intent much clearer
than random calling code checking getServerCount() all over the
place. In addition, this yields better separation of concern.

Also, cleanup the LoadBalancer constructer a bit and make the
validation a bit stricter.

Make some server index comparisons strict while at it.

Change-Id: Icc1a35bd65c6862ff81faa3ab9b2aa7cafe29443
2019-06-20 12:47:23 +01:00
Aaron Schulz
7b1e345abc rdbms: avoid LoadBalancer::getConnection waste when using $groups
Add a "readIndexByGroup" field that does the same thing that the
generic read index does except for query grouped connections.

Also remove some pointless checks at the end of getReaderIndex().

Change-Id: I3bd6295be4355ee930960f89ccb07811dd8c5545
2019-06-13 20:09:48 +01:00
Aaron Schulz
8669e0fbe6 rdbms: make resolveDomainID() use more consistent in LoadBalancer
Use the method in getConnection()/openConnection() and make it
handle the local domain alias as well.

Also:
* Move some code to a new getConnectionIndex() method.
* Make the text of various index/domain exceptions more consistent.
* Improve some auto-commit flag code comments.

Change-Id: I7e0d4f2134ee91ad60b0d34bf01e05115193b04a
2019-05-29 21:49:29 -07:00
jenkins-bot
b5e255e914 Merge "rdbms: add and enforce DB_REPLICA/DB_MASTER roles in DBConnRef" 2019-04-06 09:54:16 +00:00
Aaron Schulz
eb603040de rdbms: add and enforce DB_REPLICA/DB_MASTER roles in DBConnRef
Even if there is only one database, if a DB_REPLICA connection
reference is obtained and an attempt is made to write it then
it will still fail. This can make it easier to spot incorrect
database usage even in a simple vanilla developer environment.

Note that methods like ILoadBalancer::getConnectionRef() can
be used for local connections as well as foreign ones.

Change-Id: I5523daad1bdd64624d3e0dd41bfd8d078b1078b0
2019-04-05 09:03:50 +00:00
Aaron Schulz
cd4e33a3b9 Clean up LoadBalancerTest and move more code to private methods
Change-Id: I4c541537f9b95918f8d1a057ccbc29e8525ddc15
2019-04-04 21:18:37 -07:00
Aaron Schulz
5938a6efdc rdbms: add more comments and sanity checks for CONN_TRX_AUTOCOMMIT
Change-Id: I69992cf2e2ae3ef62125b0bc733a0cb7274f814e
2018-08-09 01:39:07 +00:00
Aaron Schulz
2fd62f5dc9 rdbms: add resolveDomainID() method to LBFactory/LoadBalancer
Also add LBFactory::getLocalDomainID to match the one in LoadBalancer

Change-Id: Ia31f0800bd3b692194c08b1eab9cfb2f43679c7a
2018-07-05 14:07:04 +01:00
Aaron Schulz
d16bfb0a30 rdbms: fix Sqlite::tableExists() method to avoid STATUS_TRX_ERROR
Sqlite used the base implementation of trying a SELECT 1 query and
seeing if it failed. Instead, make it use the sqlite_master table.
Also remove the base version of that method since it would always
cause this problem and all subclasses have proper implementations.

Make LoadBalancerTest::assertWriteAllowed() more explicit and add
more assertions there.

Change-Id: I6c7b0bea8894c45dfe8931748d6687f0e5d1e101
2018-06-22 22:33:45 +00:00
Aaron Schulz
b6cd5421b9 rdbms: rename onTransactionIdle() to onTransactionCommitOrIdle()
This is clearer and is consistent with onTransactionPreCommitOrIdle()

Change-Id: I3a34a0e9adea69ec55ed6ddfef47703e31e7c3b5
2018-05-09 21:07:06 +00:00
Aaron Schulz
5bfa77f8d9 rdbms: enforce and improve LBFactory/LoadBalancer callback handling
* Handle the case where an onTransaction* callback for one handle
  adds more onTransaction* callbacks to a different handle. Instead
  of supporting only a short chain of such callbacks, try to resolve
  the whole chain by using a loop in LoadBalancer and LBFactory.
* Add sanity checks to enforce the proper call order of LoadBalancer
  transaction methods, such as those that execute callbacks. This is
  the order that LBFactory already uses. Use ROUND_ERROR for problems
  that can ruin the instance state. Such problems require rollback.
* Correct setTrxEndCallbackSuppression() calls in beginMasterChanges()
  that were making tests fail.
* Make Database handle callback suppression for FLUSHING_ALL_PEERS
  instead of making LoadBalancer/LBFactory have to manage it.
* Simplify finalizeMasterChanges() given that suppression does not
  actually effect runOnTransactionPreCommitCallbacks().
* Make dangling callback warning in Database::close work properly.
* Actually use $fname in flushReplicaSnapshots().
* Use DBTransactionError instead of DBExpectedError in some places
  where stages fail.
* Fix failing testGetScopedLock() unit tests so everything passes.

Add more comments to setTransactionListener and onTransactionIdle.

Change-Id: I6a25a6e4e5ba666e0da065a24846cbab7e786c7b
2018-04-30 21:55:25 +00:00
Aaron Schulz
628a3a9b26 rdbms: make sure cpPosIndex cookie is applied to LBFactory in time
Once getMain() was called in setSchemaAliases(), the ChronologyProtector
was initialized and the setRequestInfo() call in Setup.php had no effect.
Only the request values read in LBFactory::__construct() were used, which
reflect $_GET but not cookie values.

Use the $wgDBtype variable to avoid this and add an exception when that
sort of thing happens.

Further defer instantiation of ChronologyProtector so that methods like
ILBFactory::getMainLB() do not trigger construction.

Bug: T192611
Change-Id: I735d3ade5cd12a5d609f4dae19ac88fec4b18b51
2018-04-23 15:44:02 +00:00
Aaron Schulz
e9d42c0e7e rdbms: add $flags argument to ILoadBalancer::getAnyOpenConnection
Make LoadMonitor use this flag for getting a connection.

Change-Id: I32ea9aadc0223c86db5979d6579d781a6af0ff53
2018-04-16 14:17:52 -07:00
Brad Jorsch
78c3b9c21a LoadBalancerTest: Clean up transaction handling for sqlite
We need to make sure a DBO_TRX transaction was started before doing the
CREATE TABLE, because CREATE TABLE itself won't start one and sqlite
breaks if schema changes are done on one handle while another is open.

Also, incidentally, have the handles in these LoadBalancerTests log to
the standard channel. And clean up the auto-rollback of DBO_TRX
transactions to use ->rollback() instead of ->doRollback() plus
incorrect manual setting of trxStatus.

Bug: T191863
Change-Id: Ib422ef89e7eba21281e6ea98def9f98ae762b9fe
2018-04-13 13:42:47 -04:00
Aaron Schulz
40b2e059c0 Reset table sequences and skip some test assertions for sqlite
Various revision storage tests assume the sequences are reset
and fail otherwise.

Also disable some failing tests that are not applicable to sqlite
as well as postgres.

Change-Id: Ibdb034121a44e16bb35059a92baafb1867951ea8
2018-04-13 10:36:08 +00:00
Aaron Schulz
f8c2486d15 rdbms: rename CONN_TRX_AUTO constant to CONN_TRX_AUTOCOMMIT
The "AUTO" means AUTOCOMMIT, not "automatic transactions"/DBO_TRX,
which is basically the opposite concept. The new name does not
suffer from that ambiguity.

Keep the old constant as an alias for backwards compatibility.

Also remove LoadBalancer comment about non-existing field

Change-Id: I63beeb061fc9be73f320308e4d6393b58628b8c8
2018-04-12 13:01:52 -07:00
Umherirrender
d9fb8bab5e Move phpunit @group from file comment to class comment
Remove @group from non tests

Change-Id: Iae9ee3bc5f539a9b4ded8374006ab2993234450e
2018-03-10 11:48:28 +01:00
Aaron Schulz
c1aa9450ad rdbms: make LoadBalancer::getConnection() ignore CONN_TRX_AUTO when unusable
Change-Id: I1fd13171c3cfbe071e8e398d561281188d998767
2018-03-02 18:40:39 +00:00
Umherirrender
45da581551 Use ::class to resolve class names in tests
This helps to find renamed or misspelled classes earlier.
Phan will check the class names

Change-Id: Ie541a7baae10ab6f5c13f95ac2ff6598b8f8950c
2018-01-26 22:49:13 +01:00
Aaron Schulz
05fafcc075 rdbms: add LoadBalancer::getLocalDomainId() method
This returns the default domain without getting a connection

Also avoid assuming that LoadBalancerTest has the same DB handle
as other tests even though it makes its own LB object. That breaks
if temporary tables are used.

Change-Id: I351d42de38b3126222c5a40627a2a12f76a60939
2018-01-13 12:52:59 -08:00
Kunal Mehta
9e7e12252a Add @covers tags to database tests
Change-Id: Ic46bb961b41856d390ffe0e3632790c506f5655b
2017-12-28 08:53:23 +00:00
Roan Kattouw
9a6f63cd8c Follow-up e213462f7c: fix typo ("constriant" -> "constraint")
Change-Id: I6e1fd74e387af5a913301bf296fe9f6da0dd1837
2017-12-21 12:59:53 +01:00
daniel
e213462f7c Test for writes being prevented on replica connections.
Bug: T183265
Change-Id: I319efa418cf8a46985f6faa60096559efa15d267
2017-12-21 12:33:39 +01:00