2018-05-17 06:02:47 -07:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
package record
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2021-06-29 07:38:46 -07:00
|
|
|
"github.com/prometheus/prometheus/pkg/histogram"
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/encoding"
|
2019-09-19 02:15:41 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
2018-05-17 06:02:47 -07:00
|
|
|
)
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// Type represents the data type of a record.
|
|
|
|
type Type uint8
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
const (
|
2020-10-05 02:09:59 -07:00
|
|
|
// Unknown is returned for unrecognised WAL record types.
|
|
|
|
Unknown Type = 255
|
2019-09-19 02:15:41 -07:00
|
|
|
// Series is used to match WAL records of type Series.
|
|
|
|
Series Type = 1
|
|
|
|
// Samples is used to match WAL records of type Samples.
|
|
|
|
Samples Type = 2
|
|
|
|
// Tombstones is used to match WAL records of type Tombstones.
|
|
|
|
Tombstones Type = 3
|
2021-05-06 13:53:52 -07:00
|
|
|
// Exemplars is used to match WAL records of type Exemplars.
|
|
|
|
Exemplars Type = 4
|
2018-05-17 06:02:47 -07:00
|
|
|
)
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
var (
|
|
|
|
// ErrNotFound is returned if a looked up resource was not found. Duplicate ErrNotFound from head.go.
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
// RefSeries is the series labels with the series ID.
|
|
|
|
type RefSeries struct {
|
|
|
|
Ref uint64
|
|
|
|
Labels labels.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefSample is a timestamp/value pair associated with a reference to a series.
|
|
|
|
type RefSample struct {
|
|
|
|
Ref uint64
|
|
|
|
T int64
|
|
|
|
V float64
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:53:52 -07:00
|
|
|
// RefExemplar is an exemplar with it's labels, timestamp, value the exemplar was collected/observed with, and a reference to a series.
|
|
|
|
type RefExemplar struct {
|
|
|
|
Ref uint64
|
|
|
|
T int64
|
|
|
|
V float64
|
|
|
|
Labels labels.Labels
|
|
|
|
}
|
|
|
|
|
2021-06-29 07:38:46 -07:00
|
|
|
// RefHistogram is a histogram.
|
|
|
|
type RefHistogram struct {
|
|
|
|
Ref uint64
|
|
|
|
T int64
|
|
|
|
H histogram.SparseHistogram
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// Decoder decodes series, sample, and tombstone records.
|
2018-05-17 06:02:47 -07:00
|
|
|
// The zero value is ready to use.
|
2019-09-19 02:15:41 -07:00
|
|
|
type Decoder struct {
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of the record.
|
2020-10-05 02:09:59 -07:00
|
|
|
// Returns RecordUnknown if no valid record type is found.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (d *Decoder) Type(rec []byte) Type {
|
2018-05-17 06:02:47 -07:00
|
|
|
if len(rec) < 1 {
|
2020-10-05 02:09:59 -07:00
|
|
|
return Unknown
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-09-19 02:15:41 -07:00
|
|
|
switch t := Type(rec[0]); t {
|
2021-05-06 13:53:52 -07:00
|
|
|
case Series, Samples, Tombstones, Exemplars:
|
2018-05-17 06:02:47 -07:00
|
|
|
return t
|
|
|
|
}
|
2020-10-05 02:09:59 -07:00
|
|
|
return Unknown
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Series appends series in rec to the given slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (d *Decoder) Series(rec []byte, series []RefSeries) ([]RefSeries, error) {
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := encoding.Decbuf{B: rec}
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
if Type(dec.Byte()) != Series {
|
2018-05-17 06:02:47 -07:00
|
|
|
return nil, errors.New("invalid record type")
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
for len(dec.B) > 0 && dec.Err() == nil {
|
|
|
|
ref := dec.Be64()
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
lset := make(labels.Labels, dec.Uvarint())
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
for i := range lset {
|
2019-02-22 09:11:11 -08:00
|
|
|
lset[i].Name = dec.UvarintStr()
|
|
|
|
lset[i].Value = dec.UvarintStr()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
sort.Sort(lset)
|
|
|
|
|
|
|
|
series = append(series, RefSeries{
|
|
|
|
Ref: ref,
|
|
|
|
Labels: lset,
|
|
|
|
})
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return nil, dec.Err()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
return series, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Samples appends samples in rec to the given slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (d *Decoder) Samples(rec []byte, samples []RefSample) ([]RefSample, error) {
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := encoding.Decbuf{B: rec}
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
if Type(dec.Byte()) != Samples {
|
2018-05-17 06:02:47 -07:00
|
|
|
return nil, errors.New("invalid record type")
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Len() == 0 {
|
2018-05-17 06:02:47 -07:00
|
|
|
return samples, nil
|
|
|
|
}
|
|
|
|
var (
|
2019-02-22 09:11:11 -08:00
|
|
|
baseRef = dec.Be64()
|
|
|
|
baseTime = dec.Be64int64()
|
2018-05-17 06:02:47 -07:00
|
|
|
)
|
2019-02-22 09:11:11 -08:00
|
|
|
for len(dec.B) > 0 && dec.Err() == nil {
|
|
|
|
dref := dec.Varint64()
|
|
|
|
dtime := dec.Varint64()
|
|
|
|
val := dec.Be64()
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
samples = append(samples, RefSample{
|
|
|
|
Ref: uint64(int64(baseRef) + dref),
|
|
|
|
T: baseTime + dtime,
|
|
|
|
V: math.Float64frombits(val),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return nil, errors.Wrapf(dec.Err(), "decode error after %d samples", len(samples))
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
return samples, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tombstones appends tombstones in rec to the given slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (d *Decoder) Tombstones(rec []byte, tstones []tombstones.Stone) ([]tombstones.Stone, error) {
|
2019-02-22 09:11:11 -08:00
|
|
|
dec := encoding.Decbuf{B: rec}
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
if Type(dec.Byte()) != Tombstones {
|
2018-05-17 06:02:47 -07:00
|
|
|
return nil, errors.New("invalid record type")
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
for dec.Len() > 0 && dec.Err() == nil {
|
2019-09-19 02:15:41 -07:00
|
|
|
tstones = append(tstones, tombstones.Stone{
|
|
|
|
Ref: dec.Be64(),
|
|
|
|
Intervals: tombstones.Intervals{
|
2019-02-22 09:11:11 -08:00
|
|
|
{Mint: dec.Varint64(), Maxt: dec.Varint64()},
|
2018-05-17 06:02:47 -07:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if dec.Err() != nil {
|
|
|
|
return nil, dec.Err()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
return tstones, nil
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:53:52 -07:00
|
|
|
func (d *Decoder) Exemplars(rec []byte, exemplars []RefExemplar) ([]RefExemplar, error) {
|
|
|
|
dec := encoding.Decbuf{B: rec}
|
|
|
|
t := Type(dec.Byte())
|
|
|
|
if t != Exemplars {
|
|
|
|
return nil, errors.New("invalid record type")
|
|
|
|
}
|
|
|
|
if dec.Len() == 0 {
|
|
|
|
return exemplars, nil
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
baseRef = dec.Be64()
|
|
|
|
baseTime = dec.Be64int64()
|
|
|
|
)
|
|
|
|
for len(dec.B) > 0 && dec.Err() == nil {
|
|
|
|
dref := dec.Varint64()
|
|
|
|
dtime := dec.Varint64()
|
|
|
|
val := dec.Be64()
|
|
|
|
|
|
|
|
lset := make(labels.Labels, dec.Uvarint())
|
|
|
|
for i := range lset {
|
|
|
|
lset[i].Name = dec.UvarintStr()
|
|
|
|
lset[i].Value = dec.UvarintStr()
|
|
|
|
}
|
|
|
|
sort.Sort(lset)
|
|
|
|
|
|
|
|
exemplars = append(exemplars, RefExemplar{
|
|
|
|
Ref: baseRef + uint64(dref),
|
|
|
|
T: baseTime + dtime,
|
|
|
|
V: math.Float64frombits(val),
|
|
|
|
Labels: lset,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if dec.Err() != nil {
|
|
|
|
return nil, errors.Wrapf(dec.Err(), "decode error after %d exemplars", len(exemplars))
|
|
|
|
}
|
|
|
|
if len(dec.B) > 0 {
|
|
|
|
return nil, errors.Errorf("unexpected %d bytes left in entry", len(dec.B))
|
|
|
|
}
|
|
|
|
return exemplars, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
// Encoder encodes series, sample, and tombstones records.
|
2018-05-17 06:02:47 -07:00
|
|
|
// The zero value is ready to use.
|
2019-09-19 02:15:41 -07:00
|
|
|
type Encoder struct {
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Series appends the encoded series to b and returns the resulting slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (e *Encoder) Series(series []RefSeries, b []byte) []byte {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf := encoding.Encbuf{B: b}
|
2019-09-19 02:15:41 -07:00
|
|
|
buf.PutByte(byte(Series))
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
for _, s := range series {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutBE64(s.Ref)
|
|
|
|
buf.PutUvarint(len(s.Labels))
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
for _, l := range s.Labels {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutUvarintStr(l.Name)
|
|
|
|
buf.PutUvarintStr(l.Value)
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
return buf.Get()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Samples appends the encoded samples to b and returns the resulting slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (e *Encoder) Samples(samples []RefSample, b []byte) []byte {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf := encoding.Encbuf{B: b}
|
2019-09-19 02:15:41 -07:00
|
|
|
buf.PutByte(byte(Samples))
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
if len(samples) == 0 {
|
2019-02-22 09:11:11 -08:00
|
|
|
return buf.Get()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store base timestamp and base reference number of first sample.
|
|
|
|
// All samples encode their timestamp and ref as delta to those.
|
|
|
|
first := samples[0]
|
|
|
|
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutBE64(first.Ref)
|
|
|
|
buf.PutBE64int64(first.T)
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
for _, s := range samples {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutVarint64(int64(s.Ref) - int64(first.Ref))
|
|
|
|
buf.PutVarint64(s.T - first.T)
|
|
|
|
buf.PutBE64(math.Float64bits(s.V))
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
return buf.Get()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tombstones appends the encoded tombstones to b and returns the resulting slice.
|
2019-09-19 02:15:41 -07:00
|
|
|
func (e *Encoder) Tombstones(tstones []tombstones.Stone, b []byte) []byte {
|
2019-02-22 09:11:11 -08:00
|
|
|
buf := encoding.Encbuf{B: b}
|
2019-09-19 02:15:41 -07:00
|
|
|
buf.PutByte(byte(Tombstones))
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
for _, s := range tstones {
|
2019-09-19 02:15:41 -07:00
|
|
|
for _, iv := range s.Intervals {
|
|
|
|
buf.PutBE64(s.Ref)
|
2019-02-22 09:11:11 -08:00
|
|
|
buf.PutVarint64(iv.Mint)
|
|
|
|
buf.PutVarint64(iv.Maxt)
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 09:11:11 -08:00
|
|
|
return buf.Get()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2021-05-06 13:53:52 -07:00
|
|
|
|
|
|
|
func (e *Encoder) Exemplars(exemplars []RefExemplar, b []byte) []byte {
|
|
|
|
buf := encoding.Encbuf{B: b}
|
|
|
|
buf.PutByte(byte(Exemplars))
|
|
|
|
|
|
|
|
if len(exemplars) == 0 {
|
|
|
|
return buf.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store base timestamp and base reference number of first sample.
|
|
|
|
// All samples encode their timestamp and ref as delta to those.
|
|
|
|
first := exemplars[0]
|
|
|
|
|
|
|
|
buf.PutBE64(first.Ref)
|
|
|
|
buf.PutBE64int64(first.T)
|
|
|
|
|
|
|
|
for _, ex := range exemplars {
|
|
|
|
buf.PutVarint64(int64(ex.Ref) - int64(first.Ref))
|
|
|
|
buf.PutVarint64(ex.T - first.T)
|
|
|
|
buf.PutBE64(math.Float64bits(ex.V))
|
|
|
|
|
|
|
|
buf.PutUvarint(len(ex.Labels))
|
|
|
|
for _, l := range ex.Labels {
|
|
|
|
buf.PutUvarintStr(l.Name)
|
|
|
|
buf.PutUvarintStr(l.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Get()
|
|
|
|
}
|