Commit graph

92 commits

Author SHA1 Message Date
Aaron Schulz
633eb437a3 rdbms: clean up return values of IDatabase write methods
Also improved the atomicity and affected row count logic for
insert/replace with sqlite.

Also remove unused "fileHandle" code from insert().

Change-Id: If7b9148fd44f3a958899885753c7c86ba66bf193
2018-10-30 03:34:52 +00:00
Aaron Schulz
fe0af6cad5 rdbms: Database::selectDB() update the domain and handle failure better
LoadBalancer uses Database::getDomainId() for deciding which keys to use
in the foreign connection handle arrays. This method should reflect any
changes made to the DB selection.

If the query fails, then do not change domain field. This is the sort of
approach that LoadBalancer is expects in openForeignConnection(). Also,
throw an exception when selectDB() fails.

The db/schema/prefix fields of Database no longer exist in favor of just
using the newer currentDomain field.

Also:
* Add IDatabase::selectDomain() method and made selectDB() wrap it.
* Extract the DB name from sqlite files if not explicitly provided.
* Fix inconsistent open() return values from Database subclasses.
* Make a relationSchemaQualifier() method to handle the concern of
  omitting schema names in queries. The means that getDomainId() can
  still return the right value, rather than confusingly omitt the schema.
* Make RevisionStore::checkDatabaseWikiId() account for the domain schema.
  Unlike d2a4d614fc, this does not incorrectly assume the storage is
  always for the current wiki domain. Also, LBFactorySingle sets the local
  domain so it is defined even in install.php.
* Make RevisionStoreDbTestBase actually set the LoadBalancer local domain.
* Make RevisionTest::testLoadFromTitle() account for the domain schema.

Bug: T193565
Change-Id: I6e51cd54c6da78830b38906b8c46789c79498ab5
2018-10-10 12:03:30 -07:00
Tim Starling
9fbbce857c Fix guarding of MySQL's numRows()
It can be true for successful write queries, not just false.

f3a197e49b introduced a caller which calls numRows() on the return
value of CREATE TEMPORARY TABLE queries, and it improved guarding of
numRows() in the PostgreSQL and SQLite cases accordingly, but it
neglected MySQL.

Bug: T201900
Change-Id: I8ae754a2518d9e47b093c31c20d98daaba913513
2018-10-08 18:27:05 +11:00
Umherirrender
a33827a61f Fix caller name in DatabaseMysqlBase::getMasterServerInfo/getServerId
Seeing {closure} in the logs as caller is not helpful

Change-Id: I63adbfcf4944747670a3a30182f6bd5cb6d0e48b
2018-09-30 17:38:05 +00:00
Aaron Schulz
41a37d14fa rdbms: make Database::open() protected
This is not called externally and there is little reason for that to
change. The current caller pattern is to use factory(), possibly with
initConnection() afterwards, or to use a LoadBalancer to begin with.

Change-Id: Ib1fdd5c960f1ed877fcd17bcb99b999d5d894716
2018-08-21 18:34:51 -07:00
Aaron Schulz
d9fdc4098b rdbms: Avoid numRows() warnings for mysqli after table creation
Bug: T201900
Change-Id: Ie86a7b8e680d79ad3f9be6ca4ec260b0589e5d0e
2018-08-14 18:52:27 +00:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Niklas Laxström
0e1566505f Fix typo in method documentation
Change-Id: I891f9725acfac993966238abff255e59e9aee07a
2018-06-14 10:47:08 +02:00
Bartosz Dziewoński
485f66f174 Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient
Find: /isset\(\s*([^()]+?)\s*\)\s*\?\s*\1\s*:\s*/
Replace with: '\1 ?? '

(Everywhere except includes/PHPVersionCheck.php)
(Then, manually fix some line length and indentation issues)

Then manually reviewed the replacements for cases where confusing
operator precedence would result in incorrect results
(fixing those in I478db046a1cc162c6767003ce45c9b56270f3372).

Change-Id: I33b421c8cb11cdd4ce896488c9ff5313f03a38cf
2018-05-30 18:06:13 -07:00
Reedy
765370a6db Add @deprecated tags to various class_alias calls
Bug: T195576
Change-Id: I10cd8415891bfe4a278eee06c9cfe905b3e036dc
2018-05-29 13:10:20 -07:00
Timo Tijhof
c7fc4ef9bf rdbms: Replace reportConnectionError() with direct throws
When reading through DatabaseMysqlBase::open(), it was not
obvious that execution would not continue after the conditional
`!$this->conn` block, given it ends in a method call, without
return or throw. I considered adding a return statement after it
for clarity, but it seems in this case it might make more sense
to throw directly given $error here has already gone through a
fallback to getLastError() a few lines up.

Replace the other three calls to reportConnectionError() as well,
which previously passed a useful string that was overwritten
with lastError(). Instead, log both. And make their call to
queryLogger->error() match the previous ones to have an 'error' as well.

This leaves reportConnectionError() as being unused, except for
a call from LoadBalancer. That call was problematic because
it was inside a conditional for IDatabase, but the method isn't
part of that interface. Replace it with a direct throw as well.

Deprecate the method as its now unused in core, and also remove its
'# New method' comment which hasn't made sense since r75341 (16cded8b32).

Change-Id: I0f2ef00ba44bf7090a3ce54edeb8c7e8e543e46a
2018-04-26 04:25:17 +00:00
jenkins-bot
296fe3b900 Merge "rdbms: ignore inactive mysql GTIDs in replication position methods" 2018-04-12 01:03:15 +00:00
jenkins-bot
671e5b4d81 Merge "rdbms: make Database query error handling more strict" 2018-04-05 15:29:20 +00:00
Aaron Schulz
315ffb840b rdbms: ignore inactive mysql GTIDs in replication position methods
If it is known that master writes will use GTIDs with a certain
domain and server ID, ignore other ones on masterPosWait().

This restores ceb7d61ee except it uses the same server variables
and MySQLMasterPos preserves the active server/domain information
across serialization.

Change-Id: I1a4f143adcbec642966d7d1a55edb0f414a7f0c4
2018-04-04 21:58:03 -07:00
Aaron Schulz
3975e04cf4 rdbms: make Database query error handling more strict
Handle all errors in query() that might have caused rollback by
putting the Database handle into an error state that can only be
resolved by cancelAtomic() or rollback(). Other queries will be
rejected until then.

This results in more immediate exceptions in some cases where
atomic section mismatch errors would have been thrown, such as a
an error bubbling up from a child atomic section. Most cases were
a try/catch block assumes that only the statement was rolled back
now result in an error and rollback.

Callers using try/catch to handle key conflicts should instead use
SELECT FOR UPDATE to find conflicts beforehand, or use IGNORE, or
the upsert()/replace() methods. The try/catch pattern is unsafe and
no longer allowed, except for some common errors known to just
rollback the statement. Even then, such statements can come from
child atomic sections, so committing would be unsafe. Luckily, in
such cases, there will be a mismatch detected on endAtomic() or a
dangling section detected in close(), resulting in rollback.

Remove caching from DatabaseMyslBase::getServerVariableSettings
in case some SET query changes the values.

Bug: T189999
Change-Id: I532bc5201681a915d0c8aa7a3b1c143b040b142e
2018-04-04 21:26:11 -07:00
jenkins-bot
b7d1782e10 Merge "rdbms: rename and clarify getTransactionLagStatus method regarding begin()" 2018-04-04 21:36:36 +00:00
jenkins-bot
6c169ee1fd Merge "rdbms: clean up DBO_TRX behavior for onTransaction* callbacks" 2018-04-04 16:18:14 +00:00
Aaron Schulz
d4c31cf841 rdbms: clean up DBO_TRX behavior for onTransaction* callbacks
* Make onTransactionIdle() wait until any transaction round
  is gone, even if there is no SQL transaction active. This
  is what onTransactionPreCommitOrIdle() already does.
* Decouple "transaction round mode" (DBO_TRX) from whether a
  round is active via a 'trxRoundId' LB info field. If rounds
  are enabled, but not is started, then the transaction state
  should be interpreted as "idle".
* Improve related documentation.
* Add more related unit tests.

Change-Id: I3ab18f577ec0375897fcb63f18f4ee2deeb436e9
2018-04-03 23:16:45 -07:00
Aaron Schulz
92972a59cd rdbms: rename and clarify getTransactionLagStatus method regarding begin()
Make sure any value from the last transaction is cleared and not used.

For sanity, make getLagFromPtHeartbeat() use this regardless of the age of
the transaction unless it returns null.

Change-Id: I52df6147f99736ad1a389ae70d347ae968e50c7f
2018-04-02 14:46:51 -07:00
Aaron Schulz
9ce9f5c4e4 Revert "rdbms: make getMasterPos() ignore GTIDs outside of gtid_domain_id"
This had a noticeable increase in LoadBalancer::doWait timeouts.

This reverts commit ceb7d61ee7.

Change-Id: I7004d55a05c20f646f70d778d7b6496123e270a4
2018-04-02 14:02:47 -07:00
Aaron Schulz
c3f084d44b rdbms: avoid incorrect warnings in getLagFromPtHeartbeat()
Change-Id: I4c0d42be0ee0c518d7bacdae0f76feb53007861e
2018-04-02 12:56:11 -07:00
jenkins-bot
1314f66769 Merge "rdbms: avoid lag estimates in getLagFromPtHeartbeat ruined by snapshots" 2018-03-31 08:31:11 +00:00
Aaron Schulz
24353a60d2 rdbms: avoid lag estimates in getLagFromPtHeartbeat ruined by snapshots
Bug: T190960
Change-Id: I57dd8d3d0ca96d6fb2f9e83f062f29b1d53224dd
2018-03-31 01:39:57 +00:00
jenkins-bot
b163a90a46 Merge "rdbms: update IDatabase::getLag comments" 2018-03-31 00:52:09 +00:00
Aaron Schulz
7d8ea236ca rdbms: add more error logging to DatabaseMysqlBase::masterPosWait
Change-Id: Ia687ddeffc9c9427c6126d8ed831fb52b38e8260
2018-03-30 04:39:05 -07:00
Aaron Schulz
4255b4fa6d rdbms: update IDatabase::getLag comments
Also add missing __METHOD__ to getLagFromPtHeartbeat()

Change-Id: I4257b1d47a88779b47d807a881561c331ff3aa30
2018-03-29 16:14:37 -07:00
Aaron Schulz
ceb7d61ee7 rdbms: make getMasterPos() ignore GTIDs outside of gtid_domain_id
* Filter out GTIDs with a domain that is not the one binlog
  events would be written to if the Database handle was given
  write queries. Likewise for the MariaDB server_id component.
* Also improve MySQL GTID support to better match that of MariaDB.
  This covers position retrieval, replication waiting, and ranges
  in GTIDs (which are almost always present).
* Make some MySQLMasterPos variables private by making use of
  accesors instead.
* Store the gtids array keyed by domain ID for convenience.
* Clean up dynamic call to static method.

Change-Id: Ic6ab517bc8f200c968ff892ade69ad1b9394ab21
2018-03-22 23:53:05 +00:00
jenkins-bot
90b02397d0 Merge "rdbms: make selectRowCount() use $var argument to exclude NULLs" 2018-03-21 16:45:33 +00:00
Aaron Schulz
fe92778888 rdbms: add IDatabase::wasConnectionLoss() method
This takes the logic from wasErrorReissuable(), but puts it under
better name. The way that method was used, as well its comments,
were only about connection loss.

Make wasErrorReissuable() check if there was any error that
does not preclude the ability to retry. This matches the actual
name of the method.

Also improve some other related comments.

Change-Id: I68455d803afb2370897fecab0e79aadbb5d1a740
2018-03-20 01:10:43 +00:00
Aaron Schulz
d395dfb039 rdbms: make selectRowCount() use $var argument to exclude NULLs
If the $var argument is provided, then it will make the resulting
count exclude rows where the value for that column is NULL.

Also add buildSelectSubquery() method and Subquery
wrapper class for use with select() for calculated tables.

Change-Id: I549d629af99afdf370602de095f7fba6d1546c37
2018-03-18 01:34:33 +00:00
jenkins-bot
a0cea14c45 Merge "rdbms: Add $join_conds to IDatabase::estimateRowCount()" 2018-03-14 02:06:55 +00:00
Aaron Schulz
4ccb228bde rdbms: inject the mysql index name aliases into Database
Also added LBFactory::setTableAlias() for consistency with this

Change-Id: Ie49003ff8fd5b99f75db9fae8fe0a184444254d4
2018-03-12 18:51:53 +00:00
Brad Jorsch
6261b4187a rdbms: Add $join_conds to IDatabase::estimateRowCount()
So queries with joins can be estimated.

Change-Id: I9163cf9005d2c2001a88bb102eb4142f0322b0df
2018-03-12 12:15:14 -04:00
addshore
0526f2d671 Introduce IDatabase::buildIntegerCast
Change-Id: Ib24856d1ebe017ff07ae497972c764b4a3f3c7df
2018-03-07 13:00:18 +00:00
jenkins-bot
799d51d9dc Merge "rdbms: avoid strange uses of empty()" 2018-03-02 19:13:58 +00:00
Aaron Schulz
3b9e6bec3e rdbms: add missing hint check DatabaseMysqlBase::isInsertSelectSafe
This was lost when a bunch of other logic was split off in 671368a59e

Change-Id: I3d3f744f8fce007ecf88cbd2c9f99918b06f0573
2018-03-01 21:03:54 -08:00
Aaron Schulz
55ab84e17d rdbms: avoid strange uses of empty()
Change-Id: Id1a8d1aae72cdee48e43ddb3227cd697516411e0
2018-03-01 20:30:07 -08:00
Aaron Schulz
671368a59e rdbms: make Database::insertSelect() stricter about replication safety
Avoid the native INSERT SELECT method if a LIMIT clause is present.

Change-Id: Ibf9b8a4a42092fbc98d7ebd45167203a6a8801ee
2018-03-02 02:40:02 +00:00
Brad Jorsch
99b65649c0 rdbms: allow callers to hint that native insertSelect() is safe
An INSERT SELECT in MySQL/MariaDB is unsafe for replication if a column
is getting values from auto-increment, statement-based replication is in
use, and the default innodb_autoinc_lock_mode is set.

I9173f655 added checks to force non-native insertSelect for the
statement-based replication and innodb_autoinc_lock_mode != 2 case, but
determining whether a column is getting values from auto-increment is
too hard to do automatically there.

Instead, let's add a flag to let the caller hint that the query isn't
getting any auto-increment values. And use it in MysqlUpdater when
appropriate.

Bug: T160993
Change-Id: If70450a64aa3bcbf763c62838bb21306d124ae3d
2018-02-28 13:58:37 -05:00
Aaron Schulz
9b5b407a1b rdbms: make DatabaseMysql::masterPosWait() handle inactive GTIDs
Change-Id: I543deef24f6cbf99094a4f3bee7cabe768fa221a
2018-02-16 19:27:42 +00:00
Aaron Schulz
c9ad7037ce rdbms: do not bother making DBO_TRX transactions in IDatabase::lock()
Named locks are session-level constructs and this transaction agnostic.
Also make lockIsFree() a bit more consistent when the thread has the
lock itself.

Change-Id: Ief51196161bbc50c798740f3c738fd0e39880508
2018-02-15 16:32:35 -08:00
Aaron Schulz
ec550d4823 rdbms: remove "m" prefix from Database fields
Done using the PhpStorm refactor->rename tool.

Also move "defaultBigSelects" declaration to DatabaseMysqlBase
as no other classes uses that.

Change-Id: I424a2d9815de3a5d4cca2522f3db23a5efe6b592
2018-02-15 23:29:34 +00:00
jenkins-bot
bed43f5255 Merge "rdbms: avoid "SHOW MASTER/SLAVE STATUS" queries in the GTID case" 2018-02-14 23:37:34 +00:00
Reedy
39f0f919c5 Update suppressWarning()/restoreWarning() calls
Bug: T182273
Change-Id: I9e1b628fe5949ca54258424c2e45b2fb6d491d0f
2018-02-10 08:50:12 +00:00
Aaron Schulz
e59b0556f4 rdbms: make DOMAIN_ANY ignore bogus MySQL DB names in config
This regressed in 14ee3f2, trying to select the server config
"dbname" value as the database on connect. If that config is
bogus, then the connection attempt would fail. Instead, always
pass null to the driver's connection function if the DB domain
doesn't matter.

This affected WMF sites during lag checks, on account of the
"serverTemplate" value in $wgLBFactoryConf always using the local
wiki domain (whether the cluster had such a database or not).

Use strlen() as mysqli sees null and "" as the same for $dbname.

Bug: T186764
Change-Id: I6699d17c0125a08415046211fee7906bbaf2c366
2018-02-09 06:43:24 +00:00
Aaron Schulz
a114bd9f4d rdbms: avoid "SHOW MASTER/SLAVE STATUS" queries in the GTID case
The binlog file/pos where only being used in __toString() for the GTID
case. Make that method use the GTID set instead and avoid querying the
old-fashioned binlog fields all together in that case. The STATUS
queries involve some global lock contention.

Bug: T180918
Change-Id: I18123a702e4f554b87bf5f90017b248062e73049
2018-02-08 09:26:48 -08:00
Aaron Schulz
8f45d13fe1 rdbms: remove obsolete getReplicaPos() code
Also fixed incorrect comment for getGtidCoordinates()

Change-Id: Ie2ce8a8bba230ceb6d068957e5ffeb360c3fe473
2018-02-06 17:18:00 -08:00
Aaron Schulz
128068a4d8 rdbms: avoid pointless "SHOW SLAVE STATUS" calls in masterPosWait()
This code branch is not useful in the GTID case

Change-Id: Ia112c9b4b9c1f1297cc4eaac7f4a5a1ca882f02e
2018-01-31 00:11:33 +00:00
Brad Jorsch
d40916cb85 Make DatabaseMysqlBase::insertSelect() safer to use
Certain server configurations, including the current MariaDB defaults,
make INSERT SELECT unsafe for replication in MySQL/MariaDB. When the
server configuration is not known to be safe, force the use of the
non-native implementation. Note this only has effect in CLI mode, as
non-CLI mode already forces the non-native implementation since
I2dba6024.

Also, native INSERT SELECT won't be safe with any statement-based
replication method if the order of rows in the SELECT is not
deterministic. Add a warning to the method's documentation pointing this
out.

Change-Id: I9173f6559809bd01830bd0a9f443c7269cc58ce2
2018-01-26 09:10:22 -08:00
Aaron Schulz
62bb392e9e Lower DatabaseMysqlBase::lock logging to INFO
Whether the lock() acquisition failing is a huge problem depends
on what the caller is doing. Let the caller do any logging it needs.

Bug: T180793
Change-Id: I3d05138d312d8b973df153bb511e69619d663c9d
2017-12-03 03:56:32 +00:00