Skip to content

Commit

Permalink
Merge pull request #750 from ChandraAddala/blueflood-rollup-issue
Browse files Browse the repository at this point in the history
Expiring locator cache after 3 days of creation of locator
  • Loading branch information
ChandraAddala committed Nov 1, 2016
2 parents 306834a + 605cccd commit fb65eef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class LocatorCache {
// written per slot. Simply, if a locator has been seen for a slot, don't bother.
private final Cache<String, Boolean> insertedDelayedLocators;

private static LocatorCache instance = new LocatorCache(10, TimeUnit.MINUTES);
private static LocatorCache instance = new LocatorCache(10, TimeUnit.MINUTES,
3, TimeUnit.DAYS);


static {
Expand All @@ -46,20 +47,29 @@ public static LocatorCache getInstance() {
return instance;
}

protected LocatorCache(long expireAfterAccessDuration, TimeUnit expireAfterAccessTimeUnit) {
protected LocatorCache(long expireAfterAccessDuration, TimeUnit expireAfterAccessTimeUnit,
long expireAfterWriteDuration, TimeUnit expireAfterWriteTimeUnit) {

insertedLocators =
CacheBuilder.newBuilder().expireAfterAccess(expireAfterAccessDuration,
expireAfterAccessTimeUnit).concurrencyLevel(16).build();
CacheBuilder.newBuilder()
.expireAfterAccess(expireAfterAccessDuration, expireAfterAccessTimeUnit)
.expireAfterWrite(expireAfterWriteDuration, expireAfterWriteTimeUnit)
.concurrencyLevel(16)
.build();

insertedDelayedLocators =
CacheBuilder.newBuilder().expireAfterAccess(expireAfterAccessDuration,
expireAfterAccessTimeUnit).concurrencyLevel(16).build();
CacheBuilder.newBuilder()
.expireAfterAccess(expireAfterAccessDuration, expireAfterAccessTimeUnit)
.concurrencyLevel(16)
.build();
}

@VisibleForTesting
public static LocatorCache getInstance(Long duration, TimeUnit timeUnit) {
return new LocatorCache(duration, timeUnit);
public static LocatorCache getInstance(long expireAfterAccessDuration, TimeUnit expireAfterAccessTimeUnit,
long expireAfterWriteDuration, TimeUnit expireAfterWriteTimeUnit) {

return new LocatorCache(expireAfterAccessDuration, expireAfterAccessTimeUnit,
expireAfterWriteDuration, expireAfterWriteTimeUnit);
}

public long getCurrentLocatorCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class LocatorCacheTest {

@Before
public void setup() {
locatorCache = LocatorCache.getInstance(2L, TimeUnit.SECONDS);
locatorCache = LocatorCache.getInstance(2L, TimeUnit.SECONDS, 3L, TimeUnit.SECONDS);

}

Expand All @@ -37,6 +37,35 @@ public void testSetLocatorCurrent() throws InterruptedException {
assertTrue("locator not expired from cache", !locatorCache.isLocatorCurrent(LOCATOR));
}

@Test
public void testSetLocatorCurrentCheckForExpirationWithoutReads() throws InterruptedException {

locatorCache.setLocatorCurrent(LOCATOR);

Thread.sleep(3001L);
// it has been more than 3 seconds since it was written.
assertTrue("locator not expired from cache", !locatorCache.isLocatorCurrent(LOCATOR));

}

@Test
public void testSetLocatorCurrentCheckForExpiration() throws InterruptedException {

locatorCache.setLocatorCurrent(LOCATOR);
assertTrue("locator not stored in cache", locatorCache.isLocatorCurrent(LOCATOR));

Thread.sleep(1000L);
assertTrue("locator not stored in cache", locatorCache.isLocatorCurrent(LOCATOR));

Thread.sleep(1000L);
assertTrue("locator not stored in cache", locatorCache.isLocatorCurrent(LOCATOR));

Thread.sleep(1001L);
// it has been more than 3 seconds since it was written. So it has to expire even if it was accessed 1sec before.
assertTrue("locator not expired from cache", !locatorCache.isLocatorCurrent(LOCATOR));

}

@Test
public void testIsLocatorCurrent() throws InterruptedException {
assertTrue("locator which was never set is present in cache", !locatorCache.isLocatorCurrent(LOCATOR));
Expand Down

0 comments on commit fb65eef

Please sign in to comment.