Commit graph

336 commits

Author SHA1 Message Date
Daimona Eaytoy
598c4d7fcb build: Upgrade phan to 0.9.0
Scalar casts are still allowed (for now), because there's a huge amount
of false positives. Ditto for invalid array offsets.

Thoughts about the rest: luckily, many false positives with array offsets
have gone. Moreover, since *Internal issues are suppressed in the base
config, we can remove inline suppressions.

Unfortunately, there are a couple of new issues about array additions
with only false positives, because apparently they don't take
branches into account.

Change-Id: I5a3913c6e762f77bfdae55051a395fae95d1f841
2019-12-07 20:16:19 +00:00
Brad Jorsch
9a1ecf2efd rdbms: Have Database::makeWhereFrom2d assume $subKey is string-based
Until I70473280, integer literals were always quoted as strings, because
the databases we support all have no problem with casting
string-literals for comparisons and such.

But it turned out that gave MySQL/MariaDB's planner problems in some
queries, so we changed it to not quote actual PHP integers.

But then we run into the fact that PHP associative arrays don't preserve
the types of keys, it converts integer-like strings into actual
integers. And when those are passed to the DB unquoted for comparison
with a string-typed column, MySQL/MariaDB's planner has problems again
while PostgreSQL simply throws an error. Sigh.

This patch adjusts Database::makeWhereFrom2d to assume that the $subKey
column is going to need all values quoted, as is the case for all
callers in MediaWiki.

Bug: T239877
Change-Id: I69c125e8ab9e4d463eab35c6833aabdc436d7674
2019-12-06 11:55:31 +00:00
sbassett
2e11b14455 rdbms: Log debug message traces as 'exception.trace' instead of 'trace'
Code cleanup and hardening (see also: T234014) of Database-related
lib code in MediaWiki core.

Bug: T233342
Change-Id: I3c968f4f5300374253dc80d99596cac50fbeb59e
2019-12-04 21:45:28 +00:00
Tim Starling
673d496f2d Have Database::addQuotes() pass through bare integers without quoting
Quotes started being added to integers in r4984 (August 2004). Before
that, is_numeric() was used to determine whether to add quotes, so
quotes were omitted from numeric strings, which is obviously wrong.

The idea here is to use the type of the variable to hint to the database
as to whether quotes are needed. The results are somewhat inconsistent,
since some callers do not convert numeric strings obtained from user
input to integers. That makes it a more conservative change. Callers can
opt out of unquoted integers by casting them to string.

The reason for doing this is that quoting integers turns out to be not
as harmless as originally assumed. We found a case of it confusing the
MariaDB query planner, causing inappropriate indexes to be used.

I also made addQuotes() consistently return a string, instead of
returning an integer for boolean values. This was already the case for
MySQL, but it seems like a good idea everywhere.

Bug: T238378
Change-Id: I70473280f542ee5ecd79e187f580807410fbd548
2019-11-18 11:40:28 +11:00
Daimona Eaytoy
ae424ce5da build: Upgrade mediawiki-phan-config to 0.8.0
This is to ensure that the CI job is working with the new version.

Note: redundant_condition_detection should have worked as expected by
this version, but unfortunately it still has false positives.

Bug: T235049
Bug: T231636
Change-Id: Idaba6584cb5b2ff19b6455c7bbec6b89619ddbff
2019-10-22 09:16:45 +00:00
Daimona Eaytoy
754d163e29 Fix new phan errors, part 9
This should be the last part before we can upgrade.

Bug: T231636
Change-Id: I076986ac2d2c9cbbdd1a4bf8c66e16cc014c8ffb
2019-10-22 08:09:36 +00:00
jenkins-bot
c3c45ff689 Merge "Fix new phan errors, part 6" 2019-10-20 18:05:18 +00:00
Daimona Eaytoy
114ee6e412 Fix new phan errors, part 6
Bug: T231636
Change-Id: I1870b6cbeb31e54fde5e675fec51446b330e06c5
2019-10-20 17:53:48 +00:00
jenkins-bot
4b6468ba66 Merge "rdbms: Restore debug toolbar "Queries" feature" 2019-10-15 18:29:02 +00:00
Timo Tijhof
2e404a2582 rdbms: Restore debug toolbar "Queries" feature
This broke after e0cc49ce39, due to the field 'master'
being removed from the log context. The LegacyLogger logic
forwarding these messages to MWDebug (for the debug toolbar)
however, was dependant on.

Users of debug toolbar experienced a silent failure because the
logic in question is very tolerant of missing fields. This is
because it uses those fields to distinguish the 'sql' messages
from channel=DBQuery from other messages in the same channel.
Making that less fragile is outside the scope of this commit.

This commit:

* Restore the basic functionality by making sure MWDebug::query()
  gets called again for DBQuery messages.

* Remove the code relating to the 'master' field as this no longer
  exists in RDBMS. It also wasn't used anywhere (to be used,
  it would need to be read by mediawiki.debug/debug.js).

* Remove unexpanded "{method}" and "{runtime}" noise in the debug
  toolbar text. This was introduced by he conversion to PSR-3
  logging.. These fields are already rendered separately by
  the toolbar and should not be part of the "SQL" column.
  To do this, we need to log the $sql bit as its own key, so
  I've made this a context field as well.

* Reduce the condition logic in LegacyLogger to only looking for
  'DBQuery' and 'sql'. This way, if it breaks again it will
  still call the logic within and emit E_NOTICE instead, which
  would help detect the issue (and still fallback to at least
  showing the queries). Unlike before this commit where it took
  quite some time to figure out why it wasn't working.

* The above fixes still weren't enough to get queries to show
  up in the Debug toolbar for me. Turns out, this was because
  my local setup (mediawiki-docker-dev) uses a master-replica
  set up. The setup doesn't use any custom LBFactory config,
  just plain $wgDBservers. The logic for turning these plain
  settings into LBFactory (in MWLBFactory.php) does kick in,
  and does run (unlike if I had custom wgLBFactoryConf).
  But, the DBO_DEBUG flag didn't make it through because of
  the += operator preferring any pre-existing value my setup
  has, which is just `DBO_DEFAULT`.
  Merging 'flags' keys seems unsafe in general, but adding
  DBO_DEBUG based on $wgDebugDumpSql seems innocent and doesn't
  affect other behaviour (it's a case of DWIM).

Bug: T231742
Change-Id: I122bb1a65620a7ae4e1943136c975b63524a5111
2019-10-11 20:04:05 +01: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
Daimona Eaytoy
69cadf44a9 Unsuppress PhanParamsTooMany
This is the last repo-wide suppressed issue. Hurrah.

Bug: T231636
Change-Id: I3dc939f115bea14848c9c40bc52a7601f86ca0a7
2019-10-10 12:44:08 -07:00
Daimona Eaytoy
8ba620b15a Fixup phan warning toomanyparams (part 2)
Bug: T231636
Change-Id: Idde7a766bb0d084d6b67bb0c940d7ad704847ad2
2019-10-10 12:52:01 +00:00
Aaron Schulz
0350220e99 rdbms: rename $a => $rows in IDatabase and add Database::isMultiRowArray()
Change-Id: Ie98754539d1e722cfad2c36c06862344bc103208
2019-10-08 11:36:33 -07:00
jenkins-bot
662c41a0fb Merge "Revert "rdbms: optimize insert(), replace(), and upsert() for sqlite when possible"" 2019-10-08 17:06:53 +00:00
Anomie
be8ef0f2c3 Revert "rdbms: optimize insert(), replace(), and upsert() for sqlite when possible"
This reverts commit 4bd1b4b455.

Reason for revert: Incorrect syntax for SQLite "ON CONFLICT (...) DO UPDATE".

Bug: T234795
Change-Id: I987e6e7052368d5d9d0b6de9dd189edc4feaebe5
2019-10-08 16:51:37 +00:00
jenkins-bot
de33d9a50a Merge "Fix typos in comments of Database.php" 2019-10-08 09:30:48 +00:00
jenkins-bot
85d7ca3985 Merge "Allow IDatabase::estimateRowCount to take array for tables" 2019-10-06 08:34:52 +00:00
Ammar
9f4dff453a Fix typo: occured → occurred
Change-Id: I23ce827b1bb0e78adc69baa679657a79767ce015
2019-10-06 02:35:49 +01:00
Umherirrender
97d75b3fe3 Allow IDatabase::estimateRowCount to take array for tables
The var is passed to IDatabase::select, which support the array

Change-Id: I072e76a09d8854a9ce0cbeb12d2a088da597acbc
2019-10-04 16:54:48 +02:00
Max Semenik
ff8767a0bb rdbms: Update comment for HHVM removal
Change-Id: Ifb26048ec1e786c58a3d11e89a79d360b0d64f51
2019-10-02 21:50:31 -07:00
Aaron Schulz
4bd1b4b455 rdbms: optimize insert(), replace(), and upsert() for sqlite when possible
Change-Id: Ic884a4ce42a99333f5176f7b681f8a8bba15d2a1
2019-10-03 01:45:24 +00:00
Petr Pchelko
14a3e4d2a1 Don't log the exception that is saved to status cause
Logging this exception will make a duplicate entry in logs
cause 'query' will call 'reportQueryError' later on that will
log the same error.

Bug: T233756
Change-Id: Ic44f81761f24f6c5ee7b4d8d0fc51ba8661eaa8c
2019-10-02 09:26:17 +00:00
petarpetkovic
5f08692701 Fix typos in comments of Database.php
Change-Id: Ic98b3391c4e3c2ead00e22b3d5a732b0e42f875d
2019-09-24 01:42:40 +02:00
Aaron Schulz
1fbb16c712 rdbms: expand on LoadBalancer ownership concept
Enforce this pattern for the remaining LoadBalancer methods.
Carry this over into Database::close() to decide how loud the
error handling should be.

In LBFactory, clean up ownership of newMainLB()/newExternalLB().
The should have a null owner if called from outside the class
since the LBFactory does not track nor care about them anymore
in that case. Disable newMainLB() for LBFactorySingle as it
makes no sense and was broken.

Also remove some redundant abstract LBFactory methods that
just duplciate ILBFactory.

Bug: T231443
Bug: T217819
Depends-On: I7ed5c799320430c196a9a8e81af901999e2de7d0
Change-Id: I90b30a79754cfcc290277d302052e60df4fbc649
2019-09-05 12:08:13 -07:00
Aaron Schulz
2f8be5ca9d rdbms: cleanup visibility of Database::makeSelectOptions()
Change-Id: I2e18dd3d450ceec9895d3143074743bc206a943d
2019-09-03 20:27:27 -07:00
Aaron Schulz
3d5faa10f4 rdbms: clean up use of ATTACH queries in DatabaseSqlite
Defer the queries until a connection exists. Only issue issue the
them for databases that are different than the currently opened file.
Also, make handleSessionLossPreconnect() aware of attached databases.

In LoadBalancer::reallyOpenConnection(), avoid having the "catch" block
appear like it returns a half-constructed Database.

Change-Id: I9f676bb72a1ab06f0eac5820dce28231741c283d
2019-09-01 21:55:45 +00:00
Daimona Eaytoy
7f7efbe026 Globally unsuppress phan issues with low count
All of these suppression prevent the detection of many common mistakes,
and could easily prevent things like T231488. Especially if there are
few issues of a given type, it's way better to suppress them inline,
instead of disabling them for the whole core.
This patch only touches the one with a lower count (although those
counts may be out of date).

Bug: T231636
Change-Id: Ica50297ec7c71a81ba2204f9763499da925067bd
2019-08-30 09:40:47 +00:00
Daimona Eaytoy
f18af0b61f Remove more Oracle and Mssql leftovers
Follows-up 4d10bb14e8 and 807d793ab9.

According to codesearch [0], these were the last usages. Note that this
patch leaves two constants in places, IDatabase::DBO_SYSDBA and
DBO_DDLMODE. These are public constants used "mostly for oracle" according
to the docs, but maybe we could find other use cases in the future (?).

[0] - https://codesearch.wmflabs.org/core/?q=oracle%7Cmssql&i=fosho&files=%5C.%5B%5Ej%5Cd%5D%7Cen%5C.json&repos=

Bug: T230418
Change-Id: Ibfb748b4b23b885a77f4de161af4bf2ab9649a89
2019-08-25 17:21:49 +00:00
Urbanecm
6ded91313c Revert "rdbms: make LoadBalancer::reallyOpenConnection() handle setting DBO_TRX"
This reverts commit 45831e619c.

Reason for revert: Caused beta not work at all.

Bug: T231162
Change-Id: Icc5c1fa0dc01082a622641ad96c22c939cd56d48
2019-08-25 16:39:33 +00:00
Aaron Schulz
45831e619c rdbms: make LoadBalancer::reallyOpenConnection() handle setting DBO_TRX
Make LoadBalancer::reallyOpenConnection() handle initializing DBO_TRX
instead of Database::__construct().

Also:
* Avoid having the "catch" block appear like it returns a
  half-constructed Database.
* Use the variable name $conn instead of $db to be consistent
  throughout the class. Only send Database::__construct() parameters
  that it recognizes instead of mixing in setLBInfo() data.

Change-Id: Iffc3d1d0713051a164adb51a4c4ee12e4ac887c3
2019-08-24 20:06:17 +00:00
Aaron Schulz
b9e68b2d50 rdbms: various field name and style cleanups to Database
Rename Database::hasFlags() to sound less similar to getFlag()/setFlag().
The order of class fields is roughly:
* Objects and resources
* Configuration
* Mutable options that do not have to be kept in sync with the connection
* Session level state tracking (should be in sync with the connection)
* Transaction level state tracking (should be in sync with the connection)
* Results from or timestamp of the last time a certain event occurred

Change-Id: Ibdca2fefe7ed2c792344c5602b5191a950eed933
2019-08-23 20:34:12 +00:00
Aaron Schulz
cbc8451d71 rdbms: remove IDatabase::clearFlag() calls made redundant by QUERY_IGNORE_DBO_TRX
Change-Id: I493c63f94c813ad71dc2eb8eaf9119a1f11d62cb
2019-08-21 19:10:25 +00:00
Aaron Schulz
02934ef503 rdbms: ignore DBO_NOBUFFER flag in IDatabase
Callers should use query batching instead. Without buffering, either
the RAM burden is shifted onto the RDBMS server (with MVCC vacuuming
also being stalled) or the RDBMS server has to keep adding locks as
the cursor advances. Also, if a caller does not read all the results
(possibly due to an exception), then the SQL commands sent/read get
out of sync, which is too fragile.

There are no DBO_NOBUFFER callers in WMF gerrit repos.

Change-Id: I3712149633d0f01bb6990e324e53dd58abba9cfd
2019-08-15 11:05:06 -04:00
Aaron Schulz
5fdb43cd3b rdbms: deprecate seldom used bufferResults() method
Change-Id: If67c91fa020f09a89fe2dd50fe4ad30b75d676ef
2019-08-15 14:50:12 +00:00
Amir Sarabadani
4d10bb14e8 Drop Oracle and Mssql
After approval of RFC T191231, we are going to drop oracle and mssql
and it will be possible to bring back the support using the abstract schema

Adding to release notes will be done in a follow-up

Bug: T230418
Change-Id: I90bd5cfcc3e18011b193c965fdb1fa54675040b5
2019-08-14 11:31:41 +00:00
Aaron Schulz
daae485724 rdbms: remove references to SQL_CACHE/SQL_NOCACHE
Change-Id: I4bace17f7f5171eea9bbf13e7f40fd7027a1ce35
2019-08-07 22:15:47 +00:00
Aaron Schulz
3cdf794c95 rdbms: remove HIGH_PRIORITY/LOW_PRIORITY SQL references from IDatabase
These should not be used even with MySQL as they do not help concurrency

Change-Id: I5385fcbe65e676b184268286bedd5c2299b447d4
2019-08-07 15:04:53 -07:00
Aaron Schulz
a3a6dfed2e rdbms: allow automatic PDO creation of SQLite database files
Define missing DatabaseSqlite::doSelectDomain() method to handle attempts
to change the database, prefix, and/or schema.

Also add sanity check to serverIsReadOnly() to make sure open() was called

Change-Id: I72c25bf4dab5e01def3fb9472217e7637aede1d4
2019-08-07 01:05:35 +00:00
jenkins-bot
b14dd60f43 Merge "rdbms: In the query log, show the server name in the message" 2019-08-05 14:43:08 +00:00
Tim Starling
e9a9622db3 rdbms: In the query log, show the server name in the message
So that it is possible to distinguish master queries from replica
queries. Useful when debugging issues such as T221763.

Change-Id: I38896efa46f2c7cf289e5bdc543a1692e251ec1d
2019-07-26 15:31:45 +00:00
jenkins-bot
436f8eb32f Merge "rdbms: remove various deprecated methods" 2019-07-26 15:16:38 +00:00
Aaron Schulz
4c7e4575f5 rdbms: remove various deprecated methods
Change-Id: I5ae923065a08078225b7df080cb92edca799ebaf
2019-07-26 15:56:02 +01:00
Aaron Schulz
023c73f612 rdbms: normalize Database open() code and error handling
Mainly:
* Use oci_new_connect() for Oracle to avoid broken connection reuse
  similar to the PGSQL_CONNECT_FORCE_NEW flag in DatabasePostgres
* Set 'client_min_messages' unconditionally for PostgreSQL
* Factor out Database::getConnectExceptionAndLog() helper method
* Use the same style of query() calls in DatabaseOracle::open() as
  the other subclasses
* Make sure the Database driver handle field is null on failure
  instead of false for sanity

Also:
* Disallow changing of Database handle DBO_* flags after construction
  where it does not make sense to change them
* Do not mention DBO_* flags meant for non-config use in $wgDBservers
* Ignore DBO_PERSISTENT for SQLite if DBO_TRX is also set for sanity
* Remove $wgDBOracleDRCP variable to discourage careless automatic
  setting of DBO_PERSISTENT that breaks LoadBalancer assumptions

Change-Id: Iea948f7f872294ea8fc5d897fc10c9d29b7141d5
2019-07-26 15:24:28 +01:00
Aaron Schulz
ef38199422 rdbms: fix IDatabase::setLBInfo() handling of null and allow clearing keys
Change-Id: I20cb799b54cabb1172940f8ece93b7f45d7cf0ba
2019-07-15 03:56:16 +00:00
Aaron Schulz
d322031d52 rdbms: switch to AtEast warning suppression in Database classes
Change-Id: Ia32f1ba048a540438f78b11a1e94f80acfc7bf50
2019-07-13 01:49:05 +00:00
Aaron Schulz
95ccb2b907 rdbms: better normalize and document constructor $params in Database
Change-Id: I6531cd3a34d7d6bdf277db779301d88ca1e45a95
2019-07-12 16:46:16 +00:00
Aaron Schulz
aaf8d30204 rdbms: avoid dbSchema() in Database::replaceLostConnection() and Database::__clone()
Since dbSchema() always casts the result to a string, using this method
with a call to open() is broken if the RDBMs does not support DB schemas
and thus requires null

Follows-up 7911da9c6f (last week), which added the check in
DatabaseMysqlBase::open() check. Also follows fe0af6cad (last year),
which made dbSchema() consistently return string. Before that, an
implicit null was passed in from Database::factory for mysql, which hid
the class default of empty string.

Bug: T227708
Change-Id: I67207fbaa39c5cc3fe062077cc654f048090e009
2019-07-11 01:53:22 +00: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
04d591935c rdbms: normalize Database/LBFactory logging and add snapshot flushing warnings
Make flushSnapshot() logging more detailed and check for explicit transaction
rounds. Also removing periods and trailing newlines from log/exception messages.

Change-Id: I0f6520f563680ab3a65b6338ced59ba25a2ec7b5
2019-07-06 13:10:24 -07:00