Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rotate log files on startup when interval is configured and rotateonstartup is disabled #17613

Merged
merged 6 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix concurrency issues in convert processor when used in the global context. {pull}17032[17032]
- Fix bug with `monitoring.cluster_uuid` setting not always being exposed via GET /state Beats API. {issue}16732[16732] {pull}17420[17420]
- Fix building on FreeBSD by removing build flags from `add_cloudfoundry_metadata` processor. {pull}17486[17486]
- Do not rotate log files on startup when interval is configured and rotateonstartup is disabled. {pull}17613[17613]

*Auditbeat*

Expand Down
45 changes: 41 additions & 4 deletions libbeat/common/file/interval_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ package file
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)

Expand All @@ -45,7 +48,7 @@ func (realClock) Now() time.Time {
return time.Now()
}

func newIntervalRotator(interval time.Duration) (*intervalRotator, error) {
func newIntervalRotator(log Logger, interval time.Duration, rotateOnStartup bool, filename string) (*intervalRotator, error) {
if interval == 0 {
return nil, nil
}
Expand All @@ -54,11 +57,11 @@ func newIntervalRotator(interval time.Duration) (*intervalRotator, error) {
}

ir := &intervalRotator{interval: (interval / time.Second) * time.Second} // drop fractional seconds
ir.initialize()
ir.initialize(log, rotateOnStartup, filename)
return ir, nil
}

func (r *intervalRotator) initialize() error {
func (r *intervalRotator) initialize(log Logger, rotateOnStartup bool, filename string) {
r.clock = realClock{}

switch r.interval {
Expand Down Expand Up @@ -93,7 +96,41 @@ func (r *intervalRotator) initialize() error {
return lastInterval != currentInterval
}
}
return nil

// if rotation is not allowed on startup, find the newest rotated file last modification time
// or that of the unrotated log file
kvch marked this conversation as resolved.
Show resolved Hide resolved
if !rotateOnStartup {
logsDir := filepath.Dir(filename)
logfiles, err := ioutil.ReadDir(logsDir)
if err != nil && log != nil {
log.Debugw("Not attempting to find last rotated time, configured logs dir cannot be opened: %v", err)
return
}

r.lastRotate = 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")
}

r.lastRotate = logfiles[0].ModTime()
return
}
basenamePrefix := filepath.Base(filename) + "-"
ycombinator marked this conversation as resolved.
Show resolved Hide resolved
for _, fi := range logfiles {
if strings.HasPrefix(fi.Name(), basenamePrefix) {
if fi.ModTime().After(r.lastRotate) {
r.lastRotate = fi.ModTime()
}
}
}

if log != nil {
log.Debugw("Set last rotated time to", r.lastRotate)
}
}
kvch marked this conversation as resolved.
Show resolved Hide resolved

return
ycombinator marked this conversation as resolved.
Show resolved Hide resolved
}

func (r *intervalRotator) LogPrefix(filename string, modTime time.Time) string {
Expand Down
24 changes: 14 additions & 10 deletions libbeat/common/file/interval_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

func TestSecondRotator(t *testing.T) {
a, err := newIntervalRotator(time.Second)
a, err := newMockIntervalRotator(time.Second)
if err != nil {
t.Fatal(err)
}
Expand All @@ -51,7 +51,7 @@ func TestSecondRotator(t *testing.T) {
}

func TestMinuteRotator(t *testing.T) {
a, err := newIntervalRotator(time.Minute)
a, err := newMockIntervalRotator(time.Minute)
if err != nil {
t.Fatal(err)
}
Expand All @@ -77,7 +77,7 @@ func TestMinuteRotator(t *testing.T) {
}

func TestHourlyRotator(t *testing.T) {
a, err := newIntervalRotator(time.Hour)
a, err := newMockIntervalRotator(time.Hour)
if err != nil {
t.Fatal(err)
}
Expand All @@ -103,7 +103,7 @@ func TestHourlyRotator(t *testing.T) {
}

func TestDailyRotator(t *testing.T) {
a, err := newIntervalRotator(24 * time.Hour)
a, err := newMockIntervalRotator(24 * time.Hour)
if err != nil {
t.Fatal(err)
}
Expand All @@ -129,7 +129,7 @@ func TestDailyRotator(t *testing.T) {
}

func TestWeeklyRotator(t *testing.T) {
a, err := newIntervalRotator(7 * 24 * time.Hour)
a, err := newMockIntervalRotator(7 * 24 * time.Hour)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestWeeklyRotator(t *testing.T) {
}

func TestMonthlyRotator(t *testing.T) {
a, err := newIntervalRotator(30 * 24 * time.Hour)
a, err := newMockIntervalRotator(30 * 24 * time.Hour)
if err != nil {
t.Fatal(err)
}
Expand All @@ -184,7 +184,7 @@ func TestMonthlyRotator(t *testing.T) {
}

func TestYearlyRotator(t *testing.T) {
a, err := newIntervalRotator(365 * 24 * time.Hour)
a, err := newMockIntervalRotator(365 * 24 * time.Hour)
if err != nil {
t.Fatal(err)
}
Expand All @@ -210,7 +210,7 @@ func TestYearlyRotator(t *testing.T) {
}

func TestArbitraryIntervalRotator(t *testing.T) {
a, err := newIntervalRotator(3 * time.Second)
a, err := newMockIntervalRotator(3 * time.Second)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -253,15 +253,15 @@ func TestArbitraryIntervalRotator(t *testing.T) {
}

func TestIntervalIsTruncatedToSeconds(t *testing.T) {
a, err := newIntervalRotator(2345 * time.Millisecond)
a, err := newMockIntervalRotator(2345 * time.Millisecond)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 2*time.Second, a.interval)
}

func TestZeroIntervalIsNil(t *testing.T) {
a, err := newIntervalRotator(0)
a, err := newMockIntervalRotator(0)
if err != nil {
t.Fatal(err)
}
Expand All @@ -275,3 +275,7 @@ type testClock struct {
func (t testClock) Now() time.Time {
return t.time
}

func newMockIntervalRotator(interval time.Duration) (*intervalRotator, error) {
return newIntervalRotator(nil, interval, false, "foo")
}
2 changes: 1 addition & 1 deletion libbeat/common/file/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func NewFileRotator(filename string, options ...RotatorOption) (*Rotator, error)
return nil, errors.Errorf("file rotator permissions mask of %o is invalid", r.permissions)
}
var err error
r.intervalRotator, err = newIntervalRotator(r.interval)
r.intervalRotator, err = newIntervalRotator(r.log, r.interval, r.rotateOnStartup, r.filename)
if err != nil {
return nil, err
}
Expand Down