forked from myc880628/lilraft
/
log.go
273 lines (253 loc) · 6.26 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
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
package lilraft
import (
"bufio"
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"os"
"path"
"sync"
"github.com/golang/protobuf/proto"
)
// Log is used as a way to ensure consensus
// TODO: fill this, add mutex log maybe.
type Log struct {
entries []*LogEntry
commitIndex int64
file *os.File
sync.RWMutex
}
func newLog() (l *Log) {
l = &Log{
entries: make([]*LogEntry, 0),
commitIndex: -1,
}
return
}
func (s *Server) logPath() string {
return path.Join(s.path, "log")
}
// TODO: add more procedure
func (log *Log) setCommitIndex(commitIndex int64, context interface{}) (interface{}, error) {
log.Lock()
defer log.Unlock()
startIndex := log.startIndex()
if startIndex < 0 {
return nil, fmt.Errorf("set commit index on empty log")
}
w := bufio.NewWriter(log.file)
var val interface{}
var retErr error
for i := log.commitIndex + 1; i <= commitIndex; i++ {
entryIndex := i - startIndex
entry := log.entries[entryIndex]
Encode(w, entry)
if entry.GetCommandName() == cOldNewStr || entry.GetCommandName() == cNewStr {
continue
}
command, err := newCommand(entry.GetCommandName(), entry.GetCommand())
if err != nil {
return nil, err
}
if i != commitIndex {
command.Apply(context)
} else {
println("the command name: ", command.Name())
val, retErr = command.Apply(context)
}
}
w.Flush()
go func() {
log.file.Sync()
}()
log.commitIndex = commitIndex
return val, retErr
}
// first index in log entries
func (log *Log) startIndex() int64 {
if len(log.entries) > 0 {
return log.entries[0].GetIndex()
}
return -1
}
func (log *Log) lastLogIndex() int64 {
log.RLock()
defer log.RUnlock()
if len(log.entries) > 0 {
return *log.entries[len(log.entries)-1].Index
}
return -1
}
func (log *Log) lastLogTerm() int64 {
log.RLock()
defer log.RUnlock()
if len(log.entries) > 0 {
return *log.entries[len(log.entries)-1].Term
}
return -1
}
// TODO: use this to prevent replicated commands
func (log *Log) lastLogEntry() *LogEntry {
log.RLock()
defer log.RUnlock()
if len(log.entries) == 0 {
return nil
}
return log.entries[len(log.entries)-1]
}
func (log *Log) prevLogTerm(index int64) int64 {
log.RLock()
defer log.RUnlock()
startIndex := log.startIndex()
if startIndex < 0 || index <= startIndex {
return -1
}
return log.entries[index-startIndex-1].GetTerm()
}
func (log *Log) prevLogIndex(index int64) int64 {
log.RLock()
defer log.RUnlock()
startIndex := log.startIndex()
if startIndex < 0 || index <= startIndex {
return -1
}
return log.entries[index-startIndex-1].GetIndex()
}
func (log *Log) entriesAfer(index int64) []*LogEntry {
log.RLock()
defer log.RUnlock()
startIndex := log.startIndex()
if index < startIndex {
return nil
}
if index-startIndex >= int64(len(log.entries)) {
return nil
}
return log.entries[index-startIndex:]
}
func (log *Log) appendEntry(logEntry *LogEntry) {
log.Lock()
defer log.Unlock()
log.entries = append(log.entries, logEntry)
}
func (log *Log) appendEntries(s *Server, prevIndex int64, logEntries []*LogEntry) {
log.Lock()
defer log.Unlock()
if len(log.entries) == 0 {
log.entries = logEntries
return
}
log.entries = log.entries[:prevIndex-log.startIndex()+1]
// log.entries = append(log.entries[:(index-log.startIndex())+1], logEntries...)
for _, entry := range logEntries {
if entry.GetCommandName() == cOldNewStr {
nodes := []Node{}
if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
logger.Println("decode Cold,new config err: ", err.Error())
}
s.config.setState(cOldNew)
s.config.cNewNode = make(nodeMap)
for _, node := range nodes {
s.config.cNewNode[node.id()] = node
}
} else if entry.GetCommandName() == cNewStr {
nodes := []Node{}
if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
logger.Println("decode Cnew config err: ", err.Error())
}
s.config.setState(cOld)
s.config.cOldNode = makeNodeMap(nodes...)
s.config.cNewNode = nil
}
log.entries = append(log.entries, entry)
}
}
func (log *Log) newLogEntry(term int64, command Command) (*LogEntry, error) {
var bytesBuffer bytes.Buffer
if err := json.NewEncoder(&bytesBuffer).Encode(command); err != nil {
return nil, err
}
pbEntry := &LogEntry{
Index: proto.Int64(log.lastLogIndex() + 1),
Term: proto.Int64(term),
CommandName: proto.String(command.Name()),
Command: bytesBuffer.Bytes(),
}
return pbEntry, nil
}
func (log *Log) newConfigEntry(term int64, nodes []Node, state string) (*LogEntry, error) {
var bytesBuffer bytes.Buffer
if err := gob.NewEncoder(&bytesBuffer).Encode(nodes); err != nil {
return nil, err
}
pbEntry := &LogEntry{
Index: proto.Int64(log.lastLogIndex() + 1),
Term: proto.Int64(term),
CommandName: proto.String(state),
Command: bytesBuffer.Bytes(),
}
return pbEntry, nil
}
func (log *Log) contains(index int64, term int64) bool {
log.RLock()
defer log.RUnlock()
if log.lastLogIndex() < index {
return false
}
if index < 0 { // if index<0, means leader's request is empty
return true
}
if len(log.entries) < 0 { // empty log, impossible to contain index
return false
}
if log.entries[index-log.startIndex()].GetTerm() == term {
return true
}
return false
}
func (log *Log) recover(logPath string, context interface{}, recoverContex bool) (err error) {
log.file, err = os.OpenFile(logPath, os.O_RDWR, 0700)
if err != nil {
if os.IsNotExist(err) {
if log.file, err = os.OpenFile(logPath, os.O_WRONLY|os.O_CREATE, 0700); err != nil {
return err
}
return nil
} else {
return err
}
}
println(log.file.Name())
for {
entry := &LogEntry{}
if err = Decode(log.file, entry); err != nil {
if err != io.EOF {
logger.Println("recover and decode entry err:", err.Error())
}
break
}
if entry.GetIndex() > log.startIndex() {
log.entries = append(log.entries, entry)
if recoverContex {
command, err := newCommand(entry.GetCommandName(), entry.GetCommand())
if err != nil {
println(err.Error())
continue
}
if entry.GetCommandName() == cOldNewStr || entry.GetCommandName() == cNewStr {
continue
}
command.Apply(context)
}
}
}
return nil
}
func (log *Log) close() {
if log.file != nil {
log.file.Close()
log.file = nil
}
}