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

release-19.1: util/log: ensure that secondary loggers do not l… #41233

Merged
merged 1 commit into from
Oct 1, 2019
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
45 changes: 23 additions & 22 deletions pkg/util/log/clog.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func formatHeader(
cp = nil
}

buf := logging.getBuffer()
buf := getBuffer()
if line < 0 {
line = 0 // not a real line number, but acceptable to someDigits
}
Expand Down Expand Up @@ -700,13 +700,6 @@ type loggingT struct {
// Level flag for output to files.
fileThreshold Severity

// freeList is a list of byte buffers, maintained under freeListMu.
freeList *buffer
// freeListMu maintains the free list. It is separate from the main mutex
// so buffers can be grabbed and printed to without holding the main lock,
// for better parallelization.
freeListMu syncutil.Mutex

// mu protects the remaining elements of this structure and is
// used to synchronize logging.
mu syncutil.Mutex
Expand Down Expand Up @@ -746,6 +739,12 @@ type loggingT struct {
clusterID string
}

var freeList struct {
syncutil.Mutex
// head is the head of a list of byte buffers, maintained under the mutex.
head *buffer
}

// buffer holds a byte Buffer for reuse. The zero value is ready for use.
type buffer struct {
bytes.Buffer
Expand Down Expand Up @@ -796,13 +795,13 @@ func (l *loggingT) setVState(verbosity level, filter []modulePat, setFilter bool
}

// getBuffer returns a new, ready-to-use buffer.
func (l *loggingT) getBuffer() *buffer {
l.freeListMu.Lock()
b := l.freeList
func getBuffer() *buffer {
freeList.Lock()
b := freeList.head
if b != nil {
l.freeList = b.next
freeList.head = b.next
}
l.freeListMu.Unlock()
freeList.Unlock()
if b == nil {
b = new(buffer)
} else {
Expand All @@ -813,15 +812,15 @@ func (l *loggingT) getBuffer() *buffer {
}

// putBuffer returns a buffer to the free list.
func (l *loggingT) putBuffer(b *buffer) {
func putBuffer(b *buffer) {
if b.Len() >= 256 {
// Let big buffers die a natural death.
return
}
l.freeListMu.Lock()
b.next = l.freeList
l.freeList = b
l.freeListMu.Unlock()
freeList.Lock()
b.next = freeList.head
freeList.head = b
freeList.Unlock()
}

// ensureFile ensures that l.file is set and valid.
Expand Down Expand Up @@ -942,10 +941,11 @@ func (l *loggingT) outputLogEntry(s Severity, file string, line int, msg string)
if err := l.writeToFile(data); err != nil {
l.exitLocked(err)
l.mu.Unlock()
putBuffer(buf)
return
}

l.putBuffer(buf)
putBuffer(buf)
}
// Flush and exit on fatal logging.
if s == Severity_FATAL {
Expand Down Expand Up @@ -991,10 +991,11 @@ func (l *loggingT) printPanicToFile(r interface{}) {

func (l *loggingT) outputToStderr(entry Entry, stacks []byte) {
buf := l.processForStderr(entry, stacks)
if _, err := OrigStderr.Write(buf.Bytes()); err != nil {
_, err := OrigStderr.Write(buf.Bytes())
putBuffer(buf)
if err != nil {
l.exitLocked(err)
}
l.putBuffer(buf)
}

// processForStderr formats a log entry for output to standard error.
Expand Down Expand Up @@ -1157,11 +1158,11 @@ func (sb *syncBuffer) rotateFile(now time.Time) error {
}, nil, nil)
var n int
n, err = sb.file.Write(buf.Bytes())
putBuffer(buf)
sb.nbytes += int64(n)
if err != nil {
return err
}
logging.putBuffer(buf)
}

select {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/log/clog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestEntryDecoder(t *testing.T) {
buf := formatHeader(s, now, gid, file, line, nil)
buf.WriteString(msg)
buf.WriteString("\n")
defer logging.putBuffer(buf)
defer putBuffer(buf)
return buf.String()
}

Expand Down Expand Up @@ -724,7 +724,7 @@ func TestExitOnFullDisk(t *testing.T) {
func BenchmarkHeader(b *testing.B) {
for i := 0; i < b.N; i++ {
buf := formatHeader(Severity_INFO, timeutil.Now(), 200, "file.go", 100, nil)
logging.putBuffer(buf)
putBuffer(buf)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func MakeEntry(s Severity, t int64, file string, line int, msg string) Entry {
// Format writes the log entry to the specified writer.
func (e Entry) Format(w io.Writer) error {
buf := formatLogEntry(e, nil, nil)
defer logging.putBuffer(buf)
defer putBuffer(buf)
_, err := w.Write(buf.Bytes())
return err
}
Expand Down