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
Add BufferedWriteSyncer #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zapcore | ||
|
||
import ( | ||
"bufio" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type BufferedWriteSyncer struct { | ||
WriteSyncer | ||
sync.Mutex | ||
|
||
Size int | ||
FlushInterval time.Duration | ||
Clock Clock | ||
|
||
// unexported fields for state | ||
size int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused field. can delete. |
||
writer *bufio.Writer | ||
ticker *time.Ticker | ||
stop chan struct{} | ||
initialized bool | ||
} | ||
|
||
const ( | ||
// _defaultBufferSize specifies the default size used by Buffer. | ||
_defaultBufferSize = 256 * 1024 // 256 kB | ||
|
||
// _defaultFlushInterval specifies the default flush interval for | ||
// Buffer. | ||
_defaultFlushInterval = 30 * time.Second | ||
) | ||
|
||
func (s *BufferedWriteSyncer) init() { | ||
size := s.Size | ||
if size == 0 { | ||
size = _defaultBufferSize | ||
} | ||
|
||
flushInterval := s.FlushInterval | ||
if flushInterval == 0 { | ||
flushInterval = _defaultFlushInterval | ||
} | ||
|
||
if s.Clock != nil { | ||
s.ticker = s.Clock.NewTicker(flushInterval) | ||
} else { | ||
s.ticker = time.NewTicker(flushInterval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use a default clock implementation if unset rather than if-else on using the clock versus using the system. So the systemClock should be available in zapcore to be used. |
||
} | ||
|
||
s.writer = bufio.NewWriterSize(s.WriteSyncer, size) | ||
s.stop = make(chan struct{}) | ||
s.initialized = true | ||
go s.flushLoop() | ||
} | ||
|
||
// Write writes log data into buffer syncer directly, multiple Write calls will be batched, | ||
// and log data will be flushed to disk when the buffer is full or periodically. | ||
func (s *BufferedWriteSyncer) Write(bs []byte) (int, error) { | ||
if !s.initialized { | ||
s.init() | ||
} | ||
|
||
s.Lock() | ||
defer s.Unlock() | ||
|
||
Comment on lines
+84
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're trying to make Write thread-safe, the |
||
// To avoid partial writes from being flushed, we manually flush the existing buffer if: | ||
// * The current write doesn't fit into the buffer fully, and | ||
// * The buffer is not empty (since bufio will not split large writes when the buffer is empty) | ||
if len(bs) > s.writer.Available() && s.writer.Buffered() > 0 { | ||
if err := s.writer.Flush(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
|
||
return s.writer.Write(bs) | ||
} | ||
|
||
// Sync flushes buffered log data into disk directly. | ||
func (s *BufferedWriteSyncer) Sync() error { | ||
s.Lock() | ||
defer s.Unlock() | ||
|
||
return s.writer.Flush() | ||
} | ||
|
||
// flushLoop flushes the buffer at the configured interval until Close is | ||
// called. | ||
func (s *BufferedWriteSyncer) flushLoop() { | ||
for { | ||
select { | ||
case <-s.ticker.C: | ||
// we just simply ignore error here | ||
// because the underlying bufio writer stores any errors | ||
// and we return any error from Sync() as part of the close | ||
_ = s.Sync() | ||
case <-s.stop: | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Close closes the buffer, cleans up background goroutines, and flushes | ||
// remaining, unwritten data. | ||
func (s *BufferedWriteSyncer) Close() error { | ||
s.ticker.Stop() | ||
close(s.stop) | ||
return s.Sync() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zapcore | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func BenchmarkBufferedWriteSyncer(b *testing.B) { | ||
b.Run("write file with buffer", func(b *testing.B) { | ||
file, err := ioutil.TempFile("", "log") | ||
assert.NoError(b, err) | ||
defer file.Close() | ||
defer os.Remove(file.Name()) | ||
|
||
w := &BufferedWriteSyncer{ | ||
WriteSyncer: AddSync(file), | ||
} | ||
defer w.Close() | ||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
w.Write([]byte("foobarbazbabble")) | ||
} | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zapcore | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/goleak" | ||
"go.uber.org/zap/internal/ztest" | ||
) | ||
|
||
func TestBufferWriter(t *testing.T) { | ||
defer goleak.VerifyNone(t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not necessary if leak_test provides this via TestMain |
||
|
||
// If we pass a plain io.Writer, make sure that we still get a WriteSyncer | ||
// with a no-op Sync. | ||
t.Run("sync", func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)} | ||
|
||
requireWriteWorks(t, ws) | ||
assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.") | ||
assert.NoError(t, ws.Sync(), "Unexpected error calling a no-op Sync method.") | ||
assert.Equal(t, "foo", buf.String(), "Unexpected log string") | ||
assert.NoError(t, ws.Close()) | ||
}) | ||
|
||
t.Run("close", func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)} | ||
requireWriteWorks(t, ws) | ||
assert.Empty(t, buf.String(), "Unexpected log calling a no-op Write method.") | ||
assert.NoError(t, ws.Close()) | ||
assert.Equal(t, "foo", buf.String(), "Unexpected log string") | ||
}) | ||
|
||
t.Run("wrap twice", func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
bufsync := &BufferedWriteSyncer{WriteSyncer: AddSync(buf)} | ||
ws := &BufferedWriteSyncer{WriteSyncer: bufsync} | ||
requireWriteWorks(t, ws) | ||
assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.") | ||
assert.NoError(t, ws.Close()) | ||
assert.NoError(t, bufsync.Close()) | ||
assert.Equal(t, "foo", buf.String(), "Unexpected log string") | ||
}) | ||
|
||
t.Run("small buffer", func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
ws := &BufferedWriteSyncer{WriteSyncer: AddSync(buf), Size: 5} | ||
|
||
requireWriteWorks(t, ws) | ||
assert.Equal(t, "", buf.String(), "Unexpected log calling a no-op Write method.") | ||
requireWriteWorks(t, ws) | ||
assert.Equal(t, "foo", buf.String(), "Unexpected log string") | ||
assert.NoError(t, ws.Close()) | ||
}) | ||
|
||
t.Run("flush error", func(t *testing.T) { | ||
ws := &BufferedWriteSyncer{WriteSyncer: &ztest.FailWriter{}, Size: 4} | ||
n, err := ws.Write([]byte("foo")) | ||
require.NoError(t, err, "Unexpected error writing to WriteSyncer.") | ||
require.Equal(t, 3, n, "Wrote an unexpected number of bytes.") | ||
ws.Write([]byte("foo")) | ||
assert.Error(t, ws.Close(), "Expected close to fail.") | ||
}) | ||
|
||
t.Run("flush timer", func(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
clock := newControlledClock() | ||
ws := &BufferedWriteSyncer{ | ||
WriteSyncer: AddSync(buf), | ||
Size: 6, | ||
FlushInterval: time.Microsecond, | ||
Clock: clock, | ||
} | ||
requireWriteWorks(t, ws) | ||
clock.Add(10 * time.Millisecond) | ||
assert.Equal(t, "foo", buf.String(), "Unexpected log string") | ||
|
||
// flush twice to validate loop logic | ||
requireWriteWorks(t, ws) | ||
clock.Add(10 * time.Millisecond) | ||
assert.Equal(t, "foofoo", buf.String(), "Unexpected log string") | ||
assert.NoError(t, ws.Close()) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, anything you embed is part of the public API. That's probably not desirable for the mutex, but we can do it for the WriteSyncer if we want to.
https://github.com/uber-go/guide/blob/master/style.md#avoid-embedding-types-in-public-structs