Commit graph

82 commits

Author SHA1 Message Date
Tim Starling
dcb784311b Fix infinite recursion in DBLoadBalancerFactoryConfigBuilder service
When $wgMainCache is CACHE_ANYTHING, recursion proceeds via
LoadBalancer::isPrimaryRunningReadOnly() until the DB server hits its
maximum connection limit. The try/catch blocks in ServiceWiring were no
longer effectively preventing this.

So, inspect the configuration to detect whether the main cache type will
be CACHE_DB, without actually instantiating it.

Bug: T334970
Change-Id: Ibdbc19c45eb20bb85df720669f7ce1d50e3656ff
2023-05-03 13:39:44 +10:00
Timo Tijhof
27aa07815c rdbms: Rename internal ServerInfoHolder to ServerInfo
Follows-up I177d57e2e34aea (3909c1440a).

Change-Id: Ic7332abc5512a74f62ff01e1bd9566047b81cb59
2023-03-07 23:36:39 +00:00
Timo Tijhof
d16eba24df objectcache: Move main cache to internal "_LocalClusterCache" service
This is preparation for re-using it in the MainWANObjectCache
service in a way that preserves proper dependency injection.

This is related to a declined task (T243233) which proposed offering
it as a non-internal service. It is expected to remain internal, with
public callers generally migrated to WANObjectCache or WRStats or
in some cases to a (not yet implemented) lock manager service.

Bug: T329680
Change-Id: I8af9667529b4896f17a22b66823e7eac859d4229
2023-02-21 21:31:42 +00:00
Timo Tijhof
bf34238252 objectcache,rdbms: Widen needlessly narrow @covers test annotations
We'd lose more useful coverage and spend more effort keeping these
accurate through refactors etc than is worth the theoretically "bad"
accidental coverage. Plus, even then we still very often forget to
update these and waste time digging into seemingly uncovered code
and either write duplicate test or discover they are already covered.

Especially for private methods this can be a flawed endavour as
we tend to trust tests when changing those and thus feel we shouldn't
update tests, except it's riddled with private method mentions for
coverage purposes (not in terms of actual called code).

I think the best practice is to write good narrow unit tests,
not to write bad tests and then hide their coverage. Generally
that's what we do. Maybe these are useful when invoking a huge
legacy class, but generally we don't need these I think, at least
in the libs our team maintains.

Change-Id: I4c7d826c7ec654b9efa20778c46c498784661f1c
2023-02-21 21:27:40 +00:00
jenkins-bot
b58397e2bf Merge "rdbms: Introduce ServerInfoHolder to limit access to servers in LB" 2023-02-21 16:30:09 +00:00
Amir Sarabadani
3909c1440a rdbms: Introduce ServerInfoHolder to limit access to servers in LB
For example, any part of LB can write and change number of servers.

Bug: T326274
Change-Id: I177d57e2e34aea176a53c4d96d58f428b9a16634
2023-02-20 18:58:15 +01:00
daniel
7dcfbf2a62 Allow some maintenance scripts to be called without a LocalSettings
Subclasses of Maintenance that can function without database access can
now override canExecuteWithoutLocalSettings to return true.
This is useful for scripts that need to be able to run before or without
installing MediaWiki.

When no config file is present, the storage backend will be disabled.
Any attempt to access the database will result in an error.

NOTE: This makes MediaWikiServices::disableBackendServices() more
comprehensive, to avoid premature failures during service
construction. This change makes it necessary to adjust how the
installer manages service overrides.

The CLI installer is covered by CI, I tested the web installer
manually.

Change-Id: Ie84010c80f32cbdfd34aff9dde1bfde1ed531793
2023-02-16 21:11:33 +00:00
Tim Starling
3cabe8bf8a SqlBagOStuff: Fix modtoken comparison
String offsets in MariaDB are 1-based, except in "Oracle compatible"
mode. SUBSTR(modtoken,0,13) was always the empty string and so the
modtoken comparison was always true. I was able to reproduce a failure
to reach consistency using ring replication.

Add regression test.

Bug: T315271
Change-Id: I74e54e8aba44505dd04426c12d91a9ea0de17f22
2022-08-19 11:11:58 +10:00
Derick Alangi
175832a8e9 Migrate usage of setMwGlobals() to overrideConfigValue(s)
Change-Id: I49d97864d8f9d8cadf01b0c0dc653fbac4ca9e94
2022-08-01 06:16:22 +01:00
Tim Starling
bcfb90cdd7 objectcache: Simplify SqlBagOStuff class configuration
Substantive changes:

* Remove the parameters globalKeyLB and localKeyLB, not needed in
  production, probably not used anywhere.
* Add a "cluster" parameter, for production.
* Don't call markServerDown() in LoadBalancer mode.

Style changes:

* Document config parameters separately from constructor parameters, to
  clarify the role of the wiring normalisation.
* Inject a load balancer from ObjectCache to SqlBagOStuff, using a
  closure to defer construction.
* Clarify the split between LoadBalancer mode and server array mode by
  introducing a useLB property. Use it instead of special shard index
  values and array counts.
* multiPrimaryModeType wasn't needed anywhere and wasn't available in
  the constructor, so it just became a boolean multiPrimaryMode.
* Add a test covering most of the server array (!useLB) branches.

Bug: T212129
Change-Id: Ib097082efb40b514a29a42286dc362f2e3743ee4
2022-05-27 15:04:43 +00:00
Aaron Schulz
cc1bb73490 objectcache: remove "multiPrimaryMode" DB type assertion
Since no mysql-specific queries are required, allow sqlite and
Postgres setups to use "multiPrimaryMode" for easier testing.

Avoid "CASE types text and integer cannot be matched" Postgres
error by making dbEncodeSerialValue() cast the result to a string
so that addQuotes() applies in buildUpsertSetForOverwrite().

Bug: T212129
Change-Id: Ic84a804c272a7070779c8d41d3a33003852d0839
2022-04-21 16:20:39 -07:00
Aaron Schulz
c96dec041e objectcache: make "multiPrimaryMode" work with LB-based SqlBagOStuff
This avoids the previous database type exception in the constructor.

Bug: T212129
Change-Id: I43c6a86883f4122f7395b022bdc6ed7a0cf88121
2022-04-05 01:43:53 +00:00
Tim Starling
e582572622 Improve ObjectCache integration tests
* Add BagOStuffTest subclasses for all core BagOStuff subclasses,
  replacing PHPUNIT_USE_BAGOSTUFF, as suggested in a todo comment.
* Add config $wgEnableRemoteBagOStuffTests which causes all tests
  enabled by $wgObjectCache to execute, which means that the memcached
  tests are executed by default.

I have verified all except RESTBagOStuff and WinCacheBagOStuff. The
memcached tests fail against memcached 1.5.x but pass against memcached
1.6.x.

Bug: T90875
Change-Id: Id74b5226669f8cb857f859fbc35bc58ab001e873
2022-02-11 10:20:33 +11:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
DannyS712
9c8db4b14d Make SqlBagOStuffTest a pure unit test
No integration needed, remove misplace @database tag

Change-Id: I0dbbc362243f6f6434ccf9b93453b368e3d3fc3b
2021-02-27 02:22:41 +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
Aaron Schulz
6a8943d8c5 objectcache: dependency inject LoadBalancer into SqlBagOStuff
Clean up the recursive DB dependency mitigation logic by having
ServiceContainer detect recursion and throw an appropriate error.
Catch the error and use EmptyBagOStuff in such cases. This works
better than checking getQoS() since that begs the question by
requiring the cache instance to begin with.

Also add support for using different LoadBalancer instances for
local and global keys in SqlBagOStuff. This makes it easier to
share keys between projects.

Bug: T229062
Change-Id: Ib8ec1845bcf1b86cbb3bededa0ca7621a0ca293a
2020-05-18 21:04:17 -07:00
Petr Pchelko
fe40625afb SqlBagOStuff: Add a limit to key length
Bug: T224808
Change-Id: I66a547b1fa4a7720ce15797f2df8dc0556c6b35b
2020-02-26 13:22:51 -08:00
Max Semenik
48a323f702 tests: Add explicit return type void to setUp() and tearDown()
Bug: T192167
Depends-On: I581e54278ac5da3f4e399e33f2c7ad468bae6b43
Change-Id: I3a21fb55db76bac51afdd399cf40ed0760e4f343
2019-10-30 14:31:22 -07:00
Aryeh Gregor
7fb4a95563 Remove unneeded overrideMwServices/resetServices
Change-Id: If6cbdec05b8f310ef3a0b4649aaa16d9fb80a047
2019-08-29 14:26:18 +03:00
Aryeh Gregor
47464abb4f Call resetServices() when setting globals in tests
Now that resetServices() will preserve (but reset) customized services,
it should be reasonably safe to call it every time globals are changed,
and much more effective than relying on tests to call it every time
themselves.

Depends-On: Iab8ea3a61bbc6803805d855ef23c071067646f71
Depends-On: I00e35ecea6a27468674b2a6e7d9d9eb6518e3bd5
Change-Id: Ie7a89f6ed7d52a0bc01672019ff92e7ee105a1f3
2019-08-29 14:26:13 +03:00
Amir Sarabadani
57261a926c Move unit tests, round III
Depends-On: I16691fc8ac063705ba0c2bc63b96c4534ca8660b
Bug: T87781
Change-Id: I5e1ab06e3decef6cc6090551d54dc4314ab9314a
2019-07-09 22:06:59 +02:00
Máté Szabó
344481f60d Move trivially compatible tests to the unit tests suite
This changeset resumes work on T89432 and related tickets
by porting an initial set of tests to the new unit test suite
separated out in I69b92db3e70093570e05cc0a64c7780a278b321a.
The tests were only ported if they worked immediately without
requiring any changes other than changing the test case class
to MediaWikiUnitTestCase and moving the test to the new suite.
If a test failed for any reason (even trivial misconfiguration),
it was NOT ported.

With this change, the unit tests suite now consits of a total
of 455 tests. As before, you can run these tests via the following
command:
$ composer phpunit:unit

Bug: T84948
Bug: T89432
Bug: T87781
Change-Id: Ibb8175981092d7f41864e641cc3c118af70a5c76
2019-06-30 15:23:53 +02:00
Legoktm
4e35134f7a Revert "Separate MediaWiki unit and integration tests"
This reverts commit 0a2b996278.

Reason for revert: Broke postgres tests.

Change-Id: I27d8e0c807ad5f0748b9611a4f3df84cc213fbe1
2019-06-13 23:00:08 +00:00
Máté Szabó
0a2b996278 Separate MediaWiki unit and integration tests
This changeset implements T89432 and related tickets and is based on exploration
done at the Prague Hackathon. The goal is to identify tests in MediaWiki core
that can be run without having to install & configure MediaWiki and its dependencies,
and provide a way to execute these tests via the standard phpunit entry point,
allowing for faster development and integration with existing tooling like IDEs.

The initial set of tests that met these criteria were identified using the work Amir did in
I88822667693d9e00ac3d4639c87bc24e5083e5e8. These tests were then moved into a new subdirectory
under phpunit/ and organized into a separate test suite. The environment for this suite
is set up via a PHPUnit bootstrap file without a custom entry point.

You can execute these tests by running:
$ vendor/bin/phpunit -d memory_limit=512M -c tests/phpunit/unit-tests.xml

Bug: T89432
Bug: T87781
Bug: T84948
Change-Id: Iad01033a0548afd4d2a6f2c1ef6fcc9debf72c0d
2019-06-13 22:56:31 +02:00
Aaron Schulz
b09b3980f9 objectcache: add object segmentation support to BagOStuff
Use it for ApiStashEdit so that large PaserOutput can be stored.

Add flag to allow for value segmentation on set() in BagOStuff.
Also add a flag for immediate deletion of segments on delete().

BagOStuff now has base serialize()/unserialize() methods.

Bug: T204742
Change-Id: I0667a02612526d8ddfd91d5de48b6faa78bd1ab5
2019-06-11 16:14:17 +01:00
Bill Pirkle
bf2d6d7240 Add additional configuation parameters to RESTBagOStuff
Class RESTBagOStuff is needed for the Kask session storage
service. Add additional configuration parameters so that
they will work together.

Bug: T215533
Change-Id: Ifbb9f8847bd10d65cc5e24f4aa6cb20cb71de3ca
2019-05-21 08:07:44 -05:00
Umherirrender
63d96c15fd build: Updating mediawiki/mediawiki-codesniffer to 16.0.0
Change-Id: I59b59f79bbf3ce4feff3b3a20c1c31bc16370531
2018-02-17 13:29:13 +01:00
Umherirrender
45da581551 Use ::class to resolve class names in tests
This helps to find renamed or misspelled classes earlier.
Phan will check the class names

Change-Id: Ie541a7baae10ab6f5c13f95ac2ff6598b8f8950c
2018-01-26 22:49:13 +01:00
Kunal Mehta
75160bdd3b Use MediaWikiCoversValidator for tests that don't use MediaWikiTestCase
Change-Id: I8c4de7e9c72c9969088666007b54c6fd23f6cc13
2018-01-01 08:28:02 +00:00
Kunal Mehta
cf0ebed65a Add @covers tags to objectcache tests
Change-Id: I5db623a582a0424f50d939aaa14fe29da9358b4f
2017-12-24 23:29:00 -08:00
Aaron Schulz
67072948b6 objectcache: Make MemcachedBagOStuff::makeKeyInternal always have a key class
Even if a key is too long and shortened, it should still have some key class.

Change-Id: I006b6b03ad1302e9e49362bbd051332bc6105837
2017-11-21 02:45:01 +00:00
Gergő Tisza
525bfbc8df Switch to librarized version of TestingAccessWrapper
Replaces \TestingAccessWrapper (defined in core) with
\Wikimedia\TestingAccessWrapper (defined in the composer package
wikimedia/testing-access-wrapper).

See https://gerrit.wikimedia.org/r/#/q/topic:librarize-testing-access-wrapper
for downstream patches.

The core version of the class is kept around for a while to avoid
circular dependency problems.

Bug: T163434
Change-Id: I52cc257e593da3d6c3b01a909e554a950225aec8
2017-04-20 14:15:57 +00:00
Timo Tijhof
b586d83422 objectcache: Complete coverage for newAnything()
* Fix typo that disabled testNewAnythingNoAccel().
  Follows-up c5a0fa5bed, accidentally committed a local hack
  to disable the test.

* Add missing case other types falling back and no DB.
* Add missing case of no other types and no DB.

Change-Id: If158f21053f0b3741f2625fe4455fdb31955a22f
2017-04-10 14:52:04 -07:00
Brian Wolff
c5a0fa5bed objectcache: Never use CACHE_NONE for CACHE_ANYTHING
If $wgMainCacheType = CACHE_ACCEL, but there is no APC, then its
possible that CACHE_ANYTHING will default to CACHE_NONE because
that's what CACHE_ACCEL would do.

Possibly also T147161

Bug: T160519
Change-Id: I9ac2d071437b35a0f9cd3678e2279628f7b1931e
2017-03-30 01:38:15 +00:00
Bartosz Dziewoński
ecdef925bb Miscellaneous indentation tweaks
I was bored. What? Don't look at me that way.

I mostly targetted mixed tabs and spaces, but others were not spared.
Note that some of the whitespace changes are inside HTML output,
extended regexps or SQL snippets.

Change-Id: Ie206cc946459f6befcfc2d520e35ad3ea3c0f1e0
2017-02-27 19:23:54 +01:00
Timo Tijhof
e1decd825d objectcache: Disable RedisBagOStuff constructor in unit test
Follows-up c4e698dc2. The tests are failing on Travis CI because
php5-redis isn't installed by default. These tests shouldn't need
that to be installed (and even then, we should skip gracefully as
it is an optional dependency).

> RedisBagOStuffTest::testUnserialize with data set #0
> Exception: RedisConnectionPool requires a Redis client library.
> ./mediawiki/includes/clientpool/RedisConnectionPool.php:86

Fixed by using a mock and disabling the original constructor.

Change-Id: Icced7c30a75516c2118489ad29eac2aa5cff80ad
2016-07-25 16:55:36 +01:00
Stanislav Malyshev
7ade0a7c67 Create BagOStuff implementation to talk to RestBase
Or any other HTTP REST server.

Bug: T137272
Change-Id: Iefef24ffa831ba59d7725da8d20d5addb544b3ab
2016-07-06 17:52:07 +00:00
Matthew Flaschen
c4e698dc27 Follow-up cdc93a62bf: add serialize/unserialize tests for RedisBagOStuff
Bug: T134923
Change-Id: I0a7bec40c3fbf301720a248fbe21d46f5e64154c
2016-05-12 22:41:16 +00:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Aaron Schulz
d2d935483f Make makeKeyInternal() limit more conservative
This should avoid error log entries for long WAN cache keys

Change-Id: I401482d25dd5bf47052a3c6729c5f8bc9fd68770
2015-11-01 19:37:21 -08:00
Thiemo Mättig
e6cf3aa0b3 Add tests for MemcachedBagOStuff::validateKeyEncoding
If3e20c6 and the following patches introduced a breaking change and
cause a regression in Wikibase because we are using the version number
constant as part of a cache key prefix. Currently the Wikibase version
is set to "0.5 alpha".

Space characters were allowed before and encoded as "%20". This does
not happen any more.

Change-Id: Ia2fd4ed6738a10e02050bced947ef5d4e8b98980
2015-10-28 18:12:35 +01:00
Aaron Schulz
cce813a922 Move MultiWriteBagOStuff to /libs
Also moved related tests files to /libs.

Change-Id: I806eeaa30205733d497adde933baf0c4157f7aae
2015-10-24 12:15:26 -07:00
Aaron Schulz
0877963f95 Fixes to MemcachedBagOStuff::makeKeyInternal()
* Follow-up to 0c9fb12265
* Make sure colons actually get escaped
* Added more unit tests
* Also fixed the test actual/expected order

Change-Id: Ie04ea6059ee1eb6d1da8f30fefdec52fa49d38fb
2015-10-24 04:53:20 +00:00
Ori Livneh
0c9fb12265 Escape colons in BagOStuff key segments
For the sake of safety and correctness, the following BagOStuff::makeKey()
invocations should return distinct keys:

   $cache->makeKey( 'ab:', 'cd' );
   $cache->makeKey( 'ab', ':cd' );

That is not currently the case, because while we use ':' as a key path
separator, we don't escape ':' in the input supplied to makeKey(). So, make
BagOStuff::makeKeyInternal() URL-encode colons.

To prevent this from messing up the max. key length calculations, reproduce
this logic in MemcachedBagOStuff::makeKeyInternal(), in lieu of having the
method call its parent.

Change-Id: I83ea7e7336a1c9e64aa42284c2517089a736efe5
2015-10-23 20:26:49 -07:00
Ori Livneh
cdb5432728 Ensure all key transformations are applied by BagOStuff::makeKeyInternal()
Currently we have to undo any transformations we apply to keys in getMulti() by
iterating over the response array keys reversing any changes. This is hairy and
complicated. So --

* Replace calls to MemcachedBagOStuff::encodeKey() with calls to a new method,
  MemcachedBagOStuff::validateKeyEncoding(). The new method only validates that
  the key is compatible with memcached. If it is not, it throws an exception.
  If it is, it returns the key unmodified.

* Remove MemcachedBagOStuff::{encode,decode}Key(), since they no longer serve a
  purpose, and have no callers.

Change-Id: If3e20c6a1a1b42fc1f2823aa660328e37c26eccb
2015-10-23 18:46:04 -07:00
Ori Livneh
bf11cee6c5 Improve normalization and sanitization of memcached keys
The motivation for this patch came from seeing the following error in the
memcached log:

  memcached ERROR: Memcached error for key "flowdb:flow_ref:wiki:by-source:\
   v3:0:tawikinews:பூமிக்கு_அச்சுறுத்தலான_சிறுகோள்களைக்_கண்டுபிடிக்கும்_முயற்சியில்_தனியார்_நிறுவன0jம்:4.7" \
   on server ":": A BAD KEY WAS PROVIDED/CHARACTERS OUT OF RANGE

I submitted a fix to Flow in I26e531f6, but I noticed that AbuseFilter had a
similar issue (fixed by Aaron in I27b51a4b), so I started thinking about how
to solve this more generally:

* The regular expression we current use to sanitize keys does not cover
  characters outside the ASCII range, but such characters can be illegal
  if one of their constituent bytes (when taken by itself) is an ASCII
  control character. So change the regular expression to cover any and all
  characters that fall outside the range \x22-\x7e (and '#' -- see below).

* Enforce a key length limit of 255 bytes, which is the maximum length
  permitted by the memcached protocol. The Tamil segment in the key above is 84
  characters, but 233 bytes in UTF-8, which become 684 characters when
  URL-encoded. To fix this, try to shrink any segment that would push the total
  key length over the limit by md5()ing it. If the end result is *still* over
  the limit (this would happen if, for example, $args consists of many short
  strings), then concatenate all args together and MD5 them.

* MD5'd arguments are prefixed with '#'. Any "organic" '#'s in the key segments
  are URL-encoded.

Change-Id: Ia46987d3b0a09bb6b1952abd936d4c72ea7c56a0
2015-10-23 16:21:04 -07:00
Aaron Schulz
5c8ef13306 Add WRITE_SYNC flag to BagOStuff::set()/merge()
* This blocks on writing to all replicas
  and returns false if any failed.
* This is useful if ChronologyProtector is to work across
  domains by having the writes go everywhere so that later
  reads will see them (and be local at the same time).
* Redundant doc comments were also removed.

Change-Id: I9ed098d563c64dba605e7809bc96731da3b3e79d
2015-10-22 01:44:09 +00:00
Aaron Schulz
1171cc00cd Inject MultiWriteBagOStuff addCallableUpdate() dependency
Inject the DeferredUpdates::addCallableUpdate method via the
ObjectCache. This brings it closer to being able to move to /libs.

Change-Id: Ifa0d893002c3d709a4dc7346c263a92162274bd7
2015-10-20 10:31:36 -07:00
Aaron Schulz
f84b32af37 objectcache: Remove getWithSetCallback() signature backwards-compatability
All callers have been migrated

Change-Id: I7b6b87594dd724434ba24d8206fe07d66c1d5d25
2015-10-19 23:47:04 +00:00