-
Notifications
You must be signed in to change notification settings - Fork 455
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
Make commitlog msgpack encoder really, really fast. #1160
Merged
Merged
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
198417a
Convert commitlog chan to batches
984eb42
refactor
f3a966c
Add write batch function
62f263b
Fast commitlog path
5d2bdde
Add comments
40420d6
Break benchmarks into separate file
f4eede3
Move fast encoder to its own file
46e3ef0
Revert "Add write batch function"
c84e95a
Revert "refactor"
6f8527c
Revert "Convert commitlog chan to batches"
c4e93cf
Fix benchmark
a0db923
Fix imports
a342994
Improve comment
b07b1c9
Fix typo
8fc4f8a
Optimize encoding log metadata as well
39a6dcb
Use fast path for encoding log metadata in commitlog
9ca2313
Automatically reset buffers if they get too big and improve comments
f63a8c1
Improve comment
d0371af
Comment out dead code
de9ab7e
Remove unnecesary lines
c9a0525
Fix bug
ab50047
sort imports
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,8 @@ const ( | |
chunkHeaderChecksumDataLen | ||
|
||
defaultBitSetLength = 65536 | ||
|
||
defaultEncoderBuffSize = 16384 | ||
) | ||
|
||
var ( | ||
|
@@ -95,21 +97,22 @@ type chunkWriter interface { | |
type flushFn func(err error) | ||
|
||
type writer struct { | ||
filePathPrefix string | ||
newFileMode os.FileMode | ||
newDirectoryMode os.FileMode | ||
nowFn clock.NowFn | ||
start time.Time | ||
duration time.Duration | ||
chunkWriter chunkWriter | ||
chunkReserveHeader []byte | ||
buffer *bufio.Writer | ||
sizeBuffer []byte | ||
seen *bitset.BitSet | ||
logEncoder *msgpack.Encoder | ||
metadataEncoder *msgpack.Encoder | ||
tagEncoder serialize.TagEncoder | ||
tagSliceIter ident.TagsIterator | ||
filePathPrefix string | ||
newFileMode os.FileMode | ||
newDirectoryMode os.FileMode | ||
nowFn clock.NowFn | ||
start time.Time | ||
duration time.Duration | ||
chunkWriter chunkWriter | ||
chunkReserveHeader []byte | ||
buffer *bufio.Writer | ||
sizeBuffer []byte | ||
seen *bitset.BitSet | ||
logEncoder *msgpack.Encoder | ||
logEncoderBuff []byte | ||
metadataEncoderBuff []byte | ||
tagEncoder serialize.TagEncoder | ||
tagSliceIter ident.TagsIterator | ||
} | ||
|
||
func newCommitLogWriter( | ||
|
@@ -119,19 +122,20 @@ func newCommitLogWriter( | |
shouldFsync := opts.Strategy() == StrategyWriteWait | ||
|
||
return &writer{ | ||
filePathPrefix: opts.FilesystemOptions().FilePathPrefix(), | ||
newFileMode: opts.FilesystemOptions().NewFileMode(), | ||
newDirectoryMode: opts.FilesystemOptions().NewDirectoryMode(), | ||
nowFn: opts.ClockOptions().NowFn(), | ||
chunkWriter: newChunkWriter(flushFn, shouldFsync), | ||
chunkReserveHeader: make([]byte, chunkHeaderLen), | ||
buffer: bufio.NewWriterSize(nil, opts.FlushSize()), | ||
sizeBuffer: make([]byte, binary.MaxVarintLen64), | ||
seen: bitset.NewBitSet(defaultBitSetLength), | ||
logEncoder: msgpack.NewEncoder(), | ||
metadataEncoder: msgpack.NewEncoder(), | ||
tagEncoder: opts.FilesystemOptions().TagEncoderPool().Get(), | ||
tagSliceIter: ident.NewTagsIterator(ident.Tags{}), | ||
filePathPrefix: opts.FilesystemOptions().FilePathPrefix(), | ||
newFileMode: opts.FilesystemOptions().NewFileMode(), | ||
newDirectoryMode: opts.FilesystemOptions().NewDirectoryMode(), | ||
nowFn: opts.ClockOptions().NowFn(), | ||
chunkWriter: newChunkWriter(flushFn, shouldFsync), | ||
chunkReserveHeader: make([]byte, chunkHeaderLen), | ||
buffer: bufio.NewWriterSize(nil, opts.FlushSize()), | ||
sizeBuffer: make([]byte, binary.MaxVarintLen64), | ||
seen: bitset.NewBitSet(defaultBitSetLength), | ||
logEncoder: msgpack.NewEncoder(), | ||
logEncoderBuff: make([]byte, 0, defaultEncoderBuffSize), | ||
metadataEncoderBuff: make([]byte, 0, defaultEncoderBuffSize), | ||
tagEncoder: opts.FilesystemOptions().TagEncoderPool().Get(), | ||
tagSliceIter: ident.NewTagsIterator(ident.Tags{}), | ||
} | ||
} | ||
|
||
|
@@ -140,6 +144,15 @@ func (w *writer) Open(start time.Time, duration time.Duration) (File, error) { | |
return File{}, errCommitLogWriterAlreadyOpen | ||
} | ||
|
||
// Reset buffers since they will grow 2x on demand so we want to make sure that | ||
// one exceptionally large write does not cause them to remain oversized forever. | ||
if cap(w.logEncoderBuff) != defaultEncoderBuffSize { | ||
w.logEncoderBuff = make([]byte, 0, defaultEncoderBuffSize) | ||
} | ||
if cap(w.metadataEncoderBuff) != defaultEncoderBuffSize { | ||
w.metadataEncoderBuff = make([]byte, 0, defaultEncoderBuffSize) | ||
} | ||
|
||
commitLogsDir := fs.CommitLogsDirPath(w.filePathPrefix) | ||
if err := os.MkdirAll(commitLogsDir, w.newDirectoryMode); err != nil { | ||
return File{}, err | ||
|
@@ -224,22 +237,30 @@ func (w *writer) Write( | |
metadata.Namespace = series.Namespace.Bytes() | ||
metadata.Shard = series.Shard | ||
metadata.EncodedTags = encodedTags | ||
w.metadataEncoder.Reset() | ||
if err := w.metadataEncoder.EncodeLogMetadata(metadata); err != nil { | ||
|
||
w.metadataEncoderBuff = w.metadataEncoderBuff[:0] | ||
var err error | ||
|
||
w.metadataEncoderBuff, err = msgpack.EncodeLogMetadataFast(w.metadataEncoderBuff, metadata) | ||
if err != nil { | ||
return err | ||
} | ||
logEntry.Metadata = w.metadataEncoder.Bytes() | ||
logEntry.Metadata = w.metadataEncoderBuff | ||
} | ||
|
||
logEntry.Timestamp = datapoint.Timestamp.UnixNano() | ||
logEntry.Value = datapoint.Value | ||
logEntry.Unit = uint32(unit) | ||
logEntry.Annotation = annotation | ||
w.logEncoder.Reset() | ||
if err := w.logEncoder.EncodeLogEntry(logEntry); err != nil { | ||
|
||
w.logEncoderBuff = w.logEncoderBuff[:0] | ||
|
||
var err error | ||
w.logEncoderBuff, err = msgpack.EncodeLogEntryFast(w.logEncoderBuff, logEntry) | ||
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. Same here, can collapse to |
||
if err != nil { | ||
return err | ||
} | ||
if err := w.write(w.logEncoder.Bytes()); err != nil { | ||
if err := w.write(w.logEncoderBuff); err != nil { | ||
return err | ||
} | ||
|
||
|
103 changes: 103 additions & 0 deletions
103
src/dbnode/persist/fs/msgpack/encoder_decoder_bench_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) 2018 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 msgpack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkLogEntryDecoder(b *testing.B) { | ||
var ( | ||
enc = NewEncoder() | ||
dec = NewDecoder(nil) | ||
stream = NewDecoderStream(nil) | ||
err error | ||
) | ||
|
||
require.NoError(b, enc.EncodeLogEntry(testLogEntry)) | ||
buf := enc.Bytes() | ||
for n := 0; n < b.N; n++ { | ||
stream.Reset(buf) | ||
dec.Reset(stream) | ||
_, err = dec.DecodeLogEntry() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
var benchmarkBuf []byte | ||
|
||
func BenchmarkLogEntryEncoderFast(b *testing.B) { | ||
var err error | ||
benchmarkBuf = []byte{} | ||
|
||
for n := 0; n < b.N; n++ { | ||
benchmarkBuf, err = EncodeLogEntryFast(benchmarkBuf[:0], testLogEntry) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkLogEntryEncoder(b *testing.B) { | ||
var ( | ||
enc = NewEncoder() | ||
err error | ||
) | ||
|
||
for n := 0; n < b.N; n++ { | ||
enc.EncodeLogEntry(testLogEntry) | ||
if err != nil { | ||
panic(err) | ||
} | ||
benchmarkBuf = enc.Bytes() | ||
} | ||
} | ||
|
||
func BenchmarkLogMetadataEncoder(b *testing.B) { | ||
var ( | ||
enc = NewEncoder() | ||
err error | ||
) | ||
|
||
for n := 0; n < b.N; n++ { | ||
enc.EncodeLogMetadata(testLogMetadata) | ||
if err != nil { | ||
panic(err) | ||
} | ||
benchmarkBuf = enc.Bytes() | ||
} | ||
} | ||
|
||
func BenchmarkLogMetadataEncoderFast(b *testing.B) { | ||
var err error | ||
benchmarkBuf = []byte{} | ||
|
||
for n := 0; n < b.N; n++ { | ||
benchmarkBuf, err = EncodeLogMetadataFast(benchmarkBuf[:0], testLogMetadata) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
p.s. I realized this can be collapsed somewhat to do the
[:0]
just as you pass it to the EncodeLogMetadataFast method: