在读取提交模式下点燃事务不一致



我们在READ_COMMITED模式下使用 Ignite 事务,我们还使用乐观离线锁设计模式来满足数据一致性要求和读取时的延迟。对于我们的业务来说,重要的是在读取时不要在缓存中看到部分更新的数据。REPEATABLE_READ阻止我们的读取,这就是我们不使用它的原因。

读取事务:

@Override
public SearchResult apply(ComputeTaskInData<SearchProductOffer> data) {
while (true) {
try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
Long initialTimestamp = getCurrentTimestampFromIgniteCache();
... // multiple caches read where we can see commited data in the middle of read
Long finalTimestamp = getCurrentTimestampFromigniteCache();
// Check if transaction was commited in the middle of read request. If so, retry. This 
timestamp increments in update transaction.
if (finalTimestamp > initialTimestamp) continue;
return readResult;
}

}

更新事务:

try (Transaction transaction = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
// Multiple caches update.
IgniteCache<OfflineLockKey, Long> offerOfflineLock = 
ignite.getOrCreateCache(OFFLINE_LOCK.name());
OfflineLockKey offlineLockKey = new 
OfflineLockKey(segmentIndex.segmentInfo.getSegmentId());
Long offerTimestamp = offerOfflineLock.get(offlineLockKey);
if (offerTimestamp == null) {
offerTimestamp = 0L;
}
offerTimestamp++;
offerOfflineLock.put(offlineLockKey, offerTimestamp);
transaction.commit();
}

但问题是,我们读取不一致的数据并且不会重试。似乎 ignite 事务不是原子的,我们没有得到递增的时间戳。它应该是悲观READ_COMMITED模式的行为吗? 我们为offline_lock缓存尝试了不同的缓存模式。 是的,所有缓存都是事务性的。

我这边的问题是缓存配置。

您必须使用该配置获取缓存(在我的情况下,它是我的本地缓存)

ingite.getOrCreateCache(new CacheConfiguration("cacheName")
.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL)
.setCacheMode(CacheMode.LOCAL));

最新更新