|
@@ -19,7 +19,7 @@ import (
|
|
|
|
|
|
|
|
// A log is a collection of log entries that are persisted to durable storage.
|
|
// A log is a collection of log entries that are persisted to durable storage.
|
|
|
type Log struct {
|
|
type Log struct {
|
|
|
- ApplyFunc func(Command) (interface{}, error)
|
|
|
|
|
|
|
+ ApplyFunc func(*LogEntry, Command) (interface{}, error)
|
|
|
file *os.File
|
|
file *os.File
|
|
|
path string
|
|
path string
|
|
|
entries []*LogEntry
|
|
entries []*LogEntry
|
|
@@ -160,7 +160,7 @@ func (l *Log) open(path string) error {
|
|
|
entry, _ := newLogEntry(l, nil, 0, 0, nil)
|
|
entry, _ := newLogEntry(l, nil, 0, 0, nil)
|
|
|
entry.Position, _ = l.file.Seek(0, os.SEEK_CUR)
|
|
entry.Position, _ = l.file.Seek(0, os.SEEK_CUR)
|
|
|
|
|
|
|
|
- n, err := entry.decode(l.file)
|
|
|
|
|
|
|
+ n, err := entry.Decode(l.file)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
if err == io.EOF {
|
|
if err == io.EOF {
|
|
|
debugln("open.log.append: finish ")
|
|
debugln("open.log.append: finish ")
|
|
@@ -179,7 +179,7 @@ func (l *Log) open(path string) error {
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
- l.ApplyFunc(command)
|
|
|
|
|
|
|
+ l.ApplyFunc(entry, command)
|
|
|
}
|
|
}
|
|
|
debugln("open.log.append log index ", entry.Index())
|
|
debugln("open.log.append log index ", entry.Index())
|
|
|
}
|
|
}
|
|
@@ -368,7 +368,7 @@ func (l *Log) setCommitIndex(index uint64) error {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Apply the changes to the state machine and store the error code.
|
|
// Apply the changes to the state machine and store the error code.
|
|
|
- returnValue, err := l.ApplyFunc(command)
|
|
|
|
|
|
|
+ returnValue, err := l.ApplyFunc(entry, command)
|
|
|
|
|
|
|
|
debugf("setCommitIndex.set.result index: %v, entries index: %v", i, entryIndex)
|
|
debugf("setCommitIndex.set.result index: %v, entries index: %v", i, entryIndex)
|
|
|
if entry.event != nil {
|
|
if entry.event != nil {
|
|
@@ -517,7 +517,7 @@ func (l *Log) appendEntry(entry *LogEntry) error {
|
|
|
entry.Position = position
|
|
entry.Position = position
|
|
|
|
|
|
|
|
// Write to storage.
|
|
// Write to storage.
|
|
|
- if _, err := entry.encode(l.file); err != nil {
|
|
|
|
|
|
|
+ if _, err := entry.Encode(l.file); err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -544,7 +544,7 @@ func (l *Log) writeEntry(entry *LogEntry, w io.Writer) (int64, error) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Write to storage.
|
|
// Write to storage.
|
|
|
- size, err := entry.encode(w)
|
|
|
|
|
|
|
+ size, err := entry.Encode(w)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return -1, err
|
|
return -1, err
|
|
|
}
|
|
}
|
|
@@ -589,7 +589,7 @@ func (l *Log) compact(index uint64, term uint64) error {
|
|
|
position, _ := l.file.Seek(0, os.SEEK_CUR)
|
|
position, _ := l.file.Seek(0, os.SEEK_CUR)
|
|
|
entry.Position = position
|
|
entry.Position = position
|
|
|
|
|
|
|
|
- if _, err = entry.encode(file); err != nil {
|
|
|
|
|
|
|
+ if _, err = entry.Encode(file); err != nil {
|
|
|
file.Close()
|
|
file.Close()
|
|
|
os.Remove(new_file_path)
|
|
os.Remove(new_file_path)
|
|
|
return err
|
|
return err
|