Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Initial draft of SharedMem Cache Algorithm #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/CacheIPCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Random;

public class CacheIPCTest {

private static String output(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
} finally {
br.close();
}
return sb.toString();
}


public static void startAnotherJVM() throws Exception {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder =
new ProcessBuilder(path, "-cp",
classpath,
CacheTest.class.getName());
Process process = processBuilder.start();
System.out.println("Output:\n" + output(process.getInputStream()));
process.waitFor();
}

public static void startAndKillAnotherJVM() throws Exception {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder =
new ProcessBuilder(path, "-cp",
classpath,
CacheTest.class.getName());
Process process = processBuilder.start();
Random rnd = new Random(new Date().getTime());
Thread.sleep(rnd.nextInt(3000) + 300 );
process.destroy();
System.out.println("Distroyed Output:\n" + output(process.getInputStream()));
process.waitFor();
}


public static void main(String[] args) throws Exception {
int numOfJVMs = 5;

Runnable r1 = new Runnable() {

public void run() {
try {
startAnotherJVM();
} catch (Exception e) {
throw (new Error("JVM is flying ...."));
}
}
};


Runnable r2 = new Runnable() {

public void run() {
try {
startAndKillAnotherJVM();
} catch (Exception e) {
throw (new Error("JVM is flying ...."));
}
}
};


Thread[] tArray = new Thread[numOfJVMs];

Runnable r = r1;

for (int i = 0; i < numOfJVMs; i++) {
if (i == numOfJVMs - 2)
r = r2;
tArray[i] = new Thread(r);
tArray[i].start();
}

for (int i = 0; i < numOfJVMs; i++) {
tArray[i].join();
}
}
}
38 changes: 23 additions & 15 deletions src/CacheTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import cache.*;

import java.util.Random;
import java.util.UUID;
import java.util.Arrays;

public class CacheTest {
private static final long K = 1024;
Expand All @@ -9,31 +11,38 @@ public class CacheTest {

private static final long MAGIC = 54331;

private static final int WARMUP_COUNT = 100000;
private static final int RUN_COUNT = 1000000;
private static final int WARMUP_COUNT = 100;
private static final int RUN_COUNT = 1000;
private static final long CACHE_LIMIT = 10*M ;
private static final long DATA_VALUE_SIZE = (CACHE_LIMIT / RUN_COUNT)*4;



public static void testWrite(ICache cache, int count) {
Random random = new Random(0);
Random random = new Random(1);
for (int i = 0; i < count; i++) {
long key = random.nextInt(1 << 20) * MAGIC;
cache.put(key, new byte[random.nextInt(8192)]);
//byte[] key = Arrays.copyOfRange(UUID.randomUUID().toString().intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
byte[] key = Arrays.copyOfRange(Long.toHexString(((random.nextInt() % RUN_COUNT) << 20) * MAGIC).intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
cache.put(key, new SharedMemoryCache.CacheMetaInfo((byte)0,DATA_VALUE_SIZE));
}
}

public static void testRead(ICache cache, int count) {
Random random = new Random(1);
for (int i = 0; i < count; i++) {
long key = random.nextInt(1 << 20) * MAGIC;
byte[] key = Arrays.copyOfRange(Long.toHexString(((random.nextInt() % RUN_COUNT) << 20) * MAGIC).intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
// byte[] key = Arrays.copyOfRange( UUID.randomUUID().toString().intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
cache.get(key);
}
}

public static void testRead9Write1(ICache cache, int count) {
Random random = new Random(2);
Random random = new Random(1);
for (int i = 0; i < count; i++) {
long key = random.nextInt(1 << 20) * MAGIC;
byte[] key = Arrays.copyOfRange(Long.toHexString(((random.nextInt() % RUN_COUNT) << 20) * MAGIC).intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
// byte[] key = Arrays.copyOfRange( UUID.randomUUID().toString().intern().getBytes(), 0, SharedMemoryCache.KEY_SIZE);
if (random.nextInt(10) == 0) {
cache.put(key, new byte[random.nextInt(8192)]);
cache.put(key,new SharedMemoryCache.CacheMetaInfo((byte)0,DATA_VALUE_SIZE));
} else {
cache.get(key);
}
Expand Down Expand Up @@ -65,17 +74,16 @@ public static void testAll(ICache cache) {
System.out.println(cacheClass + " read-write: " + (end - start));
}




public static void main(String[] args) throws Exception {
String type = args.length == 0 ? null : args[0];
ICache cache;
if ("ehcache".equals(type)) {
cache = new Ehcache(2*G);
} else if ("jcs".equals(type)) {
cache = new JCSCache("sampleCache");
} else if ("chm".equals(type)) {
if ("chm".equals(type)) {
cache = new ConcurrentHashMapCache(3000000, 256);
} else {
cache = new UnsafeMemoryCache(new MemoryCacheConfiguration(2*G, 200*K, "/dev/shm/cache-test"));
cache = new SharedMemoryCache(new MemoryCacheConfiguration(100*K, 10*K, CACHE_LIMIT, "/tmp/cache-shm-test"));
}
testAll(cache);
cache.close();
Expand Down
9 changes: 4 additions & 5 deletions src/cache/ConcurrentHashMapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapCache extends ConcurrentHashMap<Long, byte[]> implements ICache {
public class ConcurrentHashMapCache extends ConcurrentHashMap<byte[], SharedMemoryCache.CacheMetaInfo> implements ICache {

public ConcurrentHashMapCache(int capacity, int concurrencyLevel) {
super(capacity, 0.75f, concurrencyLevel);
}

@Override
public byte[] get(long key) {
public SharedMemoryCache.CacheMetaInfo get(byte[]key) {
return super.get(key);
}

@Override
public boolean put(long key, byte[] value) {
super.put(key, value);
return true;
public SharedMemoryCache.CacheMetaInfo put(byte[] key, SharedMemoryCache.CacheMetaInfo value) {
return super.put(key, value) ;
}

@Override
Expand Down
37 changes: 0 additions & 37 deletions src/cache/Ehcache.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/cache/ICache.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cache;

public interface ICache {
byte[] get(long key);
boolean put(long key, byte[] value);
SharedMemoryCache.CacheMetaInfo get(byte[] key);
SharedMemoryCache.CacheMetaInfo put(byte[]key, SharedMemoryCache.CacheMetaInfo value);
void close();
}
32 changes: 0 additions & 32 deletions src/cache/JCSCache.java

This file was deleted.

10 changes: 9 additions & 1 deletion src/cache/MemoryCacheConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ public class MemoryCacheConfiguration {
private long capacity;
private long segmentSize;
private String imageFile;
private long limit;

public MemoryCacheConfiguration(long capacity, long segmentSize, String imageFile) {
public MemoryCacheConfiguration(long capacity, long segmentSize, long limit, String imageFile) {
this.capacity = capacity;
this.segmentSize = segmentSize;
this.imageFile = imageFile;
this.limit = limit;
}

public long getCapacity() {
Expand All @@ -19,7 +21,13 @@ public long getSegmentSize() {
return segmentSize;
}

public long getLimit() {
return limit;
}

public String getImageFile() {
return imageFile;
}


}
Loading