2017-04-19 05:43:09 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2016-12-24 16:40:28 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-10-04 12:04:15 -07:00
|
|
|
"context"
|
2017-09-18 03:32:17 -07:00
|
|
|
"sync"
|
2017-02-01 06:59:37 -08:00
|
|
|
"time"
|
2016-12-24 16:40:28 -08:00
|
|
|
|
2019-01-03 06:13:21 -08:00
|
|
|
"github.com/alecthomas/units"
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
2017-05-22 02:53:08 -07:00
|
|
|
"github.com/pkg/errors"
|
2017-02-28 00:33:14 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-06-16 06:07:34 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-12-24 16:40:28 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2016-12-25 02:12:57 -08:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
2016-12-24 16:40:28 -08:00
|
|
|
)
|
|
|
|
|
2017-09-18 03:32:17 -07:00
|
|
|
// ErrNotReady is returned if the underlying storage is not ready yet.
|
|
|
|
var ErrNotReady = errors.New("TSDB not ready")
|
|
|
|
|
|
|
|
// ReadyStorage implements the Storage interface while allowing to set the actual
|
|
|
|
// storage at a later point in time.
|
|
|
|
type ReadyStorage struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
a *adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the storage.
|
2017-10-18 04:08:14 -07:00
|
|
|
func (s *ReadyStorage) Set(db *tsdb.DB, startTimeMargin int64) {
|
2017-09-18 03:32:17 -07:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
s.a = &adapter{db: db, startTimeMargin: startTimeMargin}
|
2017-09-18 03:32:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the storage.
|
|
|
|
func (s *ReadyStorage) Get() *tsdb.DB {
|
|
|
|
if x := s.get(); x != nil {
|
|
|
|
return x.db
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ReadyStorage) get() *adapter {
|
|
|
|
s.mtx.RLock()
|
|
|
|
x := s.a
|
|
|
|
s.mtx.RUnlock()
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
// StartTime implements the Storage interface.
|
|
|
|
func (s *ReadyStorage) StartTime() (int64, error) {
|
|
|
|
if x := s.get(); x != nil {
|
|
|
|
return x.StartTime()
|
|
|
|
}
|
|
|
|
return int64(model.Latest), ErrNotReady
|
|
|
|
}
|
|
|
|
|
2017-09-18 03:32:17 -07:00
|
|
|
// Querier implements the Storage interface.
|
2017-10-04 12:04:15 -07:00
|
|
|
func (s *ReadyStorage) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
|
2017-09-18 03:32:17 -07:00
|
|
|
if x := s.get(); x != nil {
|
2017-10-04 12:04:15 -07:00
|
|
|
return x.Querier(ctx, mint, maxt)
|
2017-09-18 03:32:17 -07:00
|
|
|
}
|
|
|
|
return nil, ErrNotReady
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appender implements the Storage interface.
|
|
|
|
func (s *ReadyStorage) Appender() (storage.Appender, error) {
|
|
|
|
if x := s.get(); x != nil {
|
|
|
|
return x.Appender()
|
|
|
|
}
|
|
|
|
return nil, ErrNotReady
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the Storage interface.
|
|
|
|
func (s *ReadyStorage) Close() error {
|
|
|
|
if x := s.Get(); x != nil {
|
|
|
|
return x.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
// Adapter return an adapter as storage.Storage.
|
|
|
|
func Adapter(db *tsdb.DB, startTimeMargin int64) storage.Storage {
|
|
|
|
return &adapter{db: db, startTimeMargin: startTimeMargin}
|
2017-07-06 05:38:40 -07:00
|
|
|
}
|
|
|
|
|
2016-12-25 02:34:22 -08:00
|
|
|
// adapter implements a storage.Storage around TSDB.
|
|
|
|
type adapter struct {
|
2017-10-18 04:08:14 -07:00
|
|
|
db *tsdb.DB
|
|
|
|
startTimeMargin int64
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2017-02-01 06:59:37 -08:00
|
|
|
// Options of the DB storage.
|
|
|
|
type Options struct {
|
|
|
|
// The timestamp range of head blocks after which they get persisted.
|
|
|
|
// It's the minimum duration of any persisted block.
|
2017-06-16 06:07:34 -07:00
|
|
|
MinBlockDuration model.Duration
|
2017-02-01 06:59:37 -08:00
|
|
|
|
|
|
|
// The maximum timestamp range of compacted blocks.
|
2017-06-16 06:07:34 -07:00
|
|
|
MaxBlockDuration model.Duration
|
2017-02-01 06:59:37 -08:00
|
|
|
|
2019-01-03 06:13:21 -08:00
|
|
|
// The maximum size of each WAL segment file.
|
|
|
|
WALSegmentSize units.Base2Bytes
|
|
|
|
|
2017-03-17 08:06:04 -07:00
|
|
|
// Duration for how long to retain data.
|
2019-01-18 05:48:36 -08:00
|
|
|
RetentionDuration model.Duration
|
|
|
|
|
|
|
|
// Maximum number of bytes to be retained.
|
|
|
|
MaxBytes units.Base2Bytes
|
2017-05-09 03:56:51 -07:00
|
|
|
|
|
|
|
// Disable creation and consideration of lockfile.
|
|
|
|
NoLockfile bool
|
2019-03-04 11:42:45 -08:00
|
|
|
|
|
|
|
// When true it disables the overlapping blocks check.
|
|
|
|
// This in-turn enables vertical compaction and vertical query merge.
|
|
|
|
AllowOverlappingBlocks bool
|
2019-07-03 06:23:13 -07:00
|
|
|
|
|
|
|
// When true records in the WAL will be compressed.
|
|
|
|
WALCompression bool
|
2017-02-01 06:59:37 -08:00
|
|
|
}
|
|
|
|
|
2018-11-21 02:57:32 -08:00
|
|
|
var (
|
|
|
|
startTime prometheus.GaugeFunc
|
|
|
|
headMaxTime prometheus.GaugeFunc
|
|
|
|
headMinTime prometheus.GaugeFunc
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerMetrics(db *tsdb.DB, r prometheus.Registerer) {
|
|
|
|
|
|
|
|
startTime = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_tsdb_lowest_timestamp_seconds",
|
|
|
|
Help: "Lowest timestamp value stored in the database.",
|
|
|
|
}, func() float64 {
|
|
|
|
bb := db.Blocks()
|
|
|
|
if len(bb) == 0 {
|
|
|
|
return float64(db.Head().MinTime()) / 1000
|
|
|
|
}
|
|
|
|
return float64(db.Blocks()[0].Meta().MinTime) / 1000
|
|
|
|
})
|
|
|
|
headMinTime = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_tsdb_head_min_time_seconds",
|
|
|
|
Help: "Minimum time bound of the head block.",
|
|
|
|
}, func() float64 {
|
|
|
|
return float64(db.Head().MinTime()) / 1000
|
|
|
|
})
|
|
|
|
headMaxTime = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
Name: "prometheus_tsdb_head_max_time_seconds",
|
|
|
|
Help: "Maximum timestamp of the head block.",
|
|
|
|
}, func() float64 {
|
|
|
|
return float64(db.Head().MaxTime()) / 1000
|
|
|
|
})
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
r.MustRegister(
|
|
|
|
startTime,
|
|
|
|
headMaxTime,
|
|
|
|
headMinTime,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
// Open returns a new storage backed by a TSDB database that is configured for Prometheus.
|
2017-08-11 11:45:52 -07:00
|
|
|
func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*tsdb.DB, error) {
|
2017-10-25 17:24:49 -07:00
|
|
|
if opts.MinBlockDuration > opts.MaxBlockDuration {
|
2017-10-30 04:09:56 -07:00
|
|
|
opts.MaxBlockDuration = opts.MinBlockDuration
|
2017-10-25 17:24:49 -07:00
|
|
|
}
|
2017-08-10 05:51:02 -07:00
|
|
|
// Start with smallest block duration and create exponential buckets until the exceed the
|
|
|
|
// configured maximum block duration.
|
2017-08-24 03:36:07 -07:00
|
|
|
rngs := tsdb.ExponentialBlockRanges(int64(time.Duration(opts.MinBlockDuration).Seconds()*1000), 10, 3)
|
2017-08-10 05:51:02 -07:00
|
|
|
|
|
|
|
for i, v := range rngs {
|
|
|
|
if v > int64(time.Duration(opts.MaxBlockDuration).Seconds()*1000) {
|
|
|
|
rngs = rngs[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
db, err := tsdb.Open(path, l, r, &tsdb.Options{
|
2019-03-04 11:42:45 -08:00
|
|
|
WALSegmentSize: int(opts.WALSegmentSize),
|
|
|
|
RetentionDuration: uint64(time.Duration(opts.RetentionDuration).Seconds() * 1000),
|
|
|
|
MaxBytes: int64(opts.MaxBytes),
|
|
|
|
BlockRanges: rngs,
|
|
|
|
NoLockfile: opts.NoLockfile,
|
|
|
|
AllowOverlappingBlocks: opts.AllowOverlappingBlocks,
|
2019-07-03 06:23:13 -07:00
|
|
|
WALCompression: opts.WALCompression,
|
2017-02-01 06:59:37 -08:00
|
|
|
})
|
2016-12-24 16:40:28 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-21 02:57:32 -08:00
|
|
|
registerMetrics(db, r)
|
|
|
|
|
2017-07-06 05:38:40 -07:00
|
|
|
return db, nil
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2017-10-18 04:08:14 -07:00
|
|
|
// StartTime implements the Storage interface.
|
|
|
|
func (a adapter) StartTime() (int64, error) {
|
2017-10-24 07:30:39 -07:00
|
|
|
var startTime int64
|
2017-10-18 04:08:14 -07:00
|
|
|
|
|
|
|
if len(a.db.Blocks()) > 0 {
|
2017-10-24 07:30:39 -07:00
|
|
|
startTime = a.db.Blocks()[0].Meta().MinTime
|
2017-10-18 04:08:14 -07:00
|
|
|
} else {
|
2018-03-21 02:25:22 -07:00
|
|
|
startTime = time.Now().Unix() * 1000
|
2017-10-18 04:08:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a safety margin as it may take a few minutes for everything to spin up.
|
|
|
|
return startTime + a.startTimeMargin, nil
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:04:15 -07:00
|
|
|
func (a adapter) Querier(_ context.Context, mint, maxt int64) (storage.Querier, error) {
|
2017-10-16 06:27:09 -07:00
|
|
|
q, err := a.db.Querier(mint, maxt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return querier{q: q}, nil
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appender returns a new appender against the storage.
|
2016-12-25 02:34:22 -08:00
|
|
|
func (a adapter) Appender() (storage.Appender, error) {
|
|
|
|
return appender{a: a.db.Appender()}, nil
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the storage and all its underlying resources.
|
2016-12-25 02:34:22 -08:00
|
|
|
func (a adapter) Close() error {
|
|
|
|
return a.db.Close()
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type querier struct {
|
|
|
|
q tsdb.Querier
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
func (q querier) Select(_ *storage.SelectParams, ms ...*labels.Matcher) (storage.SeriesSet, storage.Warnings, error) {
|
2017-11-23 04:50:06 -08:00
|
|
|
set, err := q.q.Select(ms...)
|
|
|
|
if err != nil {
|
2019-01-02 03:10:13 -08:00
|
|
|
return nil, nil, err
|
2017-11-23 04:50:06 -08:00
|
|
|
}
|
2018-11-30 06:27:12 -08:00
|
|
|
return seriesSet{set: set}, nil, nil
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2019-06-17 00:31:17 -07:00
|
|
|
func (q querier) LabelValues(name string) ([]string, storage.Warnings, error) {
|
|
|
|
v, err := q.q.LabelValues(name)
|
|
|
|
return v, nil, err
|
|
|
|
}
|
|
|
|
func (q querier) LabelNames() ([]string, storage.Warnings, error) {
|
|
|
|
v, err := q.q.LabelNames()
|
|
|
|
return v, nil, err
|
|
|
|
}
|
|
|
|
func (q querier) Close() error { return q.q.Close() }
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
type seriesSet struct {
|
|
|
|
set tsdb.SeriesSet
|
|
|
|
}
|
|
|
|
|
2017-01-02 04:33:37 -08:00
|
|
|
func (s seriesSet) Next() bool { return s.set.Next() }
|
|
|
|
func (s seriesSet) Err() error { return s.set.Err() }
|
|
|
|
func (s seriesSet) At() storage.Series { return series{s: s.set.At()} }
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
type series struct {
|
|
|
|
s tsdb.Series
|
|
|
|
}
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
func (s series) Labels() labels.Labels { return s.s.Labels() }
|
|
|
|
func (s series) Iterator() storage.SeriesIterator { return s.s.Iterator() }
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
type appender struct {
|
|
|
|
a tsdb.Appender
|
|
|
|
}
|
|
|
|
|
2017-09-07 05:14:41 -07:00
|
|
|
func (a appender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
|
2019-11-18 11:53:33 -08:00
|
|
|
ref, err := a.a.Add(lset, t, v)
|
2017-02-01 06:59:37 -08:00
|
|
|
|
2017-05-22 02:53:08 -07:00
|
|
|
switch errors.Cause(err) {
|
2017-02-01 06:59:37 -08:00
|
|
|
case tsdb.ErrNotFound:
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrNotFound
|
2017-02-01 06:59:37 -08:00
|
|
|
case tsdb.ErrOutOfOrderSample:
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrOutOfOrderSample
|
2017-02-01 06:59:37 -08:00
|
|
|
case tsdb.ErrAmendSample:
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrDuplicateSampleForTimestamp
|
2017-07-06 05:18:31 -07:00
|
|
|
case tsdb.ErrOutOfBounds:
|
2017-09-07 05:14:41 -07:00
|
|
|
return 0, storage.ErrOutOfBounds
|
2017-02-01 06:59:37 -08:00
|
|
|
}
|
|
|
|
return ref, err
|
2016-12-28 00:16:48 -08:00
|
|
|
}
|
2017-01-02 04:33:37 -08:00
|
|
|
|
2017-09-07 05:14:41 -07:00
|
|
|
func (a appender) AddFast(_ labels.Labels, ref uint64, t int64, v float64) error {
|
2017-02-01 06:59:37 -08:00
|
|
|
err := a.a.AddFast(ref, t, v)
|
2017-01-15 08:33:07 -08:00
|
|
|
|
2017-05-22 02:53:08 -07:00
|
|
|
switch errors.Cause(err) {
|
2017-01-15 08:33:07 -08:00
|
|
|
case tsdb.ErrNotFound:
|
|
|
|
return storage.ErrNotFound
|
|
|
|
case tsdb.ErrOutOfOrderSample:
|
|
|
|
return storage.ErrOutOfOrderSample
|
|
|
|
case tsdb.ErrAmendSample:
|
|
|
|
return storage.ErrDuplicateSampleForTimestamp
|
2017-07-06 05:18:31 -07:00
|
|
|
case tsdb.ErrOutOfBounds:
|
|
|
|
return storage.ErrOutOfBounds
|
2017-01-15 08:33:07 -08:00
|
|
|
}
|
|
|
|
return err
|
2017-01-13 05:48:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a appender) Commit() error { return a.a.Commit() }
|
|
|
|
func (a appender) Rollback() error { return a.a.Rollback() }
|