Raft如何知道先前的任期日志条目是否已提交。

huangapple go评论52阅读模式
英文:

How Raft know previous term log entry committed or not

问题

当我学习Raft时,我遇到了一个问题。

一个Raft集群有5台服务器,我们称它们为a、b、c、d、e。a是领导者。现在一切正常。
然后,a处理一个客户端请求,创建了一个日志条目。

场景1,
b和c复制了日志条目,d和e没有。
然后a和b崩溃了。
c有日志条目,d和e没有。
该日志被提交。

场景2,
b复制了日志条目,c、d、e没有。
然后a和e崩溃了。
b有日志条目,c和d没有。
该日志未被提交。

Raft是如何处理它们的?

英文:

When I study raft, I have a problem.

A Raft cluster has 5 servers. we call them a,b,c,d,e. a is the leader. Now everything is ok.
Then, A handle a client request, makes a log entry.

scenario 1,
b & c replicate the log entry, d & e don't.
Then a & b crash.
c has the log entry, d & e not.
the log is committed.

scenario 2,
b replicate the log entry, c, d, e don't.
Then a & e crash.
b has the log entry, c & d not.
the log is not committed.

How raft handle them?

答案1

得分: 0

This statement "Then, A handle a client request, makes a log entry." should be extended to "然后,A处理客户请求,等待至少2个(b、c、d、e)接受请求后,才记录日志条目。"

Since there are five nodes - one leader and four followers - the majority requires three nodes: the leader and any two followers.

So, the leader adds entry to log when at least two followers accepted the request.

When a follower accepts a request, if does not mean the request is committed. A follower will commit a request only after the leader will tell it to do so.

Scenario 1: b & c replicate the log entry, d & e don't. Then a & b crash. c has the log entry, d & e not. the log is committed.

From the context, "replicate" means that an entry was committed (in terms of raft). When a & b crash a new election process will have to happen. As usual, majority is needed to elect a new leader, hence all three (c, d, e) will communicate with each other.

Raft guarantees that a node with most up to date log wins an election. In our set (c, d, e) C has the most up to date log. Hence, C will be elected as a new leader. On election, C will send out the record missing from D and E.

Scenario 2, b replicate the log entry, c, d, e don't. Then a & e crash. b has the log entry, c & d not. the log is not committed.

When B (and the leader) have the log entry, the record is not committed as no majority accepted the entry. On failure, a new election will happen and B will win the election, same as in scenario 1.

Few notes

  • In Raft, when an election is happening, a node with the most up-to-date log wins the election.
  • A record may be in two states: proposed and committed. Committed state happens only after the majority of nodes have the record. Even if a crash happens after commit, at least one of the remaining nodes will have the record, hence that node will win a new election.
  • It is interesting to consider what a client of a Raft cluster sees: if a client sends a request to a leader, and the leader fails before returning a reply - in this case, the client does not know if the record was recorded or not - this is a very important property - not knowing the outcome. This uncertainty happens as the client does not know what exactly went wrong, did the cluster fail before or after committing the request.
英文:

This statement "Then, A handle a client request, makes a log entry." should be extended to "Then, A handle a client request, A waits when at least 2 of (b,c,d,e) accept the request,[and then] makes a log entry."

Since there are five nodes - one leader and four followers - the majority requires three nodes: the leader and any two followers.

So, the leader adds entry to log when at least two followers accepted the request.

When a follower accepts a request, if does not mean the request is committed. A follower will commit a request only after the leader will tell it to do so.

Scenario 1: b & c replicate the log entry, d & e don't. Then a & b crash. c has the log entry, d & e not. the log is committed.

From the context, "replicate" means that an entry was committed (in terms of raft). When a&b crash a new election process will have to happen. As usual, majority is needed to elect a new leader, hence all three (c, d, e) will communicate with each other.

Raft guarantees that a node with most up to date log wins an election. In our set (c,d,e) C has the most up to date log. Hence, C will be elected as a new leader. On election, C will send out the record missing from D and E.

Scenario 2, b replicate the log entry, c, d, e don't. Then a & e crash. b has the log entry, c & d not. the log is not committed.

When B (and the leader) have the log entry, the record is not committed as no majority accepted the entry. On failure, new election will happen and B will win the election, same as in scenario 1.

Few notes

  • in raft, when an election is happening a node with most up to date log wins the election
  • a record may be in two states: proposed and committed. Committed state happens only after majority of nodes have the record. Even if a crash happens after commit, at least one of remaining nodes will have the record, hence that node will win a new election
  • it is interesting to consider, what a client of a raft cluster sees: if a client sends a request to a leader, and the leader fails before returning a reply - in this case the client does not know if the record was recorded or not - this is a very important property - not knowing the outcome. This uncertainty happens as the client does not know what exactly went wrong, did the cluster failed before or after committing the request

huangapple
  • 本文由 发表于 2023年4月17日 15:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定