Avoid computing negative values for offsets, making zero the lowest possible value

Fixes #25
This commit is contained in:
R. Tyler Croy 2015-03-22 10:56:26 -07:00
parent dc33298435
commit 3a9caa2535
3 changed files with 21 additions and 3 deletions

View File

@ -7,7 +7,7 @@ apply plugin: 'application'
group = "com.github.lookout"
description = "A utility for monitoring the delay of Kafka consumers"
version = '0.1.7'
version = '0.1.8'
mainClassName = 'com.github.lookout.verspaetung.Main'
defaultTasks 'clean', 'check'
sourceCompatibility = '1.7'

View File

@ -39,7 +39,15 @@ class ConsumerGauge implements Gauge<Integer>, Tagged {
(!this.topics.containsKey(topicPartition))) {
return 0
}
return ((Integer)this.topics[topicPartition]) - this.consumers[consumer]
/*
* Returning the maximum value of the computation and zero, there are
* some cases where we can be "behind" on the Kafka latest offset
* polling and this could result in an erroneous negative value. See:
* <https://github.com/lookout/verspaetung/issues/25> for more details
*/
return Math.max(0,
((Integer)this.topics[topicPartition]) - this.consumers[consumer])
}
@Override

View File

@ -35,7 +35,17 @@ class ConsumerGaugeSpec extends Specification {
def "getValue() should return zero for a consumer not in the map"() {
given:
ConsumerGauge gauge = new ConsumerGauge(consumer, [:], [:])
ConsumerGauge gauge = new ConsumerGauge(this.consumer, [:], [:])
expect:
gauge.value == 0
}
def "getValue() should return zero instead of a negative number"() {
given:
ConsumerGauge gauge = new ConsumerGauge(this.consumer,
[(this.consumer) : 10],
[(this.tp) : 5])
expect:
gauge.value == 0