2012-12-09 07:27:12 -08:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2013-01-27 09:49:45 -08:00
|
|
|
"github.com/prometheus/prometheus/coding"
|
|
|
|
"github.com/prometheus/prometheus/coding/indexable"
|
|
|
|
"github.com/prometheus/prometheus/model"
|
|
|
|
dto "github.com/prometheus/prometheus/model/generated"
|
2013-01-04 08:55:58 -08:00
|
|
|
"time"
|
2012-12-09 07:27:12 -08:00
|
|
|
)
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
func (l *LevelDBMetricPersistence) setLabelPairFingerprints(labelPair *dto.LabelPair, fingerprints *dto.FingerprintCollection) (err error) {
|
|
|
|
begin := time.Now()
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
defer func() {
|
|
|
|
duration := time.Now().Sub(begin)
|
|
|
|
|
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: setLabelPairFingerprints, result: success}, map[string]string{operation: setLabelPairFingerprints, result: failure})
|
|
|
|
}()
|
2012-12-25 04:50:36 -08:00
|
|
|
|
2012-12-09 07:27:12 -08:00
|
|
|
labelPairEncoded := coding.NewProtocolBufferEncoder(labelPair)
|
|
|
|
fingerprintsEncoded := coding.NewProtocolBufferEncoder(fingerprints)
|
2013-01-23 08:18:45 -08:00
|
|
|
err = l.labelSetToFingerprints.Put(labelPairEncoded, fingerprintsEncoded)
|
|
|
|
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
func (l *LevelDBMetricPersistence) setLabelNameFingerprints(labelName *dto.LabelName, fingerprints *dto.FingerprintCollection) (err error) {
|
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
duration := time.Now().Sub(begin)
|
|
|
|
|
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: setLabelNameFingerprints, result: success}, map[string]string{operation: setLabelNameFingerprints, result: failure})
|
|
|
|
}()
|
|
|
|
|
2012-12-09 07:27:12 -08:00
|
|
|
labelNameEncoded := coding.NewProtocolBufferEncoder(labelName)
|
|
|
|
fingerprintsEncoded := coding.NewProtocolBufferEncoder(fingerprints)
|
2013-01-23 08:18:45 -08:00
|
|
|
|
|
|
|
err = l.labelNameToFingerprints.Put(labelNameEncoded, fingerprintsEncoded)
|
|
|
|
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
func (l *LevelDBMetricPersistence) appendLabelPairFingerprint(labelPair *dto.LabelPair, fingerprint *dto.Fingerprint) (err error) {
|
2013-01-23 08:18:45 -08:00
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
duration := time.Now().Sub(begin)
|
|
|
|
|
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: appendLabelPairFingerprint, result: success}, map[string]string{operation: appendLabelPairFingerprint, result: failure})
|
|
|
|
}()
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
has, err := l.HasLabelPair(labelPair)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
var fingerprints *dto.FingerprintCollection
|
|
|
|
if has {
|
|
|
|
fingerprints, err = l.getFingerprintsForLabelSet(labelPair)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
} else {
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprints = &dto.FingerprintCollection{}
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprints.Member = append(fingerprints.Member, fingerprint)
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
err = l.setLabelPairFingerprints(labelPair, fingerprints)
|
|
|
|
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
func (l *LevelDBMetricPersistence) appendLabelNameFingerprint(labelPair *dto.LabelPair, fingerprint *dto.Fingerprint) (err error) {
|
2013-01-23 08:18:45 -08:00
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
duration := time.Now().Sub(begin)
|
|
|
|
|
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: appendLabelNameFingerprint, result: success}, map[string]string{operation: appendLabelNameFingerprint, result: failure})
|
|
|
|
}()
|
|
|
|
|
2012-12-09 07:27:12 -08:00
|
|
|
labelName := &dto.LabelName{
|
|
|
|
Name: labelPair.Name,
|
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
has, err := l.HasLabelName(labelName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
var fingerprints *dto.FingerprintCollection
|
|
|
|
if has {
|
|
|
|
fingerprints, err = l.GetLabelNameFingerprints(labelName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
} else {
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprints = &dto.FingerprintCollection{}
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprints.Member = append(fingerprints.Member, fingerprint)
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
err = l.setLabelNameFingerprints(labelName, fingerprints)
|
|
|
|
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
func (l *LevelDBMetricPersistence) appendFingerprints(m *dto.Metric) (err error) {
|
2013-01-23 08:18:45 -08:00
|
|
|
begin := time.Now()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
duration := time.Now().Sub(begin)
|
|
|
|
|
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: appendFingerprints, result: success}, map[string]string{operation: appendFingerprints, result: failure})
|
|
|
|
}()
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprintDTO, err := model.MessageToFingerprintDTO(m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
fingerprintKey := coding.NewProtocolBufferEncoder(fingerprintDTO)
|
|
|
|
metricDTOEncoder := coding.NewProtocolBufferEncoder(m)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
err = l.fingerprintToMetrics.Put(fingerprintKey, metricDTOEncoder)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
labelCount := len(m.LabelPair)
|
|
|
|
labelPairErrors := make(chan error, labelCount)
|
|
|
|
labelNameErrors := make(chan error, labelCount)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
for _, labelPair := range m.LabelPair {
|
|
|
|
go func(labelPair *dto.LabelPair) {
|
|
|
|
labelNameErrors <- l.appendLabelNameFingerprint(labelPair, fingerprintDTO)
|
|
|
|
}(labelPair)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
go func(labelPair *dto.LabelPair) {
|
|
|
|
labelPairErrors <- l.appendLabelPairFingerprint(labelPair, fingerprintDTO)
|
|
|
|
}(labelPair)
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
for i := 0; i < cap(labelPairErrors); i++ {
|
|
|
|
err = <-labelPairErrors
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
for i := 0; i < cap(labelNameErrors); i++ {
|
|
|
|
err = <-labelNameErrors
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
func (l *LevelDBMetricPersistence) AppendSample(sample *model.Sample) (err error) {
|
2013-01-23 08:18:45 -08:00
|
|
|
begin := time.Now()
|
2012-12-25 04:50:36 -08:00
|
|
|
defer func() {
|
2013-01-23 08:18:45 -08:00
|
|
|
duration := time.Now().Sub(begin)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
recordOutcome(storageOperations, storageLatency, duration, err, map[string]string{operation: appendSample, result: success}, map[string]string{operation: appendSample, result: failure})
|
2012-12-25 04:50:36 -08:00
|
|
|
}()
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
metricDTO := model.SampleToMetricDTO(sample)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
indexHas, err := l.hasIndexMetric(metricDTO)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-04 08:55:58 -08:00
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
if !indexHas {
|
|
|
|
err = l.indexMetric(metricDTO)
|
2012-12-25 04:50:36 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
err = l.appendFingerprints(metricDTO)
|
2013-01-04 08:55:58 -08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-25 04:50:36 -08:00
|
|
|
}
|
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
fingerprintDTO, err := model.MessageToFingerprintDTO(metricDTO)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sampleKeyDTO := &dto.SampleKey{
|
|
|
|
Fingerprint: fingerprintDTO,
|
|
|
|
Timestamp: indexable.EncodeTime(sample.Timestamp),
|
|
|
|
}
|
|
|
|
sampleValueDTO := &dto.SampleValue{
|
|
|
|
Value: proto.Float32(float32(sample.Value)),
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|
2013-01-23 08:18:45 -08:00
|
|
|
sampleKeyEncoded := coding.NewProtocolBufferEncoder(sampleKeyDTO)
|
|
|
|
sampleValueEncoded := coding.NewProtocolBufferEncoder(sampleValueDTO)
|
2012-12-09 07:27:12 -08:00
|
|
|
|
2013-01-23 08:18:45 -08:00
|
|
|
err = l.metricSamples.Put(sampleKeyEncoded, sampleValueEncoded)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-01-04 08:55:58 -08:00
|
|
|
|
2012-12-25 04:50:36 -08:00
|
|
|
return
|
2012-12-09 07:27:12 -08:00
|
|
|
}
|