Skip to content

Commit

Permalink
Fix Purger handling of large window sizes
Browse files Browse the repository at this point in the history
Large window sizes such as `1000` would trigger an incorrect trimmed
window size due to int arithmetic overflow.
  • Loading branch information
johnmartel committed Aug 28, 2017
1 parent e347410 commit cb77645
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/main/java/dk/dma/ais/coverage/Purger.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
*/
package dk.dma.ais.coverage;

import java.util.Date;

import dk.dma.ais.coverage.data.ICoverageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dk.dma.ais.coverage.data.ICoverageData;
import java.util.Date;

public class Purger extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(Purger.class);
Expand All @@ -28,8 +27,7 @@ public class Purger extends Thread {
private final ICoverageData dataHandler;
private final int pollTimeInSeconds;

public Purger(int maxWindowSize, ICoverageData dataHandler,
int pollTimeInSeconds) {
public Purger(int maxWindowSize, ICoverageData dataHandler, int pollTimeInSeconds) {
this.maxWindowSize = maxWindowSize;
this.dataHandler = dataHandler;
this.pollTimeInSeconds = pollTimeInSeconds;
Expand All @@ -39,20 +37,13 @@ public Purger(int maxWindowSize, ICoverageData dataHandler,
public void run() {
while (true) {
if (Helper.latestMessage != null && Helper.firstMessage != null) {
int windowSize = (int) ((Helper.getCeilDate(
Helper.latestMessage).getTime() - Helper.getFloorDate(
Helper.firstMessage).getTime()) / 1000 / 60 / 60);
int currentWindowSize = getCurrentWindowSize();

if (windowSize > maxWindowSize) {
Date trimPoint = new Date(Helper.getCeilDate(
Helper.latestMessage).getTime()
- (1000 * 60 * 60 * maxWindowSize));
LOG.info("Window size: " + windowSize
+ ". Max window size: " + maxWindowSize
+ ". Lets purge data until " + trimPoint);
if (currentWindowSize > maxWindowSize) {
Date trimPoint = getTrimPoint();
LOG.info("Window size: {}. Max window size: {}. Lets purge data until {}", currentWindowSize, maxWindowSize, trimPoint);

dataHandler.trimWindow(trimPoint);

}
}

Expand All @@ -64,4 +55,14 @@ public void run() {
}
}

private int getCurrentWindowSize() {
long latestMessageCeilingDate = Helper.getCeilDate(Helper.latestMessage).getTime();
long firstMessageFloorDate = Helper.getFloorDate(Helper.firstMessage).getTime();
return (int) ((latestMessageCeilingDate - firstMessageFloorDate) / 1000 / 60 / 60);
}

Date getTrimPoint() {
return new Date(Helper.getCeilDate(Helper.latestMessage).getTime() - (1000L * 60 * 60 * maxWindowSize));
}

}
27 changes: 27 additions & 0 deletions src/test/java/dk/dma/ais/coverage/PurgerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dk.dma.ais.coverage;

import dk.dma.commons.util.DateTimeUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.PeriodType;
import org.junit.Test;

import java.util.Date;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class PurgerTest {

@Test
public void givenLargeWindowSize_whenGetTrimPoint_windowSizeCorrectlyConvertedToHours() {
Purger purger = new Purger(1000, null, 1);
Helper.firstMessage = new Date();
final int maxWindowSize = 1000;
Helper.latestMessage = DateUtils.addHours(new Date(), 2 * maxWindowSize);

Date trimPoint = purger.getTrimPoint();

assertThat(DateTimeUtil.toInterval(Helper.firstMessage, trimPoint).toPeriod(PeriodType.hours()).getHours(), is(equalTo(maxWindowSize)));
}
}

0 comments on commit cb77645

Please sign in to comment.