当值为 Treemap 并且 treemap 中的数据增加到 10K 时,Ignite 缓存调用更新非常非常慢
Ignite cache invoke update very very slow when value is Treemap and data in treemap increase upto 10K
我有更新点燃缓存记录的代码逻辑,
缓存定义为:
IgniteCache<String, TreeMap<Long, InfoResultRecord>> txInfoCache;
键是缓存类型字符串,对于值我使用TreeMap来保持记录的顺序(我需要对数据进行排序),但是用于更新的时间随着TreeMap大小的增加而增加,我发现是当TreeMap大小在10K左右时,每次调用一条记录添加到缓存值treemap很慢,大约2秒,如果我有1K数据需要添加到Treemap,需要2000秒,真的很慢并且不可接受。
我使用调用来更新缓存以将记录添加到树图中:
txInfoCache.invoke(txType, new TxInfoProcessor(), record);
缓存的配置是:
CacheConfiguration<String, TreeMap<Long, InfoResultRecord>> cacheCfg =
new CacheConfiguration<>("TxInfoCache");
cacheCfg.setCacheMode(CacheMode.REPLICATED);
//cacheCfg.setStoreKeepBinary(true);
cacheCfg.setAtomicityMode(ATOMIC);
cacheCfg.setBackups(0);
txInfoCache = ignite.getOrCreateCache(cacheCfg);
并且将记录添加到 Treemap 的处理器是:
private static class TxInfoProcessor implements EntryProcessor<
String,
TreeMap<Long, InfoResultRecord>,
TreeMap<Long, InfoResultRecord>> {
@Override
public TreeMap<Long, InfoResultRecord> process(
MutableEntry<String,
TreeMap<Long, InfoResultRecord>> entry, Object... args) {
InfoResultRecord record = (InfoResultRecord) args[0];
final Long oneDayMsSeconds = 24 * 60 * 60 * 1000L;
TreeMap<Long, InfoResultRecord>
InfoResultRecordTreeMap = entry.getValue();
if (InfoResultRecordTreeMap == null) {
InfoResultRecordTreeMap = new TreeMap<>();
}
InfoResultRecordTreeMap.put(record.getDealTime() + oneDayMsSeconds, record);
entry.setValue(InfoResultRecordTreeMap);
return null;
}
}
有什么问题吗?或者我以错误的方式使用缓存?
我还写了一个简单的测试代码来验证 get/put 使用 TreeMap 时的速度:
public class Server2 {
public static void main(String[] args) throws IgniteException {
try (Ignite ignite = Ignition.start("server-start.xml")) {
IgniteCache<String, TreeMap<Long, String>> testCache = ignite.getOrCreateCache("testCache");
testCache.put("my",new TreeMap<>());
while (true) {
StopWatch stopWatch = new StopWatch();
stopWatch.start("1");
TreeMap<Long, String> map = testCache.get("my");
stopWatch.stop();
stopWatch.start("2");
map.put(System.currentTimeMillis(),String.valueOf(new Random().nextInt(1000000000)));
testCache.put("my",map);
stopWatch.stop();
System.out.println("cacheSize:"+map.size()+","+stopWatch.prettyPrint());
}
}
}
}
cacheSize:1000,StopWatch '': running time (millis) = 195
-----------------------------------------
ms % Task name
-----------------------------------------
00080 041% 1
00115 059% 2
cacheSize:1001,StopWatch '': running time (millis) = 38
-----------------------------------------
ms % Task name
-----------------------------------------
00028 074% 1
00010 026% 2
cacheSize:3000,StopWatch '': running time (millis) = 139
-----------------------------------------
ms % Task name
-----------------------------------------
00055 040% 1
00084 060% 2
cacheSize:3001,StopWatch '': running time (millis) = 68
-----------------------------------------
ms % Task name
-----------------------------------------
00042 062% 1
00026 038% 2
很明显当Treemap的大小增加时,ignite cache的消耗时间增加get/put,我认为这应该是1~2ms,但这里是xx ms并且随着大小的增加它甚至会达到xxxms秒。
很明显,在这种情况下 read/write 时间会增加,因为在每次操作时都需要将数据从堆外复制到堆(反之亦然),serialize/deserialize 它, 但是,我已经在本地检查了您的代码,时间并没有那么剧烈地增长。例如,我得到
cacheSize:10082,StopWatch '': running time (millis) = 18
-----------------------------------------
ms % Task name
-----------------------------------------
00009 050% 1
00009 050% 2
无论如何,我认为您没有为您的用例选择一个好的解决方案 - 我建议单独存储所有这些数据,即引入带有 TreeMap 字符串名称字段的键和一行的某些长键和按行存储这些数据。使用它,你的写操作会快得多。此外,在这种情况下,您将需要 collocate data by treemap 名称。要请求所有数据,您可以使用 SQL 和 ORDER BY 语句。并且不要忘记使用索引!
我有更新点燃缓存记录的代码逻辑,
缓存定义为:
IgniteCache<String, TreeMap<Long, InfoResultRecord>> txInfoCache;
键是缓存类型字符串,对于值我使用TreeMap来保持记录的顺序(我需要对数据进行排序),但是用于更新的时间随着TreeMap大小的增加而增加,我发现是当TreeMap大小在10K左右时,每次调用一条记录添加到缓存值treemap很慢,大约2秒,如果我有1K数据需要添加到Treemap,需要2000秒,真的很慢并且不可接受。
我使用调用来更新缓存以将记录添加到树图中:
txInfoCache.invoke(txType, new TxInfoProcessor(), record);
缓存的配置是:
CacheConfiguration<String, TreeMap<Long, InfoResultRecord>> cacheCfg =
new CacheConfiguration<>("TxInfoCache");
cacheCfg.setCacheMode(CacheMode.REPLICATED);
//cacheCfg.setStoreKeepBinary(true);
cacheCfg.setAtomicityMode(ATOMIC);
cacheCfg.setBackups(0);
txInfoCache = ignite.getOrCreateCache(cacheCfg);
并且将记录添加到 Treemap 的处理器是:
private static class TxInfoProcessor implements EntryProcessor<
String,
TreeMap<Long, InfoResultRecord>,
TreeMap<Long, InfoResultRecord>> {
@Override
public TreeMap<Long, InfoResultRecord> process(
MutableEntry<String,
TreeMap<Long, InfoResultRecord>> entry, Object... args) {
InfoResultRecord record = (InfoResultRecord) args[0];
final Long oneDayMsSeconds = 24 * 60 * 60 * 1000L;
TreeMap<Long, InfoResultRecord>
InfoResultRecordTreeMap = entry.getValue();
if (InfoResultRecordTreeMap == null) {
InfoResultRecordTreeMap = new TreeMap<>();
}
InfoResultRecordTreeMap.put(record.getDealTime() + oneDayMsSeconds, record);
entry.setValue(InfoResultRecordTreeMap);
return null;
}
}
有什么问题吗?或者我以错误的方式使用缓存?
我还写了一个简单的测试代码来验证 get/put 使用 TreeMap 时的速度:
public class Server2 {
public static void main(String[] args) throws IgniteException {
try (Ignite ignite = Ignition.start("server-start.xml")) {
IgniteCache<String, TreeMap<Long, String>> testCache = ignite.getOrCreateCache("testCache");
testCache.put("my",new TreeMap<>());
while (true) {
StopWatch stopWatch = new StopWatch();
stopWatch.start("1");
TreeMap<Long, String> map = testCache.get("my");
stopWatch.stop();
stopWatch.start("2");
map.put(System.currentTimeMillis(),String.valueOf(new Random().nextInt(1000000000)));
testCache.put("my",map);
stopWatch.stop();
System.out.println("cacheSize:"+map.size()+","+stopWatch.prettyPrint());
}
}
}
}
cacheSize:1000,StopWatch '': running time (millis) = 195
-----------------------------------------
ms % Task name
-----------------------------------------
00080 041% 1
00115 059% 2
cacheSize:1001,StopWatch '': running time (millis) = 38
-----------------------------------------
ms % Task name
-----------------------------------------
00028 074% 1
00010 026% 2
cacheSize:3000,StopWatch '': running time (millis) = 139
-----------------------------------------
ms % Task name
-----------------------------------------
00055 040% 1
00084 060% 2
cacheSize:3001,StopWatch '': running time (millis) = 68
-----------------------------------------
ms % Task name
-----------------------------------------
00042 062% 1
00026 038% 2
很明显当Treemap的大小增加时,ignite cache的消耗时间增加get/put,我认为这应该是1~2ms,但这里是xx ms并且随着大小的增加它甚至会达到xxxms秒。
很明显,在这种情况下 read/write 时间会增加,因为在每次操作时都需要将数据从堆外复制到堆(反之亦然),serialize/deserialize 它, 但是,我已经在本地检查了您的代码,时间并没有那么剧烈地增长。例如,我得到
cacheSize:10082,StopWatch '': running time (millis) = 18
-----------------------------------------
ms % Task name
-----------------------------------------
00009 050% 1
00009 050% 2
无论如何,我认为您没有为您的用例选择一个好的解决方案 - 我建议单独存储所有这些数据,即引入带有 TreeMap 字符串名称字段的键和一行的某些长键和按行存储这些数据。使用它,你的写操作会快得多。此外,在这种情况下,您将需要 collocate data by treemap 名称。要请求所有数据,您可以使用 SQL 和 ORDER BY 语句。并且不要忘记使用索引!