Commit graph

88 commits

Author SHA1 Message Date
Amir Sarabadani
a1f2056feb rdbms: Simplify $options argument in commitPrimaryChanges and more
These options only support one value in the array: maxWriteDuration

Instead of turning this into a full array and pass it around, just pass
the integer to simplify the logic, avoid mistakes by typos and more.

Bug: T326274
Depends-On: Ib5c76346d0a61c3a6906365b3ced9fca2d43e4d2
Change-Id: Ib60f25ba4a7ca1d14d062d9121fe34e94ccc3b70
2023-01-25 12:02:27 +01:00
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
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
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
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
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
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
jenkins-bot
4d236a74ee Merge "LoadBalancerTest: Avoid using hard-coded table prefix" 2022-03-18 13:39:05 +00: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
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
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
Reedy
6e29611642 Remove or replace usages of "sanity"
Still some more to go...

Bug: T254646
Change-Id: Ia117f01e443c35b4765f3275cab4f2707e1be96f
2021-11-21 16:42:31 +00:00
James D. Forrester
3983572c49 ILoadBalancer: Rename forEachOpenMasterConnection to forEachOpenPrimaryConnection
Bug: T282894
Change-Id: I4892084aba6c7fdfbeccec42de88b9f23490b61c
2021-09-03 13:05:23 -07:00
James D. Forrester
02d04502c9 Switch uses of ILoadBalancer::approveMasterChanges to ::approvePrimaryChanges
Bug: T282894
Change-Id: Iba3aa865d49af2836f3c6af15de307a289a32a2b
2021-09-03 09:59:56 -07:00
James D. Forrester
3391bc0df7 ILoadBalancer: Rename runMasterTransactionListenerCallbacks to runPrimaryTransactionListenerCallbacks
Hard-deprecating immediately as no known users in git outside of tests.

Bug: T282894
Change-Id: Ic1a866439cfa1d537aeef72f91096eb56239e0a9
2021-09-02 12:07:38 -07:00
James D. Forrester
065813dd84 ILoadBalancer: Rename runMasterTransactionIdleCallbacks to runPrimaryTransactionIdleCallbacks
Hard-deprecating immediately as no known users in git outside of tests.

Bug: T282894
Change-Id: I05a13ccecc353ad8ce4c721392c47a5681add2ff
2021-09-02 12:04:42 -07:00
James D. Forrester
029f532837 IDatabase: Rename getTopologyRootMaster to getTopologyRootPrimary
Hard-deprecating immediately as no known users in git outside of tests.

Bug: T282894
Change-Id: I79b801d9f03431613b60ebe09b5e8b873369ca79
2021-09-02 11:58:44 -07:00
James D. Forrester
5ad7ca7eba ILoadBalancer/ILBFactory: Rename commitMasterChanges() to commitPrimaryChanges()
Bug: T282894
Change-Id: I0d80be56e683924254c4e38d05e1109ea74eeab5
2021-09-02 11:27:10 -07:00
James D. Forrester
577e1c3710 ILoadBalancer/ILBFactory: Rename beginMasterChanges() to beginPrimaryChanges()
Bug: T282894
Change-Id: I10e607215e6772c48670659719948f7135472a7f
2021-09-01 22:15:45 +00:00
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