forked from goraft/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_entry.go
193 lines (164 loc) · 4.64 KB
/
log_entry.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package raft
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A log entry stores a single item in the log.
type LogEntry struct {
log *Log
Index uint64 `json:"index"`
Term uint64 `json:"term"`
Command Command `json:"command"`
}
// A temporary interface used for unmarshaling log entries.
type logEntryRawMessage struct {
Index uint64 `json:"index"`
Term uint64 `json:"term"`
Name string `json:"name"`
Command json.RawMessage `json:"command"`
}
//------------------------------------------------------------------------------
//
// Constructor
//
//------------------------------------------------------------------------------
// Creates a new log entry associated with a log.
func NewLogEntry(log *Log, index uint64, term uint64, command Command) *LogEntry {
return &LogEntry{
log: log,
Index: index,
Term: term,
Command: command,
}
}
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
//--------------------------------------
// Encoding
//--------------------------------------
// Encodes the log entry to a buffer.
func (e *LogEntry) Encode(w io.Writer) error {
if w == nil {
return errors.New("raft.LogEntry: Writer required to encode")
}
encodedCommand, err := json.Marshal(e.Command)
if err != nil {
return err
}
// Write log line to temporary buffer.
var b bytes.Buffer
if _, err = fmt.Fprintf(&b, "%016x %016x %s %s\n", e.Index, e.Term, e.Command.CommandName(), encodedCommand); err != nil {
return err
}
// Generate checksum.
checksum := crc32.ChecksumIEEE(b.Bytes())
// Write log entry with checksum.
_, err = fmt.Fprintf(w, "%08x %s", checksum, b.String())
return err
}
// Decodes the log entry from a buffer. Returns the number of bytes read.
func (e *LogEntry) Decode(r io.Reader) (pos int, err error) {
pos = 0
if r == nil {
err = errors.New("raft.LogEntry: Reader required to decode")
return
}
// Read the expected checksum first.
var checksum uint32
if _, err = fmt.Fscanf(r, "%08x", &checksum); err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to read checksum: %v", err)
return
}
pos += 8
// Read the rest of the line.
bufr := bufio.NewReader(r)
if c, _ := bufr.ReadByte(); c != ' ' {
err = fmt.Errorf("raft.LogEntry: Expected space, received %02x", c)
return
}
pos += 1
line, err := bufr.ReadString('\n')
pos += len(line)
if err == io.EOF {
err = fmt.Errorf("raft.LogEntry: Unexpected EOF")
return
} else if err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to read line: %v", err)
return
}
b := bytes.NewBufferString(line)
// Verify checksum.
bchecksum := crc32.ChecksumIEEE(b.Bytes())
if checksum != bchecksum {
err = fmt.Errorf("raft.LogEntry: Invalid checksum: Expected %08x, calculated %08x", checksum, bchecksum)
return
}
// Read term, index and command name.
var commandName string
if _, err = fmt.Fscanf(b, "%016x %016x %s ", &e.Index, &e.Term, &commandName); err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to scan: %v", err)
return
}
// Instantiate command by name.
command, err := NewCommand(commandName)
if err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to instantiate command (%s): %v", commandName, err)
return
}
// Deserialize command.
if err = json.NewDecoder(b).Decode(&command); err != nil {
err = fmt.Errorf("raft.LogEntry: Unable to decode: %v", err)
return
}
e.Command = command
// Make sure there's only an EOF remaining.
c, err := b.ReadByte()
if err != io.EOF {
err = fmt.Errorf("raft.LogEntry: Expected EOL, received %02x", c)
return
}
err = nil
return
}
//--------------------------------------
// Encoding
//--------------------------------------
// Encodes a log entry into JSON.
func (e *LogEntry) MarshalJSON() ([]byte, error) {
obj := map[string]interface{}{
"index": e.Index,
"term": e.Term,
}
if e.Command != nil {
obj["name"] = e.Command.CommandName()
obj["command"] = e.Command
}
return json.Marshal(obj)
}
// Decodes a log entry from a JSON byte array.
func (e *LogEntry) UnmarshalJSON(data []byte) error {
// Extract base log entry info.
obj := &logEntryRawMessage{}
json.Unmarshal(data, obj)
e.Index, e.Term = obj.Index, obj.Term
// Create a command based on the name.
var err error
if e.Command, err = NewCommand(obj.Name); err != nil {
return err
}
json.Unmarshal(obj.Command, e.Command)
return nil
}