2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2013 The Prometheus Authors
|
2013-03-21 10:06:15 -07:00
|
|
|
// 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 ast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2013-06-25 05:02:27 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/stats"
|
2014-06-06 02:55:53 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/local"
|
2013-03-21 10:06:15 -07:00
|
|
|
)
|
|
|
|
|
2015-02-17 17:30:41 -08:00
|
|
|
// preloadTimes tracks which instants or ranges to preload for a set of
|
|
|
|
// fingerprints. One of these structs is collected for each offset by the query
|
|
|
|
// analyzer.
|
|
|
|
type preloadTimes struct {
|
|
|
|
// Instants require single samples to be loaded along the entire query
|
|
|
|
// range, with intervals between the samples corresponding to the query
|
|
|
|
// resolution.
|
|
|
|
instants map[clientmodel.Fingerprint]struct{}
|
|
|
|
// Ranges require loading a range of samples at each resolution step,
|
|
|
|
// stretching backwards from the current evaluation timestamp. The length of
|
|
|
|
// the range into the past is given by the duration, as in "foo[5m]".
|
|
|
|
ranges map[clientmodel.Fingerprint]time.Duration
|
|
|
|
}
|
2013-03-21 10:06:15 -07:00
|
|
|
|
2015-02-17 17:44:55 -08:00
|
|
|
// A queryAnalyzer recursively traverses the AST to look for any nodes
|
2014-02-13 09:48:56 -08:00
|
|
|
// which will need data from the datastore. Instantiate with
|
2015-02-17 17:44:55 -08:00
|
|
|
// newQueryAnalyzer.
|
|
|
|
type queryAnalyzer struct {
|
2015-02-17 17:30:41 -08:00
|
|
|
// Tracks one set of times to preload per offset that occurs in the query
|
|
|
|
// expression.
|
|
|
|
offsetPreloadTimes map[time.Duration]preloadTimes
|
2013-05-07 04:15:10 -07:00
|
|
|
// The underlying storage to which the query will be applied. Needed for
|
|
|
|
// extracting timeseries fingerprint information during query analysis.
|
2014-09-16 06:47:24 -07:00
|
|
|
storage local.Storage
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:44:55 -08:00
|
|
|
// newQueryAnalyzer returns a pointer to a newly instantiated
|
|
|
|
// queryAnalyzer. The storage is needed to extract timeseries
|
2014-02-13 09:48:56 -08:00
|
|
|
// fingerprint information during query analysis.
|
2015-02-17 17:44:55 -08:00
|
|
|
func newQueryAnalyzer(storage local.Storage) *queryAnalyzer {
|
|
|
|
return &queryAnalyzer{
|
2015-02-17 17:30:41 -08:00
|
|
|
offsetPreloadTimes: map[time.Duration]preloadTimes{},
|
|
|
|
storage: storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:44:55 -08:00
|
|
|
func (analyzer *queryAnalyzer) getPreloadTimes(offset time.Duration) preloadTimes {
|
2015-02-17 17:30:41 -08:00
|
|
|
if _, ok := analyzer.offsetPreloadTimes[offset]; !ok {
|
|
|
|
analyzer.offsetPreloadTimes[offset] = preloadTimes{
|
|
|
|
instants: map[clientmodel.Fingerprint]struct{}{},
|
|
|
|
ranges: map[clientmodel.Fingerprint]time.Duration{},
|
|
|
|
}
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
return analyzer.offsetPreloadTimes[offset]
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:44:55 -08:00
|
|
|
// visit implements the visitor interface.
|
|
|
|
func (analyzer *queryAnalyzer) visit(node Node) {
|
2013-03-21 10:06:15 -07:00
|
|
|
switch n := node.(type) {
|
2014-02-22 13:29:32 -08:00
|
|
|
case *VectorSelector:
|
2015-02-17 17:30:41 -08:00
|
|
|
pt := analyzer.getPreloadTimes(n.offset)
|
2014-06-06 02:55:53 -07:00
|
|
|
fingerprints := analyzer.storage.GetFingerprintsForLabelMatchers(n.labelMatchers)
|
2013-03-27 06:06:30 -07:00
|
|
|
n.fingerprints = fingerprints
|
2014-06-06 02:55:53 -07:00
|
|
|
for _, fp := range fingerprints {
|
2015-02-17 17:30:41 -08:00
|
|
|
// Only add the fingerprint to the instants if not yet present in the
|
|
|
|
// ranges. Ranges always contain more points and span more time than
|
|
|
|
// instants for the same offset.
|
|
|
|
if _, alreadyInRanges := pt.ranges[fp]; !alreadyInRanges {
|
|
|
|
pt.instants[fp] = struct{}{}
|
2014-02-13 10:10:56 -08:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
|
|
|
|
n.metrics[fp] = analyzer.storage.GetMetricForFingerprint(fp)
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-02-22 13:29:32 -08:00
|
|
|
case *MatrixSelector:
|
2015-02-17 17:30:41 -08:00
|
|
|
pt := analyzer.getPreloadTimes(n.offset)
|
2014-06-06 02:55:53 -07:00
|
|
|
fingerprints := analyzer.storage.GetFingerprintsForLabelMatchers(n.labelMatchers)
|
2013-03-27 06:06:30 -07:00
|
|
|
n.fingerprints = fingerprints
|
2014-06-06 02:55:53 -07:00
|
|
|
for _, fp := range fingerprints {
|
2015-02-17 17:30:41 -08:00
|
|
|
if pt.ranges[fp] < n.interval {
|
|
|
|
pt.ranges[fp] = n.interval
|
|
|
|
// Delete the fingerprint from the instants. Ranges always contain more
|
|
|
|
// points and span more time than instants, so we don't need to track
|
|
|
|
// an instant for the same fingerprint, should we have one.
|
|
|
|
delete(pt.instants, fp)
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
|
|
|
|
n.metrics[fp] = analyzer.storage.GetMetricForFingerprint(fp)
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
type iteratorInitializer struct {
|
2014-09-16 06:47:24 -07:00
|
|
|
storage local.Storage
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 17:44:55 -08:00
|
|
|
func (i *iteratorInitializer) visit(node Node) {
|
2014-06-06 02:55:53 -07:00
|
|
|
switch n := node.(type) {
|
|
|
|
case *VectorSelector:
|
|
|
|
for _, fp := range n.fingerprints {
|
|
|
|
n.iterators[fp] = i.storage.NewIterator(fp)
|
|
|
|
}
|
|
|
|
case *MatrixSelector:
|
|
|
|
for _, fp := range n.fingerprints {
|
|
|
|
n.iterators[fp] = i.storage.NewIterator(fp)
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
func prepareInstantQuery(node Node, timestamp clientmodel.Timestamp, storage local.Storage, queryStats *stats.TimerGroup) (local.Preloader, error) {
|
2015-02-02 23:04:27 -08:00
|
|
|
totalTimer := queryStats.GetTimer(stats.TotalEvalTime)
|
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
analyzeTimer := queryStats.GetTimer(stats.QueryAnalysisTime).Start()
|
2015-02-17 17:44:55 -08:00
|
|
|
analyzer := newQueryAnalyzer(storage)
|
2014-06-06 02:55:53 -07:00
|
|
|
Walk(analyzer, node)
|
2013-06-03 08:07:03 -07:00
|
|
|
analyzeTimer.Stop()
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
preloadTimer := queryStats.GetTimer(stats.PreloadTime).Start()
|
|
|
|
p := storage.NewPreloader()
|
2015-02-17 17:30:41 -08:00
|
|
|
for offset, pt := range analyzer.offsetPreloadTimes {
|
|
|
|
ts := timestamp.Add(-offset)
|
|
|
|
for fp, rangeDuration := range pt.ranges {
|
|
|
|
if et := totalTimer.ElapsedTime(); et > *queryTimeout {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, queryTimeoutError{et}
|
|
|
|
}
|
|
|
|
if err := p.PreloadRange(fp, ts.Add(-rangeDuration), ts, *stalenessDelta); err != nil {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-02 23:04:27 -08:00
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
for fp := range pt.instants {
|
|
|
|
if et := totalTimer.ElapsedTime(); et > *queryTimeout {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, queryTimeoutError{et}
|
|
|
|
}
|
|
|
|
if err := p.PreloadRange(fp, ts, ts, *stalenessDelta); err != nil {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
preloadTimer.Stop()
|
2013-06-03 08:07:03 -07:00
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
ii := &iteratorInitializer{
|
|
|
|
storage: storage,
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
Walk(ii, node)
|
|
|
|
|
|
|
|
return p, nil
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
func prepareRangeQuery(node Node, start clientmodel.Timestamp, end clientmodel.Timestamp, interval time.Duration, storage local.Storage, queryStats *stats.TimerGroup) (local.Preloader, error) {
|
2015-02-02 23:04:27 -08:00
|
|
|
totalTimer := queryStats.GetTimer(stats.TotalEvalTime)
|
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
analyzeTimer := queryStats.GetTimer(stats.QueryAnalysisTime).Start()
|
2015-02-17 17:44:55 -08:00
|
|
|
analyzer := newQueryAnalyzer(storage)
|
2014-06-06 02:55:53 -07:00
|
|
|
Walk(analyzer, node)
|
2013-06-03 08:07:03 -07:00
|
|
|
analyzeTimer.Stop()
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
preloadTimer := queryStats.GetTimer(stats.PreloadTime).Start()
|
|
|
|
p := storage.NewPreloader()
|
2015-02-17 17:30:41 -08:00
|
|
|
for offset, pt := range analyzer.offsetPreloadTimes {
|
|
|
|
offsetStart := start.Add(-offset)
|
|
|
|
offsetEnd := end.Add(-offset)
|
|
|
|
for fp, rangeDuration := range pt.ranges {
|
|
|
|
if et := totalTimer.ElapsedTime(); et > *queryTimeout {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, queryTimeoutError{et}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
if err := p.PreloadRange(fp, offsetStart.Add(-rangeDuration), offsetEnd, *stalenessDelta); err != nil {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
if interval < rangeDuration {
|
|
|
|
if err := p.GetMetricRange(fp, offsetEnd, offsetEnd.Sub(offsetStart)+rangeDuration); err != nil {
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := p.GetMetricRangeAtInterval(fp, offsetStart, offsetEnd, interval, rangeDuration); err != nil {
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2015-02-02 23:04:27 -08:00
|
|
|
}
|
2015-02-17 17:30:41 -08:00
|
|
|
for fp := range pt.instants {
|
|
|
|
if et := totalTimer.ElapsedTime(); et > *queryTimeout {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, queryTimeoutError{et}
|
|
|
|
}
|
|
|
|
if err := p.PreloadRange(fp, offsetStart, offsetEnd, *stalenessDelta); err != nil {
|
|
|
|
preloadTimer.Stop()
|
|
|
|
p.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
preloadTimer.Stop()
|
2013-06-03 08:07:03 -07:00
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
ii := &iteratorInitializer{
|
|
|
|
storage: storage,
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
Walk(ii, node)
|
|
|
|
|
|
|
|
return p, nil
|
2013-03-21 10:06:15 -07:00
|
|
|
}
|