This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 01e80550f0c9c58a79e637b271f11eb503e059a8
Author: LanKhuat <khuatdang...@gmail.com>
AuthorDate: Mon Jun 15 16:32:44 2020 +0700

    JAMES-3269 Use moving average in MessageFastViewProjectionHealthCheck
---
 .../java/org/apache/james/metrics/api/Metric.java  | 11 +++++++++++
 .../james/metrics/dropwizard/DropWizardMetric.java | 23 +++++++++++++---------
 .../dropwizard/DropWizardMetricFactory.java        |  2 +-
 .../metrics/dropwizard/DropWizardMetricTest.java   |  2 +-
 .../MessageFastViewProjectionHealthCheck.java      | 10 +++++-----
 .../MessageFastViewProjectionHealthCheckTest.java  | 15 +++++++-------
 6 files changed, 39 insertions(+), 24 deletions(-)

diff --git 
a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java 
b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
index 8f35998..87efdd9 100644
--- a/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
+++ b/metrics/metrics-api/src/main/java/org/apache/james/metrics/api/Metric.java
@@ -35,4 +35,15 @@ public interface Metric {
      * Implementation might be doing strict validation (throwing on negative 
value) or lenient (log and sanitize to 0)
      */
     long getCount();
+
+    /**
+     * Moving average.
+     *
+     * Period of the moving average is implementation defined.
+     *
+     * Default to count (naive implementation with period starting at boot 
time)
+     */
+    default double movingAverage() {
+        return Long.valueOf(getCount()).doubleValue();
+    }
 }
diff --git 
a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
 
b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
index 58a0332..02d6fa5 100644
--- 
a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
+++ 
b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetric.java
@@ -23,43 +23,43 @@ import org.apache.james.metrics.api.Metric;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.codahale.metrics.Counter;
+import com.codahale.metrics.Meter;
 
 public class DropWizardMetric implements Metric {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(DropWizardMetric.class);
 
-    private final Counter counter;
+    private final Meter meter;
     private final String metricName;
 
-    public DropWizardMetric(Counter counter, String metricName) {
-        this.counter = counter;
+    public DropWizardMetric(Meter meter, String metricName) {
+        this.meter = meter;
         this.metricName = metricName;
     }
 
     @Override
     public void increment() {
-        counter.inc();
+        meter.mark();
     }
 
     @Override
     public void decrement() {
-        counter.dec();
+        meter.mark(-1);
     }
 
     @Override
     public void add(int value) {
-        counter.inc(value);
+        meter.mark(value);
     }
 
     @Override
     public void remove(int value) {
-        counter.dec(value);
+        meter.mark(-1 * value);
     }
 
     @Override
     public long getCount() {
-        long value = counter.getCount();
+        long value = meter.getCount();
         if (value < 0) {
             LOGGER.error("counter value({}) of the metric '{}' should not be a 
negative number", value, metricName);
             return 0;
@@ -67,4 +67,9 @@ public class DropWizardMetric implements Metric {
 
         return value;
     }
+
+    @Override
+    public double movingAverage() {
+        return meter.getFiveMinuteRate();
+    }
 }
diff --git 
a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
 
b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
index b18a04e..a647407 100644
--- 
a/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
+++ 
b/metrics/metrics-dropwizard/src/main/java/org/apache/james/metrics/dropwizard/DropWizardMetricFactory.java
@@ -50,7 +50,7 @@ public class DropWizardMetricFactory implements 
MetricFactory, Startable {
 
     @Override
     public Metric generate(String name) {
-        return new DropWizardMetric(metricRegistry.counter(name), name);
+        return new DropWizardMetric(metricRegistry.meter(name), name);
     }
 
     @Override
diff --git 
a/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
 
b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
index f7ab436..7651d7f 100644
--- 
a/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
+++ 
b/metrics/metrics-dropwizard/src/test/java/org/apache/james/metrics/dropwizard/DropWizardMetricTest.java
@@ -37,7 +37,7 @@ class DropWizardMetricTest implements MetricContract {
     @BeforeEach
     void setUp() {
         MetricRegistry registry = new MetricRegistry();
-        testee = new DropWizardMetric(registry.counter(METRIC_NAME), 
METRIC_NAME);
+        testee = new DropWizardMetric(registry.meter(METRIC_NAME), 
METRIC_NAME);
     }
 
     @Override
diff --git 
a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheck.java
 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheck.java
index 25adc02..74f24b2 100644
--- 
a/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheck.java
+++ 
b/server/data/data-jmap/src/main/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheck.java
@@ -53,23 +53,23 @@ public class MessageFastViewProjectionHealthCheck 
implements HealthCheck {
 
     @Override
     public Mono<Result> check() {
-        return Mono.fromCallable(() -> retrieveMissCountMetric.getCount())
+        return Mono.fromCallable(retrieveMissCountMetric::movingAverage)
             .flatMap(missCount -> {
                 if (missCount == 0) {
                     return Mono.just(Result.healthy(COMPONENT_NAME));
                 } else {
-                    return Mono.fromCallable(() -> 
retrieveHitCountMetric.getCount())
+                    return 
Mono.fromCallable(retrieveHitCountMetric::movingAverage)
                         .map(hitCount -> check(hitCount, missCount));
                 }
             });
     }
 
-    private Result check(long hitCount, long missCount) {
-        long totalCount = hitCount + missCount;
+    private Result check(double hitCount, double missCount) {
+        double totalCount = hitCount + missCount;
         double missCountPercentage = missCount * 100.0d / totalCount;
         if (missCountPercentage > MAXIMUM_MISS_PERCENTAGE_ACCEPTED) {
             return Result.degraded(COMPONENT_NAME,
-                String.format("retrieveMissCount percentage %s%% (%d/%d) is 
higher than the threshold %s%%",
+                String.format("retrieveMissCount percentage %s%% (%s/%s) is 
higher than the threshold %s%%",
                     missCountPercentage, missCount, totalCount, 
MAXIMUM_MISS_PERCENTAGE_ACCEPTED));
         }
 
diff --git 
a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheckTest.java
 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheckTest.java
index 2b4dc5f..da44964 100644
--- 
a/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheckTest.java
+++ 
b/server/data/data-jmap/src/test/java/org/apache/james/jmap/api/projections/MessageFastViewProjectionHealthCheckTest.java
@@ -72,7 +72,7 @@ class MessageFastViewProjectionHealthCheckTest {
             missMetric.increment();
 
             assertThat(testee.check().block())
-                .isEqualTo(Result.degraded(COMPONENT_NAME, "retrieveMissCount 
percentage 100.0% (2/2) is higher than the threshold 10.0%"));
+                .isEqualTo(Result.degraded(COMPONENT_NAME, "retrieveMissCount 
percentage 100.0% (2.0/2.0) is higher than the threshold 10.0%"));
         }
 
         @Test
@@ -100,7 +100,7 @@ class MessageFastViewProjectionHealthCheckTest {
 
             assertThat(testee.check().block())
                 .isEqualTo(Result.degraded(COMPONENT_NAME,
-                    "retrieveMissCount percentage 25.0% (1/4) is higher than 
the threshold 10.0%"));
+                    "retrieveMissCount percentage 25.0% (1.0/4.0) is higher 
than the threshold 10.0%"));
         }
 
         @Test
@@ -116,7 +116,7 @@ class MessageFastViewProjectionHealthCheckTest {
             SoftAssertions.assertSoftly(softly -> {
                 softly.assertThat(resultWithLessHit)
                     .isEqualTo(Result.degraded(COMPONENT_NAME,
-                    "retrieveMissCount percentage 50.0% (1/2) is higher than 
the threshold 10.0%"));
+                    "retrieveMissCount percentage 50.0% (1.0/2.0) is higher 
than the threshold 10.0%"));
                 softly.assertThat(resultWithMoreHit)
                     .isEqualTo(Result.healthy(COMPONENT_NAME));
             });
@@ -135,15 +135,15 @@ class MessageFastViewProjectionHealthCheckTest {
             SoftAssertions.assertSoftly(softly -> {
                 softly.assertThat(resultWithLessHit)
                     .isEqualTo(Result.degraded(COMPONENT_NAME,
-                    "retrieveMissCount percentage 50.0% (1/2) is higher than 
the threshold 10.0%"));
+                    "retrieveMissCount percentage 50.0% (1.0/2.0) is higher 
than the threshold 10.0%"));
                 softly.assertThat(resultWithMoreHit)
                     .isEqualTo(Result.degraded(COMPONENT_NAME,
-                        "retrieveMissCount percentage 20.0% (1/5) is higher 
than the threshold 10.0%"));
+                        "retrieveMissCount percentage 20.0% (1.0/5.0) is 
higher than the threshold 10.0%"));
             });
         }
 
         @Test
-        void checkShouldReturnDegradedAfterMoreHiss() {
+        void checkShouldReturnDegradedAfterMoreMiss() {
             missMetric.increment();
             // enough of hits
             hitMetric.add(10);
@@ -158,8 +158,7 @@ class MessageFastViewProjectionHealthCheckTest {
                 softly.assertThat(resultWithEnoughOfHits)
                     .isEqualTo(Result.healthy(COMPONENT_NAME));
                 softly.assertThat(resultWithMoreMiss)
-                    .isEqualTo(Result.degraded(COMPONENT_NAME,
-                        "retrieveMissCount percentage 16.666666666666668% 
(2/12) is higher than the threshold 10.0%"));
+                    .isEqualTo(Result.degraded(COMPONENT_NAME, 
"retrieveMissCount percentage 16.666666666666668% (2.0/12.0) is higher than the 
threshold 10.0%"));
             });
         }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to