-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
65 lines (54 loc) · 1.23 KB
/
log.go
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
package raft
import (
"fmt"
"github.com/Mathew-Estafanous/raft/pb"
)
type logType byte
var (
Entry logType = 'E'
Snapshot logType = 'S'
)
// Log entries represent commands that alter the state of the FSM.
// These entries are replicated across a majority of raft instances
// before being considered as committed.
type Log struct {
// Type is the kind of log that this represents.
Type logType
// Index represents the index in the list of log entries.
Index int64
// Term contains the election term it was added.
Term uint64
// Cmd represents the command applied to the FSM.
Cmd []byte
}
func (l Log) String() string {
return fmt.Sprintf("{%v}", string(l.Cmd))
}
type logTask struct {
errorTask
log *Log
}
func logsToEntries(logs []*Log) []*pb.Entry {
entries := make([]*pb.Entry, 0, len(logs))
for _, l := range logs {
entries = append(entries, &pb.Entry{
Type: []byte{byte(l.Type)},
Term: l.Term,
Index: l.Index,
Data: l.Cmd,
})
}
return entries
}
func entriesToLogs(entries []*pb.Entry) []*Log {
logs := make([]*Log, 0, len(entries))
for _, e := range entries {
logs = append(logs, &Log{
Type: logType(e.Type[0]),
Term: e.Term,
Index: e.Index,
Cmd: e.Data,
})
}
return logs
}