-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
334 lines (266 loc) · 8.8 KB
/
Copy pathlog.go
File metadata and controls
334 lines (266 loc) · 8.8 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package raft
import (
"bufio"
"io"
"os"
"github.com/navgeet/raft/internal/errors"
)
var (
errIndexDoesNotExist = errors.New("index does not exist")
errLogNotOpen = errors.New("log is not open")
)
// Log is an interface representing the internal component of RaftCore that is responsible
// for durably storing and retrieving log entries.
type Log interface {
// Open opens the log for reading and writing.
Open() error
// Replay reads the persisted state of the log into
// memory. Log must be open.
Replay() error
// Close closes the log.
Close() error
// GetEntry returns the log entry located at the specified index.
GetEntry(index uint64) (*LogEntry, error)
// AppendEntry appends a log entry to the log.
AppendEntry(entry *LogEntry) error
// AppendEntries appends multiple log entries to the log.
AppendEntries(entries []*LogEntry) error
// Truncate deletes all log entries with index greater than
// or equal to the provided index.
Truncate(index uint64) error
// DiscardEntries deletes all in-memory and persistent data in the
// log. The provided term and index indicate at what term and index
// the now empty log will start at. Primarily intended to be used
// for snapshotting.
DiscardEntries(index uint64, term uint64) error
// Compact deletes all log entries with index less than
// or equal to the provided index.
Compact(index uint64) error
// Contains checks if the log contains an entry at the specified index.
Contains(index uint64) bool
// LastIndex returns the largest index that exists in the log and zero
// if the log is empty.
LastIndex() uint64
// LastTerm returns the largest term in the log and zero if the log
// is empty.
LastTerm() uint64
// NextIndex returns the next index to append to the log.
NextIndex() uint64
}
// LogEntry represents a log entry in the log.
type LogEntry struct {
// The Index of the log entry.
Index uint64
// The Term of the log entry.
Term uint64
// The Offset of the log entry.
Offset int64
// The Data of the log entry.
Data []byte
}
// NewLogEntry creates a new instance of LogEntry with the provided
// index, term, and data.
func NewLogEntry(index uint64, term uint64, data []byte) *LogEntry {
return &LogEntry{Index: index, Term: term, Data: data}
}
// IsConflict checks whether the current log entry conflicts with another log entry.
// Two log entries are considered conflicting if they have the same index but different terms.
func (e *LogEntry) IsConflict(other *LogEntry) bool {
return e.Index == other.Index && e.Term != other.Term
}
// persistentLog implements the Log interface. Not concurrent safe.
type persistentLog struct {
// The in-memory log entries of the log.
entries []*LogEntry
// The file that the log is written to.
file *os.File
// The path to the file that the log is written to.
path string
}
// newPersistentLog creates a new instance of PersistentLog at the provided path.
// This implementation is not concurrent safe and should only be used within the
// RaftCore implementation.
func newPersistentLog(path string) *persistentLog {
return &persistentLog{path: path}
}
func (l *persistentLog) Open() error {
file, err := os.OpenFile(l.path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return errors.WrapError(err, "failed to open log file: path = %s", l.path)
}
l.file = file
l.entries = make([]*LogEntry, 0)
return nil
}
func (l *persistentLog) Replay() error {
reader := bufio.NewReader(l.file)
for {
entry, err := decodeLogEntry(reader)
if err == io.EOF {
break
}
if err != nil {
return errors.WrapError(err, "failed to decode log entry")
}
l.entries = append(l.entries, &entry)
}
// The log must always contain at least one entry.
// The first entry is a placeholder entry used for indexing into the log.
if len(l.entries) == 0 {
entry := NewLogEntry(0, 0, nil)
if err := encodeLogEntry(l.file, entry); err != nil {
return errors.WrapError(err, "failed to encode log entry")
}
if err := l.file.Sync(); err != nil {
return errors.WrapError(err, "failed to sync log file")
}
l.entries = append(l.entries, entry)
}
return nil
}
func (l *persistentLog) Close() error {
if l.file == nil {
return nil
}
if err := l.file.Close(); err != nil {
return errors.WrapError(err, "failed to close log")
}
l.entries = nil
l.file = nil
return nil
}
func (l *persistentLog) GetEntry(index uint64) (*LogEntry, error) {
if l.file == nil {
return nil, errLogNotOpen
}
logIndex := index - l.entries[0].Index
lastIndex := l.entries[len(l.entries)-1].Index
if logIndex <= 0 || logIndex > lastIndex {
return nil, errIndexDoesNotExist
}
entry := l.entries[logIndex]
return entry, nil
}
func (l *persistentLog) Contains(index uint64) bool {
logIndex := index - l.entries[0].Index
lastIndex := l.entries[len(l.entries)-1].Index
return !(logIndex <= 0 || logIndex > lastIndex)
}
func (l *persistentLog) AppendEntry(entry *LogEntry) error {
return l.AppendEntries([]*LogEntry{entry})
}
func (l *persistentLog) AppendEntries(entries []*LogEntry) error {
if l.file == nil {
return errLogNotOpen
}
for _, entry := range entries {
offset, err := l.file.Seek(0, io.SeekCurrent)
if err != nil {
return errors.WrapError(err, "failed to seek to offset in log file: offset = %d", offset)
}
entry.Offset = offset
if err := encodeLogEntry(l.file, entry); err != nil {
return errors.WrapError(err, "failed to encode log entry")
}
}
if err := l.file.Sync(); err != nil {
return errors.WrapError(err, "failed to sync log file")
}
l.entries = append(l.entries, entries...)
return nil
}
func (l *persistentLog) Truncate(index uint64) error {
if l.file == nil {
return errLogNotOpen
}
logIndex := index - l.entries[0].Index
lastIndex := l.entries[len(l.entries)-1].Index
if logIndex <= 0 || logIndex > lastIndex {
return errIndexDoesNotExist
}
// The offset of the entry at the provided index is the
// new size of the file.
size := l.entries[logIndex].Offset
if err := l.file.Truncate(size); err != nil {
return errors.WrapError(err, "failed to truncate log file: size = %d", size)
}
// Update to I/O offset to the new size.
if _, err := l.file.Seek(size, io.SeekStart); err != nil {
return errors.WrapError(err, "failed to seek to offset in log file: offset = %d", size)
}
l.entries = l.entries[:logIndex]
return nil
}
func (l *persistentLog) Compact(index uint64) error {
if l.file == nil {
return errLogNotOpen
}
logIndex := index - l.entries[0].Index
lastIndex := l.entries[len(l.entries)-1].Index
if logIndex <= 0 || logIndex > lastIndex {
return errIndexDoesNotExist
}
newEntries := make([]*LogEntry, len(l.entries)-int(logIndex))
copy(newEntries[:], l.entries[logIndex:])
// Create a temporary file to write the compacted log to.
compactedFile, err := os.Create(l.path + ".bin")
if err != nil {
return errors.WrapError(err, "failed to create temporary log file")
}
// Write the entries contained in the compacted log to the
// temporary file.
for _, entry := range newEntries {
offset, err := compactedFile.Seek(0, io.SeekCurrent)
if err != nil {
return errors.WrapError(err, "failed to seek to offset in log file: offset = %d", offset)
}
entry.Offset = offset
if err := encodeLogEntry(compactedFile, entry); err != nil {
return errors.WrapError(err, "failed to encode log entry")
}
}
if err := compactedFile.Sync(); err != nil {
return errors.WrapError(err, "failed to sync log file")
}
// Atomically rename the temporary file to the actual file.
if err := os.Rename(compactedFile.Name(), l.path); err != nil {
return errors.WrapError(err, "failed to rename temporary log file")
}
l.file = compactedFile
l.entries = newEntries
return nil
}
func (l *persistentLog) DiscardEntries(index uint64, term uint64) error {
if l.file == nil {
return errLogNotOpen
}
// Create a temporary file for the new log.
newLogFile, err := os.Create(l.path + ".tmp")
if err != nil {
return errors.WrapError(err, "failed to create temporary log file")
}
// Write a placeholder entry to the temporary file with the provided term and index.
entry := &LogEntry{Index: index, Term: term}
if err := encodeLogEntry(newLogFile, entry); err != nil {
return errors.WrapError(err, "failed to encode log entry")
}
if err := newLogFile.Sync(); err != nil {
return errors.WrapError(err, "failed to sync log file")
}
// Atomically rename the temporary file to the actual file.
if err := os.Rename(newLogFile.Name(), l.path); err != nil {
return errors.WrapError(err, "failed to rename temporary log file")
}
l.file = newLogFile
l.entries = []*LogEntry{entry}
return nil
}
func (l *persistentLog) LastTerm() uint64 {
return l.entries[len(l.entries)-1].Term
}
func (l *persistentLog) LastIndex() uint64 {
return l.entries[len(l.entries)-1].Index
}
func (l *persistentLog) NextIndex() uint64 {
return l.entries[len(l.entries)-1].Index + 1
}