diff --git a/log.go b/log.go index d380c93ef3..34b0bc737e 100644 --- a/log.go +++ b/log.go @@ -160,25 +160,23 @@ func (l *Log) open(path string) error { } debugln("log.open.exist ", path) - reader := bufio.NewReader(l.file) - // Read the file and decode entries. for { - if _, err := reader.Peek(1); err == io.EOF { - debugln("open.log.append: finish ") - break - } // Instantiate log entry and decode into it. entry, _ := newLogEntry(l, 0, 0, nil) entry.Position, _ = l.file.Seek(0, os.SEEK_CUR) - n, err := entry.decode(reader) + n, err := entry.decode(l.file) if err != nil { + if err == io.EOF { + debugln("open.log.append: finish ") + break + } + if err = os.Truncate(path, readBytes); err != nil { return fmt.Errorf("raft.Log: Unable to recover: %v", err) } - break } // Append entry. diff --git a/log_entry.go b/log_entry.go index 8d77f02a82..47cfa5dc08 100644 --- a/log_entry.go +++ b/log_entry.go @@ -66,7 +66,7 @@ func (e *LogEntry) encode(w io.Writer) (int, error) { return -1, err } - _, err = fmt.Fprintf(w, "%x\n", len(p.Bytes())) + _, err = fmt.Fprintf(w, "%8x\n", len(p.Bytes())) if err != nil { return -1, err @@ -80,17 +80,17 @@ func (e *LogEntry) encode(w io.Writer) (int, error) { func (e *LogEntry) decode(r io.Reader) (int, error) { var length int - _, err := fmt.Fscanf(r, "%x\n", &length) - + _, err := fmt.Fscanf(r, "%8x\n", &length) if err != nil { return -1, err } data := make([]byte, length) - - _, err = r.Read(data) + num, err := r.Read(data) + fmt.Println(data, " ", num) if err != nil { + panic(err) return -1, err } @@ -99,6 +99,7 @@ func (e *LogEntry) decode(r io.Reader) (int, error) { err = p.Unmarshal(pb) if err != nil { + panic(err) return -1, err }