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

A question about how the DefaultRheaKVStore Commit Index advances #305

Closed JerryYangSH closed 4 years ago

JerryYangSH commented 4 years ago

Your question

I looked into the code in DefaultRegionKVService.java#handleXXXRequest, I don't see the commit index is advanced right after write op done. and in RaftRawKVStore.java# delete(key, closure), the call just return applyOperation, but no commit was seen. Describe your question clearly Client对RaftKV写操作,ApplyOperation 没有等commit完成, 如何保证正确性的?

Your scenes

Describe your use scenes (why need this feature) Curious

Your advice

Describe the advice or solution you'd like

Environment

Latest branch.

fengjiachun commented 4 years ago

Client对RaftKV写操作,ApplyOperation 没有等commit完成, 如何保证正确性的?

你应该没有注意到 ApplyOperation 时有一个 closure 回调,closure 在写成功后才会被回调

JerryYangSH commented 4 years ago

我注意到了那个closure, 但是那closure也未见有操作commit index。请明示, 谢谢

fengjiachun commented 4 years ago

@JerryYangSH closure 由 jraft 状态机在应用了当前 log 后调用,可以看下 KVStoreStateMachine

你所说的 commit index 是 raft 的概念,这个是在 jraft 内部维护和更新的

rheakv 里 raft 相关只需关注两个地方:

  1. RaftRawKVStore 里的 this.node.apply(task); 提交 raft task,task 里包含一个 closure 回调
  2. KVStoreStateMachine, 这是 rheakv 实现的 jraft 状态机,其中 com.alipay.sofa.jraft.rhea.storage.KVStoreStateMachine#onApply 方法由 jraft 在日志形成多数派时调用,你所说的 committedIndex 是在 jraft 调用 onApply 之前更新, closure 是在 onApply 后调用
fengjiachun commented 4 years ago

https://github.com/sofastack/sofa-jraft/blob/master/jraft-core/src/main/java/com/alipay/sofa/jraft/core/BallotBox.java#L96

committedIndex 推进相关代码见这里(commitAt 方法),建议你在这里打一个断点 debug 一下, jraft 为了减少 block, 设计为一个 callback 为核心的模型,硬看不容易看懂,最好时不时 debug 一下

JerryYangSH commented 4 years ago

非常感谢!

killme2008 commented 4 years ago

有一些分析文章可以看下

https://www.jianshu.com/c/263f8f0fa8de

没有更多问题先关闭了。