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

Cache overlapping blocks #2239

Merged
merged 5 commits into from
Jun 22, 2020
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
6 changes: 5 additions & 1 deletion pkg/chunkenc/dumb_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ func (c *dumbChunk) Bytes() ([]byte, error) {
return nil, nil
}

func (c *dumbChunk) Blocks() int {
func (c *dumbChunk) Blocks(_ time.Time, _ time.Time) []Block {
return nil
}

func (c *dumbChunk) BlockCount() int {
return 0
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/chunkenc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,27 @@ type Chunk interface {
SpaceFor(*logproto.Entry) bool
Append(*logproto.Entry) error
Iterator(ctx context.Context, from, through time.Time, direction logproto.Direction, filter logql.LineFilter) (iter.EntryIterator, error)
// Returns the list of blocks in the chunks.
Blocks(mintT, maxtT time.Time) []Block
Size() int
Bytes() ([]byte, error)
Blocks() int
BlockCount() int
Utilization() float64
UncompressedSize() int
CompressedSize() int
Close() error
}

// Block is a chunk block.
type Block interface {
// MinTime is the minimum time of entries in the block
MinTime() int64
// MaxTime is the maximum time of entries in the block
MaxTime() int64
// Offset is the offset/position of the block in the chunk. Offset is unique for a given block per chunk.
Offset() int
// Entries is the amount of entries in the block.
Entries() int
// Iterator returns an entry iterator for the block.
Iterator(context.Context, logql.LineFilter) iter.EntryIterator
}
31 changes: 0 additions & 31 deletions pkg/chunkenc/lazy_chunk.go

This file was deleted.

44 changes: 38 additions & 6 deletions pkg/chunkenc/memchunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type block struct {

offset int // The offset of the block in the chunk.
uncompressedSize int // Total uncompressed size in bytes when the chunk is cut.

readers ReaderPool
}

// This block holds the un-compressed entries. Once it has enough data, this is
Expand Down Expand Up @@ -212,7 +214,9 @@ func NewByteChunk(b []byte, blockSize, targetSize int) (*MemChunk, error) {
bc.blocks = make([]block, 0, num)

for i := 0; i < num; i++ {
blk := block{}
blk := block{
readers: bc.readers,
}
// Read #entries.
blk.numEntries = db.uvarint()

Expand Down Expand Up @@ -339,8 +343,8 @@ func (c *MemChunk) Size() int {
return ne
}

// Blocks implements Chunk.
func (c *MemChunk) Blocks() int {
// BlockCount implements Chunk.
func (c *MemChunk) BlockCount() int {
return len(c.blocks)
}

Expand Down Expand Up @@ -431,6 +435,7 @@ func (c *MemChunk) cut() error {
}

c.blocks = append(c.blocks, block{
readers: c.readers,
b: b,
numEntries: len(c.head.entries),
mint: c.head.mint,
Expand Down Expand Up @@ -477,7 +482,7 @@ func (c *MemChunk) Iterator(ctx context.Context, mintT, maxtT time.Time, directi
if maxt < b.mint || b.maxt < mint {
continue
}
its = append(its, b.iterator(ctx, c.readers, filter))
its = append(its, b.Iterator(ctx, filter))
}

if !c.head.isEmpty() {
Expand All @@ -497,11 +502,38 @@ func (c *MemChunk) Iterator(ctx context.Context, mintT, maxtT time.Time, directi
return iter.NewEntryReversedIter(iterForward)
}

func (b block) iterator(ctx context.Context, pool ReaderPool, filter logql.LineFilter) iter.EntryIterator {
// Blocks implements Chunk
func (c *MemChunk) Blocks(mintT, maxtT time.Time) []Block {
mint, maxt := mintT.UnixNano(), maxtT.UnixNano()
blocks := make([]Block, 0, len(c.blocks))

for _, b := range c.blocks {
if maxt > b.mint && b.maxt > mint {
blocks = append(blocks, b)
}
}
return blocks
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we gain anything by slicing up the existing c.blocks instead of allocating a new slice? Also curious if c.blocks was a slice of pointers if we could save a copy of the block here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I’ll check if it does help. There’s other place where I don’t reslice intentionally because reslicing keep underlying references.

}

func (b block) Iterator(ctx context.Context, filter logql.LineFilter) iter.EntryIterator {
if len(b.b) == 0 {
return emptyIterator
}
return newBufferedIterator(ctx, pool, b.b, filter)
return newBufferedIterator(ctx, b.readers, b.b, filter)
}

func (b block) Offset() int {
return b.offset
}

func (b block) Entries() int {
return b.numEntries
}
func (b block) MinTime() int64 {
return b.mint
}
func (b block) MaxTime() int64 {
return b.maxt
}

func (hb *headBlock) iterator(ctx context.Context, mint, maxt int64, filter logql.LineFilter) iter.EntryIterator {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ingester/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *stream) Push(ctx context.Context, entries []logproto.Entry, synchronize
chunk.closed = true

samplesPerChunk.Observe(float64(chunk.chunk.Size()))
blocksPerChunk.Observe(float64(chunk.chunk.Blocks()))
blocksPerChunk.Observe(float64(chunk.chunk.BlockCount()))
chunksCreatedTotal.Inc()

s.chunks = append(s.chunks, chunkDesc{
Expand Down
Loading