16. 两阶段提交
Cohort Cohort
QUERY TO COMMIT
Coordinator
Request
The flow of message with protocol (Two-Phase Commit )
An * next to the record type means that the record is forced to stable storage.
QUERY TO COMMIT
VOTE YES/NOVOTE YES/NO prepare*/abort*
COMMIT/ROLLBACK COMMIT/ROLLBACK
commit*/abort*
ACKNOWLEDGMENT ACKNOWLEDGMENT
prepare*/abort*
commit*/abort* commit*/abort*
17. 三阶段提交
Cohort Cohort
canCommit?
Coordinator
Request
yes
haveCommitted
Uncertain
Timeout cause abort
Committed
Soliciting Votes…
canCommit?
yes Uncertain
Timeout cause abort
preCommit
ack
preCommit
ack
Commit authorized
Timeout cause abort
The flow of message with protocol (Three-Phase Commit )
An * next to the record type means that the record is forced to stable storage.
Prepare to commit
Timeout cause abort
Prepare to commit
Timeout cause abort
doCommitdoCommit
Finalizing commit
Timeout cause abort
Done
Committed
haveCommitted
26. Proposers Acceptors
Phase 1
1) Choose new proposal number n
2) Broadcast Prepare(n) to all servers
3) Respond to Prepare(n):
if n > maxN then
maxN = n
return (acceptedN, acceptedV)
Phase 2
4) When responses received from majority:
if any acceptedV returned then
replace v with acceptedV for highest acceptedN
5) Broadcast Accept(n, v) to all servers
6) Respond to Accept(n, v):
if n >= maxN then
maxN = n
acceptedN = n
acceptedV = v
return (maxN)
7) When responses received from majority:
v is chosen
Acceptors must record maxN, acceptedN and acceptedV on stable storage (disk)
27. maxN=0
Paxos Basic - Phase1
0, nil 0, nil0, nil
N=1
X
acceptedN=0
acceptedV=nil
1, nil 0, nil1, nil
Proposer
X
Acceptor
maxN=1
maxN=0
3) Respond to Prepare(n):
if n > maxN then
maxN = n
return (acceptedN, acceptedV)
1) Choose new proposal number n
2) Broadcast Prepare(n) to all servers
28. maxN=0
4) When responses received from majority:
if any acceptedV returned then
replace v with acceptedV for highest acceptedN
5) Broadcast Accept(n, v) to all servers
Paxos Basic - Phase2
0, nil 0, nil0, nil
X 1, any 0, nil1, any
Proposer
X
Acceptor
maxN=1
6) Respond to Accept(n, v):
if n >= maxN then
maxN = n
acceptedN = n
acceptedV = v
return (maxN)
7) When responses received from majority:
v is chosen
maxN=0
acceptedN=1
acceptedV=any
maxN=1
acceptedN=1
29. maxN=1
maxN=0
PaxosBasic-并发更新
2, nil 2, nil1, any1X
maxN=2
acceptedN=1
acceptedV=any1
maxN=1
Y
Proposer
0, nil 0, nil0, nil
N=1
Proposer
X
Acceptor
maxN=0
X
acceptedN=0
acceptedV=nil
1, nil 0, nil1, nil
maxN=1
maxN=0
1, nil 0, nil1, nil
maxN=1
N=2
Y
maxN=2
2, nil 2, nil1, nil
acceptedN=1
acceptedV=nil
acceptedN=2
acceptedV=nil
2, nil 2, nil1, any1
maxN=2maxN=1
maxN=2
2, any2 2, any21, any1
maxN=2maxN=1
Y
Y
acceptedN=2
acceptedN=2
acceptedV=any2
P1 for X
P1 for Y
P2 for X
P2 for Y
30. 3, any3 2, any23, any3
maxN=2maxN=3
Proposer C
Acceptor A
maxN=3
2, any2 2, any23, any3
maxN=2maxN=1
Acceptor C Acceptor E
X
timeout
X
Proposer EProposer A
一条日志在 A/C 上持久化成功,已经形成多数派,然后C宕机
一条日志只在 A 上持久化成功,超时未形成多数派,然后C宕机
最终的状态都是 A 上有这条日志,E 上没有,怎么处理?
最大 Commit 原则