From c212ef03265262ecc61c370b27468c4c0af7fcca Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Wed, 5 Oct 2016 20:32:55 +0200 Subject: [PATCH] Add Chunk.Utilization() methods When using the chunking code in other projects (both Weave Prism and ChronixDB ingester), you sometimes want to know how well you are utilizing your chunks when closing/storing them. --- storage/local/chunk/chunk.go | 1 + storage/local/chunk/delta.go | 5 +++++ storage/local/chunk/doubledelta.go | 5 +++++ storage/local/chunk/varbit.go | 6 ++++++ 4 files changed, 17 insertions(+) diff --git a/storage/local/chunk/chunk.go b/storage/local/chunk/chunk.go index d26237394..738e678f1 100644 --- a/storage/local/chunk/chunk.go +++ b/storage/local/chunk/chunk.go @@ -273,6 +273,7 @@ type Chunk interface { Unmarshal(io.Reader) error UnmarshalFromBuf([]byte) error Encoding() Encoding + Utilization() float64 } // Iterator enables efficient access to the content of a chunk. It is diff --git a/storage/local/chunk/delta.go b/storage/local/chunk/delta.go index f9bbff4a8..911ab63fa 100644 --- a/storage/local/chunk/delta.go +++ b/storage/local/chunk/delta.go @@ -267,6 +267,11 @@ func (c *deltaEncodedChunk) UnmarshalFromBuf(buf []byte) error { // encoding implements chunk. func (c deltaEncodedChunk) Encoding() Encoding { return Delta } +// Utilization implements chunk. +func (c deltaEncodedChunk) Utilization() float64 { + return float64(len(c)) / float64(cap(c)) +} + func (c deltaEncodedChunk) timeBytes() deltaBytes { return deltaBytes(c[deltaHeaderTimeBytesOffset]) } diff --git a/storage/local/chunk/doubledelta.go b/storage/local/chunk/doubledelta.go index debf5232d..51cc60346 100644 --- a/storage/local/chunk/doubledelta.go +++ b/storage/local/chunk/doubledelta.go @@ -277,6 +277,11 @@ func (c *doubleDeltaEncodedChunk) UnmarshalFromBuf(buf []byte) error { // encoding implements chunk. func (c doubleDeltaEncodedChunk) Encoding() Encoding { return DoubleDelta } +// Utilization implements chunk. +func (c doubleDeltaEncodedChunk) Utilization() float64 { + return float64(len(c)) / float64(cap(c)) +} + func (c doubleDeltaEncodedChunk) baseTime() model.Time { return model.Time( binary.LittleEndian.Uint64( diff --git a/storage/local/chunk/varbit.go b/storage/local/chunk/varbit.go index f5590abd3..21f7cd759 100644 --- a/storage/local/chunk/varbit.go +++ b/storage/local/chunk/varbit.go @@ -322,6 +322,12 @@ func (c varbitChunk) UnmarshalFromBuf(buf []byte) error { // encoding implements chunk. func (c varbitChunk) Encoding() Encoding { return Varbit } +// Utilization implements chunk. +func (c varbitChunk) Utilization() float64 { + // 15 bytes is the length of the chunk footer. + return math.Min(float64(c.nextSampleOffset()/8+15)/float64(cap(c)), 1) +} + // FirstTime implements chunk. func (c varbitChunk) FirstTime() model.Time { return model.Time(