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

Cut new blocks once the existing ones are full #71

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 10 additions & 1 deletion pkg/chunkenc/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,15 @@ func (c *MemChunk) SpaceFor(*logproto.Entry) bool {

// Append implements Chunk.
func (c *MemChunk) Append(entry *logproto.Entry) error {
return c.head.append(entry.Timestamp.UnixNano(), entry.Line)
if err := c.head.append(entry.Timestamp.UnixNano(), entry.Line); err != nil {
return err
}

if c.head.size >= c.blockSize {
return c.cut()
}

return nil
}

// Close implements Chunk.
Expand Down Expand Up @@ -341,6 +349,7 @@ func (c *MemChunk) cut() error {

c.head.entries = c.head.entries[:0]
c.head.mint = 0 // Will be set on first append.
c.head.size = 0

return nil
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/chunkenc/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,37 @@ func TestGZIPSerialisation(t *testing.T) {
require.True(t, bytes.Equal(byt, byt2))
}

func TestGZIPChunkFilling(t *testing.T) {
chk := NewMemChunk(EncGZIP)
chk.blockSize = 1024

// We should be able to append only 10KB of logs.
Copy link
Member

Choose a reason for hiding this comment

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

Might want to clarify that this is because chunks are hardcoded to allow 10 blocks per chunk.

Copy link
Member

Choose a reason for hiding this comment

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

Or even better, should we define the number of blocks per chunk as a constant.

logLine := string(make([]byte, 512))
entry := &logproto.Entry{
Timestamp: time.Unix(0, 0),
Line: logLine,
}

i := int64(0)
for ; chk.SpaceFor(entry) && i < 30; i++ {
entry.Timestamp = time.Unix(0, i)
require.NoError(t, chk.Append(entry))
}

require.Equal(t, int64(20), i)

it, err := chk.Iterator(time.Unix(0, 0), time.Unix(0, 100), logproto.FORWARD)
require.NoError(t, err)
i = 0
for it.Next() {
entry := it.Entry()
require.Equal(t, i, entry.Timestamp.UnixNano())
i++
}

require.Equal(t, int64(20), i)
}

func logprotoEntry(ts int64, line string) *logproto.Entry {
return &logproto.Entry{
Timestamp: time.Unix(0, ts),
Expand Down