Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
index 369b2bf..017d745 100644 (file)
@@ -30,9 +30,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                parent::setUp();
 
                $this->cache = new WANObjectCache( [
-                       'cache' => new HashBagOStuff(),
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => new HashBagOStuff()
                ] );
 
                $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
@@ -123,6 +121,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        }
 
        public function testProcessCache() {
+               $mockWallClock = 1549343530.2053;
+               $this->cache->setMockTime( $mockWallClock );
+
                $hit = 0;
                $callback = function () use ( &$hit ) {
                        ++$hit;
@@ -156,18 +157,28 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 6, $hit, "New values cached" );
 
                foreach ( $keys as $i => $key ) {
+                       // Should evict from process cache
                        $this->cache->delete( $key );
+                       $mockWallClock += 0.001; // cached values will be newer than tombstone
+                       // Get into cache (specific process cache group)
                        $this->cache->getWithSetCallback(
                                $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
                }
-               $this->assertEquals( 9, $hit, "Values evicted" );
+               $this->assertEquals( 9, $hit, "Values evicted by delete()" );
 
-               $key = reset( $keys );
                // Get into cache (default process cache group)
+               $key = reset( $keys );
+               $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
+               $this->assertEquals( 9, $hit, "Value recently interim-cached" );
+
+               $mockWallClock += 0.2; // interim key not brand new
+               $this->cache->clearProcessCache();
                $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
-               $this->assertEquals( 10, $hit, "Value calculated" );
+               $this->assertEquals( 10, $hit, "Value calculated (interim key not recent and reset)" );
                $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
-               $this->assertEquals( 10, $hit, "Value cached" );
+               $this->assertEquals( 10, $hit, "Value process cached" );
+
+               $mockWallClock += 0.2; // interim key not brand new
                $outerCallback = function () use ( &$callback, $key ) {
                        $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
 
@@ -242,7 +253,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $t2 = $cache->getCheckKeyTime( $cKey2 );
                $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
 
-               $mockWallClock += 0.01;
+               $mockWallClock += 0.2; // interim key is not brand new and check keys have past values
                $priorTime = $mockWallClock; // reference time
                $wasSet = 0;
                $v = $cache->getWithSetCallback(
@@ -439,10 +450,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        return $value;
                };
 
-               $cache = new NearExpiringWANObjectCache( [
-                       'cache'        => new HashBagOStuff(),
-                       'pool'         => 'empty',
-               ] );
+               $cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
 
                $wasSet = 0;
                $key = wfRandomString();
@@ -450,6 +460,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
                $this->assertEquals( $value, $v, "Value returned" );
                $this->assertEquals( 1, $wasSet, "Value calculated" );
+
+               $mockWallClock += 0.2; // interim key is not brand new
                $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
                $this->assertEquals( 2, $wasSet, "Value re-calculated" );
 
@@ -468,7 +480,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                };
                $cache = new NearExpiringWANObjectCache( [
                        'cache'        => new HashBagOStuff(),
-                       'pool'         => 'empty',
                        'asyncHandler' => $asyncHandler
                ] );
 
@@ -500,8 +511,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( $value, $v, "New value stored" );
 
                $cache = new PopularityRefreshingWANObjectCache( [
-                       'cache'   => new HashBagOStuff(),
-                       'pool'    => 'empty'
+                       'cache'   => new HashBagOStuff()
                ] );
 
                $mockWallClock = $priorTime;
@@ -694,7 +704,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
                        WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
                ] );
-               $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
+               $wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
 
                // Warm the process cache
                $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
@@ -877,6 +887,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $key = wfRandomString();
                $value = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
                $func = function () use ( &$calls, $value, $cache, $key ) {
                        ++$calls;
@@ -897,6 +910,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 1, $calls, 'Callback was not used' );
 
                $cache->delete( $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $ret = $cache->getWithSetCallback( $key, 30, $func,
                        [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
                $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
@@ -916,30 +930,77 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        public function testLockTSESlow() {
                $cache = $this->cache;
                $key = wfRandomString();
+               $key2 = wfRandomString();
                $value = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
-               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
+               $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, &$mockWallClock ) {
                        ++$calls;
-                       $setOpts['since'] = microtime( true ) - 10;
-                       // Immediately kill any mutex rather than waiting a second
-                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+                       $setOpts['since'] = $mockWallClock - 10;
                        return $value;
                };
 
-               // Value should be marked as stale due to snapshot lag
+               // Value should be given a low logical TTL due to snapshot lag
                $curTTL = null;
-               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
                $this->assertEquals( $value, $ret );
                $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
-               $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
+               $this->assertEquals( 1, $curTTL, 'Value has reduced logical TTL', 0.01 );
                $this->assertEquals( 1, $calls, 'Value was generated' );
 
+               $mockWallClock += 2; // low logical TTL expired
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback was not used (interim value used)' );
+
+               $mockWallClock += 2; // low logical TTL expired
                // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
-               $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
                $this->assertEquals( $value, $ret );
-               $this->assertEquals( 1, $calls, 'Callback was not used' );
+               $this->assertEquals( 2, $calls, 'Callback was not used (mutex not acquired)' );
+
+               $mockWallClock += 301; // physical TTL expired
+               // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
+               $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
+
+               $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' );
+
+               $calls = 0;
+               $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
+                       ++$calls;
+                       $setOpts['lag'] = 15;
+                       return $value;
+               };
+
+               // Value should be given a low logical TTL due to replication lag
+               $curTTL = null;
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
+               $this->assertEquals( 30, $curTTL, 'Value has reduced logical TTL', 0.01 );
+               $this->assertEquals( 1, $calls, 'Value was generated' );
+
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 1, $calls, 'Callback was used (not expired)' );
+
+               $mockWallClock += 31;
+
+               $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
+               $this->assertEquals( $value, $ret );
+               $this->assertEquals( 2, $calls, 'Callback was used (mutex acquired)' );
        }
 
        /**
@@ -952,11 +1013,12 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $value = wfRandomString();
                $busyValue = wfRandomString();
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $calls = 0;
                $func = function () use ( &$calls, $value, $cache, $key ) {
                        ++$calls;
-                       // Immediately kill any mutex rather than waiting a second
-                       $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
                        return $value;
                };
 
@@ -964,6 +1026,8 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( $value, $ret );
                $this->assertEquals( 1, $calls, 'Value was populated' );
 
+               $mockWallClock += 0.2; // interim keys not brand new
+
                // Acquire a lock to verify that getWithSetCallback uses busyValue properly
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
 
@@ -985,6 +1049,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
 
                $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $ret = $cache->getWithSetCallback( $key, 30, $func,
                        [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
                $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
@@ -1305,6 +1370,9 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
        public function testInterimHoldOffCaching() {
                $cache = $this->cache;
 
+               $mockWallClock = 1549343530.2053;
+               $cache->setMockTime( $mockWallClock );
+
                $value = 'CRL-40-940';
                $wasCalled = 0;
                $func = function () use ( &$wasCalled, $value ) {
@@ -1319,10 +1387,16 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 1, $wasCalled, 'Value cached' );
+
                $cache->delete( $key );
+               $mockWallClock += 0.001; // cached values will be newer than tombstone
                $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
                $v = $cache->getWithSetCallback( $key, 60, $func );
+               $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
+
+               $mockWallClock += 0.2; // interim key not brand new
+               $v = $cache->getWithSetCallback( $key, 60, $func );
                $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
                // Lock up the mutex so interim cache is used
                $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
@@ -1504,9 +1578,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( false );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $isStale = null;
@@ -1556,8 +1628,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $localBag->expects( $this->never() )->method( 'delete' );
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1582,8 +1652,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'set' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1600,8 +1668,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'set' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1618,8 +1684,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->setMethods( [ 'delete' ] )->getMock();
                $wanCache = new WANObjectCache( [
                        'cache' => $localBag,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] ),
                        'mcrouterAware' => true,
                        'region' => 'pmtpa',
                        'cluster' => 'mw-wan'
@@ -1633,7 +1697,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        public function testEpoch() {
                $bag = new HashBagOStuff();
-               $cache = new WANObjectCache( [ 'cache' => $bag, 'pool' => 'testcache-hash' ] );
+               $cache = new WANObjectCache( [ 'cache' => $bag ] );
                $key = $cache->makeGlobalKey( 'The whole of the Law' );
 
                $now = microtime( true );
@@ -1649,7 +1713,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
                $cache = new WANObjectCache( [
                        'cache' => $bag,
-                       'pool' => 'testcache-hash',
                        'epoch' => $now - 3600
                ] );
                $cache->setMockTime( $now );
@@ -1660,7 +1723,6 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                $now += 30;
                $cache = new WANObjectCache( [
                        'cache' => $bag,
-                       'pool' => 'testcache-hash',
                        'epoch' => $now + 3600
                ] );
                $cache->setMockTime( $now );
@@ -1746,9 +1808,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( 'special' );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
@@ -1764,9 +1824,7 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
                        ->willReturn( 'special' );
 
                $wanCache = new WANObjectCache( [
-                       'cache' => $backend,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => $backend
                ] );
 
                $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
@@ -1783,16 +1841,14 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
 
        /**
         * @dataProvider statsKeyProvider
-        * @covers WANObjectCache::determineKeyClass
+        * @covers WANObjectCache::determineKeyClassForStats
         */
        public function testStatsKeyClass( $key, $class ) {
                $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
-                       'cache' => new HashBagOStuff,
-                       'pool' => 'testcache-hash',
-                       'relayer' => new EventRelayerNull( [] )
+                       'cache' => new HashBagOStuff
                ] ) );
 
-               $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
+               $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
        }
 }