Skip to content

Commit

Permalink
[FLINK-10990][metrics] Enforce minimum timespan in MeterView
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitozi authored and zentol committed Nov 27, 2018
1 parent b379316 commit 4cc9479
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public MeterView(int timeSpanInSeconds) {

public MeterView(Counter counter, int timeSpanInSeconds) {
this.counter = counter;
this.timeSpanInSeconds = timeSpanInSeconds - (timeSpanInSeconds % UPDATE_INTERVAL_SECONDS);
// the time-span must be larger than the update-interval as otherwise the array has a size of 1,
// for which no rate can be computed as no distinct before/after measurement exists.
this.timeSpanInSeconds = Math.max(
timeSpanInSeconds - (timeSpanInSeconds % UPDATE_INTERVAL_SECONDS),
UPDATE_INTERVAL_SECONDS);
this.values = new long[this.timeSpanInSeconds / UPDATE_INTERVAL_SECONDS + 1];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.junit.Test;

import static org.apache.flink.metrics.View.UPDATE_INTERVAL_SECONDS;
import static org.junit.Assert.assertEquals;

/**
Expand Down Expand Up @@ -94,4 +95,14 @@ public void testGetRate() {
assertEquals(0.0, m.getRate(), 0.1); // 480 - 480 / 60

}

@Test
public void testTimeSpanBelowUpdateRate() {
int timeSpanInSeconds = 1;
MeterView m = new MeterView(timeSpanInSeconds);
assert timeSpanInSeconds < UPDATE_INTERVAL_SECONDS;
m.markEvent();
m.update();
assertEquals(0.2, m.getRate(), 0.0);
}
}

0 comments on commit 4cc9479

Please sign in to comment.