Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add textual description of the raft.Apply process #511

Merged
merged 5 commits into from Jul 14, 2022

Conversation

huikang
Copy link
Contributor

@huikang huikang commented Jun 21, 2022

No description provided.

@huikang huikang force-pushed the doc-textual-description-apply branch from cbb2678 to 22cdaea Compare June 22, 2022 14:21
@mkeeler mkeeler self-requested a review June 22, 2022 21:15
docs/apply.md Outdated Show resolved Hide resolved
docs/apply.md Outdated
12. The actual place applying the commited log entries is in the main loop of `runFSM()`.

13. After the log entries that contains the client req are applied to the fsm, the fsm
module will set the reponses to the client request (`req.future.respond(nil)`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to close the loop on the whole process it might be worth noting that the future returned by raft.Apply should now be unblocked and calls to Error or Response should return the data at this point.

Comment on lines +71 to +72
8. If all went well, the follower responds success (`resp.Success = true`) to the
`appendEntries` RPC call.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not exactly sure where in this flow but do you think it would be good to point out the stepping nature of the processLogs within the append entries RPC.

One RPC will contain new logs entries but the LeaderCommitIndex will lag behind those log indexes. For example the following flow is likely

AppenEntries(Log: 1..5, LeaderCommitIndex: 0)
AppendEntries(Log: 6..8, LeaderCommitIndex: 4)
AppendEntries(Log: 9, LeaderCommitIndex: 8)
AppendEntries(Log: , LeaderCommitIndex: 9)

Sometimes append entries rpcs wont contain new logs but only be bumping the commit index. Only if a server is far enough behind the majority of all servers will it ever see logs with a leader commit index which is greater or equal to the logs index within the rpc.

This subtlety is important because when the first AppendEntries RPC from my example above returns, the follower will not have queued up any applies to the FSM.

Due to this behavior its possible that a very small window of time exists when all followers have committed the log to disk, the write has been realized in the FSM of the leader but the followers have not yet applied the log to their FSM.

Paul's replies in this issue are relevant: #508

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkeeler , thanks for your suggestions. Totally agreed that it is worth to point out the details. Will send a new commit to update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @mkeeler , could you please take another look at the updated PR when you have a chance? Thanks.

docs/apply.md Outdated
Apply is the primary operation provided by raft.

Apply is the primary operation provided by raft. A client calls `raft.Apply` to apply
a command to the FSM. If no error is returned, the applied command is commited to a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to make a distinction between committed and applied here?

committed == durably stored to disk
applied == applied to the fsm.

I am thinking maybe its worthwhile to define both terms so that the reader knows the two are not interchangeable.

@huikang huikang force-pushed the doc-textual-description-apply branch from 9fa3509 to b86d3df Compare June 24, 2022 02:41
docs/apply.md Outdated Show resolved Hide resolved
Co-authored-by: Matt Keeler <mkeeler@users.noreply.github.com>
@huikang huikang merged commit c3134d7 into hashicorp:main Jul 14, 2022
@liyuan35023
Copy link

I deployed a consul cluster of 3 server nodes.I stoped all servers and start again, its possible that a very small window of time exists when consul returns stale data. The reason is that consul restores from the snapshot and needs to re-apply the committed but unapplied logs to fsm. In this case, the server may return stale data.
I wonder if this is a bug or a scenario that could be allowed. And any advice if I want to avoid this situation?
Thanks. @mkeeler

@mkeeler
Copy link
Member

mkeeler commented Feb 13, 2023

If you perform a consistent query in Consul it should ensure all old logs have been applied to the FSM before reading/returning the data. (consistent=true query param or you can make this the default

The way that is implemented is that the Raft leader (where the RPC for the query will be forwarded) will perform a Raft Barrier operation. Calling that Barrier method on Raft will send a new log through Raft and only unblock once that log and all previous logs have been applied. This ensures that the data read will not be more stale than when the request was made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants