2016-12-24 16:40:28 -08:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-10-04 12:04:15 -07:00
|
|
|
"context"
|
2016-12-24 16:40:28 -08:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
|
|
|
)
|
|
|
|
|
2017-07-04 02:24:13 -07:00
|
|
|
// The errors exposed.
|
2016-12-24 16:40:28 -08:00
|
|
|
var (
|
2017-01-15 08:33:07 -08:00
|
|
|
ErrNotFound = errors.New("not found")
|
2016-12-24 16:40:28 -08:00
|
|
|
ErrOutOfOrderSample = errors.New("out of order sample")
|
|
|
|
ErrDuplicateSampleForTimestamp = errors.New("duplicate sample for timestamp")
|
2017-07-04 02:24:13 -07:00
|
|
|
ErrOutOfBounds = errors.New("out of bounds")
|
2016-12-24 16:40:28 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage ingests and manages samples, along with various indexes. All methods
|
|
|
|
// are goroutine-safe. Storage implements storage.SampleAppender.
|
|
|
|
type Storage interface {
|
2017-10-18 04:08:14 -07:00
|
|
|
// StartTime returns the oldest timestamp stored in the storage.
|
|
|
|
StartTime() (int64, error)
|
|
|
|
|
2016-12-24 16:40:28 -08:00
|
|
|
// Querier returns a new Querier on the storage.
|
2017-10-04 12:04:15 -07:00
|
|
|
Querier(ctx context.Context, mint, maxt int64) (Querier, error)
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
// Appender returns a new appender against the storage.
|
|
|
|
Appender() (Appender, error)
|
|
|
|
|
|
|
|
// Close closes the storage and all its underlying resources.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Querier provides reading access to time series data.
|
|
|
|
type Querier interface {
|
|
|
|
// Select returns a set of series that matches the given label matchers.
|
2016-12-25 02:34:22 -08:00
|
|
|
Select(...*labels.Matcher) SeriesSet
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
// LabelValues returns all potential values for a label name.
|
|
|
|
LabelValues(name string) ([]string, error)
|
|
|
|
|
|
|
|
// Close releases the resources of the Querier.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appender provides batched appends against a storage.
|
|
|
|
type Appender interface {
|
2017-09-07 05:14:41 -07:00
|
|
|
Add(l labels.Labels, t int64, v float64) (uint64, error)
|
2017-01-13 05:48:01 -08:00
|
|
|
|
2017-09-07 05:14:41 -07:00
|
|
|
AddFast(l labels.Labels, ref uint64, t int64, v float64) error
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
// Commit submits the collected samples and purges the batch.
|
|
|
|
Commit() error
|
2017-01-13 05:48:01 -08:00
|
|
|
|
|
|
|
Rollback() error
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeriesSet contains a set of series.
|
|
|
|
type SeriesSet interface {
|
|
|
|
Next() bool
|
2017-01-02 04:33:37 -08:00
|
|
|
At() Series
|
2016-12-24 16:40:28 -08:00
|
|
|
Err() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Series represents a single time series.
|
|
|
|
type Series interface {
|
|
|
|
// Labels returns the complete set of labels identifying the series.
|
|
|
|
Labels() labels.Labels
|
|
|
|
|
|
|
|
// Iterator returns a new iterator of the data of the series.
|
|
|
|
Iterator() SeriesIterator
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeriesIterator iterates over the data of a time series.
|
|
|
|
type SeriesIterator interface {
|
|
|
|
// Seek advances the iterator forward to the value at or after
|
|
|
|
// the given timestamp.
|
|
|
|
Seek(t int64) bool
|
2017-01-02 04:33:37 -08:00
|
|
|
// At returns the current timestamp/value pair.
|
|
|
|
At() (t int64, v float64)
|
2016-12-24 16:40:28 -08:00
|
|
|
// Next advances the iterator by one.
|
|
|
|
Next() bool
|
|
|
|
// Err returns the current error.
|
|
|
|
Err() error
|
|
|
|
}
|
2017-04-04 02:07:21 -07:00
|
|
|
|
|
|
|
// dedupedSeriesSet takes two series sets and returns them deduplicated.
|
|
|
|
// The input sets must be sorted and identical if two series exist in both, i.e.
|
|
|
|
// if their label sets are equal, the datapoints must be equal as well.
|
|
|
|
type dedupedSeriesSet struct {
|
|
|
|
a, b SeriesSet
|
|
|
|
|
|
|
|
cur Series
|
|
|
|
adone, bdone bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeduplicateSeriesSet merges two SeriesSet and removes duplicates.
|
|
|
|
// If two series exist in both sets, their datapoints must be equal.
|
|
|
|
func DeduplicateSeriesSet(a, b SeriesSet) SeriesSet {
|
2017-04-04 02:13:46 -07:00
|
|
|
if a == nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
if b == nil {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-04-04 02:07:21 -07:00
|
|
|
s := &dedupedSeriesSet{a: a, b: b}
|
|
|
|
s.adone = !s.a.Next()
|
|
|
|
s.bdone = !s.b.Next()
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dedupedSeriesSet) At() Series {
|
|
|
|
return s.cur
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dedupedSeriesSet) Err() error {
|
|
|
|
if s.a.Err() != nil {
|
|
|
|
return s.a.Err()
|
|
|
|
}
|
|
|
|
return s.b.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dedupedSeriesSet) compare() int {
|
|
|
|
if s.adone {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if s.bdone {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return labels.Compare(s.a.At().Labels(), s.b.At().Labels())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dedupedSeriesSet) Next() bool {
|
|
|
|
if s.adone && s.bdone || s.Err() != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
d := s.compare()
|
|
|
|
|
|
|
|
// Both sets contain the current series. Chain them into a single one.
|
|
|
|
if d > 0 {
|
|
|
|
s.cur = s.b.At()
|
|
|
|
s.bdone = !s.b.Next()
|
|
|
|
} else if d < 0 {
|
|
|
|
s.cur = s.a.At()
|
|
|
|
s.adone = !s.a.Next()
|
|
|
|
} else {
|
|
|
|
s.cur = s.a.At()
|
|
|
|
s.adone = !s.a.Next()
|
|
|
|
s.bdone = !s.b.Next()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|