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

Improve wal entries encoding. #3153

Merged
merged 1 commit into from
Jan 11, 2021
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
11 changes: 3 additions & 8 deletions pkg/ingester/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,11 @@ outer:

for _, s := range ref.Entries {
buf.PutVarint64(s.Timestamp.UnixNano() - first)
// denote line length
byteLine := []byte(s.Line)
buf.PutUvarint(len(byteLine))
buf.PutBytes(byteLine)
buf.PutUvarint(len(s.Line))
buf.PutString(s.Line)
}
}
return buf.Get()

}

func decodeEntries(b []byte, rec *WALRecord) error {
Expand Down Expand Up @@ -164,7 +161,6 @@ func decodeEntries(b []byte, rec *WALRecord) error {
return errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
}
return nil

}

func decodeWALRecord(b []byte, walRec *WALRecord) (err error) {
Expand Down Expand Up @@ -205,15 +201,14 @@ func decodeWALRecord(b []byte, walRec *WALRecord) (err error) {
func EncWith(b []byte) (res Encbuf) {
res.B = b
return res

}

// Encbuf extends encoding.Encbuf with support for multi byte encoding
type Encbuf struct {
encoding.Encbuf
}

func (e *Encbuf) PutBytes(c []byte) { e.B = append(e.B, c...) }
func (e *Encbuf) PutString(s string) { e.B = append(e.B, s...) }

func DecWith(b []byte) (res Decbuf) {
res.B = b
Expand Down
33 changes: 32 additions & 1 deletion pkg/ingester/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ func Test_Encoding_Entries(t *testing.T) {
require.Equal(t, record, decoded)
}

func Benchmark_EncodeEntries(b *testing.B) {
var entries []logproto.Entry
for i := int64(0); i < 10000; i++ {
entries = append(entries, logproto.Entry{
Timestamp: time.Unix(0, i),
Line: fmt.Sprintf("long line with a lot of data like a log %d", i),
})
}
record := &WALRecord{
entryIndexMap: make(map[uint64]int),
UserID: "123",
RefEntries: []RefEntries{
{
Ref: 456,
Entries: entries,
},
{
Ref: 789,
Entries: entries,
},
},
}
b.ReportAllocs()
b.ResetTimer()
buf := recordPool.GetBytes()[:0]
defer recordPool.PutBytes(buf)

for n := 0; n < b.N; n++ {
record.encodeEntries(buf)
}
}

func fillChunk(t *testing.T, c chunkenc.Chunk) int64 {
t.Helper()
var i, inserted int64
Expand Down Expand Up @@ -115,7 +147,6 @@ func dummyConf() *Config {
}

func Test_EncodingChunks(t *testing.T) {

conf := dummyConf()
c := chunkenc.NewMemChunk(chunkenc.EncGZIP, conf.BlockSize, conf.TargetChunkSize)
fillChunk(t, c)
Expand Down