Skip to content

Commit

Permalink
simplify behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Apr 9, 2020
1 parent 5664e54 commit 7bd2deb
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 111 deletions.
37 changes: 2 additions & 35 deletions libbeat/common/file/interval_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ package file
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -98,50 +95,20 @@ func (r *intervalRotator) initialize(log Logger, rotateOnStartup bool, filename
}
}

// if rotation is not allowed on startup, find the newest rotated file last modification time
// or that of the unrotated log file
if !rotateOnStartup {
logsDir := filepath.Dir(filename)
logfiles, err := ioutil.ReadDir(logsDir)
fi, err := os.Stat(filename)
if err != nil {
if log != nil {
log.Debugw("Not attempting to find last rotated time, configured logs dir cannot be opened: %v", err)
}
return
}
r.lastRotate = determineTimeOfLastRotation(log, filename, logfiles)
r.lastRotate = fi.ModTime()
}

return
}

func determineTimeOfLastRotation(log Logger, filename string, logfiles []os.FileInfo) time.Time {
if len(logfiles) == 1 && logfiles[0].Name() == filename {
if log != nil {
log.Debugw("Setting last rotated time to the last modification time of the log")
}

return logfiles[0].ModTime()
}
lastRotate := time.Time{}
basenamePrefix := filepath.Base(filename) + "-"
for _, fi := range logfiles {
if fi.Name() == filepath.Base(filename) {
return fi.ModTime()
}
if strings.HasPrefix(fi.Name(), basenamePrefix) {
if fi.ModTime().After(lastRotate) {
lastRotate = fi.ModTime()
}
}
}

if log != nil {
log.Debugw("Set last rotated time to", lastRotate)
}
return lastRotate
}

func (r *intervalRotator) LogPrefix(filename string, modTime time.Time) string {
var t time.Time
if r.lastRotate.IsZero() {
Expand Down
76 changes: 0 additions & 76 deletions libbeat/common/file/interval_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package file

import (
"os"
"testing"
"time"

Expand Down Expand Up @@ -269,81 +268,6 @@ func TestZeroIntervalIsNil(t *testing.T) {
assert.True(t, a == nil)
}

func TestSelectingLastRotateTime(t *testing.T) {
const oldestTsSec = 1586434581
cases := map[string]struct {
logfiles []os.FileInfo
expectedRotateTime time.Time
}{
"one file unrotated file": {
logfiles: []os.FileInfo{
fileInf{
name: "foo",
modTime: time.Unix(oldestTsSec, 0),
},
},
expectedRotateTime: time.Unix(oldestTsSec, 0),
},
"one file unrotated file, several rotated files": {
logfiles: []os.FileInfo{
fileInf{
name: "foo",
modTime: time.Unix(oldestTsSec+4*60, 0),
},
fileInf{
name: "foo-01",
modTime: time.Unix(oldestTsSec+3*60, 0),
},
fileInf{
name: "foo-02",
modTime: time.Unix(oldestTsSec+2*60, 0),
},
fileInf{
name: "foo-03",
modTime: time.Unix(oldestTsSec+1*60, 0),
},
},
expectedRotateTime: time.Unix(oldestTsSec+4*60, 0),
},
"several rotated files": {
logfiles: []os.FileInfo{
fileInf{
name: "foo-01",
modTime: time.Unix(oldestTsSec+3*60, 0),
},
fileInf{
name: "foo-02",
modTime: time.Unix(oldestTsSec+2*60, 0),
},
fileInf{
name: "foo-03",
modTime: time.Unix(oldestTsSec+1*60, 0),
},
},
expectedRotateTime: time.Unix(oldestTsSec+3*60, 0),
},
}

for name, test := range cases {
t.Run(name, func(t *testing.T) {
rotatedTime := determineTimeOfLastRotation(nil, "foo", test.logfiles)
assert.Equal(t, rotatedTime.Sub(test.expectedRotateTime), time.Duration(0))
})
}
}

type fileInf struct {
name string
modTime time.Time
}

func (f fileInf) Name() string { return f.name }
func (f fileInf) ModTime() time.Time { return f.modTime }
func (f fileInf) Size() int64 { return 0 }
func (f fileInf) Mode() os.FileMode { return 0666 }
func (f fileInf) IsDir() bool { return false }
func (f fileInf) Sys() interface{} { return nil }

type testClock struct {
time time.Time
}
Expand Down

0 comments on commit 7bd2deb

Please sign in to comment.