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

Fix race condition in logging #4519

Merged
merged 1 commit into from
Jun 19, 2017
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.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha1...master[Check the HEAD d
*Affecting all Beats*

- Don't stop with error loading the ES template if the ES output is not enabled. {pull}4436[4436]
- Fix race condition in internal logging rotator. {pull}4519[4519]

*Filebeat*

Expand Down
14 changes: 14 additions & 0 deletions libbeat/logp/file_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
)

const RotatorMaxFiles = 1024
Expand All @@ -21,6 +22,7 @@ type FileRotator struct {

current *os.File
currentSize uint64
currentLock sync.RWMutex
}

func (rotator *FileRotator) CreateDirectory() error {
Expand Down Expand Up @@ -73,16 +75,26 @@ func (rotator *FileRotator) WriteLine(line []byte) error {
}

line = append(line, '\n')

rotator.currentLock.RLock()
_, err := rotator.current.Write(line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the Write and the increment of the currentSize occur within the same lock block? Without that it seems that the currentSize may not be accurate when it is checked by other goroutines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right, I was trying to keep Write under a RLock as it may block for a while, and sacrifice some consistency in currentSize in favor of performance. Putting everything under a write lock would make goroutines logging wait on others, but yeah, this is probably overkill. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say having currentSize slightly out of date is fine, we just use it for the rotator so it's ok if that starts a bit late.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think @andrewkroh? I can also add some comment explaining this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with the behavior as is.

rotator.currentLock.RUnlock()

if err != nil {
return err
}

rotator.currentLock.Lock()
rotator.currentSize += uint64(len(line))
rotator.currentLock.Unlock()

return nil
}

func (rotator *FileRotator) shouldRotate() bool {
rotator.currentLock.RLock()
defer rotator.currentLock.RUnlock()

if rotator.current == nil {
return true
}
Expand Down Expand Up @@ -112,6 +124,8 @@ func (rotator *FileRotator) FileExists(fileNo int) bool {
}

func (rotator *FileRotator) Rotate() error {
rotator.currentLock.Lock()
defer rotator.currentLock.Unlock()

if rotator.current != nil {
if err := rotator.current.Close(); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions libbeat/logp/file_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,32 @@ func TestConfigSane(t *testing.T) {
}
assert.NotNil(t, rotator.CheckIfConfigSane())
}

func TestRaceConditions(t *testing.T) {
// Make sure concurrent `WriteLine` calls don't end up in race conditions around `rotator.current`
if testing.Verbose() {
LogInit(LOG_DEBUG, "", false, true, []string{"rotator"})
}

dir, err := ioutil.TempDir("", "test_rotator_")
if err != nil {
t.Errorf("Error: %s", err.Error())
return
}

Debug("rotator", "Directory: %s", dir)

rotateeverybytes := uint64(10)
keepfiles := 20

rotator := FileRotator{
Path: dir,
Name: "testbeat",
RotateEveryBytes: &rotateeverybytes,
KeepFiles: &keepfiles,
}

for i := 0; i < 1000; i++ {
go rotator.WriteLine([]byte(string(i)))
}
}