2013-08-03 09:46:02 -07:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// 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.
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
package tiered
|
2013-08-03 09:46:02 -07:00
|
|
|
|
|
|
|
import (
|
2013-08-14 09:52:55 -07:00
|
|
|
"io"
|
2013-08-03 09:46:02 -07:00
|
|
|
"sort"
|
2013-08-14 09:52:55 -07:00
|
|
|
"sync"
|
2013-08-03 09:46:02 -07:00
|
|
|
|
|
|
|
"code.google.com/p/goprotobuf/proto"
|
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
2014-04-11 00:27:05 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-08-03 09:46:02 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/raw"
|
|
|
|
"github.com/prometheus/prometheus/storage/raw/leveldb"
|
2013-08-07 03:07:35 -07:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-08-12 08:18:02 -07:00
|
|
|
|
|
|
|
dto "github.com/prometheus/prometheus/model/generated"
|
2013-08-03 09:46:02 -07:00
|
|
|
)
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// FingerprintMetricMapping is an in-memory map of Fingerprints to Metrics.
|
2013-08-03 09:46:02 -07:00
|
|
|
type FingerprintMetricMapping map[clientmodel.Fingerprint]clientmodel.Metric
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// FingerprintMetricIndex models a database mapping Fingerprints to Metrics.
|
2013-08-03 09:46:02 -07:00
|
|
|
type FingerprintMetricIndex interface {
|
2014-02-14 10:36:27 -08:00
|
|
|
raw.Database
|
2013-08-05 08:31:49 -07:00
|
|
|
raw.Pruner
|
|
|
|
|
2013-08-03 09:46:02 -07:00
|
|
|
IndexBatch(FingerprintMetricMapping) error
|
|
|
|
Lookup(*clientmodel.Fingerprint) (m clientmodel.Metric, ok bool, err error)
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// LevelDBFingerprintMetricIndex implements FingerprintMetricIndex using
|
|
|
|
// leveldb.
|
2013-08-06 03:00:31 -07:00
|
|
|
type LevelDBFingerprintMetricIndex struct {
|
2014-02-14 10:36:27 -08:00
|
|
|
*leveldb.LevelDBPersistence
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexBatch implements FingerprintMetricIndex.
|
2013-08-06 03:00:31 -07:00
|
|
|
func (i *LevelDBFingerprintMetricIndex) IndexBatch(mapping FingerprintMetricMapping) error {
|
2013-08-03 09:46:02 -07:00
|
|
|
b := leveldb.NewBatch()
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
for f, m := range mapping {
|
2014-02-14 10:36:27 -08:00
|
|
|
k := &dto.Fingerprint{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpFingerprint(k, &f)
|
2014-02-14 10:36:27 -08:00
|
|
|
v := &dto.Metric{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpMetric(v, m)
|
|
|
|
|
|
|
|
b.Put(k, v)
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
return i.LevelDBPersistence.Commit(b)
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Lookup implements FingerprintMetricIndex.
|
2013-08-06 03:00:31 -07:00
|
|
|
func (i *LevelDBFingerprintMetricIndex) Lookup(f *clientmodel.Fingerprint) (m clientmodel.Metric, ok bool, err error) {
|
2014-02-14 10:36:27 -08:00
|
|
|
k := &dto.Fingerprint{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpFingerprint(k, f)
|
2014-02-14 10:36:27 -08:00
|
|
|
v := &dto.Metric{}
|
|
|
|
if ok, err := i.LevelDBPersistence.Get(k, v); !ok {
|
2013-08-03 09:46:02 -07:00
|
|
|
return nil, false, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = clientmodel.Metric{}
|
|
|
|
|
|
|
|
for _, pair := range v.LabelPair {
|
|
|
|
m[clientmodel.LabelName(pair.GetName())] = clientmodel.LabelValue(pair.GetValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, true, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewLevelDBFingerprintMetricIndex returns a LevelDBFingerprintMetricIndex
|
|
|
|
// object ready to use.
|
2014-02-27 05:34:46 -08:00
|
|
|
func NewLevelDBFingerprintMetricIndex(o leveldb.LevelDBOptions) (*LevelDBFingerprintMetricIndex, error) {
|
|
|
|
s, err := leveldb.NewLevelDBPersistence(o)
|
2013-08-03 09:46:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-08-06 03:00:31 -07:00
|
|
|
return &LevelDBFingerprintMetricIndex{
|
2014-02-14 10:36:27 -08:00
|
|
|
LevelDBPersistence: s,
|
2013-08-03 09:46:02 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
// LabelNameLabelValuesMapping is an in-memory map of LabelNames to
|
|
|
|
// LabelValues.
|
|
|
|
type LabelNameLabelValuesMapping map[clientmodel.LabelName]clientmodel.LabelValues
|
|
|
|
|
|
|
|
// LabelNameLabelValuesIndex models a database mapping LabelNames to
|
|
|
|
// LabelValues.
|
|
|
|
type LabelNameLabelValuesIndex interface {
|
|
|
|
raw.Database
|
|
|
|
raw.Pruner
|
|
|
|
|
|
|
|
IndexBatch(LabelNameLabelValuesMapping) error
|
|
|
|
Lookup(clientmodel.LabelName) (values clientmodel.LabelValues, ok bool, err error)
|
|
|
|
Has(clientmodel.LabelName) (ok bool, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LevelDBLabelNameLabelValuesIndex implements LabelNameLabelValuesIndex using
|
|
|
|
// leveldb.
|
|
|
|
type LevelDBLabelNameLabelValuesIndex struct {
|
|
|
|
*leveldb.LevelDBPersistence
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexBatch implements LabelNameLabelValuesIndex.
|
|
|
|
func (i *LevelDBLabelNameLabelValuesIndex) IndexBatch(b LabelNameLabelValuesMapping) error {
|
|
|
|
batch := leveldb.NewBatch()
|
|
|
|
defer batch.Close()
|
|
|
|
|
|
|
|
for labelName, labelValues := range b {
|
|
|
|
sort.Sort(labelValues)
|
|
|
|
|
|
|
|
key := &dto.LabelName{
|
|
|
|
Name: proto.String(string(labelName)),
|
|
|
|
}
|
|
|
|
value := &dto.LabelValueCollection{}
|
|
|
|
value.Member = make([]string, 0, len(labelValues))
|
|
|
|
for _, labelValue := range labelValues {
|
|
|
|
value.Member = append(value.Member, string(labelValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Put(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.LevelDBPersistence.Commit(batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup implements LabelNameLabelValuesIndex.
|
|
|
|
func (i *LevelDBLabelNameLabelValuesIndex) Lookup(l clientmodel.LabelName) (values clientmodel.LabelValues, ok bool, err error) {
|
|
|
|
k := &dto.LabelName{}
|
|
|
|
dumpLabelName(k, l)
|
|
|
|
v := &dto.LabelValueCollection{}
|
|
|
|
ok, err = i.LevelDBPersistence.Get(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range v.Member {
|
|
|
|
values = append(values, clientmodel.LabelValue(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
return values, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has implements LabelNameLabelValuesIndex.
|
|
|
|
func (i *LevelDBLabelNameLabelValuesIndex) Has(l clientmodel.LabelName) (ok bool, err error) {
|
|
|
|
return i.LevelDBPersistence.Has(&dto.LabelName{
|
|
|
|
Name: proto.String(string(l)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLevelDBLabelNameLabelValuesIndex returns a LevelDBLabelNameLabelValuesIndex
|
|
|
|
// ready to use.
|
|
|
|
func NewLevelDBLabelNameLabelValuesIndex(o leveldb.LevelDBOptions) (*LevelDBLabelNameLabelValuesIndex, error) {
|
|
|
|
s, err := leveldb.NewLevelDBPersistence(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LevelDBLabelNameLabelValuesIndex{
|
|
|
|
LevelDBPersistence: s,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// LabelPairFingerprintMapping is an in-memory map of LabelPairs to
|
|
|
|
// Fingerprints.
|
2014-04-11 00:27:05 -07:00
|
|
|
type LabelPairFingerprintMapping map[metric.LabelPair]clientmodel.Fingerprints
|
2013-08-03 09:46:02 -07:00
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// LabelPairFingerprintIndex models a database mapping LabelPairs to
|
|
|
|
// Fingerprints.
|
2013-08-07 03:07:35 -07:00
|
|
|
type LabelPairFingerprintIndex interface {
|
2014-02-14 10:36:27 -08:00
|
|
|
raw.Database
|
2013-08-03 09:46:02 -07:00
|
|
|
raw.ForEacher
|
2013-08-05 08:31:49 -07:00
|
|
|
raw.Pruner
|
2013-08-03 09:46:02 -07:00
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
IndexBatch(LabelPairFingerprintMapping) error
|
2014-04-11 00:27:05 -07:00
|
|
|
Lookup(*metric.LabelPair) (m clientmodel.Fingerprints, ok bool, err error)
|
|
|
|
Has(*metric.LabelPair) (ok bool, err error)
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// LevelDBLabelPairFingerprintIndex implements LabelPairFingerprintIndex using
|
|
|
|
// leveldb.
|
2013-08-07 03:07:35 -07:00
|
|
|
type LevelDBLabelPairFingerprintIndex struct {
|
2014-02-14 10:36:27 -08:00
|
|
|
*leveldb.LevelDBPersistence
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexBatch implements LabelPairFingerprintMapping.
|
2013-08-07 03:07:35 -07:00
|
|
|
func (i *LevelDBLabelPairFingerprintIndex) IndexBatch(m LabelPairFingerprintMapping) error {
|
2013-08-03 09:46:02 -07:00
|
|
|
batch := leveldb.NewBatch()
|
|
|
|
defer batch.Close()
|
|
|
|
|
|
|
|
for pair, fps := range m {
|
|
|
|
sort.Sort(fps)
|
|
|
|
|
|
|
|
key := &dto.LabelPair{
|
|
|
|
Name: proto.String(string(pair.Name)),
|
|
|
|
Value: proto.String(string(pair.Value)),
|
|
|
|
}
|
2014-02-14 10:36:27 -08:00
|
|
|
value := &dto.FingerprintCollection{}
|
2013-08-03 09:46:02 -07:00
|
|
|
for _, fp := range fps {
|
2014-02-14 10:36:27 -08:00
|
|
|
f := &dto.Fingerprint{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpFingerprint(f, fp)
|
|
|
|
value.Member = append(value.Member, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Put(key, value)
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
return i.LevelDBPersistence.Commit(batch)
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Lookup implements LabelPairFingerprintMapping.
|
2014-04-11 00:27:05 -07:00
|
|
|
func (i *LevelDBLabelPairFingerprintIndex) Lookup(p *metric.LabelPair) (m clientmodel.Fingerprints, ok bool, err error) {
|
2013-08-03 09:46:02 -07:00
|
|
|
k := &dto.LabelPair{
|
|
|
|
Name: proto.String(string(p.Name)),
|
|
|
|
Value: proto.String(string(p.Value)),
|
|
|
|
}
|
2014-02-14 10:36:27 -08:00
|
|
|
v := &dto.FingerprintCollection{}
|
2013-08-03 09:46:02 -07:00
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
ok, err = i.LevelDBPersistence.Get(k, v)
|
2013-08-03 09:46:02 -07:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pair := range v.Member {
|
2014-02-14 10:36:27 -08:00
|
|
|
fp := &clientmodel.Fingerprint{}
|
2013-08-03 09:46:02 -07:00
|
|
|
loadFingerprint(fp, pair)
|
|
|
|
m = append(m, fp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, true, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Has implements LabelPairFingerprintMapping.
|
2014-04-11 00:27:05 -07:00
|
|
|
func (i *LevelDBLabelPairFingerprintIndex) Has(p *metric.LabelPair) (ok bool, err error) {
|
2013-08-03 09:46:02 -07:00
|
|
|
k := &dto.LabelPair{
|
|
|
|
Name: proto.String(string(p.Name)),
|
|
|
|
Value: proto.String(string(p.Value)),
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
return i.LevelDBPersistence.Has(k)
|
2013-08-05 00:25:47 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewLevelDBLabelSetFingerprintIndex returns a LevelDBLabelPairFingerprintIndex
|
|
|
|
// object ready to use.
|
2014-02-27 05:34:46 -08:00
|
|
|
func NewLevelDBLabelSetFingerprintIndex(o leveldb.LevelDBOptions) (*LevelDBLabelPairFingerprintIndex, error) {
|
|
|
|
s, err := leveldb.NewLevelDBPersistence(o)
|
2013-08-03 09:46:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
return &LevelDBLabelPairFingerprintIndex{
|
2014-02-14 10:36:27 -08:00
|
|
|
LevelDBPersistence: s,
|
2013-08-03 09:46:02 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// MetricMembershipIndex models a database tracking the existence of Metrics.
|
2013-08-03 09:46:02 -07:00
|
|
|
type MetricMembershipIndex interface {
|
2014-02-14 10:36:27 -08:00
|
|
|
raw.Database
|
2013-08-05 08:31:49 -07:00
|
|
|
raw.Pruner
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
IndexBatch(FingerprintMetricMapping) error
|
2013-08-03 09:46:02 -07:00
|
|
|
Has(clientmodel.Metric) (ok bool, err error)
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// LevelDBMetricMembershipIndex implements MetricMembershipIndex using leveldb.
|
2013-08-06 03:00:31 -07:00
|
|
|
type LevelDBMetricMembershipIndex struct {
|
2014-02-14 10:36:27 -08:00
|
|
|
*leveldb.LevelDBPersistence
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
var existenceIdentity = &dto.MembershipIndexValue{}
|
2013-08-03 09:46:02 -07:00
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexBatch implements MetricMembershipIndex.
|
2013-08-07 03:07:35 -07:00
|
|
|
func (i *LevelDBMetricMembershipIndex) IndexBatch(b FingerprintMetricMapping) error {
|
2013-08-03 09:46:02 -07:00
|
|
|
batch := leveldb.NewBatch()
|
|
|
|
defer batch.Close()
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
for _, m := range b {
|
2014-02-14 10:36:27 -08:00
|
|
|
k := &dto.Metric{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpMetric(k, m)
|
|
|
|
batch.Put(k, existenceIdentity)
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
return i.LevelDBPersistence.Commit(batch)
|
2013-08-03 09:46:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Has implements MetricMembershipIndex.
|
2013-08-06 03:00:31 -07:00
|
|
|
func (i *LevelDBMetricMembershipIndex) Has(m clientmodel.Metric) (ok bool, err error) {
|
2014-02-14 10:36:27 -08:00
|
|
|
k := &dto.Metric{}
|
2013-08-03 09:46:02 -07:00
|
|
|
dumpMetric(k, m)
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
return i.LevelDBPersistence.Has(k)
|
2013-08-05 08:31:49 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewLevelDBMetricMembershipIndex returns a LevelDBMetricMembershipIndex object
|
|
|
|
// ready to use.
|
2014-02-27 05:34:46 -08:00
|
|
|
func NewLevelDBMetricMembershipIndex(o leveldb.LevelDBOptions) (*LevelDBMetricMembershipIndex, error) {
|
|
|
|
s, err := leveldb.NewLevelDBPersistence(o)
|
2013-08-03 09:46:02 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-08-06 03:00:31 -07:00
|
|
|
return &LevelDBMetricMembershipIndex{
|
2014-02-14 10:36:27 -08:00
|
|
|
LevelDBPersistence: s,
|
2013-08-03 09:46:02 -07:00
|
|
|
}, nil
|
|
|
|
}
|
2013-08-07 03:07:35 -07:00
|
|
|
|
|
|
|
// MetricIndexer indexes facets of a clientmodel.Metric.
|
|
|
|
type MetricIndexer interface {
|
|
|
|
// IndexMetric makes no assumptions about the concurrency safety of the
|
|
|
|
// underlying implementer.
|
|
|
|
IndexMetrics(FingerprintMetricMapping) error
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexerObserver listens and receives changes to a given
|
2013-08-13 09:08:02 -07:00
|
|
|
// FingerprintMetricMapping.
|
|
|
|
type IndexerObserver interface {
|
|
|
|
Observe(FingerprintMetricMapping) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexerProxy receives IndexMetric requests and proxies them to the underlying
|
|
|
|
// MetricIndexer. Upon success of the underlying receiver, the registered
|
|
|
|
// IndexObservers are called serially.
|
|
|
|
//
|
|
|
|
// If an error occurs in the underlying MetricIndexer or any of the observers,
|
|
|
|
// this proxy will not work any further and return the offending error in this
|
|
|
|
// call or any subsequent ones.
|
|
|
|
type IndexerProxy struct {
|
|
|
|
err error
|
|
|
|
|
|
|
|
i MetricIndexer
|
|
|
|
observers []IndexerObserver
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexMetrics proxies the given FingerprintMetricMapping to the underlying
|
|
|
|
// MetricIndexer and calls all registered observers with it.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (p *IndexerProxy) IndexMetrics(b FingerprintMetricMapping) error {
|
|
|
|
if p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
if p.err = p.i.IndexMetrics(b); p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range p.observers {
|
|
|
|
if p.err = o.Observe(b); p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the underlying indexer.
|
|
|
|
func (p *IndexerProxy) Close() error {
|
|
|
|
if p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
if closer, ok := p.i.(io.Closer); ok {
|
|
|
|
p.err = closer.Close()
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Flush flushes the underlying index requests before closing.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (p *IndexerProxy) Flush() error {
|
|
|
|
if p.err != nil {
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
if flusher, ok := p.i.(flusher); ok {
|
|
|
|
p.err = flusher.Flush()
|
|
|
|
return p.err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIndexerProxy builds an IndexerProxy for the given configuration.
|
|
|
|
func NewIndexerProxy(i MetricIndexer, o ...IndexerObserver) *IndexerProxy {
|
|
|
|
return &IndexerProxy{
|
|
|
|
i: i,
|
|
|
|
observers: o,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SynchronizedIndexer provides naive locking for any MetricIndexer.
|
|
|
|
type SynchronizedIndexer struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
i MetricIndexer
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexMetrics calls IndexMetrics of the wrapped MetricIndexer after acquiring
|
|
|
|
// a lock.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (i *SynchronizedIndexer) IndexMetrics(b FingerprintMetricMapping) error {
|
|
|
|
i.mu.Lock()
|
|
|
|
defer i.mu.Unlock()
|
|
|
|
|
|
|
|
return i.i.IndexMetrics(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
type flusher interface {
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Flush calls Flush of the wrapped MetricIndexer after acquiring a lock. If the
|
|
|
|
// wrapped MetricIndexer has no Flush method, this is a no-op.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (i *SynchronizedIndexer) Flush() error {
|
|
|
|
if flusher, ok := i.i.(flusher); ok {
|
|
|
|
i.mu.Lock()
|
|
|
|
defer i.mu.Unlock()
|
|
|
|
|
|
|
|
return flusher.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Close calls Close of the wrapped MetricIndexer after acquiring a lock. If the
|
|
|
|
// wrapped MetricIndexer has no Close method, this is a no-op.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (i *SynchronizedIndexer) Close() error {
|
|
|
|
if closer, ok := i.i.(io.Closer); ok {
|
|
|
|
i.mu.Lock()
|
|
|
|
defer i.mu.Unlock()
|
|
|
|
|
|
|
|
return closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewSynchronizedIndexer returns a SynchronizedIndexer wrapping the given
|
|
|
|
// MetricIndexer.
|
2013-08-13 09:08:02 -07:00
|
|
|
func NewSynchronizedIndexer(i MetricIndexer) *SynchronizedIndexer {
|
|
|
|
return &SynchronizedIndexer{
|
|
|
|
i: i,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BufferedIndexer provides unsynchronized index buffering.
|
|
|
|
//
|
|
|
|
// If an error occurs in the underlying MetricIndexer or any of the observers,
|
|
|
|
// this proxy will not work any further and return the offending error.
|
|
|
|
type BufferedIndexer struct {
|
|
|
|
i MetricIndexer
|
|
|
|
|
|
|
|
limit int
|
|
|
|
|
|
|
|
buf []FingerprintMetricMapping
|
|
|
|
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexMetrics writes the entries in the given FingerprintMetricMapping to the
|
|
|
|
// index.
|
2013-08-13 09:08:02 -07:00
|
|
|
func (i *BufferedIndexer) IndexMetrics(b FingerprintMetricMapping) error {
|
|
|
|
if i.err != nil {
|
|
|
|
return i.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(i.buf) < i.limit {
|
|
|
|
i.buf = append(i.buf, b)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i.err = i.Flush()
|
|
|
|
|
|
|
|
return i.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush writes all pending entries to the index.
|
|
|
|
func (i *BufferedIndexer) Flush() error {
|
|
|
|
if i.err != nil {
|
|
|
|
return i.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(i.buf) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
superset := FingerprintMetricMapping{}
|
|
|
|
for _, b := range i.buf {
|
|
|
|
for fp, m := range b {
|
|
|
|
if _, ok := superset[fp]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
superset[fp] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.buf = make([]FingerprintMetricMapping, 0, i.limit)
|
|
|
|
|
|
|
|
i.err = i.i.IndexMetrics(superset)
|
|
|
|
|
|
|
|
return i.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close flushes and closes the underlying buffer.
|
|
|
|
func (i *BufferedIndexer) Close() error {
|
|
|
|
if err := i.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if closer, ok := i.i.(io.Closer); ok {
|
|
|
|
return closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewBufferedIndexer returns a BufferedIndexer ready to use.
|
2013-08-13 09:08:02 -07:00
|
|
|
func NewBufferedIndexer(i MetricIndexer, limit int) *BufferedIndexer {
|
|
|
|
return &BufferedIndexer{
|
|
|
|
i: i,
|
|
|
|
limit: limit,
|
|
|
|
buf: make([]FingerprintMetricMapping, 0, limit),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
// TotalIndexer is a MetricIndexer that indexes all standard facets of a metric
|
|
|
|
// that a user or the Prometheus subsystem would want to query against:
|
|
|
|
//
|
|
|
|
// "<Label Name>" -> {Fingerprint, ...}
|
2013-08-07 04:29:10 -07:00
|
|
|
// "<Label Name> <Label Value>" -> {Fingerprint, ...}
|
2013-08-07 03:07:35 -07:00
|
|
|
//
|
|
|
|
// "<Fingerprint>" -> Metric
|
|
|
|
//
|
|
|
|
// "<Metric>" -> Existence Value
|
|
|
|
//
|
2013-08-07 04:29:10 -07:00
|
|
|
// This type supports concrete queries but only single writes, and it has no
|
2013-08-07 03:07:35 -07:00
|
|
|
// locking semantics to enforce this.
|
|
|
|
type TotalIndexer struct {
|
|
|
|
FingerprintToMetric FingerprintMetricIndex
|
2014-03-24 04:08:28 -07:00
|
|
|
LabelNameToLabelValues LabelNameLabelValuesIndex
|
2013-08-07 03:07:35 -07:00
|
|
|
LabelPairToFingerprint LabelPairFingerprintIndex
|
|
|
|
MetricMembership MetricMembershipIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func findUnindexed(i MetricMembershipIndex, b FingerprintMetricMapping) (FingerprintMetricMapping, error) {
|
|
|
|
out := FingerprintMetricMapping{}
|
|
|
|
|
|
|
|
for fp, m := range b {
|
|
|
|
has, err := i.Has(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
out[fp] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
func extendLabelNameToLabelValuesIndex(i LabelNameLabelValuesIndex, b FingerprintMetricMapping) (LabelNameLabelValuesMapping, error) {
|
|
|
|
collection := map[clientmodel.LabelName]utility.Set{}
|
|
|
|
|
|
|
|
for _, m := range b {
|
|
|
|
for l, v := range m {
|
|
|
|
set, ok := collection[l]
|
|
|
|
if !ok {
|
|
|
|
baseValues, _, err := i.Lookup(l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
set = utility.Set{}
|
|
|
|
|
|
|
|
for _, baseValue := range baseValues {
|
|
|
|
set.Add(baseValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
collection[l] = set
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Add(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
batch := LabelNameLabelValuesMapping{}
|
|
|
|
for l, set := range collection {
|
|
|
|
values := make(clientmodel.LabelValues, 0, len(set))
|
|
|
|
for e := range set {
|
|
|
|
val := e.(clientmodel.LabelValue)
|
|
|
|
values = append(values, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch[l] = values
|
|
|
|
}
|
|
|
|
|
|
|
|
return batch, nil
|
|
|
|
}
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
func extendLabelPairIndex(i LabelPairFingerprintIndex, b FingerprintMetricMapping) (LabelPairFingerprintMapping, error) {
|
2014-04-11 00:27:05 -07:00
|
|
|
collection := map[metric.LabelPair]utility.Set{}
|
2013-08-07 03:07:35 -07:00
|
|
|
|
|
|
|
for fp, m := range b {
|
|
|
|
for n, v := range m {
|
2014-04-11 00:27:05 -07:00
|
|
|
pair := metric.LabelPair{
|
2013-08-07 03:07:35 -07:00
|
|
|
Name: n,
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
set, ok := collection[pair]
|
|
|
|
if !ok {
|
|
|
|
baseFps, _, err := i.Lookup(&pair)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
set = utility.Set{}
|
|
|
|
for _, baseFp := range baseFps {
|
|
|
|
set.Add(*baseFp)
|
|
|
|
}
|
|
|
|
|
|
|
|
collection[pair] = set
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Add(fp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
batch := LabelPairFingerprintMapping{}
|
|
|
|
|
|
|
|
for pair, set := range collection {
|
|
|
|
fps := batch[pair]
|
|
|
|
for element := range set {
|
|
|
|
fp := element.(clientmodel.Fingerprint)
|
|
|
|
fps = append(fps, &fp)
|
|
|
|
}
|
|
|
|
batch[pair] = fps
|
|
|
|
}
|
|
|
|
|
|
|
|
return batch, nil
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// IndexMetrics adds the facets of all unindexed metrics found in the given
|
|
|
|
// FingerprintMetricMapping to the corresponding indices.
|
2013-08-07 03:07:35 -07:00
|
|
|
func (i *TotalIndexer) IndexMetrics(b FingerprintMetricMapping) error {
|
|
|
|
unindexed, err := findUnindexed(i.MetricMembership, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-24 04:08:28 -07:00
|
|
|
labelNames, err := extendLabelNameToLabelValuesIndex(i.LabelNameToLabelValues, unindexed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := i.LabelNameToLabelValues.IndexBatch(labelNames); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-07 03:07:35 -07:00
|
|
|
labelPairs, err := extendLabelPairIndex(i.LabelPairToFingerprint, unindexed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := i.LabelPairToFingerprint.IndexBatch(labelPairs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := i.FingerprintToMetric.IndexBatch(unindexed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.MetricMembership.IndexBatch(unindexed)
|
|
|
|
}
|