diff --git a/commit/log.go b/commit/log.go index 12c5de8db272967a04b60b545699c9d281591c10..7d3ef0eac9b9c04b04796cad6b2c8c118330280e 100644 --- a/commit/log.go +++ b/commit/log.go @@ -152,22 +152,6 @@ func (l *Logger) handleFile(path string, info os.FileInfo, err error) error { // Handle if we find the current log file. if tstring == "current" { - var err error - l.size = info.Size() - l.curFile, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.FileMode(0644)) - if err != nil { - glog.WithError(err).Fatal("Unable to open file in write mode.") - } - /* - ret, err := l.curFile.Seek(info.Size(), 0) - if err != nil || ret != info.Size() { - glog.WithError(err).Fatal("Unable to seek to end of file.") - } - */ - l.lastLogTs, err = lastTimestamp(path) - if err != nil { - glog.WithError(err).Fatal("Unable to read last log timestamp.") - } return nil } @@ -186,6 +170,28 @@ func (l *Logger) Init() { l.Lock() defer l.Unlock() + { + // First check if we have a current file. + path := filepath.Join(l.dir, fmt.Sprintf("%s-current.log", l.filePrefix)) + fi, err := os.Stat(path) + if err == nil { + // we have the file. Derive information for counters. + l.size = fi.Size() + l.logsSinceLastSync = 0 + l.lastLogTs, err = lastTimestamp(path) + if err != nil { + glog.WithError(err).Fatal("Unable to read last log timestamp.") + } + + // Open file for append. + l.curFile, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, + os.FileMode(0644)) + if err != nil { + glog.WithError(err).Fatal("Unable to open current file in append mode.") + } + } + } + if err := filepath.Walk(l.dir, l.handleFile); err != nil { glog.WithError(err).Fatal("While walking over directory") } diff --git a/commit/log_test.go b/commit/log_test.go index 810ca265d5d543a7c14f938e83f108144df1807b..0c76d30a3e65281b33fa409dd1f7418f03f6f21b 100644 --- a/commit/log_test.go +++ b/commit/log_test.go @@ -129,29 +129,31 @@ func TestRotatingLog(t *testing.T) { t.Errorf("Expected ts: %v. Got: %v", ts+int64(8), l.lastLogTs) } l.Close() + l = nil // Important to avoid re-use later. // Now, let's test a re-init of logger. nl := NewLogger(dir, "dgraph", 1024) nl.Init() + defer nl.Close() if len(nl.list) != 4 { t.Errorf("Expected 4 files. Got: %v", len(nl.list)) } if nl.size != 416 { t.Errorf("Expected size 416. Got: %v", nl.size) } - if err := l.AddLog(ts+int64(100), 0, data); err != nil { + if err := nl.AddLog(ts+int64(100), 0, data); err != nil { t.Error(err) return } if nl.size != 832 { t.Errorf("Expected size 832. Got: %v", nl.size) } - if err := l.AddLog(ts+int64(113), 0, data); err != nil { + if err := nl.AddLog(ts+int64(113), 0, data); err != nil { t.Error(err) return } if len(nl.list) != 5 { - t.Errorf("Expected 4 files. Got: %v", len(nl.list)) + t.Errorf("Expected 5 files. Got: %v", len(nl.list)) } if nl.list[4].endTs != ts+int64(100) { t.Errorf("Expected ts: %v. Got: %v", ts+int64(100), nl.list[4].endTs)