2019-05-22 23:16:55 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
2020-07-23 19:42:57 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-07-23 18:21:49 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storagepacelimiter"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
// BlockRef references a Block.
|
2019-05-22 23:16:55 +02:00
|
|
|
//
|
2020-04-27 07:13:41 +02:00
|
|
|
// BlockRef is valid only until the corresponding Search is valid,
|
|
|
|
// i.e. it becomes invalid after Search.MustClose is called.
|
|
|
|
type BlockRef struct {
|
|
|
|
p *part
|
|
|
|
bh blockHeader
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
func (br *BlockRef) reset() {
|
|
|
|
br.p = nil
|
|
|
|
br.bh = blockHeader{}
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
func (br *BlockRef) init(p *part, bh *blockHeader) {
|
|
|
|
br.p = p
|
|
|
|
br.bh = *bh
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:46:10 +01:00
|
|
|
// Init initializes br from pr and data
|
|
|
|
func (br *BlockRef) Init(pr PartRef, data []byte) error {
|
|
|
|
br.p = pr.p
|
|
|
|
tail, err := br.bh.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return fmt.Errorf("unexpected non-empty tail left after unmarshaling blockHeader; len(tail)=%d; tail=%q", len(tail), tail)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals br to dst.
|
|
|
|
func (br *BlockRef) Marshal(dst []byte) []byte {
|
|
|
|
return br.bh.Marshal(dst)
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:40:09 +02:00
|
|
|
// RowsCount returns the number of rows in br.
|
|
|
|
func (br *BlockRef) RowsCount() int {
|
|
|
|
return int(br.bh.RowsCount)
|
|
|
|
}
|
|
|
|
|
2020-11-04 15:46:10 +01:00
|
|
|
// PartRef returns PartRef from br.
|
|
|
|
func (br *BlockRef) PartRef() PartRef {
|
|
|
|
return PartRef{
|
|
|
|
p: br.p,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartRef is Part reference.
|
|
|
|
type PartRef struct {
|
|
|
|
p *part
|
|
|
|
}
|
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
// MustReadBlock reads block from br to dst.
|
2020-09-24 21:44:23 +02:00
|
|
|
//
|
|
|
|
// if fetchData is false, then only block header is read, otherwise all the data is read.
|
|
|
|
func (br *BlockRef) MustReadBlock(dst *Block, fetchData bool) {
|
2020-04-27 07:13:41 +02:00
|
|
|
dst.Reset()
|
|
|
|
dst.bh = br.bh
|
2020-09-24 21:44:23 +02:00
|
|
|
if !fetchData {
|
|
|
|
return
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
dst.timestampsData = bytesutil.Resize(dst.timestampsData[:0], int(br.bh.TimestampsBlockSize))
|
|
|
|
br.p.timestampsFile.MustReadAt(dst.timestampsData, int64(br.bh.TimestampsBlockOffset))
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
dst.valuesData = bytesutil.Resize(dst.valuesData[:0], int(br.bh.ValuesBlockSize))
|
|
|
|
br.p.valuesFile.MustReadAt(dst.valuesData, int64(br.bh.ValuesBlockOffset))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricBlockRef contains reference to time series block for a single metric.
|
|
|
|
type MetricBlockRef struct {
|
|
|
|
// The metric name
|
|
|
|
MetricName []byte
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
// The block reference. Call BlockRef.MustReadBlock in order to obtain the block.
|
|
|
|
BlockRef *BlockRef
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search is a search for time series.
|
|
|
|
type Search struct {
|
2020-04-27 07:13:41 +02:00
|
|
|
// MetricBlockRef is updated with each Search.NextMetricBlock call.
|
|
|
|
MetricBlockRef MetricBlockRef
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2021-03-22 21:41:47 +01:00
|
|
|
idb *indexDB
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
ts tableSearch
|
|
|
|
|
2020-08-10 12:45:44 +02:00
|
|
|
// tr contains time range used in the serach.
|
|
|
|
tr TimeRange
|
|
|
|
|
2020-08-10 12:36:00 +02:00
|
|
|
// tfss contains tag filters used in the search.
|
|
|
|
tfss []*TagFilters
|
|
|
|
|
2020-07-23 19:42:57 +02:00
|
|
|
// deadline in unix timestamp seconds for the current search.
|
|
|
|
deadline uint64
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
err error
|
|
|
|
|
|
|
|
needClosing bool
|
2020-07-23 18:21:49 +02:00
|
|
|
|
|
|
|
loops int
|
2021-03-23 16:56:47 +01:00
|
|
|
|
|
|
|
prevMetricID uint64
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Search) reset() {
|
2020-04-27 07:13:41 +02:00
|
|
|
s.MetricBlockRef.MetricName = s.MetricBlockRef.MetricName[:0]
|
|
|
|
s.MetricBlockRef.BlockRef = nil
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2021-03-22 21:41:47 +01:00
|
|
|
s.idb = nil
|
2019-05-22 23:16:55 +02:00
|
|
|
s.ts.reset()
|
2020-08-10 12:45:44 +02:00
|
|
|
s.tr = TimeRange{}
|
2020-08-10 12:36:00 +02:00
|
|
|
s.tfss = nil
|
2020-07-23 19:42:57 +02:00
|
|
|
s.deadline = 0
|
2019-05-22 23:16:55 +02:00
|
|
|
s.err = nil
|
|
|
|
s.needClosing = false
|
2020-07-23 18:21:49 +02:00
|
|
|
s.loops = 0
|
2021-03-23 16:56:47 +01:00
|
|
|
s.prevMetricID = 0
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes s from the given storage, tfss and tr.
|
|
|
|
//
|
|
|
|
// MustClose must be called when the search is done.
|
2020-08-06 18:17:51 +02:00
|
|
|
//
|
|
|
|
// Init returns the upper bound on the number of found time series.
|
|
|
|
func (s *Search) Init(storage *Storage, tfss []*TagFilters, tr TimeRange, maxMetrics int, deadline uint64) int {
|
2019-05-22 23:16:55 +02:00
|
|
|
if s.needClosing {
|
|
|
|
logger.Panicf("BUG: missing MustClose call before the next call to Init")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.reset()
|
2020-08-10 12:45:44 +02:00
|
|
|
s.tr = tr
|
2020-08-10 12:36:00 +02:00
|
|
|
s.tfss = tfss
|
2020-07-23 19:42:57 +02:00
|
|
|
s.deadline = deadline
|
2019-05-22 23:16:55 +02:00
|
|
|
s.needClosing = true
|
|
|
|
|
2020-07-23 19:42:57 +02:00
|
|
|
tsids, err := storage.searchTSIDs(tfss, tr, maxMetrics, deadline)
|
2020-01-30 00:59:43 +01:00
|
|
|
if err == nil {
|
2020-07-23 19:42:57 +02:00
|
|
|
err = storage.prefetchMetricNames(tsids, deadline)
|
2020-01-30 00:59:43 +01:00
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
// It is ok to call Init on error from storage.searchTSIDs.
|
|
|
|
// Init must be called before returning because it will fail
|
|
|
|
// on Seach.MustClose otherwise.
|
2020-04-27 07:13:41 +02:00
|
|
|
s.ts.Init(storage.tb, tsids, tr)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.err = err
|
2020-08-06 18:17:51 +02:00
|
|
|
return 0
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-03-22 21:41:47 +01:00
|
|
|
s.idb = storage.idb()
|
2020-08-06 18:17:51 +02:00
|
|
|
return len(tsids)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MustClose closes the Search.
|
|
|
|
func (s *Search) MustClose() {
|
|
|
|
if !s.needClosing {
|
|
|
|
logger.Panicf("BUG: missing Init call before MustClose")
|
|
|
|
}
|
|
|
|
s.ts.MustClose()
|
|
|
|
s.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the last error from s.
|
|
|
|
func (s *Search) Error() error {
|
2020-08-10 12:36:00 +02:00
|
|
|
if s.err == io.EOF || s.err == nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-10 12:45:44 +02:00
|
|
|
return fmt.Errorf("error when searching for tagFilters=%s on the time range %s: %w", s.tfss, s.tr.String(), s.err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 07:13:41 +02:00
|
|
|
// NextMetricBlock proceeds to the next MetricBlockRef.
|
2019-05-22 23:16:55 +02:00
|
|
|
func (s *Search) NextMetricBlock() bool {
|
|
|
|
if s.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for s.ts.NextBlock() {
|
2020-08-07 07:37:33 +02:00
|
|
|
if s.loops&paceLimiterSlowIterationsMask == 0 {
|
2020-07-23 19:42:57 +02:00
|
|
|
if err := checkSearchDeadlineAndPace(s.deadline); err != nil {
|
|
|
|
s.err = err
|
|
|
|
return false
|
|
|
|
}
|
2020-07-23 18:21:49 +02:00
|
|
|
}
|
|
|
|
s.loops++
|
2020-04-27 07:13:41 +02:00
|
|
|
tsid := &s.ts.BlockRef.bh.TSID
|
2021-03-23 16:56:47 +01:00
|
|
|
if tsid.MetricID != s.prevMetricID {
|
|
|
|
var err error
|
|
|
|
s.MetricBlockRef.MetricName, err = s.idb.searchMetricNameWithCache(s.MetricBlockRef.MetricName[:0], tsid.MetricID)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
// Skip missing metricName for tsid.MetricID.
|
2021-04-13 09:20:35 +02:00
|
|
|
// It should be automatically fixed. See indexDB.searchMetricNameWithCache for details.
|
2021-03-23 16:56:47 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.err = err
|
|
|
|
return false
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2021-03-23 16:56:47 +01:00
|
|
|
s.prevMetricID = tsid.MetricID
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2020-04-27 07:13:41 +02:00
|
|
|
s.MetricBlockRef.BlockRef = s.ts.BlockRef
|
2019-05-22 23:16:55 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err := s.ts.Error(); err != nil {
|
|
|
|
s.err = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
s.err = io.EOF
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchQuery is used for sending search queries from vmselect to vmstorage.
|
|
|
|
type SearchQuery struct {
|
|
|
|
MinTimestamp int64
|
|
|
|
MaxTimestamp int64
|
|
|
|
TagFilterss [][]TagFilter
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:00:50 +01:00
|
|
|
// NewSearchQuery creates new search query for the given args.
|
|
|
|
func NewSearchQuery(start, end int64, tagFilterss [][]TagFilter) *SearchQuery {
|
|
|
|
return &SearchQuery{
|
|
|
|
MinTimestamp: start,
|
|
|
|
MaxTimestamp: end,
|
|
|
|
TagFilterss: tagFilterss,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// TagFilter represents a single tag filter from SearchQuery.
|
|
|
|
type TagFilter struct {
|
|
|
|
Key []byte
|
|
|
|
Value []byte
|
|
|
|
IsNegative bool
|
|
|
|
IsRegexp bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of tf.
|
|
|
|
func (tf *TagFilter) String() string {
|
|
|
|
var bb bytesutil.ByteBuffer
|
|
|
|
fmt.Fprintf(&bb, "{Key=%q, Value=%q, IsNegative: %v, IsRegexp: %v}", tf.Key, tf.Value, tf.IsNegative, tf.IsRegexp)
|
|
|
|
return string(bb.B)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal appends marshaled tf to dst and returns the result.
|
|
|
|
func (tf *TagFilter) Marshal(dst []byte) []byte {
|
|
|
|
dst = encoding.MarshalBytes(dst, tf.Key)
|
|
|
|
dst = encoding.MarshalBytes(dst, tf.Value)
|
|
|
|
|
|
|
|
x := 0
|
|
|
|
if tf.IsNegative {
|
|
|
|
x = 2
|
|
|
|
}
|
|
|
|
if tf.IsRegexp {
|
|
|
|
x |= 1
|
|
|
|
}
|
|
|
|
dst = append(dst, byte(x))
|
|
|
|
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals tf from src and returns the tail.
|
|
|
|
func (tf *TagFilter) Unmarshal(src []byte) ([]byte, error) {
|
|
|
|
tail, k, err := encoding.UnmarshalBytes(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return tail, fmt.Errorf("cannot unmarshal Key: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
tf.Key = append(tf.Key[:0], k...)
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
tail, v, err := encoding.UnmarshalBytes(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return tail, fmt.Errorf("cannot unmarshal Value: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
tf.Value = append(tf.Value[:0], v...)
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
if len(src) < 1 {
|
|
|
|
return src, fmt.Errorf("cannot unmarshal IsNegative+IsRegexp from empty src")
|
|
|
|
}
|
|
|
|
x := src[0]
|
|
|
|
switch x {
|
|
|
|
case 0:
|
|
|
|
tf.IsNegative = false
|
|
|
|
tf.IsRegexp = false
|
|
|
|
case 1:
|
|
|
|
tf.IsNegative = false
|
|
|
|
tf.IsRegexp = true
|
|
|
|
case 2:
|
|
|
|
tf.IsNegative = true
|
|
|
|
tf.IsRegexp = false
|
|
|
|
case 3:
|
|
|
|
tf.IsNegative = true
|
|
|
|
tf.IsRegexp = true
|
|
|
|
default:
|
|
|
|
return src, fmt.Errorf("unexpected value for IsNegative+IsRegexp: %d; must be in the range [0..3]", x)
|
|
|
|
}
|
|
|
|
src = src[1:]
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of the search query.
|
|
|
|
func (sq *SearchQuery) String() string {
|
|
|
|
var bb bytesutil.ByteBuffer
|
|
|
|
fmt.Fprintf(&bb, "MinTimestamp=%s, MaxTimestamp=%s, TagFilters=[\n",
|
|
|
|
timestampToTime(sq.MinTimestamp), timestampToTime(sq.MaxTimestamp))
|
|
|
|
for _, tagFilters := range sq.TagFilterss {
|
|
|
|
for _, tf := range tagFilters {
|
|
|
|
fmt.Fprintf(&bb, "%s", tf.String())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&bb, "\n")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&bb, "]")
|
|
|
|
return string(bb.B)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal appends marshaled sq to dst and returns the result.
|
|
|
|
func (sq *SearchQuery) Marshal(dst []byte) []byte {
|
|
|
|
dst = encoding.MarshalVarInt64(dst, sq.MinTimestamp)
|
|
|
|
dst = encoding.MarshalVarInt64(dst, sq.MaxTimestamp)
|
|
|
|
dst = encoding.MarshalVarUint64(dst, uint64(len(sq.TagFilterss)))
|
|
|
|
for _, tagFilters := range sq.TagFilterss {
|
|
|
|
dst = encoding.MarshalVarUint64(dst, uint64(len(tagFilters)))
|
|
|
|
for i := range tagFilters {
|
|
|
|
dst = tagFilters[i].Marshal(dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals sq from src and returns the tail.
|
|
|
|
func (sq *SearchQuery) Unmarshal(src []byte) ([]byte, error) {
|
|
|
|
tail, minTs, err := encoding.UnmarshalVarInt64(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return src, fmt.Errorf("cannot unmarshal MinTimestamp: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
sq.MinTimestamp = minTs
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
tail, maxTs, err := encoding.UnmarshalVarInt64(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return src, fmt.Errorf("cannot unmarshal MaxTimestamp: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
sq.MaxTimestamp = maxTs
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
tail, tfssCount, err := encoding.UnmarshalVarUint64(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return src, fmt.Errorf("cannot unmarshal the count of TagFilterss: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
if n := int(tfssCount) - cap(sq.TagFilterss); n > 0 {
|
|
|
|
sq.TagFilterss = append(sq.TagFilterss[:cap(sq.TagFilterss)], make([][]TagFilter, n)...)
|
|
|
|
}
|
|
|
|
sq.TagFilterss = sq.TagFilterss[:tfssCount]
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
for i := 0; i < int(tfssCount); i++ {
|
|
|
|
tail, tfsCount, err := encoding.UnmarshalVarUint64(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return src, fmt.Errorf("cannot unmarshal the count of TagFilters: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
src = tail
|
|
|
|
|
|
|
|
tagFilters := sq.TagFilterss[i]
|
|
|
|
if n := int(tfsCount) - cap(tagFilters); n > 0 {
|
|
|
|
tagFilters = append(tagFilters[:cap(tagFilters)], make([]TagFilter, n)...)
|
|
|
|
}
|
|
|
|
tagFilters = tagFilters[:tfsCount]
|
|
|
|
for j := 0; j < int(tfsCount); j++ {
|
|
|
|
tail, err := tagFilters[j].Unmarshal(src)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return tail, fmt.Errorf("cannot unmarshal TagFilter #%d: %w", j, err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
src = tail
|
|
|
|
}
|
|
|
|
sq.TagFilterss[i] = tagFilters
|
|
|
|
}
|
|
|
|
|
|
|
|
return src, nil
|
|
|
|
}
|
2020-07-23 19:42:57 +02:00
|
|
|
|
|
|
|
func checkSearchDeadlineAndPace(deadline uint64) error {
|
|
|
|
if fasttime.UnixTimestamp() > deadline {
|
2020-08-10 12:17:12 +02:00
|
|
|
return ErrDeadlineExceeded
|
2020-07-23 19:42:57 +02:00
|
|
|
}
|
|
|
|
storagepacelimiter.Search.WaitIfNeeded()
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-07 07:37:33 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
paceLimiterFastIterationsMask = 1<<16 - 1
|
|
|
|
paceLimiterMediumIterationsMask = 1<<14 - 1
|
|
|
|
paceLimiterSlowIterationsMask = 1<<12 - 1
|
|
|
|
)
|