Commit graph

150 commits

Author SHA1 Message Date
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
Aaron Schulz
561753416c rdbms: remove DB domain parameter from various lag/read-only methods
This effects:
* LoadBalancer::getReadOnlyReason()
* LoadBalancer::getLaggedReplicaMode()
* LoadBalancer::isPrimaryRunningReadOnly()
* LoadBalancer::getRandomNonLagged()

The lag/read-only mode is designed to be tracked per-server. The DB domain
is not useful here, and passing one complicates callers, causing needless
cache fragmentation, and cause can errors when a foreign LoadBalancer falls
back (or is provided) a local DB domain (which might not reside on the server).

Clarified some related "server" vs "DB" terminology in comments.

Bug: T318904
Change-Id: I3e09c1915d012db36be1bc505e00dcb0603fd388
2022-10-19 09:59:17 +11:00
Timo Tijhof
1556f5d6bf rdbms: Update outdated docs around ILoadBalancer::getConn methods
Remove most `@throws` as these can't happen on getConnection anymore
because we use lazy connections now.

Remove reference to "live" as this is no longer relevant to callers.

Remove reference to "reference" except for methods that are explicitly
typed as returning DBConnRef.

Bug: T255493
Change-Id: I5fc42913cd59a35ee9478a538fa4dfd455f55cb1
2022-08-22 19:00:29 +00:00
jenkins-bot
b6ded77b40 Merge "rdbms: Clean up file doc comments" 2022-08-10 05:21:14 +00:00
Timo Tijhof
90babf174a rdbms: Clean up file doc comments
* Document what the LBFactory/LoadBalancer (sub)classes do.

* Move useful descriptions from file doc to class doc so that we don't
  pointlessly maintain it in two places, and to allow for the file
  block to be consistently visually ignored instead of sometimes
  containing useful information.

  This patch removes various non-applicable or unrelated descriptions
  from the file block that were blindly copy-pasted, thus proving my
  point. It also removes two duplicate/clashing definition of the
  'Database' defgroup. Keeping only the primary one in IDatabase.php.

* Move ingroup tag to class block, as indexing the source file
  in Doxygen creates noise in the navigation sidebar and does not
  add add any benefit.

* Fix dead-end reference to a sqlite/README. I'll rework this
  in a later patch.

Change-Id: Iad0e67d766f4a7d5b97e7a471b49f2d8e60c506b
2022-08-09 12:53:32 +00:00
Aaron Schulz
eebb138bd9 rdbms: add getDomainChangeException() call to DBConnRef::dbSchema()
This matches the DB domain logic enforcement in the other class methods.

Change-Id: Ibc8603790de1e7a11ab9d07cc0a6473113ed37a6
2022-08-08 15:45:18 -07:00
Umherirrender
133760bf38 rdbms: Improve documentation around false/bool
Change type bool in union types to false where true is not allowed

Add false to DatabaseSqlite::getFulltextSearchModule

Change-Id: I1199b261c4e5c3f6ff184c756f46f2650b16b0c9
2022-06-18 10:33:40 +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
Tim Starling
05701ffc40 rdbms: Remove instance ownership concept
Instance ownership is supposed to protect LoadBalancer and Database
against unauthorized calls to internal methods other than by the owning
LoadBalancer/LBFactory. This seems like unnecessary complexity. It was
introduced for T231443 and T217819, but the link was speculative and in
the end it didn't help to fix or isolate those bugs. Since then it has
caused a production error (T303885) and an intermittent CI failure
(T292239).

Instead, split the ILoadBalancer interface, introducing
ILoadBalancerForOwner, which contains the methods which are only safe
to call by LBFactory or by the caller of LBFactory::newMainLB(). This
allows phan to statically detect inappropriate calls to internal
methods.

Ownership was used for convenience for two things unrelated to its
original purpose:

* Suppressing calls to ScopedCallback::newScopedIgnoreUserAbort() when
  the caller has already called it. But nested calls are apparently
  harmless, so I just called it unconditionally.
* Suppressing exceptions from Database::close(). I extended the
  behaviour for owned instances to apply to all instances, so even
  unowned instances will no longer throw on close.

CodeSearch suggests nothing in extensions is calling these methods with
an owner parameter. One extension (Wikibase) overrides a method with an
owner parameter in a test mock class and so needs to be simultaneously
updated.

Depends-On: Ib03aba9d8f5f05b875a321d00b14483633a636a8
Change-Id: I27ba4973d24d759c88b3868c95e7db875801ca0c
2022-04-26 11:48:46 +10:00
jenkins-bot
d2e7be31d8 Merge "rdbms: make automatic connection recovery more robust" 2022-04-14 01:33:46 +00: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
Amir Sarabadani
280c2ed0d3 rdbms: Fold MaintainableDBConnRef into DBConnRef
We really don't need this complexity and it prevents us from improving
connection management.

MaintainableDatabase should stay but the connection ref shouldn't.

Bug: T255493
Change-Id: I867301dc7fa07cac298f8faba9cf82ca4617f50e
2022-04-14 01:57:11 +02: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
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
Amir Sarabadani
c3e5bd1326 rdbms: Deprecate getLazyConnectionRef
Bug: T255493
Change-Id: Ie352142c7aa120f2f8985ddcf755a1f4583a5410
2022-03-03 13:15:44 +01:00
Umherirrender
7dc25d7f93 rdbms: Fix return type for ILoadBalancer::has(Streaming)ReplicaServers
The implementation returns bool, change the docs to reflect that.

Needs adjust of a bitwise or

Found by phan strict checks

Change-Id: I0a586c70b6e8a7fbad609cfad1a782be8af9d1b4
2022-02-26 08:03:00 +00:00
Amir Sarabadani
67cd314c9b rdbms: Remove deprecated methods from ILoadBalancer
The renaming of master -> primary

Also removing a function with similar name that were unused and
removed from the interface already.

Bug: T299392
Change-Id: Ia10cd7eacb95d6eabc370dfe37c23121b7c07772
2022-01-20 20:30:02 +01:00
Amir Sarabadani
d73d429219 rdbms: Drop three unused functions from ILoadBalancer
Bug: T299392
Change-Id: Ic8b81b76c4020dcadfbde9900e8f17a6e6543943
2022-01-19 21:26:13 +00:00
Umherirrender
70e2514db8 profiler: Remove deprecated profileIn/profileOut
Bug: T241956
Change-Id: I1e0f732f22f4499c032dd4c189c2e1d2891f6192
2022-01-02 22:25:38 +01:00
Siddharth VP
60faebb725 Fix typos in comments (I-J)
Change-Id: Icaea2b6665cfc3b811d94f70c93452237f5e72bf
2021-12-30 20:38:04 +05:30
Aaron Schulz
5546d10d91 rdbms: mark getServerConnection() as @internal
Bug: T255492
Change-Id: Id16e43b2e1ce4854b07a7fab335911891349a003
2021-10-18 11:50:28 -07:00
jenkins-bot
d018c4f6ed Merge "rdbms: Add more return typehints" 2021-09-14 20:51:25 +00:00
Daimona Eaytoy
613a874635 rdbms: Add more return typehints
See full rationale at I59068cfed10aabf6c6002f9e9312a6ef6e7e9441.
Using IDatabase for now instead of DBConnRef for better BC.

Change-Id: Ie75aaf46ba91779e8706b10efeefa9580857f489
2021-09-07 08:23:36 +00:00
James D. Forrester
8426aacd96 ILoadBalancer: Rename waitForMasterPos to waitForPrimaryPos
Hard-deprecating immediately as no known users in git outside of this repo.

Bug: T282894
Change-Id: I5fa312ae89660a9c6de3c24e13d48de92893e5b0
2021-09-03 20:11:05 +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
e712b6bcf5 ILoadBalancer: Rename pendingMasterChangeCallers to pendingPrimaryChangeCallers
Hard-deprecating immediately as no known users in git outside of this repo.

Bug: T282894
Change-Id: I42360c401791d7d1b0dda3271cb69b4b9ec0f426
2021-09-03 10:06:04 -07:00
James D. Forrester
8a659ca2fe ILoadBalancer/ILBFactory: Rename hasOrMadeRecentMasterChanges to hasOrMadeRecentPrimaryChanges
Bug: T282894
Change-Id: I1d6130bcd09019f9e2de2974878902c7aafe8f0a
2021-09-02 16:34:44 -07:00
James D. Forrester
4443280e2e ILoadBalancer: Rename lastMasterChangeTimestamp to lastPrimaryChangeTimestamp
Hard-deprecating immediately as no known users in git outside of this repo.

Bug: T282894
Change-Id: I851f0bb60fcf417bcc0f97bee83513f6ad3d680f
2021-09-02 16:14:01 -07:00
James D. Forrester
2cfcca23f5 ILoadBalancer/ILBFactory: Rename hasMasterChanges to hasPrimaryChanges
Bug: T282894
Change-Id: Ief9cda8fece28c7f515ca30d0154f1e24559f687
2021-09-02 16:14:01 -07:00
James D. Forrester
b54d695165 ILoadBalancer: Rename hasMasterConnection to hasPrimaryConnection
Hard-deprecating immediately as no known users in git outside of this repo.

Bug: T282894
Change-Id: Icd346ce241d9be72fb02b5a728be70f04c300851
2021-09-02 14:28:17 -07:00
James D. Forrester
3058e70d28 ILoadBalancer: Rename flushMasterSnapshots to flushPrimarySnapshots
Hard-deprecating immediately as no known users in git outside of this repo.

Bug: T282894
Change-Id: I795c5a8c99d54283f684a8522fbc612106221e58
2021-09-02 14:28:17 -07:00
James D. Forrester
10324c232a ILoadBalancer/ILBFactory: Rename rollbackMasterChanges to rollbackPrimaryChanges
Bug: T282894
Change-Id: I31794e052d71160195dd3b6c29fea24bc98b356b
2021-09-02 12:50:52 -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
86f3270bda Tag various new 'primary' methods as @since 1.37
Change-Id: Icbbbc84154174da1bd9398990679857898f418d5
2021-09-02 11:58:19 -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
Daimona Eaytoy
9cca05a8e8 Add typehints to several methods
These not only make the code more robust, but also help a lot when
writing unit tests: if a method is return-typehinted and its class is
mocked, the mock method will automatically return a mock of its declared
return type. Otherwise it will return null, and developers are forced to
manually mock the method if the return value is used by the SUT in a way
that doesn't accept null.

Depends-On: I628fcb1807133390c7b9b47984f512f5b1ae58d0
Depends-On: I7080bc505f5838b2f51a368da562104e206063b0
Change-Id: I59068cfed10aabf6c6002f9e9312a6ef6e7e9441
2021-08-31 16:19:13 +00:00
James D. Forrester
05c1945b41 ILoadBalancer: Rename approveMasterChanges() to approvePrimaryChanges()
Bug: T282894
Change-Id: Iccc7f2ea22ae78fd9d947f88220d10aa5601f1c2
2021-08-03 03:16:57 +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
1d71fd91d6 ILoadBalancer and IDatabase: Rename getMasterPos to getPrimaryPos
Bug: T282894
Change-Id: I30a600335af735a13a6ad2d3df56a5f17e05bfab
2021-08-02 18:14:41 +00:00
James D. Forrester
d11c59538a Rename DB primary position interfaces to DBPrimaryPos and MySQLPrimaryPos
And replace all uses.

Bug: T282894
Change-Id: I5222a8568255ac9fa5e2350e2264b8d2ee5eb968
2021-08-02 17:59:39 +00:00
Aaron Schulz
a56d8c4e54 rdbms: improve caching logic for "rdbms-server-readonly" keys
Use CONN_SILENCE_ERRORS similar to other internal callers of
getServerConnection(). Also, make isMasterRunningReadOnly() handle
the isOpen() edge case. Since the invoked method should probably
throw a DBError in that case, add a FIXME comment.

Remove "lockTSE" option, which was not useful here since there
are no explicit purges.

Also improve ILoadBalaner::getServerConnection() documentation.

Change-Id: I51dc14a6261fa646106a9fadce3baddb860fd090
2021-06-29 11:21:53 -07:00
James D. Forrester
f2f9345e39 Replace uses of DB_MASTER with DB_PRIMARY in documentation and local variables
This is just a start.

Bug: T254646
Change-Id: I9213aad4660e27afe7ff9e5d2e730cbf03911068
2021-05-14 12:40:34 -07: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
64898405cb build: Upgrade mediawiki-codesniffer from v35.0.0 to v36.0.0
Change-Id: I8905d0d69738a1cd6997c104080fdf128d315e8b
2021-04-29 13:00:15 -07:00
Aaron Schulz
af206ea692 rdbms: clarify hosts vs readible "server names"
Change-Id: Ib40629cd3bfbc950dd01a57e0d00aa90a04f1dea
2021-04-19 18:44:32 +00:00