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 acquire lock for file.Sync() fsync call #404

Merged
merged 1 commit into from
Jun 11, 2024
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
25 changes: 20 additions & 5 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,8 @@ func (l *loggingT) exit(err error) {
logExitFunc(err)
return
}
l.flushAll()
files := l.flushAll()
l.syncAll(files)
OsExit(2)
}

Expand Down Expand Up @@ -1223,24 +1224,38 @@ func StartFlushDaemon(interval time.Duration) {
// lockAndFlushAll is like flushAll but locks l.mu first.
func (l *loggingT) lockAndFlushAll() {
l.mu.Lock()
l.flushAll()
files := l.flushAll()
l.mu.Unlock()
// Some environments are slow when syncing and holding the lock might cause contention.
l.syncAll(files)
}

// flushAll flushes all the logs and attempts to "sync" their data to disk.
// flushAll flushes all the logs
// l.mu is held.
func (l *loggingT) flushAll() {
func (l *loggingT) flushAll() []flushSyncWriter {
files := make([]flushSyncWriter, 0, severity.NumSeverity)
// Flush from fatal down, in case there's trouble flushing.
for s := severity.FatalLog; s >= severity.InfoLog; s-- {
file := l.file[s]
if file != nil {
_ = file.Flush() // ignore error
_ = file.Sync() // ignore error
}
files = append(files, file)
}
if logging.loggerOptions.flush != nil {
logging.loggerOptions.flush()
}
return files
}

// syncAll attempts to "sync" their data to disk.
func (l *loggingT) syncAll(files []flushSyncWriter) {
// Flush from fatal down, in case there's trouble flushing.
for _, file := range files {
if file != nil {
_ = file.Sync() // ignore error
Copy link

Choose a reason for hiding this comment

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

As noted in #403 (comment), syncBuffer is not thread-safe and here we call syncBuffer.Sync.

Will follow-up in #407.

}
}
}

// CopyStandardLogTo arranges for messages written to the Go "log" package's
Expand Down
6 changes: 4 additions & 2 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ func TestOpenAppendOnStart(t *testing.T) {
}

// ensure we wrote what we expected
logging.flushAll()
files := logging.flushAll()
logging.syncAll(files)
b, err := ioutil.ReadFile(logging.logFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -817,7 +818,8 @@ func BenchmarkLogs(b *testing.B) {
Warning("warning")
Info("info")
}
logging.flushAll()
files := logging.flushAll()
logging.syncAll(files)
}

// Test the logic on checking log size limitation.
Expand Down