[MediaWiki-commits] [Gerrit] mediawiki...ORES[master]: Rate limit thresholds failures to once per (minute x model x...

2017-11-28 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/393922 )

Change subject: Rate limit thresholds failures to once per (minute x model x 
wiki)
..


Rate limit thresholds failures to once per (minute x model x wiki)

Bug: T181567
Change-Id: I6dc4e6929cb48f4d1f060bf4ab6614db3a62d01f
---
M includes/Stats.php
1 file changed, 17 insertions(+), 15 deletions(-)

Approvals:
  jenkins-bot: Verified
  Halfak: Looks good to me, approved



diff --git a/includes/Stats.php b/includes/Stats.php
index 6f87383..777fb2c 100644
--- a/includes/Stats.php
+++ b/includes/Stats.php
@@ -19,6 +19,7 @@
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
 use Psr\Log\LoggerInterface;
+use WANObjectCache;
 
 class Stats {
 
@@ -42,7 +43,7 @@
 * @param \WANObjectCache $cache
 * @param LoggerInterface $logger
 */
-   public function __construct( Api $api, \WANObjectCache $cache, 
LoggerInterface $logger ) {
+   public function __construct( Api $api, WANObjectCache $cache, 
LoggerInterface $logger ) {
$this->api = $api;
$this->cache = $cache;
$this->logger = $logger;
@@ -52,7 +53,7 @@
$config = $this->getFiltersConfig( $model );
// Skip if the model is unconfigured or set to false.
if ( $config ) {
-   $stats = $this->tryFetchStats( $model, $fromCache );
+   $stats = $this->fetchStats( $model, $fromCache );
// Skip if stats are empty.
if ( $stats !== false ) {
return $this->parseThresholds( $stats, $model );
@@ -84,23 +85,14 @@
return $config;
}
 
-   private function tryFetchStats( $model, $fromCache ) {
-   try {
-   return $this->fetchStats( $model, $fromCache );
-   } catch ( \RuntimeException $exception ) {
-   $this->logger->error( 'Failed to fetch ORES stats: ' . 
$exception->getMessage() );
-   return false;
-   }
-   }
-
private function fetchStats( $model, $fromCache ) {
global $wgOresCacheVersion;
if ( $fromCache ) {
$key = $this->cache->makeKey( 'ORES', 
'threshold_statistics', $model, $wgOresCacheVersion );
$result = $this->cache->getWithSetCallback(
$key,
-   \WANObjectCache::TTL_DAY,
-   function () use ( $model ) {
+   WANObjectCache::TTL_DAY,
+   function ( $oldValue, &$ttl, &$setOpts, $opts ) 
use ( $model ) {
$statsdDataFactory = 
MediaWikiServices::getInstance()->getStatsdDataFactory();
// @deprecated Only catching exceptions 
to allow the
// failure to be cached, remove once 
transition is
@@ -108,10 +100,18 @@
try {
$result = 
$this->fetchStatsFromApi( $model );
$statsdDataFactory->increment( 
'ores.api.stats.ok' );
+
return $result;
} catch ( \RuntimeException $ex ) {
+   // TODO: We can also check the 
service *before* the
+   // cached value expires, and 
therefore reuse the old
+   // value until the service 
recovers in case of failure.
$statsdDataFactory->increment( 
'ores.api.stats.failed' );
-   throw $ex;
+   $this->logger->error( 'Failed 
to fetch ORES stats.' );
+
+   // Retry again soon.
+   $ttl = 
WANObjectCache::TTL_MINUTE;
+   return [];
}
}
);
@@ -276,7 +276,9 @@
}
 
$stat = $config;
-   if ( $bound === 'max' && $statsData['false'][$stat] === null ) {
+   if ( !isset( $statsData['false'] ) || !isset( 
$statsData['true'] ) ) {
+   return null;
+   } elseif ( $bound === 'max' && $statsData['false'][$stat] === 
null ) {
return null;
} elseif ( $bound === 'max' && isset( 
$statsData['false'][$stat]['threshold'] ) ) {
 

[MediaWiki-commits] [Gerrit] mediawiki...ORES[master]: Rate limit thresholds failures to once per (minute x model x...

2017-11-28 Thread Awight (Code Review)
Awight has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/393922 )

Change subject: Rate limit thresholds failures to once per (minute x model x 
wiki)
..

Rate limit thresholds failures to once per (minute x model x wiki)

Bug: T181567
Change-Id: I6dc4e6929cb48f4d1f060bf4ab6614db3a62d01f
---
M includes/Stats.php
1 file changed, 10 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES 
refs/changes/22/393922/1

diff --git a/includes/Stats.php b/includes/Stats.php
index 6f87383..e8e7582 100644
--- a/includes/Stats.php
+++ b/includes/Stats.php
@@ -52,7 +52,7 @@
$config = $this->getFiltersConfig( $model );
// Skip if the model is unconfigured or set to false.
if ( $config ) {
-   $stats = $this->tryFetchStats( $model, $fromCache );
+   $stats = $this->fetchStats( $model, $fromCache );
// Skip if stats are empty.
if ( $stats !== false ) {
return $this->parseThresholds( $stats, $model );
@@ -84,15 +84,6 @@
return $config;
}
 
-   private function tryFetchStats( $model, $fromCache ) {
-   try {
-   return $this->fetchStats( $model, $fromCache );
-   } catch ( \RuntimeException $exception ) {
-   $this->logger->error( 'Failed to fetch ORES stats: ' . 
$exception->getMessage() );
-   return false;
-   }
-   }
-
private function fetchStats( $model, $fromCache ) {
global $wgOresCacheVersion;
if ( $fromCache ) {
@@ -100,7 +91,7 @@
$result = $this->cache->getWithSetCallback(
$key,
\WANObjectCache::TTL_DAY,
-   function () use ( $model ) {
+   function ( $oldValue, &$ttl ) use ( $model ) {
$statsdDataFactory = 
MediaWikiServices::getInstance()->getStatsdDataFactory();
// @deprecated Only catching exceptions 
to allow the
// failure to be cached, remove once 
transition is
@@ -110,8 +101,15 @@
$statsdDataFactory->increment( 
'ores.api.stats.ok' );
return $result;
} catch ( \RuntimeException $ex ) {
+   // TODO: We can also check the 
service *before* the
+   // cached value expires, and 
therefore reuse the old
+   // value until the service 
recovers in case of failure.
$statsdDataFactory->increment( 
'ores.api.stats.failed' );
-   throw $ex;
+   $this->logger->error( 'Failed 
to fetch ORES stats.' );
+
+   // Retry again soon.
+   $ttl = 
\WANObjectCache::TTL_MINUTE;
+   return false;
}
}
);

-- 
To view, visit https://gerrit.wikimedia.org/r/393922
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6dc4e6929cb48f4d1f060bf4ab6614db3a62d01f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Awight 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits