2017-04-19 12:43:09 +00: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-25 00:40:28 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-02-01 14:59:37 +00:00
|
|
|
"time"
|
2016-12-25 00:40:28 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2017-05-22 09:53:08 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-02-28 08:33:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-06-16 13:07:34 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2016-12-25 00:40:28 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2016-12-25 10:12:57 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2017-04-04 09:33:51 +00:00
|
|
|
"github.com/prometheus/tsdb"
|
|
|
|
tsdbLabels "github.com/prometheus/tsdb/labels"
|
2016-12-25 00:40:28 +00:00
|
|
|
)
|
|
|
|
|
2017-07-06 12:38:40 +00:00
|
|
|
func Adapter(db *tsdb.DB) storage.Storage {
|
|
|
|
return &adapter{db: db}
|
|
|
|
}
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
// adapter implements a storage.Storage around TSDB.
|
|
|
|
type adapter struct {
|
2017-03-07 10:51:30 +00:00
|
|
|
db *tsdb.DB
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 14:59:37 +00:00
|
|
|
// Options of the DB storage.
|
|
|
|
type Options struct {
|
|
|
|
// The interval at which the write ahead log is flushed to disc.
|
|
|
|
WALFlushInterval time.Duration
|
|
|
|
|
|
|
|
// The timestamp range of head blocks after which they get persisted.
|
|
|
|
// It's the minimum duration of any persisted block.
|
2017-06-16 13:07:34 +00:00
|
|
|
MinBlockDuration model.Duration
|
2017-02-01 14:59:37 +00:00
|
|
|
|
|
|
|
// The maximum timestamp range of compacted blocks.
|
2017-06-16 13:07:34 +00:00
|
|
|
MaxBlockDuration model.Duration
|
2017-02-01 14:59:37 +00:00
|
|
|
|
2017-03-17 15:06:04 +00:00
|
|
|
// Duration for how long to retain data.
|
2017-06-16 13:07:34 +00:00
|
|
|
Retention model.Duration
|
2017-05-09 10:56:51 +00:00
|
|
|
|
|
|
|
// Disable creation and consideration of lockfile.
|
|
|
|
NoLockfile bool
|
2017-02-01 14:59:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 12:38:40 +00:00
|
|
|
// Open returns a new storage backed by a TSDB database that is configured for Prometheus.
|
2017-08-11 18:45:52 +00:00
|
|
|
func Open(path string, l log.Logger, r prometheus.Registerer, opts *Options) (*tsdb.DB, error) {
|
2017-08-10 12:51:02 +00:00
|
|
|
// Start with smallest block duration and create exponential buckets until the exceed the
|
|
|
|
// configured maximum block duration.
|
2017-08-24 10:36:07 +00:00
|
|
|
rngs := tsdb.ExponentialBlockRanges(int64(time.Duration(opts.MinBlockDuration).Seconds()*1000), 10, 3)
|
2017-08-10 12:51:02 +00:00
|
|
|
|
|
|
|
for i, v := range rngs {
|
|
|
|
if v > int64(time.Duration(opts.MaxBlockDuration).Seconds()*1000) {
|
|
|
|
rngs = rngs[:i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
db, err := tsdb.Open(path, l, r, &tsdb.Options{
|
2017-03-17 15:06:04 +00:00
|
|
|
WALFlushInterval: 10 * time.Second,
|
2017-06-16 13:07:34 +00:00
|
|
|
RetentionDuration: uint64(time.Duration(opts.Retention).Seconds() * 1000),
|
2017-08-10 12:51:02 +00:00
|
|
|
BlockRanges: rngs,
|
2017-05-09 10:56:51 +00:00
|
|
|
NoLockfile: opts.NoLockfile,
|
2017-02-01 14:59:37 +00:00
|
|
|
})
|
2016-12-25 00:40:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-06 12:38:40 +00:00
|
|
|
return db, nil
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
func (a adapter) Querier(mint, maxt int64) (storage.Querier, error) {
|
|
|
|
return querier{q: a.db.Querier(mint, maxt)}, nil
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appender returns a new appender against the storage.
|
2016-12-25 10:34:22 +00:00
|
|
|
func (a adapter) Appender() (storage.Appender, error) {
|
|
|
|
return appender{a: a.db.Appender()}, nil
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the storage and all its underlying resources.
|
2016-12-25 10:34:22 +00:00
|
|
|
func (a adapter) Close() error {
|
|
|
|
return a.db.Close()
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type querier struct {
|
|
|
|
q tsdb.Querier
|
|
|
|
}
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
func (q querier) Select(oms ...*labels.Matcher) storage.SeriesSet {
|
2016-12-25 00:40:28 +00:00
|
|
|
ms := make([]tsdbLabels.Matcher, 0, len(oms))
|
|
|
|
|
|
|
|
for _, om := range oms {
|
|
|
|
ms = append(ms, convertMatcher(om))
|
|
|
|
}
|
|
|
|
|
2016-12-28 08:16:48 +00:00
|
|
|
return seriesSet{set: q.q.Select(ms...)}
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
func (q querier) LabelValues(name string) ([]string, error) { return q.q.LabelValues(name) }
|
|
|
|
func (q querier) Close() error { return q.q.Close() }
|
2016-12-25 00:40:28 +00:00
|
|
|
|
|
|
|
type seriesSet struct {
|
|
|
|
set tsdb.SeriesSet
|
|
|
|
}
|
|
|
|
|
2017-01-02 12:33:37 +00: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-25 00:40:28 +00:00
|
|
|
|
|
|
|
type series struct {
|
|
|
|
s tsdb.Series
|
|
|
|
}
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
func (s series) Labels() labels.Labels { return toLabels(s.s.Labels()) }
|
|
|
|
func (s series) Iterator() storage.SeriesIterator { return storage.SeriesIterator(s.s.Iterator()) }
|
2016-12-25 00:40:28 +00:00
|
|
|
|
|
|
|
type appender struct {
|
|
|
|
a tsdb.Appender
|
|
|
|
}
|
|
|
|
|
2017-09-07 12:14:41 +00:00
|
|
|
func (a appender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
|
2017-02-01 14:59:37 +00:00
|
|
|
ref, err := a.a.Add(toTSDBLabels(lset), t, v)
|
|
|
|
|
2017-05-22 09:53:08 +00:00
|
|
|
switch errors.Cause(err) {
|
2017-02-01 14:59:37 +00:00
|
|
|
case tsdb.ErrNotFound:
|
2017-09-07 12:14:41 +00:00
|
|
|
return 0, storage.ErrNotFound
|
2017-02-01 14:59:37 +00:00
|
|
|
case tsdb.ErrOutOfOrderSample:
|
2017-09-07 12:14:41 +00:00
|
|
|
return 0, storage.ErrOutOfOrderSample
|
2017-02-01 14:59:37 +00:00
|
|
|
case tsdb.ErrAmendSample:
|
2017-09-07 12:14:41 +00:00
|
|
|
return 0, storage.ErrDuplicateSampleForTimestamp
|
2017-07-06 12:18:31 +00:00
|
|
|
case tsdb.ErrOutOfBounds:
|
2017-09-07 12:14:41 +00:00
|
|
|
return 0, storage.ErrOutOfBounds
|
2017-02-01 14:59:37 +00:00
|
|
|
}
|
|
|
|
return ref, err
|
2016-12-28 08:16:48 +00:00
|
|
|
}
|
2017-01-02 12:33:37 +00:00
|
|
|
|
2017-09-07 12:14:41 +00:00
|
|
|
func (a appender) AddFast(_ labels.Labels, ref uint64, t int64, v float64) error {
|
2017-02-01 14:59:37 +00:00
|
|
|
err := a.a.AddFast(ref, t, v)
|
2017-01-15 16:33:07 +00:00
|
|
|
|
2017-05-22 09:53:08 +00:00
|
|
|
switch errors.Cause(err) {
|
2017-01-15 16:33:07 +00:00
|
|
|
case tsdb.ErrNotFound:
|
|
|
|
return storage.ErrNotFound
|
|
|
|
case tsdb.ErrOutOfOrderSample:
|
|
|
|
return storage.ErrOutOfOrderSample
|
|
|
|
case tsdb.ErrAmendSample:
|
|
|
|
return storage.ErrDuplicateSampleForTimestamp
|
2017-07-06 12:18:31 +00:00
|
|
|
case tsdb.ErrOutOfBounds:
|
|
|
|
return storage.ErrOutOfBounds
|
2017-01-15 16:33:07 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-01-13 13:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a appender) Commit() error { return a.a.Commit() }
|
|
|
|
func (a appender) Rollback() error { return a.a.Rollback() }
|
2016-12-25 00:40:28 +00:00
|
|
|
|
2016-12-25 10:12:57 +00:00
|
|
|
func convertMatcher(m *labels.Matcher) tsdbLabels.Matcher {
|
2016-12-25 00:40:28 +00:00
|
|
|
switch m.Type {
|
2016-12-25 10:34:22 +00:00
|
|
|
case labels.MatchEqual:
|
2016-12-25 00:40:28 +00:00
|
|
|
return tsdbLabels.NewEqualMatcher(m.Name, m.Value)
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
case labels.MatchNotEqual:
|
2016-12-25 00:40:28 +00:00
|
|
|
return tsdbLabels.Not(tsdbLabels.NewEqualMatcher(m.Name, m.Value))
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
case labels.MatchRegexp:
|
2016-12-25 00:40:28 +00:00
|
|
|
res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
case labels.MatchNotRegexp:
|
2016-12-25 00:40:28 +00:00
|
|
|
res, err := tsdbLabels.NewRegexpMatcher(m.Name, m.Value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return tsdbLabels.Not(res)
|
|
|
|
}
|
2016-12-25 10:12:57 +00:00
|
|
|
panic("storage.convertMatcher: invalid matcher type")
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func toTSDBLabels(l labels.Labels) tsdbLabels.Labels {
|
|
|
|
return *(*tsdbLabels.Labels)(unsafe.Pointer(&l))
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLabels(l tsdbLabels.Labels) labels.Labels {
|
|
|
|
return *(*labels.Labels)(unsafe.Pointer(&l))
|
|
|
|
}
|