Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extracted Cache, implemented GuavaCache

  • Loading branch information...
commit 7c68e55cc21d30c1dc31546adb3ea92c89887178 1 parent 4978d38
Daniel Gómez Ferro authored
View
5 pom.xml
@@ -167,5 +167,10 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
+ <dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ <version>14.0-rc1</version>
+ </dependency>
</dependencies>
</project>
View
9 src/main/java/com/yahoo/omid/tso/Cache.java
@@ -0,0 +1,9 @@
+package com.yahoo.omid.tso;
+
+public interface Cache {
+
+ public abstract long set(long key, long value);
+
+ public abstract long get(long key);
+
+}
View
50 src/main/java/com/yahoo/omid/tso/CacheEvaluation.java
@@ -7,14 +7,13 @@
public class CacheEvaluation {
- final static int ENTRIES = 1000000;
- final static int WARMUP_ROUNDS = 20;
- final static int ROUNDS = 20;
- Histogram hist = new Histogram(ENTRIES * 100);
+ final static int ENTRIES = 150000000;
+ final static int WARMUP_ROUNDS = 2;
+ final static int ROUNDS = 4;
+ final static double HOT_PERC = 1;
+// Histogram hist = new Histogram(2 * ENTRIES);
- public void testEntriesAge(int asoc, PrintWriter writer) {
-
- LongCache cache = new LongCache(ENTRIES, asoc);
+ public void testEntriesAge(Cache cache, PrintWriter writer) {
Random random = new Random();
long seed = random.nextLong();
@@ -22,14 +21,21 @@ public void testEntriesAge(int asoc, PrintWriter writer) {
writer.println("# Random seed: " + seed);
random.setSeed(seed);
int removals = 0;
- long totalAge = 0;
double tempStdDev = 0;
double tempAvg = 0;
int i = 0;
int largestDeletedTimestamp = 0;
+ long hotItem = random.nextLong();
+
+ Runtime.getRuntime().gc();
+
for (; i < ENTRIES * WARMUP_ROUNDS; ++i) {
- long removed = cache.set(random.nextLong(), i);
+ long toInsert = random.nextInt(100) < HOT_PERC ? hotItem : random.nextLong();
+ long removed = cache.set(toInsert, i);
+ if (removed > largestDeletedTimestamp) {
+ largestDeletedTimestamp = (int) removed;
+ }
if (removed > largestDeletedTimestamp) {
largestDeletedTimestamp = (int) removed;
}
@@ -41,17 +47,17 @@ public void testEntriesAge(int asoc, PrintWriter writer) {
long time = System.nanoTime();
for (; i < ENTRIES * (WARMUP_ROUNDS + ROUNDS); ++i) {
- long removed = cache.set(random.nextLong(), i);
+ long toInsert = random.nextInt(100) < HOT_PERC ? hotItem : random.nextLong();
+ long removed = cache.set(toInsert, i);
if (removed > largestDeletedTimestamp) {
largestDeletedTimestamp = (int) removed;
}
int gap = i - largestDeletedTimestamp;
removals++;
- totalAge += gap;
double oldAvg = tempAvg;
tempAvg += (gap - tempAvg) / removals;
tempStdDev += (gap - oldAvg) * (gap - tempAvg);
- hist.add(gap);
+// hist.add(gap);
if (i % ENTRIES == 0) {
int round = i / ENTRIES - WARMUP_ROUNDS + 1;
System.err.format("Progress [%d/%d]\n", round, ROUNDS);
@@ -60,25 +66,29 @@ public void testEntriesAge(int asoc, PrintWriter writer) {
long elapsed = System.nanoTime() - time;
double elapsedSeconds = (elapsed / (double) 1000000000);
long totalOps = ENTRIES * ROUNDS;
+ writer.println("# Free mem before GC (MB) :" + (Runtime.getRuntime().freeMemory() / (double) (1024 * 1024)));
+ Runtime.getRuntime().gc();
+ writer.println("# Free mem (MB) :" + (Runtime.getRuntime().freeMemory() / (double) (1024 * 1024)));
writer.println("# Elapsed (s): " + elapsedSeconds);
writer.println("# Elapsed per 100 ops (ms): " + (elapsed / (double) totalOps / 100 / (double) 1000000));
writer.println("# Ops per s : " + (totalOps / elapsedSeconds));
-
- double avgAge = totalAge / (double) removals;
- // LOG.info("Avg gap: " + (avgAge ));
writer.println("# Avg gap: " + (tempAvg));
writer.println("# Std dev gap: " + Math.sqrt((tempStdDev / ENTRIES)));
- hist.print(writer);
- // System.out.println(hist.toString());
- // assertThat(avgAge, is(greaterThan(entries * .9 )));
+// hist.print(writer);
}
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
- int[] asoc = new int[] { 1, 2, 4, 8, 16, 32 };
+ int[] asoc = new int[] { 8, 16, 32 };
for (int i = 0; i < asoc.length; ++i) {
PrintWriter writer = new PrintWriter(asoc[i] + ".out", "UTF-8");
- new CacheEvaluation().testEntriesAge(asoc[i], writer);
+ new CacheEvaluation().testEntriesAge(new LongCache(ENTRIES, asoc[i]), writer);
writer.close();
}
+ {
+ PrintWriter writer = new PrintWriter("guava.out", "UTF-8");
+ new CacheEvaluation().testEntriesAge(new GuavaCache(ENTRIES), writer);
+ writer.close();
+ }
+
}
}
View
4 src/main/java/com/yahoo/omid/tso/CommitHashMap.java
@@ -34,8 +34,8 @@
class CommitHashMap {
private long largestDeletedTimestamp;
- private final LongCache startCommitMapping;
- private final LongCache rowsCommitMapping;
+ private final Cache startCommitMapping;
+ private final Cache rowsCommitMapping;
/**
* Constructs a new, empty hashtable with a default size of 1000
View
45 src/main/java/com/yahoo/omid/tso/GuavaCache.java
@@ -0,0 +1,45 @@
+package com.yahoo.omid.tso;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalCause;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.RemovalNotification;
+
+public class GuavaCache implements Cache, RemovalListener<Long, Long> {
+
+ private static final Log LOG = LogFactory.getLog(GuavaCache.class);
+ private com.google.common.cache.Cache<Long, Long> cache;
+ private long removed;
+
+ public GuavaCache(int size) {
+ cache = CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(size).initialCapacity(size)
+ .removalListener(this).build();
+ }
+
+ @Override
+ public long set(long key, long value) {
+ cache.put(key, value);
+ // cache.cleanUp();
+ return removed;
+ }
+
+ @Override
+ public long get(long key) {
+ Long result = cache.getIfPresent(key);
+ return result == null ? 0 : result;
+ }
+
+ @Override
+ public void onRemoval(RemovalNotification<Long, Long> notification) {
+ if (notification.getCause() == RemovalCause.REPLACED) {
+ return;
+ }
+// LOG.warn("Removing " + notification);
+// new Exception().printStackTrace();
+ removed = Math.max(removed, notification.getValue());
+ }
+
+}
View
6 src/main/java/com/yahoo/omid/tso/Histogram.java
@@ -10,6 +10,7 @@
final private int size;
final private int[] counts;
private int max;
+ private int min = Integer.MIN_VALUE;
public Histogram(int size) {
this.size = size;
@@ -25,6 +26,9 @@ public void add(int i) {
if (i > max) {
max = i;
}
+ if (i < min) {
+ min = i;
+ }
}
@Override
@@ -42,7 +46,7 @@ public String toString() {
}
public void log() {
- for (int i = 0; i <= max; ++i) {
+ for (int i = min; i <= max; ++i) {
LOG.debug(String.format("[%5d]\t%5d", i, counts[i]));
}
}
View
15 src/main/java/com/yahoo/omid/tso/LongCache.java
@@ -1,6 +1,6 @@
package com.yahoo.omid.tso;
-public class LongCache {
+public class LongCache implements Cache {
private final long [] cache;
private final int size;
@@ -12,12 +12,21 @@ public LongCache(int size, int associativity) {
this.associativity = associativity;
}
+ /* (non-Javadoc)
+ * @see com.yahoo.omid.tso.Cache#set(long, long)
+ */
+ @Override
public long set(long key, long value) {
final int index = index(key);
int oldestIndex = 0;
long oldestValue = Long.MAX_VALUE;
for (int i = 0; i < associativity; ++i) {
int currIndex = 2 * (index + i);
+ if (cache[currIndex] == key) {
+ oldestValue = 0;
+ oldestIndex = currIndex;
+ break;
+ }
if (cache[currIndex + 1] <= oldestValue) {
oldestValue = cache[currIndex + 1];
oldestIndex = currIndex;
@@ -28,6 +37,10 @@ public long set(long key, long value) {
return oldestValue;
}
+ /* (non-Javadoc)
+ * @see com.yahoo.omid.tso.Cache#get(long)
+ */
+ @Override
public long get(long key) {
final int index = index(key);
for (int i = 0; i < associativity; ++i) {
View
2  src/test/java/com/yahoo/omid/tso/TestLongCache.java
@@ -21,7 +21,7 @@
public void testEntriesAge() {
- LongCache cache = new LongCache(entries, 16);
+ Cache cache = new LongCache(entries, 16);
Random random = new Random();
long seed = random.nextLong();
Please sign in to comment.
Something went wrong with that request. Please try again.