我写了一个小的infinispan缓存PoC(下面的代码)来尝试和评估infinispan性能。在运行它时,我发现对于我的配置,infinispan显然没有从磁盘中清除缓存条目的旧副本,导致磁盘空间消耗比预期的数量级多。
如何才能将磁盘使用率降低到大致实际数据的大小?
这是我的测试代码:
import org.infinispan.AdvancedCache;
import org.infinispan.manager.DefaultCacheManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Random;
public class App {
final static int ELEMENTS_PER_BIN = 1000;
final static int NUM_OF_BINS = 100;
public static void main(String[] args) throws Exception {
File storeFile = new File("store/store.dat");
if (storeFile.exists() && !storeFile.delete()) {
throw new IllegalStateException("unable to delete store file from previous run");
}
DefaultCacheManager cm = new DefaultCacheManager("infinispan.xml");
AdvancedCache<String, Bin> cache = cm.<String,Bin>getCache("store").getAdvancedCache();
Random rng = new Random(System.currentTimeMillis());
for (int i=0; i<ELEMENTS_PER_BIN; i++) {
for (int j=0; j<NUM_OF_BINS; j++) {
String key = "bin-"+j;
Bin bin = cache.get(key); //get from cache
if (bin==null) {
bin = new Bin();
}
bin.add(rng.nextLong()); //modify
cache.put(key, bin); //write back
}
}
long expectedSize = 0;
for (int j=0; j<NUM_OF_BINS; j++) {
String key = "bin-"+j;
Bin bin = cache.get(key);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(bin);
oos.flush();
oos.close();
expectedSize += baos.size();
baos.close();
}
long actualSize = new File("store/store.dat").length();
System.err.println(ELEMENTS_PER_BIN+" elements x "+NUM_OF_BINS+" bins. expected="+expectedSize+" actual="+actualSize+" in "+cache.size()+" elements. diff="+(actualSize/(double)expectedSize));
}
public static class Bin implements Serializable{
private long[] data = null;
public void add(long datum) {
data = data==null ? new long[1] : Arrays.copyOf(data, data.length+1); //expand capacity
data[data.length-1] = datum;
}
}
}
这里是infinispan配置:
<infinispan
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:6.0 http://www.infinispan.org/schemas/infinispan-config-6.0.xsd"
xmlns="urn:infinispan:config:6.0">
<namedCache name="store">
<eviction strategy="LRU" maxEntries="20"/>
<persistence passivation="false">
<singleFile location="store">
<async enabled="false"/>
</singleFile>
</persistence>
</namedCache>
</infinispan>
infinispan(应该是?)配置为直写式缓存,其中包含RAM中的20个最新元素和磁盘上所有内容的实时副本。
运行上面的代码给出了这个:
1000个元素x 100个垃圾箱。预期= 807300实际= 411664404在100 元素。 DIFF = 509.92741731698254
这意味着对于788 KB的数据,我最终得到一个~392 MB的文件!
我做错了什么?
有问题的infinispan的版本是6.0.2.Final
答案 0 :(得分:2)
如果仅存储更长和更长的记录,则不会重复使用先前使用的空间。 SingleFileStore中没有碎片整理策略,可用空间保留为入口空间列表的映射,但相邻的空闲空间不会合并。 因此,新条目始终添加在文件的末尾,并且开头是分段和未使用的。
顺便说一句,为了找出预期的大小,您还应该: