Convert the header into an enum

Signed-off-by: Ganesh Vernekar <ganeshvern@gmail.com>
This commit is contained in:
Ganesh Vernekar 2021-10-07 19:53:24 +05:30
parent 175ef4ebcf
commit 5d4dc7e413
No known key found for this signature in database
GPG Key ID: 0F8729A5EB59B965
4 changed files with 27 additions and 47 deletions

View File

@ -106,45 +106,30 @@ func (c *HistoChunk) Meta() (int32, float64, []histogram.Span, []histogram.Span,
return readHistoChunkMeta(&b)
}
// SetCounterReset sets the counter reset flag to 1 if the passed argument is true, 0 otherwise.
func (c *HistoChunk) SetCounterReset(counterReset bool) {
bytes := c.Bytes()
header := bytes[2]
if counterReset {
header |= counterResetMask
} else if (header & counterResetMask) != 0 {
header ^= counterResetMask
// CounterResetHeader defines the first 2 bits of the chunk header.
type CounterResetHeader byte
const (
CounterReset CounterResetHeader = 0b10000000
NotCounterReset CounterResetHeader = 0b01000000
GaugeType CounterResetHeader = 0b11000000
UnknownCounterReset CounterResetHeader = 0b00000000
)
// SetCounterResetHeader sets the counter reset header.
func (c *HistoChunk) SetCounterResetHeader(h CounterResetHeader) {
switch h {
case CounterReset, NotCounterReset, GaugeType, UnknownCounterReset:
bytes := c.Bytes()
bytes[2] = (bytes[2] & 0b00111111) | byte(h)
default:
panic("invalid CounterResetHeader type")
}
bytes[2] = header
}
// CounterReset returns true if this new chunk was created because of a counter reset.
func (c *HistoChunk) CounterReset() bool {
if c.NumSamples() == 0 {
panic("HistoChunk.CounterReset() called on an empty chunk")
}
return (c.Bytes()[2] & counterResetMask) != 0
}
// SetNotCounterReset sets the "not counter reset" flag to 1 if the passed argument is true, 0 otherwise.
func (c *HistoChunk) SetNotCounterReset(notCounterReset bool) {
bytes := c.Bytes()
header := bytes[2]
if notCounterReset {
header |= notCounterResetMask
} else if (header & notCounterResetMask) != 0 {
header ^= notCounterResetMask
}
bytes[2] = header
}
// NotCounterReset returns true if this new chunk definitely did not have counter reset
// from the earlier chunk.
func (c *HistoChunk) NotCounterReset() bool {
if c.NumSamples() == 0 {
panic("HistoChunk.NotCounterReset() called on an empty chunk")
}
return (c.Bytes()[2] & notCounterResetMask) != 0
// GetCounterResetHeader returns the info about the first 2 bits of the chunk header.
func (c *HistoChunk) GetCounterResetHeader() CounterResetHeader {
return CounterResetHeader(c.Bytes()[2] & 0b11000000)
}
// Compact implements the Chunk interface.
@ -562,8 +547,7 @@ func (a *HistoAppender) Recode(posInterjections, negInterjections []Interjection
}
// Set the flags.
hc.SetCounterReset(byts[2]&counterResetMask != 0)
hc.SetNotCounterReset(byts[2]&notCounterResetMask != 0)
hc.SetCounterResetHeader(CounterResetHeader(byts[2] & 0b11000000))
return hc, app
}

View File

@ -17,11 +17,6 @@ import (
"github.com/prometheus/prometheus/pkg/histogram"
)
const (
counterResetMask = 0b10000000
notCounterResetMask = 0b01000000
)
func writeHistoChunkMeta(b *bstream, schema int32, zeroThreshold float64, posSpans, negSpans []histogram.Span) {
putInt64VBBucket(b, int64(schema))
putFloat64VBBucket(b, zeroThreshold)

View File

@ -644,11 +644,13 @@ func (s *memSeries) appendHistogram(t int64, sh histogram.SparseHistogram, appen
if chunkCreated {
hc := s.headChunk.chunk.(*chunkenc.HistoChunk)
header := chunkenc.UnknownCounterReset
if counterReset {
hc.SetCounterReset(true)
header = chunkenc.CounterReset
} else if okToAppend {
hc.SetNotCounterReset(true)
header = chunkenc.NotCounterReset
}
hc.SetCounterResetHeader(header)
}
s.app.AppendHistogram(t, sh)

View File

@ -718,8 +718,7 @@ func (p *populateWithDelChunkSeriesIterator) Next() bool {
)
if p.currDelIter.ChunkEncoding() == chunkenc.EncSHS {
if hc, ok := p.currChkMeta.Chunk.(*chunkenc.HistoChunk); ok {
newChunk.(*chunkenc.HistoChunk).SetCounterReset(hc.CounterReset())
newChunk.(*chunkenc.HistoChunk).SetNotCounterReset(hc.NotCounterReset())
newChunk.(*chunkenc.HistoChunk).SetCounterResetHeader(hc.GetCounterResetHeader())
}
t, h = p.currDelIter.AtHistogram()
p.curr.MinTime = t