Skip to content

Commit

Permalink
Fix rotating log makes dfdaemon.log file huge size
Browse files Browse the repository at this point in the history
When the size of dfdaemon.log file large than 20M,
the size of log file will continuous growth up.

Use Mmap to copy the file which need be truncated.

Related to issue:
dragonflyoss/dragonfly#364
  • Loading branch information
godliness authored and Chao Ma committed Feb 1, 2019
1 parent f4734b3 commit a3b6ef8
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions dfdaemon/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package initializer

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -102,29 +101,33 @@ func cleanLocalRepo(options *options.Options) {
}

// rotateLog truncates the logs file by a certain amount bytes.
func rotateLog(logFile *os.File, logFilePath string) {
func rotateLog(logFile *os.File) {
logSizeLimit := int64(20 * 1024 * 1024)
for {
time.Sleep(time.Second * 60)
stat, err := os.Stat(logFilePath)
stat, err := logFile.Stat()
if err != nil {
log.Errorf("failed to stat %s: %s", logFilePath, err)
log.Errorf("failed to stat: %s", err)
continue
}
// if it exceeds the 20MB limitation
if stat.Size() > logSizeLimit {
log.SetOutput(ioutil.Discard)
logFile.Sync()
if transFile, err := os.Open(logFilePath); err == nil {
// move the pointer to be (end - 10MB)
transFile.Seek(-10*1024*1024, 2)
// move the pointer to head
logFile.Seek(0, 0)
count, _ := io.Copy(logFile, transFile)
logFile.Truncate(count)
log.SetOutput(logFile)
transFile.Close()
truncateSize := logSizeLimit/2 - 1
mem, err := syscall.Mmap(int(logFile.Fd()), 0, int(stat.Size()), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
log.Errorf("failed to map file and memory: %s", err)
continue
}
copy(mem[0:], mem[truncateSize:])
if err := syscall.Munmap(mem); err != nil {
log.Errorf("failed to unmap file and memory: %s", err)
continue
}
logFile.Truncate(stat.Size() - truncateSize)
logFile.Seek(truncateSize, 0)
log.SetOutput(logFile)
}
}

Expand All @@ -148,7 +151,7 @@ func initLogger() {
if logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644); err == nil {
logFile.Seek(0, 2)
log.SetOutput(logFile)
go rotateLog(logFile, logFilePath)
go rotateLog(logFile)
}
}

Expand Down

0 comments on commit a3b6ef8

Please sign in to comment.