sofastack / sofa-jraft

A production-grade java implementation of RAFT consensus algorithm.
https://www.sofastack.tech/projects/sofa-jraft/
Apache License 2.0
3.57k stars 1.14k forks source link

readCommittedUserLog 可能获得不到刚写入的数据 #956

Closed hanzhihua closed 1 year ago

hanzhihua commented 1 year ago

Describe the bug

我在做NodeTest测试过程中,在测试readCommittedUserLog方法中,出现了错误 image

我发现主要是日志写入后,但lastAppliedIndex 没有原子级别更新,造成了读取不到错误 image

Environment

killme2008 commented 1 year ago

因为 onApply 中是可以回滚的,所以没法去实时更新 lastAppliedIndex,只有确认这一批次没有回滚或者回滚的正确 log index,才能去更新。 如果你需要在 onApply 过程中去 commit,可以调用日志迭代器的 Iterator#commit 方法,将当前 apply 的 log index 确认提交。然后就可以调用 readCommittedUserLog 确保可以读取到,代价就是无法回滚到 commit 之前的位置了。

hanzhihua commented 1 year ago

问一下,onApply回滚是什么意思,是指状态机apply发生异常,还是获取日志为null,另外怎么回滚呢?

killme2008 commented 1 year ago

问一下,onApply回滚是什么意思,是指状态机apply发生异常,还是获取日志为null,另外怎么回滚呢?

这个方法

https://github.com/sofastack/sofa-jraft/blob/fdf737850097eb1bf20d34b8caaa0bb6f0c92ad7/jraft-core/src/main/java/com/alipay/sofa/jraft/Iterator.java#L90

hanzhihua commented 1 year ago

哦,明白了,现在没有用到这个,目前好像没有用到,如果apply失败了,就需要人工解决了