-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrules.py
More file actions
50 lines (50 loc) · 1.58 KB
/
rules.py
File metadata and controls
50 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#*\codetitle{start new election}*\#
on (state in [FOLLOWER, CANDIDATE] and
electionAlarm < now()):
| electionAlarm = now() + rand(1.0, 2.0) *
| ELECTION_TIMEOUT
| currentTerm += 1
| votedFor = serverID
| state = CANDIDATE
| foreach peer:
| | # reset all state for peer
#*\codetitle{send RequestVote to peer}*\#
on (state == CANDIDATE and
rpcDue[peer] < now()):
| rpcDue[peer] = now() + RPC_TIMEOUT
| send RequestVote to peer {
| term: currentTerm,
| lastLogTerm: logTerm(len(log)),
| lastLogIndex: len(log)}
#*\codetitle{become leader}*\#
on (state == CANDIDATE and
sum(voteGranted) + 1 > NUM_SERVERS / 2:
| state = LEADER
| leader = localhost
| foreach peer:
| | nextIndex[peer] = len(log) + 1
#*\codetitle{send AppendEntries to peer}*\#
on (state == LEADER and
(matchIndex[peer] < len(log) or
rpcDue[peer] < now()):
| rpcDue[peer] = now() + ELECTION_TIMEOUT / 2
| lastIndex := choose in (nextIndex[peer] - 1)..len(log)
| nextIndex[peer] = lastIndex
| send AppendEntries to peer {
| term: currentTerm,
| prevIndex: nextIndex[peer] - 1,
| prevTerm: getTerm(nextIndex[peer] - 1),
| entries: log[nextIndex[peer]..lastIndex],
| commitIndex: commitIndex}
#*\codetitle{advance commit index}*\#
n := sorted(matchIndex + [len(log)])[NUM_SERVERS / 2ish]
on (state == LEADER and
logTerm(n) == currentTerm):
| commitIndex = n
#*\codetitle{advance state machine}*\#
on lastApplied < commitIndex:
| lastApplied += 1
| result := stateMachine.ap#**\#ply(log[lastApplied])
| if (state == Leader and
| logTerm(lastApplied) == currentTerm):
| | # send result to client