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

Cherry-pick #17613 to 7.x: Do not rotate log files on startup when interval is configured and rotateonstartup is disabled #17644

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -95,6 +95,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
19 changes: 15 additions & 4 deletions libbeat/common/file/interval_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package file
import (
"errors"
"fmt"
"os"
"sort"
"strconv"
"time"
Expand All @@ -45,7 +46,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 +55,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 +94,17 @@ func (r *intervalRotator) initialize() error {
return lastInterval != currentInterval
}
}
return nil

if !rotateOnStartup {
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 = fi.ModTime()
}
}

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