Skip to content

Commit d7fe4e3

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "objectcache: Migrate WANObjectCache metrics to StatsLib"
2 parents a46f635 + 68dea2a commit d7fe4e3

File tree

2 files changed

+91
-36
lines changed

2 files changed

+91
-36
lines changed

includes/ServiceWiring.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ static function () use ( $services ) {
12531253
];
12541254
if ( MW_ENTRY_POINT !== 'cli' ) {
12551255
// Send the statsd data post-send on HTTP requests; avoid in CLI mode (T181385)
1256-
$wanParams['stats'] = $services->getStatsdDataFactory();
1256+
$wanParams['stats'] = $services->getStatsFactory();
12571257
// Let pre-emptive refreshes happen post-send on HTTP requests
12581258
$wanParams['asyncHandler'] = [ DeferredUpdates::class, 'addCallableUpdate' ];
12591259
}

includes/libs/objectcache/wancache/WANObjectCache.php

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* @ingroup Cache
2020
*/
2121

22-
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
2322
use Psr\Log\LoggerAwareInterface;
2423
use Psr\Log\LoggerInterface;
2524
use Psr\Log\NullLogger;
@@ -28,6 +27,7 @@
2827
use Wikimedia\ObjectCache\BagOStuff;
2928
use Wikimedia\ObjectCache\EmptyBagOStuff;
3029
use Wikimedia\ObjectCache\IStoreKeyEncoder;
30+
use Wikimedia\Stats\StatsFactory;
3131

3232
/**
3333
* Multi-datacenter aware caching interface
@@ -168,7 +168,7 @@ class WANObjectCache implements
168168
protected $processCaches = [];
169169
/** @var LoggerInterface */
170170
protected $logger;
171-
/** @var StatsdDataFactoryInterface */
171+
/** @var StatsFactory */
172172
protected $stats;
173173
/** @var callable|null Function that takes a WAN cache callback and runs it later */
174174
protected $asyncHandler;
@@ -336,7 +336,8 @@ class WANObjectCache implements
336336
* @param array $params
337337
* - cache : BagOStuff object for a persistent cache
338338
* - logger : LoggerInterface object
339-
* - stats : StatsdDataFactoryInterface object
339+
* - stats : StatsFactory object. Since 1.43, constructing a WANObjectCache object
340+
* with an IBufferingStatsdDataFactory stats collector will emit a warning.
340341
* - asyncHandler : A function that takes a callback and runs it later. If supplied,
341342
* whenever a preemptive refresh would be triggered in getWithSetCallback(), the
342343
* current cache value is still used instead. However, the async-handler function
@@ -373,9 +374,17 @@ public function __construct( array $params ) {
373374
}
374375

375376
$this->setLogger( $params['logger'] ?? new NullLogger() );
376-
$this->stats = $params['stats'] ?? new NullStatsdDataFactory();
377-
$this->asyncHandler = $params['asyncHandler'] ?? null;
378377

378+
if ( isset( $params['stats'] ) && $params['stats'] instanceof IBufferingStatsdDataFactory ) {
379+
wfDeprecated(
380+
__METHOD__,
381+
'Use of StatsdDataFactory is deprecated in 1.43. Use StatsFactory instead.'
382+
);
383+
$params['stats'] = null;
384+
}
385+
$this->stats = $params['stats'] ?? StatsFactory::newNull();
386+
387+
$this->asyncHandler = $params['asyncHandler'] ?? null;
379388
$this->missLog = array_fill( 0, 10, [ '', 0.0 ] );
380389
}
381390

@@ -812,7 +821,11 @@ final public function set( $key, $value, $ttl = self::TTL_INDEFINITE, array $opt
812821
$opts['creating'] ?? false
813822
);
814823

815-
$this->stats->increment( "wanobjectcache.$keygroup.set." . ( $ok ? 'ok' : 'error' ) );
824+
$this->stats->getCounter( 'wanobjectcache_set_total' )
825+
->setLabel( 'keygroup', $keygroup )
826+
->setLabel( 'result', ( $ok ? 'ok' : 'error' ) )
827+
->copyToStatsdAt( "wanobjectcache.$keygroup.set." . ( $ok ? 'ok' : 'error' ) )
828+
->increment();
816829

817830
return $ok;
818831
}
@@ -1065,7 +1078,12 @@ final public function delete( $key, $ttl = self::HOLDOFF_TTL ) {
10651078
}
10661079

10671080
$keygroup = $this->determineKeyGroupForStats( $key );
1068-
$this->stats->increment( "wanobjectcache.$keygroup.delete." . ( $ok ? 'ok' : 'error' ) );
1081+
1082+
$this->stats->getCounter( 'wanobjectcache_delete_total' )
1083+
->setLabel( 'keygroup', $keygroup )
1084+
->setLabel( 'result', ( $ok ? 'ok' : 'error' ) )
1085+
->copyToStatsdAt( "wanobjectcache.$keygroup.delete." . ( $ok ? 'ok' : 'error' ) )
1086+
->increment();
10691087

10701088
return $ok;
10711089
}
@@ -1224,7 +1242,12 @@ final public function touchCheckKey( $key, $holdoff = self::HOLDOFF_TTL ) {
12241242
$ok = $this->relayVolatilePurge( $checkSisterKey, $purge, self::CHECK_KEY_TTL );
12251243

12261244
$keygroup = $this->determineKeyGroupForStats( $key );
1227-
$this->stats->increment( "wanobjectcache.$keygroup.ck_touch." . ( $ok ? 'ok' : 'error' ) );
1245+
1246+
$this->stats->getCounter( 'wanobjectcache_check_total' )
1247+
->setLabel( 'keygroup', $keygroup )
1248+
->setLabel( 'result', ( $ok ? 'ok' : 'error' ) )
1249+
->copyToStatsdAt( "wanobjectcache.$keygroup.ck_touch." . ( $ok ? 'ok' : 'error' ) )
1250+
->increment();
12281251

12291252
return $ok;
12301253
}
@@ -1261,7 +1284,12 @@ final public function resetCheckKey( $key ) {
12611284
$ok = $this->relayNonVolatilePurge( $checkSisterKey );
12621285

12631286
$keygroup = $this->determineKeyGroupForStats( $key );
1264-
$this->stats->increment( "wanobjectcache.$keygroup.ck_reset." . ( $ok ? 'ok' : 'error' ) );
1287+
1288+
$this->stats->getCounter( 'wanobjectcache_reset_total' )
1289+
->setLabel( 'keygroup', $keygroup )
1290+
->setLabel( 'result', ( $ok ? 'ok' : 'error' ) )
1291+
->copyToStatsdAt( "wanobjectcache.$keygroup.ck_reset." . ( $ok ? 'ok' : 'error' ) )
1292+
->increment();
12651293

12661294
return $ok;
12671295
}
@@ -1641,21 +1669,27 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
16411669
// Get the current key value and its metadata
16421670
$curState = $this->fetchKeys( [ $key ], $checkKeys, $startTime, $touchedCb )[$key];
16431671
$curValue = $curState[self::RES_VALUE];
1672+
16441673
// Use the cached value if it exists and is not due for synchronous regeneration
16451674
if ( $this->isAcceptablyFreshValue( $curState, $graceTTL, $minAsOf ) ) {
16461675
if ( !$this->isLotteryRefreshDue( $curState, $lowTTL, $ageNew, $hotTTR, $startTime ) ) {
1647-
$this->stats->timing(
1648-
"wanobjectcache.$keygroup.hit.good",
1649-
1e3 * ( $this->getCurrentTime() - $startTime )
1650-
);
1676+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1677+
->setLabel( 'keygroup', $keygroup )
1678+
->setLabel( 'result', 'hit' )
1679+
->setLabel( 'reason', 'good' )
1680+
->copyToStatsdAt( "wanobjectcache.$keygroup.hit.good" )
1681+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
16511682

16521683
return [ $curValue, $curState[self::RES_VERSION], $curState[self::RES_AS_OF] ];
16531684
} elseif ( $this->scheduleAsyncRefresh( $key, $ttl, $callback, $opts, $cbParams ) ) {
16541685
$this->logger->debug( "fetchOrRegenerate($key): hit with async refresh" );
1655-
$this->stats->timing(
1656-
"wanobjectcache.$keygroup.hit.refresh",
1657-
1e3 * ( $this->getCurrentTime() - $startTime )
1658-
);
1686+
1687+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1688+
->setLabel( 'keygroup', $keygroup )
1689+
->setLabel( 'result', 'hit' )
1690+
->setLabel( 'reason', 'refresh' )
1691+
->copyToStatsdAt( "wanobjectcache.$keygroup.hit.refresh" )
1692+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
16591693

16601694
return [ $curValue, $curState[self::RES_VERSION], $curState[self::RES_AS_OF] ];
16611695
} else {
@@ -1687,10 +1721,13 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
16871721
$safeMinAsOf = max( $minAsOf, $lastPurgeTime + self::TINY_POSITIVE );
16881722
if ( $this->isExtremelyNewValue( $volState, $safeMinAsOf, $startTime ) ) {
16891723
$this->logger->debug( "fetchOrRegenerate($key): volatile hit" );
1690-
$this->stats->timing(
1691-
"wanobjectcache.$keygroup.hit.volatile",
1692-
1e3 * ( $this->getCurrentTime() - $startTime )
1693-
);
1724+
1725+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1726+
->setLabel( 'keygroup', $keygroup )
1727+
->setLabel( 'result', 'hit' )
1728+
->setLabel( 'reason', 'volatile' )
1729+
->copyToStatsdAt( "wanobjectcache.$keygroup.hit.volatile" )
1730+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
16941731

16951732
return [ $volValue, $volState[self::RES_VERSION], $curState[self::RES_AS_OF] ];
16961733
}
@@ -1730,19 +1767,26 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
17301767
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable False positive
17311768
if ( $this->isValid( $volValue, $volState[self::RES_AS_OF], $minAsOf ) ) {
17321769
$this->logger->debug( "fetchOrRegenerate($key): returning stale value" );
1733-
$this->stats->timing(
1734-
"wanobjectcache.$keygroup.hit.stale",
1735-
1e3 * ( $this->getCurrentTime() - $startTime )
1736-
);
1770+
1771+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1772+
->setLabel( 'keygroup', $keygroup )
1773+
->setLabel( 'result', 'hit' )
1774+
->setLabel( 'reason', 'stale' )
1775+
->copyToStatsdAt( "wanobjectcache.$keygroup.hit.stale" )
1776+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
17371777

17381778
return [ $volValue, $volState[self::RES_VERSION], $curState[self::RES_AS_OF] ];
17391779
} elseif ( $busyValue !== null ) {
17401780
$miss = is_infinite( $minAsOf ) ? 'renew' : 'miss';
17411781
$this->logger->debug( "fetchOrRegenerate($key): busy $miss" );
1742-
$this->stats->timing(
1743-
"wanobjectcache.$keygroup.$miss.busy",
1744-
1e3 * ( $this->getCurrentTime() - $startTime )
1745-
);
1782+
1783+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1784+
->setLabel( 'keygroup', $keygroup )
1785+
->setLabel( 'result', $miss )
1786+
->setLabel( 'reason', 'busy' )
1787+
->copyToStatsdAt( "wanobjectcache.$keygroup.$miss.busy" )
1788+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
1789+
17461790
$placeholderValue = $this->resolveBusyValue( $busyValue );
17471791

17481792
return [ $placeholderValue, $version, $curState[self::RES_AS_OF] ];
@@ -1773,7 +1817,11 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
17731817

17741818
// How long it took to generate the value
17751819
$walltime = max( $postCallbackTime - $preCallbackTime, 0.0 );
1776-
$this->stats->timing( "wanobjectcache.$keygroup.regen_walltime", 1e3 * $walltime );
1820+
1821+
$this->stats->getTiming( 'wanobjectcache_regen_seconds' )
1822+
->setLabel( 'keygroup', $keygroup )
1823+
->copyToStatsdAt( "wanobjectcache.$keygroup.regen_walltime" )
1824+
->observe( 1e3 * $walltime );
17771825

17781826
// Attempt to save the newly generated value if applicable
17791827
if (
@@ -1782,7 +1830,11 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
17821830
// Current thread was not raced out of a regeneration lock or key is tombstoned
17831831
( !$useRegenerationLock || $hasLock || $isKeyTombstoned )
17841832
) {
1785-
$this->stats->timing( "wanobjectcache.$keygroup.regen_set_delay", 1e3 * $elapsed );
1833+
$this->stats->getTiming( 'wanobjectcache_regen_set_delay_seconds' )
1834+
->setLabel( 'keygroup', $keygroup )
1835+
->copyToStatsdAt( "wanobjectcache.$keygroup.regen_set_delay" )
1836+
->observe( 1e3 * $elapsed );
1837+
17861838
// If the key is write-holed then use the (volatile) interim key as an alternative
17871839
if ( $isKeyTombstoned ) {
17881840
$this->setInterimValue(
@@ -1817,10 +1869,13 @@ private function fetchOrRegenerate( $key, $ttl, $callback, array $opts, array $c
18171869

18181870
$miss = is_infinite( $minAsOf ) ? 'renew' : 'miss';
18191871
$this->logger->debug( "fetchOrRegenerate($key): $miss, new value computed" );
1820-
$this->stats->timing(
1821-
"wanobjectcache.$keygroup.$miss.compute",
1822-
1e3 * ( $this->getCurrentTime() - $startTime )
1823-
);
1872+
1873+
$this->stats->getTiming( 'wanobjectcache_getwithset_seconds' )
1874+
->setLabel( 'keygroup', $keygroup )
1875+
->setLabel( 'result', $miss )
1876+
->setLabel( 'reason', 'compute' )
1877+
->copyToStatsdAt( "wanobjectcache.$keygroup.$miss.compute" )
1878+
->observe( 1e3 * ( $this->getCurrentTime() - $startTime ) );
18241879

18251880
return [ $value, $version, $curState[self::RES_AS_OF] ];
18261881
}

0 commit comments

Comments
 (0)