Commit graph

133 commits

Author SHA1 Message Date
Timo Tijhof
28c470adc7 objectcache: Stricter assertions for WANObjectCache tests
Use the stricter comparisons from assertSame instead of
assertEquals.

In some cases this revealed a test that was asserting a value
different from the one actually returned:

* testSetAndGet:
  The test failed with the test case (null, 3), where it tries
  to store the value `null`, but gets back `false`, which is wrong.
  This appears to be a genuine bug, filed as T234583.

Change-Id: Ic3dcc7fa2e8749b0f2d68917a8ac728dda26b6ca
2019-10-06 02:10:24 +00:00
Max Semenik
bc3878e33a Begin cleaning up PHPUnit 4 code from tests
This process will be broken up into several parts for reviewability.

Bug: T192167
Change-Id: Ie415fd3308384a5ca2b3de24ba037785f8a3a714
2019-10-04 14:19:05 -07:00
Thiemo Kreuz
e4272518f7 tests: Replace PHPUnit's loose assertEquals(false) with assertFalse()
assertEquals( false, … ) still succeeds when the actual value is 0, null,
an empty string, even an empty array. All these should be reported as a
failure, I would argue.

Note this patch previously also touched assertSame( false ). I reverted
these. The only benefit would have been consistency within this codebase,
but there is no strict reason to prefer one over the other. assertFalse()
and assertSame( false ) are functionally identical.

Change-Id: Ic5f1c7d504e7249002d3184520012e03313137b4
2019-10-04 00:30:36 +00:00
Daimona Eaytoy
c12b32c9a3 HHVM removal: Kill HHVM_VERSION checks in PHPUnit tests
Change-Id: I1d18b2d6956d326e5f9e50cd01ce2bb683a5b653
2019-10-03 15:35:21 +00:00
Thiemo Kreuz
cff596804a tests: Replace assertions on count() == 0 with strict === []
The benefit of using count() is that the test would still succeed if
the return vfalue is not an array, but an iterable object. It seems
this is not needed.

Change-Id: I23529f6990aebe0cce86e236a21820fe74993204
2019-09-30 16:20:34 +02:00
jenkins-bot
864c4df8b9 Merge "tests: Replace PHPUnit's loose assertEquals(null) with assertNull()" 2019-09-27 22:43:09 +00:00
Thiemo Kreuz
c2211946f7 tests: Replace PHPUnit's loose assertEquals(null) with assertNull()
assertEquals( null, … ) still succeeds when the actual value is 0, false,
an empty string, even an empty array. All these should be reported as a
failure, I would argue.

Note this patch previously also touched assertSame( null ). I reverted
these. The only benefit would have been consistency within this codebase,
but there is no strict reason to prefer one over the other. assertNull()
and assertSame( null ) are functionally identical.

Change-Id: I92102e833a8bc6af90b9516826abf111e2b79aac
2019-09-27 19:15:38 +00:00
Aaron Schulz
223b7a3717 objectcache: fully respect "pcTTL" in WANObjectCache instead of using INF when >= 0
This was broken since 611e2d5596

Change-Id: I612eaf211ff698d5ab1c911aa58195b7bc44f00c
2019-09-24 02:56:39 -07:00
jenkins-bot
095c50ade1 Merge "tests: Prefer assertSame() when comparing the integer 0" 2019-09-20 15:33:02 +00:00
Thiemo Kreuz
32a429e8c4 tests: Prefer assertSame() when comparing the integer 0
assertSame() is guaranteed to not do any type conversion. This can be
critical when acciden tially comparing, for example, 0 to 0.0.

Change-Id: Iffcc9bda69573623ba14af655dcd697d0fcce525
2019-09-19 15:35:23 +00:00
Timo Tijhof
9244cdb977 objectcache: Escape dots from cache keys in StatsD metrics
Bug: T232907
Change-Id: Ia61256e24fe35803e69a83a1ca235e18297c75cd
2019-09-14 18:16:55 +00:00
Aaron Schulz
bee2a20903 objectcache: make WANObjectCache::relayPurge() actually use $holdoff
Fixes a regression introduced by 70547f3fa3 in 2016.

Change-Id: I5996b63c31ac3b382e838a6858b8585bdc8c585c
2019-08-24 22:41:39 +00:00
Aaron Schulz
94eb0c946f objectcache: improve BagOStuff arithmetic method signatures
Make the default $init value for incrWithInit() be $value.
This is far less suprising and also makes the operation
easier to replicate without conflicts.

Make decr() definitions more explicit since various cache
drivers do not handle negative incr() values (e.g. memcached).

Change-Id: I2b8d642656cc91c841abbd7a55d97eba101b027a
2019-08-24 12:50:37 -07:00
jenkins-bot
90d6f495bd Merge "objectcache: fix WRITE_ALLOW_SEGMENTS in BagOStuff cas() and add() methods" 2019-08-20 15:50:58 +00:00
Aaron Schulz
942f708b61 objectcache: fix WRITE_ALLOW_SEGMENTS in BagOStuff cas() and add() methods
Add MediumSpecificBagOStuff::getValueOrSegmentList() helper method.

Also:
* Use $keysMissing variable correctly in CachedBagOStuff::getMulti()
  to avoid extra overhead.
* Optimize mergeViaCas() when the current value matches the new one.

Change-Id: I5c4bd74379bc459216ac0278150ce3aecff3b851
2019-08-20 10:55:42 -04:00
Aaron Schulz
d421f22eb6 objectcache: make newFromId()/newWANcacheFromId()/getDefaultKeyspace() private
Change-Id: I30b99f23dbd9637ca8178d3a3650b4c38ec43e7d
2019-08-20 13:11:27 +00:00
Aaron Schulz
49025f52b2 objectcache: clean up MemcachedBagOStuff expiry handling
Partly a follow-up to 88640fd902.

Use real time in changeTTL() tests to fix all remaining
failures for BagOStuff sub-classes.

Change-Id: I537d665d6c8770a68a5a79233a913f1714881dfb
2019-08-10 12:12:12 -07:00
Aaron Schulz
88640fd902 objectcache: fix failing tests for non-HashBagOStuff backends
Use real time for testing absolute expirations with changeTTL().
Otherwise, backends like memcached or redis will fail since
they do not use the mock time.

Also:
* Make SqlBagOStuff actually override changeTTLMulti() by
  using the right method name
* Check TTL_INDEFINITE more explicitly for clarity
* Rename TTL conversion methods for clarity
* Use isRelativeExpiration() in MemcachedBagOStuff

Change-Id: I9365ceb31d4e7bef65906363d42b8c3020a66346
2019-08-07 14:07:22 +00:00
Aaron Schulz
47aa48f073 objectcache: make "busyValue" stricter to avoid callback ambigiuity
Change-Id: I01a1503ff5b37d65ef148fef79270505d8eb3146
2019-07-27 02:21:56 -04:00
jenkins-bot
f577358f43 Merge "objectcache: let BagOStuff::getWithSetCallback() callbacks modify the TTL" 2019-07-25 23:26:05 +00:00
jenkins-bot
7af9c25c88 Merge "objectcache: Use variadic signature for makeKey()" 2019-07-23 19:00:17 +00:00
Timo Tijhof
460e68ab3b objectcache: Use variadic signature for makeKey()
This should help fix the following issues that various repos
are getting from Phan as of late:

> Call with 5 arg(s) to \BagOStuff::makeKey() which only takes 2 arg(s)
> defined at ../../includes/libs/objectcache/BagOStuff.php:456
> <source="PhanParamTooMany"/>

Bug: T228563
Depends-On: I5cfba063821101325a5a7359e6b8ad71a0fb1b2f
Depends-On: Ifa5b96735376f2fbe3680799f960616ba8d357ff
Change-Id: Ic9df7f3ad7f356c7cbdfe1edfbe35821b931dda6
2019-07-23 20:44:06 +02:00
Aaron Schulz
f3919c7c79 objectcache: let BagOStuff::getWithSetCallback() callbacks modify the TTL
Also simplify the code by removing the is_callable() check and relying on
regular PHP errors instead of an exception for bad callbacks.

Change-Id: I084b0132c5fb05f1941a6d6839cfa74e2cf677f0
2019-07-23 07:33:40 -07:00
Aaron Schulz
735ec8d704 objectcache: reorganize WANObjectCache fields and avoid exposing internal constants
Change-Id: I95771fc8d032939e71adba3a416894004ea0847d
2019-07-20 16:04:08 -07:00
Aaron Schulz
bebe30333d objectcache: add MediumSpecificBagOStuff base class for non-proxy subclasses
This make it much clearer what needs to be overridden

Change-Id: I3073f8a0605f557c6a3a93d0d8401cddd0fb8dbe
2019-07-19 13:01:46 -07:00
jenkins-bot
ee801e1611 Merge "objectcache: clean up RedisBagOStuff and optimize changeTTLMulti()" 2019-07-18 02:07:31 +00:00
Aaron Schulz
d8b952ae47 objectcache: clean up RedisBagOStuff and optimize changeTTLMulti()
Also fix some IDEA warnings in redis classes and make it easy for IDEs
to recognize the Redis (phpredis) class calls to RedisConnRef proxies.

Bug: T113916
Change-Id: If45a37da412ac37e8c07dc3d1053826aa0a62077
2019-07-18 01:46:49 +00:00
Aaron Schulz
fb013cdf27 objectcache: relax WANObjectCache "pcTTL" nesting rule to allow set()
As long as get()s are disallowed from the process cache, the sets() should
at least still be up-to-date, so there is little reason to prevent them.

Change-Id: Ic62c8380801130de7f8412cddcf85b246e33b3cd
2019-07-17 17:39:54 +00:00
Aaron Schulz
12f4ce87e9 objectcache: make getMultiWith(Union)SetCallback() usage easier
Add WANObjectCache::multiRemap() as an array_combine() wrapper for
easily working with IDs after getMultiWith(Union)SetCallback() calls.
Make the enforcement of uniqueness in makeMultiKeys() stricter and
discourage poor key design in comments. Add WANObjectCache::hash256()
method for getting good key component hashes.

Also avoid pointless use of ArrayIterator::getArrayCopy().

Change-Id: I61ffdbf4af4374864bac180df590b4dddc8da56b
2019-07-14 14:51:51 +00:00
Aaron Schulz
611e2d5596 objectcache: move version numbers to the main wrapper in WANObjectCache
Add FLD_VALUE_VERSION key to the value wrapper array to hold the version
number used in getWithSetCallback(). Remove the VFLD_* wrapper array from
FLD_VALUE for versioned values.

Keys stored with the old VFLD_VERSION and VFLD_DATA fields will be seen as
having the wrong version. The previous WAN cache code will see the new keys
that use FLD_VALUE_VERSION as having the wrong version too. In either case,
the usual variant key logic applies, so there should not be any issues.

This means that moving from a non-versioned to a versioned cache key is no
longer a breaking change when, for the same key, some code passes a version
number to getWithSetCallback() while other code does not.

Also:
* Make "pcTTL" respect the version number for sanity
* Make sure set() respects TTL_UNCACHEABLE for completeness
* Track slow regeneration callback runtime in FLD_GENERATION_TIME
* Remove is_callable() check overhead and rely on PHP Error instances
* Refactor unwrap() to return a more immediately useful value
* Simplify getNonProcessCachedKeys() signature by using $opts
* Split out PURGE_* constants for purge entries since those keys are
  never stored in any serialize value but are only in PHP arrays
* Rename doGetWithSetCallback() to be more succinct
* Rename and reorganize some variables for clarity

Change-Id: I4060b19583cdfd9fa36c91d7014441eeef4b3609
2019-07-11 18:04:50 -07:00
Aaron Schulz
20a9c8d24e bagostuff: optimize SqlBagOStuff and fix failing segmentation tests
In SqlBagOStuff:
* Add modifyMulti() helper method to reduce code duplication
* Improve atomicity of add(), cas(), and changeTTL() queries
* Avoid integer serialization and improve atomicity of incr()
* Optimize new BagOStuff::changeTTLMulti() method

In BagOStuff:
* Add changeTTLMulti() method for subclasses to optimize
* Make set() ignore WRITE_ALLOW_SEGMENTS for integers so incr() works
* Strip WRITE_ALLOW_SEGMENTS flag from the setMulti() call in set() to
  avoid triggering bogus sanity check exceptions
* Fix BagOStuffTest::testSetSegmentable failures via the above changes
* Enforce WRITE_ALLOW_SEGMENTS sanity check in setMulti() for all the
  subclasses by using a final wrapper method
* Add WRITE_ALLOW_SEGMENTS sanity check to deleteMulti()

Bug: T113916
Change-Id: I25d1790fa9b0d1837643efccfa94a12043cfbf42
2019-07-09 22:53:18 -07: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
Timo Tijhof
4938038175 tests: Remove use of wfRandomString() for test fixtures
This reduces confidence in the test. There is no guruantee that
it won't return the same value twice during the duration of a full
PHPUnit run of all test suites, whether twice in a row or 20 minutes
apart.

For a test that needs a string of any kind, use an explicit, consinstent
and cheap literal value.

For a test that specifically needs some kind of uniqueness compared to
something else within the same test case, do so explicitly.

Tests that require something globally unique (for some undefined/vague
definition of "global") were not found, and should not exist anyway.

Also, in libs/objectcache tests, fix order of parameters in some
assertions (expected first, then actual), and use assertFalse/assertSame
instead of assertEqual for cases where false is expected to remove
tolerance of other loosely equal values.

Change-Id: Ifc60e88178da471330b94bfbf12e2731d2efc77d
2019-05-06 17:22:03 +00:00
Aaron Schulz
7ff911ba11 objectcache: Remove hacky testMerge_fork() method in BagOStuffTest
This method easily fails due to use of the same resource in each
thread after calling pcntl_fork(). This can cause thread-safety
problems like mysqli errors, which in turn make tests fail.

Change-Id: I4ff3657940c1d1cdad58bafb7449bdd0182cbe56
2019-03-28 19:39:05 -07:00
Aaron Schulz
962495b17a objectcache: merge getWithToken() into doGet() for simplicity
Change-Id: I581f866521e1086ca350973d9cdeff6656f48fe8
2019-03-27 13:03:25 -07:00
jenkins-bot
0fd9e748cc Merge "objectcache: defined some missing methods in MultiWriteBagOStuff" 2019-03-27 18:34:30 +00:00
Aaron Schulz
25fd7dbc7b objectcache: defined some missing methods in MultiWriteBagOStuff
Also, reorder some method definitions to match the base class.
This makes it easier to compare the two. In addition, refactor
the doWrite() method to make the calls clearer.

Change-Id: I795b395b9c54645f62962461a28067ae38b291ed
2019-03-27 17:26:43 +00:00
Aaron Schulz
861683b722 objectcache: fix race condition in SqlBagOStuff::changeTTL
Also fix --use-bagostuff parameter when given 0

Change-Id: I64971d4ccad2c856cc7688577ae564c8db068819
2019-03-26 17:06:21 -07:00
Aaron Schulz
e626d014ac objectcache: remove BagOStuff::mergeViaLock() and update RESTBagOStuff
All subclasses are now using the CAS variant, which respects $attempts.

Change-Id: Ia7ec6a7f3337cabe95c54c1142c3c5464c1794e7
2019-03-24 00:35:23 -07:00
Aaron Schulz
e2b03d5d30 objectcache: add BagOStuff::deleteMulti() method for consistency
Also:
* Make the BagOStuff tests actually pass for memcached and sql
* Add more unit tests for BagOStuff
* Make SqlBagOStuff::add() more atomic

Change-Id: Ic1eec0990a66b595b57c646498c3bd229442230c
2019-03-14 23:12:30 +00:00
Aaron Schulz
bcf563ef38 objectcache: add metrics for WAN cache deletes and check key touches/resets
Change-Id: I3dc707af53e480b27b7349aca53292f3bb26c45a
2019-03-06 08:48:47 -08:00
Aaron Schulz
b707afa7f4 objectcache: optimize WAN cache key updates during HOLDOFF_TTL
Avoid the ADD operation spam from all threads trying to access
a tombstoned key by checking the interim value cache timestamp.
This also avoids the GET/CAS spam from threads that manage to
get the mutex. If a single thread repeatedly accesses the same
tombstoned value in rapid succession, there will significantly
less cache operation spam.

Do the same for cache updates to keys in the holdoff state
due to "check keys" or the "touchedCallback" function.

Relatedly, fix getWithSetCallback() to disregard interim values
set prior to or at the same time as the latest delete() call.
This can slightly reduce the chance of the cache being behind
replica DBs for a second. It also avoids unit test failures
were a series of deletes and cache access happen at the same
timestamp (via time injection or regular system time calls).

In addition:
* Add PASS_BY_REF flag with backwards compatibility to avoid
  bloating the signature of get()/getMulti() with the new
  tombstone information needed for the above changes.
* Avoid confusing pass-by-reference in getInterimValue() and
  fix use of incorrect $asOf parameter.
* Move some more logic into setInterimValue().
* Update some comments regarding broadcasted operations that
  were not true for the currently assumed mcrouter setup.
* Rename $cValue => $curValue and $versioned => $needsVersion
  for better readability.

Bug: T203786
Change-Id: I0eb3f9b697193d39a70dd3c0967311ad7e194f20
2019-03-04 10:00:29 +00:00
Aaron Schulz
f50ea02994 objectcache: simplify WAN cache unwrap() method by removing FLG_STALE
Instead, make the "high read lag with lockTSE" case just lower FLD_TTL.
This also avoids constant regenerations by threads getting the mutex.

Added logging for TTL adjustments in set() for the lockTSE case.

Also remove some delete() calls from tests that were not needed.

Change-Id: Id7695f0377235e4a2f6e0efc88e870c8a990c3b0
2019-02-27 18:57:45 +00:00
Aaron Schulz
5414aee495 objectcache: remove dangling WAN cache EventRelayer references
* Remove 'channels' field references from config/setup
* Remove 'relayer'/'channels' field reference in unit tests
* Remove unused DEFAULT_PURGE_CHANNEL class constant
* Also remove long-since bogus 'pool' field references

Follow-up to 4753b0a4ed

Change-Id: If6670ff4e1dccc8ae253a08b46d205601da10024
2019-02-16 20:22:32 -08:00
Aaron Schulz
e4930d255b objectcache: avoid occasional test flakiness due to microtime()
* Fix the timestamps to static hard-coded values
* Force the timestamps before various get/set tests so they do not
  use the microtime() value either.
* Remove the direct and duplicated (causing further risk) microtime()
  calls from testGetWithSeveralCheckKeys().

Bug: T207247
Change-Id: Id30a8127f11501dbe54e075b6e9d18490353f4a5
2019-02-04 21:35:42 -08:00
Umherirrender
7c4ceae4e7 Add missing @covers to backend related tests
Change-Id: I6ecbedf7d4f6c48b4f17331c31588f2c8b772054
2019-02-01 22:08:32 +01:00
Aaron Schulz
2190b78987 objectcache: add expiration check callback to WANObjectCache::getWithSetCallback
This is useful when the timestamps to be checked depend on the value or are stored
in the database rather than as check keys.

Change-Id: I81ab08a943ee7d2f96a132d371965501941ed37f
2018-12-21 19:25:26 +00:00