From 95eecb6b1cd97ce85bc3220766072497855f005e Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 14 Jun 2016 20:27:52 +0500 Subject: [PATCH] Output file fixes * Fixed flush/Write race * Fixed names with underscores --- output_file.go | 20 ++++++++++++++++---- output_file_test.go | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/output_file.go b/output_file.go index 5b797d8c7..fc0965e90 100644 --- a/output_file.go +++ b/output_file.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "time" + "sync" ) var dateFileNameFuncs = map[string]func() string{ @@ -33,6 +34,7 @@ type FileOutputConfig struct { // FileOutput output plugin type FileOutput struct { + mu sync.Mutex pathTemplate string currentName string file *os.File @@ -87,7 +89,9 @@ func setFileIndex(name string, idx int) string { withoutExt := strings.TrimSuffix(name, ext) if i := strings.LastIndex(withoutExt, "_"); i != -1 { - withoutExt = withoutExt[:i] + if _, err := strconv.Atoi(withoutExt[i+1:]); err == nil { + withoutExt = withoutExt[:i] + } } return withoutExt + "_" + idxS + ext @@ -120,6 +124,9 @@ func (s sortByFileIndex) Less(i, j int) bool { } func (o *FileOutput) filename() string { + defer o.mu.Unlock() + o.mu.Lock() + path := o.pathTemplate for name, fn := range dateFileNameFuncs { @@ -172,6 +179,7 @@ func (o *FileOutput) Write(data []byte) (n int, err error) { } if o.file == nil || o.currentName != o.file.Name() { + o.mu.Lock() o.Close() o.file, err = os.OpenFile(o.currentName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660) @@ -188,6 +196,7 @@ func (o *FileOutput) Write(data []byte) (n int, err error) { } o.queueLength = 0 + o.mu.Unlock() } o.writer.Write(data) @@ -199,16 +208,19 @@ func (o *FileOutput) Write(data []byte) (n int, err error) { } func (o *FileOutput) flush() { + defer o.mu.Unlock() + o.mu.Lock() + if o.file != nil { if strings.HasSuffix(o.currentName, ".gz") { o.writer.(*gzip.Writer).Flush() } else { o.writer.(*bufio.Writer).Flush() } - } - if stat, err := o.file.Stat(); err != nil { - o.chunkSize = int(stat.Size()) + if stat, err := o.file.Stat(); err != nil { + o.chunkSize = int(stat.Size()) + } } } diff --git a/output_file_test.go b/output_file_test.go index ec7935101..b086740d1 100644 --- a/output_file_test.go +++ b/output_file_test.go @@ -166,6 +166,7 @@ func TestSetFileIndex(t *testing.T) { {"/tmp/logs_1", 0, "/tmp/logs_0"}, {"/tmp/logs_0", 10, "/tmp/logs_10"}, {"/tmp/logs_0.gz", 10, "/tmp/logs_10.gz"}, + {"/tmp/logs_underscores.gz", 10, "/tmp/logs_underscores_10.gz"}, } for _, c := range tests {