Use longs for Cache.CacheStats to avoid overflow

This commit is contained in:
Jason Tedor 2015-10-08 17:43:15 -04:00
parent 9ca032ae9d
commit e0fa3297bd
2 changed files with 20 additions and 21 deletions

View File

@ -504,37 +504,37 @@ public class Cache<K, V> {
* @return the current cache statistics * @return the current cache statistics
*/ */
public CacheStats stats() { public CacheStats stats() {
int hits = 0; long hits = 0;
int misses = 0; long misses = 0;
int evictions = 0; long evictions = 0;
for (int i = 0; i < segments.length; i++) { for (int i = 0; i < segments.length; i++) {
hits += segments[i].segmentStats.hits.intValue(); hits += segments[i].segmentStats.hits.longValue();
misses += segments[i].segmentStats.misses.intValue(); misses += segments[i].segmentStats.misses.longValue();
evictions += segments[i].segmentStats.evictions.intValue(); evictions += segments[i].segmentStats.evictions.longValue();
} }
return new CacheStats(hits, misses, evictions); return new CacheStats(hits, misses, evictions);
} }
public static class CacheStats { public static class CacheStats {
private int hits; private long hits;
private int misses; private long misses;
private int evictions; private long evictions;
public CacheStats(int hits, int misses, int evictions) { public CacheStats(long hits, long misses, long evictions) {
this.hits = hits; this.hits = hits;
this.misses = misses; this.misses = misses;
this.evictions = evictions; this.evictions = evictions;
} }
public int getHits() { public long getHits() {
return hits; return hits;
} }
public int getMisses() { public long getMisses() {
return misses; return misses;
} }
public int getEvictions() { public long getEvictions() {
return evictions; return evictions;
} }
} }

View File

@ -25,7 +25,6 @@ import org.junit.Before;
import java.util.*; import java.util.*;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReferenceArray; import java.util.concurrent.atomic.AtomicReferenceArray;
@ -43,7 +42,7 @@ public class CacheTests extends ESTestCase {
// cache some entries, then randomly lookup keys that do not exist, then check the stats // cache some entries, then randomly lookup keys that do not exist, then check the stats
public void testCacheStats() { public void testCacheStats() {
AtomicInteger evictions = new AtomicInteger(); AtomicLong evictions = new AtomicLong();
Set<Integer> keys = new HashSet<>(); Set<Integer> keys = new HashSet<>();
Cache<Integer, String> cache = Cache<Integer, String> cache =
CacheBuilder.<Integer, String>builder() CacheBuilder.<Integer, String>builder()
@ -59,9 +58,9 @@ public class CacheTests extends ESTestCase {
keys.add(i); keys.add(i);
cache.put(i, Integer.toString(i)); cache.put(i, Integer.toString(i));
} }
int hits = 0; long hits = 0;
int misses = 0; long misses = 0;
int missingKey = 0; Integer missingKey = 0;
for (Integer key : keys) { for (Integer key : keys) {
--missingKey; --missingKey;
if (rarely()) { if (rarely()) {
@ -74,7 +73,7 @@ public class CacheTests extends ESTestCase {
} }
assertEquals(hits, cache.stats().getHits()); assertEquals(hits, cache.stats().getHits());
assertEquals(misses, cache.stats().getMisses()); assertEquals(misses, cache.stats().getMisses());
assertEquals((int) Math.ceil(numberOfEntries / 2.0), evictions.get()); assertEquals((long) Math.ceil(numberOfEntries / 2.0), evictions.get());
assertEquals(evictions.get(), cache.stats().getEvictions()); assertEquals(evictions.get(), cache.stats().getEvictions());
} }
@ -84,7 +83,7 @@ public class CacheTests extends ESTestCase {
// for each batch // for each batch
public void testCacheEvictions() { public void testCacheEvictions() {
int maximumWeight = randomIntBetween(1, numberOfEntries); int maximumWeight = randomIntBetween(1, numberOfEntries);
AtomicInteger evictions = new AtomicInteger(); AtomicLong evictions = new AtomicLong();
List<Integer> evictedKeys = new ArrayList<>(); List<Integer> evictedKeys = new ArrayList<>();
Cache<Integer, String> cache = Cache<Integer, String> cache =
CacheBuilder.<Integer, String>builder() CacheBuilder.<Integer, String>builder()
@ -145,7 +144,7 @@ public class CacheTests extends ESTestCase {
public void testWeigher() { public void testWeigher() {
int maximumWeight = 2 * numberOfEntries; int maximumWeight = 2 * numberOfEntries;
int weight = randomIntBetween(2, 10); int weight = randomIntBetween(2, 10);
AtomicInteger evictions = new AtomicInteger(); AtomicLong evictions = new AtomicLong();
Cache<Integer, String> cache = Cache<Integer, String> cache =
CacheBuilder.<Integer, String>builder() CacheBuilder.<Integer, String>builder()
.setMaximumWeight(maximumWeight) .setMaximumWeight(maximumWeight)