【发布时间】:2018-02-15 16:49:37
【问题描述】:
在我的项目中,我最近遇到了一个与配置了 QueryCache 的 Map 相关的问题。
我有两个字段的实体:
class AccountingRule {
long key;
String code;
}
它的 Map 的 Map 配置,包括 QueryCache 配置:
MapConfig mapConfig = new MapConfig("AccountingRule")
.setInMemoryFormat(InMemoryFormat.BINARY)
.setReadBackupData(true)
.setBackupCount(1)
.addQueryCacheConfig(new QueryCacheConfig()
.setName("AccountingRule")
.setPredicateConfig(new PredicateConfig(TruePredicate.INSTANCE))
.setIncludeValue(true)
.setPopulate(true)
.setDelaySeconds(0)
);
在 main 中,我使用我的配置运行 hazelcast 实例,然后:
- 为 QueryCache 添加索引
- 将实体放入 IMap
- 通过检查 QueryCache.get 是否返回插入的实体,等到插入传播到 QueryCache
- 使用索引字段上的谓词从 QueryCache 中获取相同的实体
例如:
HazelcastInstance hazelcast = createHazelcastInstance();
IMap<Long, AccountingRule> map = hazelcast.getMap("AccountingRule");
QueryCache<Long, AccountingRule> queryCache = map.getQueryCache("AccountingRule");
queryCache.addIndex("code", false);
while (true) {
AccountingRule ar = newAccountingRule();
map.put(ar.getKey(), ar);
// wait for query cache
AccountingRule fromQueryCache = queryCache.get(ar.getKey());
while (fromQueryCache == null) {
log.info("Waiting for query cache");
fromQueryCache = queryCache.get(AccountingRule.class, ar.getKey());
}
log.info("query cache updated");
// get entity from query cache using predicate
Predicate codePredicate = equal("code", ar.getCode());
AccountingRule fromQueryCacheWithPredicate = getOnlyElement(queryCache.values(codePredicate), null);
if (fromQueryCacheWithPredicate == null) {
log.error("AccountingRule with id {} from query cash is null", ar.getKey());
System.exit(1);
}
}
不幸的是,它失败了。 QueryCache.get 返回实体,但基于索引字段谓词的查询不返回任何内容。当我稍等片刻并重试查询时,就可以了。看起来索引是异步更新的,对吗?有什么办法可以避免吗?
当它起作用时有两种选择:
- 删除索引->没有索引没问题:)
- 像这样在 QueryCache conf 中声明索引,而不是在 main 中:
代码:
MapConfig mapConfig = new MapConfig("AccountingRule")
.setInMemoryFormat(InMemoryFormat.BINARY)
.setReadBackupData(true)
.setBackupCount(1)
.addQueryCacheConfig(new QueryCacheConfig()
.setName("AccountingRule")
.setPredicateConfig(new PredicateConfig(TruePredicate.INSTANCE))
.setIncludeValue(true)
.setPopulate(true)
.setDelaySeconds(0)
.addIndexConfig(new MapIndexConfig("code", false))
);
问题是为什么当我在配置中添加索引时它不能正常工作?
【问题讨论】:
标签: indexing hazelcast query-cache