-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsensusModuleTypes.go
More file actions
79 lines (66 loc) · 1.4 KB
/
Copy pathConsensusModuleTypes.go
File metadata and controls
79 lines (66 loc) · 1.4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package raft
import (
"sync"
"time"
)
type ConsensusModuleState int
const (
Follower ConsensusModuleState = iota
Candidate
Leader
)
type Contact[j, x comparable, k any] interface {
GetPeerIds() []uint
GetLeader() uint
GetLeaderLog() []LogEntry[j]
RequestVotes(vote RequestVote[j]) []Reply
AppendEntries(entries AppendEntries[j]) []Reply
ValidLogEntryCommand(j) bool
ValidLog([]LogEntry[j]) bool
ExecuteLog(uint, []j) error
DefaultLogEntryCommand() j
LogValue([]LogEntry[j]) x
}
type LogEntry[j comparable] struct {
Command j
Term uint
}
type RequestVote[j comparable] struct {
Term uint
CandidateId uint
LastLogIndex int
LastLogTerm j
}
type Reply struct {
Term uint
VoteGranted bool
}
type AppendEntries[j comparable] struct {
Term uint
LeaderId uint
PrevLogIndex int
PrevLogTerm j
Entries []LogEntry[j]
LeaderCommit uint
}
type ConsensusModule[j, x comparable, k any] struct {
Mutex *sync.Mutex
Id uint
State ConsensusModuleState
Ticker *time.Ticker
TickerDuration time.Duration
// Volatile state in memory
LeaderId uint
CommitIndex uint
LastApplied uint
// Volatile state for leaders
NextIndex []uint
MatchIndex []uint
// Concurrent API communication
ReceiveChan *chan k
Contact Contact[j, x, k]
// Persistent state in memory
CurrentTerm uint
VotedFor int
Log []LogEntry[j]
}