Commit graph

411 commits

Author SHA1 Message Date
Aaron Schulz
934b58c8c5 rdbms: improve LoadBalancer connection tests for CONN_TRX_AUTOCOMMIT
Change-Id: I499f4a4bb005e63f1bef1ac803c8fd451ba27b01
2023-01-11 18:28:30 +00:00
Aaron Schulz
1147ad83f2 rdbms: clean up "serverName" normalization and enforce uniqueness
Factor out normalizeServerMaps() method to simplify getServerName()
by pre-setting "serverName". Add uniqueness safety check, since we
rely on this property in cache keys and in reconfigure().

Change-Id: I06672885c79611e1257adb5f3dec88194e71b705
2023-01-10 19:46:08 +00:00
Timo Tijhof
4ef0891994 rdbms: Consolidate logger channels into one
Notable changes:

* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
  injected. The rest implicitly got a NullLogger due to being absent.
  These are now effectively unsilenced.

* Database::__construct() required almost all parameters, even the
  loggers. I've wanted to move some of DatabaseFactory into the ctor
  here for a while. In order to make this change not a breaking
  change, the new 'logger' parameter is optional with NullLogger as
  default. This allowed some of the test cases, which were simply
  passing NullLogger, to be fixed by passing nothing instead of
  passing the new option name.

  The Database class is behind a dozen layers of indirection for
  real use, so this will still be injected just fine (DBF, LB, LBF,
  MWLBF, etc.).

* In LegacyLogger, the handling for $wgDBerrorLog was previously
  limited to DBConnection and DBQuery. This now includes errors
  from other (generally, newer) parts of Rdbms as well, which were
  previously missing.

  This only affects sites (typically CI and dev setup) where
  $wgDBerrorLog is used, as opposed to the more common
  $wgDebugLogGroups by-channel configuration.

* TransactionProfiler gets its logger injected in a rather odd way,
  via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
  as opposed to service wiring. This is kept as-is for now.

* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
  there are cases that intentionally produce failures of which
  the result is then observed. In CI we assert that dberror.log is
  empty so instead of adding the missing logger fields to that
  LBFactory instance, the only one set (replLogger) is removed.
  The alternative is to set 'logger' now, which would naturally
  cause CI failures due to unexpected entries coming through to
  non-mocked error log.

Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2023-01-03 22:46:38 +00:00
Aaron Schulz
529211a0c5 rdbms: clarify "waitForPos" logic in LoadBalancer
Rename lazyLoadReplicationPositions() and doWait(). The later now
handles the logic of checking if a position is set or if the reader
index is that of the primary DB.

Narrow the job of reallyOpenConnection() by removing "waitForPos"
loading logic.  It now only deals with initializing a connection and
not the logic that goes along with tracking/pooled connection handles.
Note that getReaderIndex() still triggers primary position loading and
was the only method that automatically triggered logic to actually wait
on the primary position.

Add more comments to getReaderIndex() and optimize call order slightly.

Minor cleanups to related code comments.

Change-Id: I685b1f7946aadb5463e8870edda4340be3fc4ae2
2022-12-21 00:31:57 +00:00
Amir Sarabadani
9b078129d2 rdbms: Moving replication-related code to its own component
Remove 'insertSelectIsSafe' option, unused.

Remove 'topologicalPrimaryConnRef' option, no longer used as of two
months ago with I41a57247503 (8c9398f7f9).

Remove unneeded DatabaseSqlite::getTopologyBasedServerId
implementation which can inherit null instead of overriding with string
of "0". Only caller is SqlBagOStuff::makeTimestampedModificationToken
which can be used as MainStash DB, where its important that a given
server always has the same unique name within a set of db hosts that
may replicate to each other. By inheriting null as topology server ID,
it SqlBagOStuff will use IDatabase::getServerName instead. That in turn
uses the 'host' connection parameter, which defaults to null in
DatabaseFactory, and then falls back to the string "unknown" which is
as good as "0" for this purpose.

Bug: T299691
Change-Id: Iceb65c28cdd3c4a89b3c8b34c3f95d3285718ec0
2022-12-15 00:37:02 +00:00
Aaron Schulz
c1e1512698 rdbms: improve LoadBalancer connection pool reuse (ii)
Make DBConnRef enforce the DB domain selected during its lifetime
and allow more nested and successive use of the same connection handle
via DBConnRef. This can avoid extra connections in some cases where
getConnection()/getConnectionRef() is used.

Also:
* Reduce the number of connection pools arrays from six to two
* Merge getLocalConnection()/getForeignConnection() into one method
* Expand various related code comments

Since LoadBalancer::getReadOnlyReason() no longer user the local domain
but rather DOMAIN_ANY, it should not result in "USE" errors if the local
domain does not have a database on the server.

This version of the patch removes the unused reuseConnectionInternal()
method (the method was previously added back to the patch by mistake).

Bug: T226595
Change-Id: I62502f4de4f86a54f25be1699c4d1a1c1baee60b
2022-10-18 17:23:44 -07:00
jenkins-bot
6551e19d39 Merge "rdbms: Instead of reconfiguring all of LB, just remove depooled db" 2022-10-17 10:54:34 +00:00
Amir Sarabadani
8a474d311f rdbms: Instead of reconfiguring all of LB, just remove depooled db
Bug: T298485
Change-Id: Id6d74c186dedc6253f56f29a6362b39d9be45093
2022-10-12 16:12:10 +02:00
Amir Sarabadani
fdc246801e rdbms: Introduce DatabaseFlagsHolder and move some internal logic there
Bug: T299691
Change-Id: If9da21bebf1b233da38ed198f35d3dcd51c356a5
2022-10-11 17:59:47 +02:00
jenkins-bot
056421a7b2 Merge "rdbms: Drop deprecated functions from LoadBalancer" 2022-09-29 17:31:18 +00:00
Amir Sarabadani
6b1d6e622d rdbms: Drop deprecated functions from LoadBalancer
- ::haveIndex()
  - ::isNonZeroLoad()

Deprecated in 1.34 and unused.

Change-Id: I08f8d8cad51ab1ec3c385c06737116d7ab71f2ca
2022-09-29 17:10:59 +00:00
Ladsgroup
4edae94817 Revert "rdbms: improve LoadBalancer connection pool reuse"
This reverts commit 76be4dca81.

Reason for revert: Caused T318904

Bug: T318904
Change-Id: I92f23205b4d62f69c0f27af40f972f104d1a7a7c
2022-09-29 12:12:14 +00:00
Aaron Schulz
76be4dca81 rdbms: improve LoadBalancer connection pool reuse
Make DBConnRef enforce the DB domain selected during its lifetime
and allow more nested and successive use of the same connection handle
via DBConnRef. This can avoid extra connections in some cases where
getConnection()/getConnectionRef() is used.

Also:
* Reduce the number of connection pools arrays from six to two
* Merge getLocalConnection()/getForeignConnection() into one method
* Expand various related code comments

Bug: T226595
Depends-On: If808cbab429d41e1f2289683533e4a781a4bdf5e
Change-Id: I540b08920997c57cad6445ddb09d8e663eaf4714
2022-09-23 10:53:58 +10:00
Aaron Schulz
fe7301c9ba rdbms: remove getTopologyRootPrimary() and clean up related fields/parameters
This method is unused and the naming is not very clear in terms of what
it returns (server name vs config map). Removing it reduces externally
exposed complexity.

Optimize DBConnRef::getServerName() when the server index is known.
Use this to eliminate the "topologicalMaster" parameter from Database.

Rename internal fields and paramters in Database to use "primary"
instead of "master", for consistency.

Add some additional clarifying comments.

Bug: T299691
Change-Id: I98515fa02a58a4c72a06f1ff283b249b1617c886
2022-08-26 12:26:10 -07:00
jenkins-bot
94d71c83c1 Merge "Replace trivial usages of code in strings with concatenation" 2022-08-26 16:54:56 +00:00
Thiemo Kreuz
67c56155c7 Replace trivial usages of code in strings with concatenation
This is really hard to read. What is code, what is string? These
places are so simple, they really don't need the "{$var}" syntax.

Change-Id: I589dedb8c0193eec4eef500bbb896b5b790b727b
2022-08-26 12:26:44 +00:00
Timo Tijhof
6ca9ebaf74 rdbms: Remove obsolete cross-dc wait from ChronologyProtector
This was written for the scenerio where we would support writes
on a primary DC request, immediately followed by a secondary DC
request, and we'd then first use the CP cookie to wait-for Redis
replication to communicate the MySQL position data, and then wait-for
a MySQL replica to have caught up to that point.

In the revised 2020 plan for Multi-DC (T254634), CP data is kept
dc-local only, and we use a sticky-dc cookie and other traffic
routing to remain in the primary DC for certain scenarios.

CP remains as playing a rule to ensure consistency within the primary
DC between the write request and the next few seconds of GET requests
to ensure a replica is picked or waited-for to have caught up with
prior writes in the same primary DC.

One test needed to be updated as this commit exposed a happy accident
where previously one key ('writeIndex') was unset in the mocked
array, which was tolerated with the null-ish operator. Now that we
use is_array only and then access the key, this undefined offset
got exposed. Fix by removing the hardcoded logic and instead
use $cp->key and $cp-mergePositions(), which also helps gain more
confidence in the test by relying on their side-effects.

Bug: T314434
Change-Id: I712e5aeb6712f4038011c3c48799e859aa40009d
2022-08-20 14:08:10 +00:00
Bartosz Dziewoński
910f8b526a Remove references to removed 'watchlist' query group
The 'watchlist' query group no longer exists in WMF production since
T263127, and it's not included in the list of supported query groups
in docs/database.md.

Change-Id: I6ea5c65921891ac6a705a6ff7e79b08fa5a9bf42
2022-08-16 20:33:43 +02:00
daniel
5b0b54599b Allow DB config to be reloaded on the fly
This introduces $wgLBFactoryConf['configCallback'] which can be set to a
function that returns updates to be applied to $wgLBFactoryConf. The new
method LBFactory::autoreConfigure() can be called to check the callabck
and, if the config changed, reconfigure all existing LoadBalancers.

Reconfiguring the LoadBalancers causes all open connections to be
invalidated; however, any DBConnRef instances will remain valid and will
acquire a fresh connection from the LoadBalancer automatically when
appropriate.

As a proof of concept, this patch adds support for config reloding
into WikiExporter.

Bug: T298485
Change-Id: I6c3ffde62f6e038730736abe980befd90ec43e1a
2022-08-05 17:32:12 +02:00
Thiemo Kreuz
61ae7504df Replace trivial usa of mock builder with createMock() shortcut
createMock() does the same, but is much easier to read.

A small difference is that some of the replacements made in this
patch didn't use disableOriginalConstructor() before. In case this
was relevant we should see the respective test fail. If not we can
save some CPU cycles and skip these constructors.

Change-Id: Ib98fb06e0fe753b7a53cb087a47e1159515a8ad5
2022-07-15 16:43:48 +00:00
Mark A. Hershberger
ea80f07d2f rdbms: Adapt hasOrMadeRecentPrimaryChanges test mock for PHP 8.1
Use a single callback for both mocks.  This is needed because using
PHP 8.1 results in:

> 1) LBFactoryTest::testChronologyProtector
> Implicit conversion from float 1657063075.187724
> to int loses precision
>
> tests/phpunit/includes/db/LBFactoryTest.php:313
> includes/libs/rdbms/ChronologyProtector.php:290

Bug: T312183
Change-Id: I2764cf9b81b08832c904bf7245bd54ca0f0eddad
2022-07-06 19:35:01 +00:00
Aaron Schulz
da9e4b52a5 rdbms: Add multi-statement query support to Database
Add Database::queryMulti(), which will execute an array of
queries as a batch with minimal roundtrips.

SQLite fallbacks to looping through each statement and
invoking doQuery().

Add QueryStatus class to reduce complexity in Database.

Rewrite doQuery() as doSingleStatementQuery().

Change-Id: I3d51083e36ab06fcc1d94558e51b38e106f71bb9
2022-06-09 11:45:38 +10:00
Reedy
41c42d5435 Tests: Cleanup some unnecessary nested function calls
Replace ->will( ->return with ->willReturn(

Change-Id: Ia2dfafa03cac8169d86d6fa5a30b73bfad1fe9fa
2022-06-06 01:02:34 +01:00
Amir Sarabadani
dd9b44b167 rdbms: Move selectSQLText to SQLPlatform
Bug: T307616
Change-Id: I5218bca6633ad4d2704267e0c40926ca171fb1f7
2022-06-02 21:35:04 +02:00
Umherirrender
b0398654c5 tests: Do not use class alias in @covers
Also fix case for the XMLTypeCheck test

Change-Id: I809510c8085a36e20fd0eefb5e77d2671b3148f2
2022-05-28 00:37:43 +02:00
Umherirrender
770f905900 tests: Use namespaced IDatabase class
Change-Id: I7171ff26faee00d9eaabc33c2f3d91049ea0b40d
2022-05-28 00:09:55 +02:00
Amir Sarabadani
a294e715a4 rdbms: Replace getConnection with getLazyConnectionRef
This would simplify the code for its users a lot.

Bug: T255493
Depends-On: I6ab4440a13f4682dd3a6e9cfc6c629222fbeab8a
Change-Id: I6e7544763bde56fc1e19de0358b71ba984a978ca
2022-05-12 07:43:03 +02:00
Amir Sarabadani
ca5b162c23 rdbms: Start using SQLPlatform and move more methods there
The first of many changes decoupling SQL building blocks from Database
class.

Bug: T299691
Depends-On: I5d1d5b9b875bced7bda234f45d6d22ed59db4871
Change-Id: I784e78361f5ee629d31c68629d669ee0ddddf929
2022-05-04 18:34:13 +00:00
Tim Starling
8632b5920a Replace LoadBalancer/LBFactory callback iteration with generators
Callback style iteration made sense before generators existed, but
generators make for simpler code. The "call method" variants made
sense before closures existed but defeat static analysis.

So, in LBFactory:

* Add ILBFactory::getAllLBs()
* Deprecate ILBFactory::forEachLB()
* Remove LBFactory::forEachLBCallMethod(), was protected.
* Add LBFactory::getLBsForOwner(), which is protected and has the
  internal interface in @return. Adding a new abstract method breaks
  Wikibase tests despite LBFactory not being stable to extend.
* Migrate callers. Generators allow you to return/break from the middle
  of the loop, which implies a little rearrangement for some callers.

In LoadBalancer, connections supposedly of type IDatabase were
routinely type-hinted as Database in closure parameters so that methods
could be called that were not in the interface. So it's convenient to
get rid of public iteration methods entirely in favour of private
methods returning Database[].

* Hard-deprecate ILoadBalancerForOwner::forEachOpenConnection()
  and replace it with a private generator method since nothing called
  it externally except for core tests.
* Hard-deprecate ILoadBalancerForOwner::forEachOpenPrimaryConnection()
  and replace it with a private generator. DeferredUpdates needed it for
  iterating over IDatabase::explicitTrxActive(), so add
  ILoadBalancer::explicitTrxActive() as a replacement.
* Replace private method LoadBalancer::forEachOpenReplicaConnection()
  with a generator.

Depends-On: If0b382231e27d6d1197fb7b6aef6ab50335df4e5
Change-Id: I64514e77b9bfe737be5b12e1d3c9c49976bb522f
2022-04-29 08:58:17 +10:00
Aaron Schulz
db36853718 rdbms: make automatic connection recovery more robust
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Track session state errors caused by the loss of named locks or
temp tables (e.g. during connection loss). Such errors will prevent
further queries except for rollback() and flushSession(), which must
be issued to resolve the error.

Add flushPrimarySessions() methods to LBFactory/LoadBalancer
and use it in places where session state loss is meant to be
safely aknowledged.

Change-Id: I60532f86e629c83b357d4832d1963eca17752944
2022-04-14 11:09:31 +10:00
Aaron Schulz
218b68ed4c rdbms: add $fname parameter to LBFactory::closeAll() for better logging
Change-Id: I2f4b6466c22c89d7515290324e8e2e2f720ada61
2022-04-05 21:39:21 +00:00
Aaron Schulz
e0a53cba15 rdbms: rename wasKnownStatementRollbackError() to isKnownStatementRollbackError()
Pass the error number as an argument, similar to isConnectionError()

Fix related mysql documentation links

Change-Id: Id32ef2fd27de65376960de3f5138ffdf7654ff71
2022-03-22 22:59:09 +00:00
jenkins-bot
4d236a74ee Merge "LoadBalancerTest: Avoid using hard-coded table prefix" 2022-03-18 13:39:05 +00:00
Ladsgroup
31c1ca8658 Revert "rdbms: make automatic connection recovery apply to more cases"
This reverts commit 4cac31de4e.

Reason for revert: Blocking the train, reverting the chain.

Change-Id: I7f275b3a25379c6f3256e90947c8eed4b232c0f4
2022-03-17 20:11:10 +01:00
Ladsgroup
a8c5232702 Revert "rdbms: fix owner id and RELEASE_ALL_LOCKS query in session flushing methods"
This reverts commit eed58f2f61.

Reason for revert: Blocking the train, going to revert the whole chain

Change-Id: I64f9e5a9dde106671783f958a686ca697182077b
2022-03-17 18:25:48 +00:00
Func
42c0a6ecd0 LoadBalancerTest: Avoid using hard-coded table prefix
Change-Id: I79da2f61c95292701eb9050561f19d04fc52e89c
2022-03-17 13:14:08 +08:00
Aaron Schulz
eed58f2f61 rdbms: fix owner id and RELEASE_ALL_LOCKS query in session flushing methods
Use the LoadBalancer id in flushPrimarySessions(), not the LBFactory one,
and use assertOwnership() to check $owner, similar to other methods.

In DatabaseMysqlBase::doFlushSession(), change RELEASE_ALL_LOCKS() query
to use RELEASE_LOCK(), since only newer MariaDB versions (>=10.5.2) support
it. No errors were thrown in the method since they are suppressed, but the
syntax error would cause the transaction to be placed in an error state.

Add assertion to testTransactionCallbackChains() that would otherwise fail.

Randomize lock names in lock() tests to avoid contention.

Bug: T292239
Bug: T303887
Follow-Up: ee3c65d541
Follow-Up: 4cac31de4e
Change-Id: I414d737028338cfd5369eee24576df4aa26a2f6f
2022-03-17 02:55:43 +00:00
Amir Sarabadani
46374a8e3e rdbms: Remove deprecated IDatabase functions
Soft-deprecated in 1.37, hard-deprecated in 1.38

Bug: T286694
Change-Id: Icd23271149bba3e4632d595c057a733d13707503
2022-03-15 19:33:32 +01:00
jenkins-bot
b9b75d9613 Merge "rdbms: make automatic connection recovery apply to more cases" 2022-03-10 00:08:35 +00:00
Amir Sarabadani
49a9d44b18 rdbms: Hard-deprecate LoadBalancer::getLazyConnectionRef
All deployed usages have been migrated.

Bug: T255493
Change-Id: I82683596d644bb61a109e79dde6b0cb22c72b399
2022-03-09 10:45:00 +01:00
Aaron Schulz
4cac31de4e rdbms: make automatic connection recovery apply to more cases
Rename canRecoverFromDisconnect() in order to better describe
its function. Make it use the transaction ID and query walltime
as arguments and return an ERR_* class constant instead of a bool.
Avoid retries of slow queries that yield lost connection errors.

Add methods and class constants to track session state errors
caused by the loss of named locks or temp tables. Such errors can
be resolved by a "session flush" method.

Make assertQueryIsCurrentlyAllowed() better distinguish ROLLBACK
queries from ROLLBACK TO SAVEPOINT queries. For some scenarios,
only full tranasction ROLLBACK queries should be allowed.

Add flushSession() method to Database and flushPrimarySessions()
methods to LBFactory/LoadBalancer.

Also:
* Rename wasKnownStatementRollbackError() and make it take the
  error number as an argument, similar to wasConnectionError().
  Add mysql error codes for query timeouts since they only cause
  statement rollbacks.
* Rename wasConnectionError() and mark it as protected. This is an
  internal method with no outside callers.
* Rename wasQueryTimeout(), remove some HHVM-specific code, and
  simplify the arguments.
* Make executeQuery() use a for loop for the query retry logic
  to reduce code duplication.
* Move the error state setting logic in executeQueryAttempt() up
  in order to reduce code duplication.
* Move the beginIfImplied() call in executeQueryAttempt() up to the
  retry loop in executeQuery(). This narrows the executeQueryAttempt()
  concerns to sending a single query and updating tracking fields.
* Make closeConnection() and doHandleSessionLossPreconnect() in
  DatabaseSqlite more consistent with the base class by releasing named locks.
* Mark trxStatus() as @internal.

Bug: T281451
Bug: T293859
Change-Id: I200f90e413b8a725828745f81925b54985c72180
2022-03-09 15:49:38 +11:00
Timo Tijhof
128debb64b tests: Change use of AtEase to at operator
Follows-up I361fde0de7f4406bce6ed075ed397effa5be3359.

Per T253461, not mass-changing source code, but the use of the native
error silencing operator (@) is especially useful in tests because:

1. It requires any/all statements to be explicitly marked. The
   suppressWarnings/restoreWarnings sections encourage developers to
   be "lazy" and thus encapsulate more than needed if there are multiple
   ones near each other, which would ignore potentially important
   warnings in a test case, which is generally exactly the time when
   it is really useful to get warnings etc.

2. It avoids leaking state, for example in LBFactoryTest the
   assertFalse call would throw a PHPUnit assertion error (not meant
   to be caught by the local catch), and thus won't reach
   AtEase::restoreWarnings. This then causes later code to end up
   in a mismatching state and creates a confusing error_reporting
   state.

See .phpcs.xml, where the at operator is allowed for all test code.

Change-Id: I68d1725d685e0a7586468bc9de6dc29ceea31b8a
2022-02-24 21:29:51 +00:00
Aaron Schulz
70d0e5fde5 Remove deprecated LoadBalancer::openConnection() method
Change-Id: Id88dc030c31b5509078412a1b845814252589dda
2022-02-14 23:03:09 -08:00
Amir Sarabadani
e7f3f202dd rdbms: Move more internal code from Database to TransactionManager
Most of transaction status handling is now in TransactionManager.

Bug: T299698
Change-Id: I399380768dfa58504b2205403392c53eee73096e
2022-02-05 04:58:40 +00:00
Amir Sarabadani
87e1a6a2be rdbms: Hard-deprecate legacy cruft from introduction of ResultWrapper
Bug: T286694
Change-Id: Ifc58dd478592be49dd55caddfc9aeb536da1e1d6
2022-01-06 07:17:38 +01:00
jenkins-bot
16bd6eaeb4 Merge "rdbms: Remove DatabaseTestHelper::dataSeek()" 2021-12-21 23:55:52 +00:00
jenkins-bot
7414fa5d07 Merge "rdbms: Remove IDatabase::fieldName() method" 2021-12-21 23:55:45 +00:00
jenkins-bot
90be4ac358 Merge "rdbms: Remove IDatabase::numFields() method" 2021-12-21 23:34:42 +00:00
Amir Sarabadani
c3e877d17d rdbms: Remove DatabaseTestHelper::dataSeek()
Follow up to I1d196172b3f50e1.

Change-Id: If034d3256ef7c49ed74bade6a92ec8e6e2b5bd7d
2021-12-21 23:12:22 +00:00
Amir Sarabadani
be11c21d21 rdbms: Remove IDatabase::fieldName() method
Deprecated and unused,
https://codesearch.wmcloud.org/search/?q=%5CbfieldName%5C(&i=fosho&files=%5C.php&excludeFiles=&repos=

Bug: T296960
Bug: T286694
Change-Id: I3c1a1a90a904c9ead03b7158a87f6c4ee005b987
2021-12-21 23:11:23 +00:00