app/vmselect/promql: allow dropping trailing sample only for default_rollup function

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/850
This commit is contained in:
Aliaksandr Valialkin 2020-11-02 02:09:27 +02:00
parent 3ed9f1d5a9
commit 66de02fbb4

View File

@ -258,15 +258,16 @@ func getRollupConfigs(name string, rf rollupFunc, expr metricsql.Expr, start, en
} }
newRollupConfig := func(rf rollupFunc, tagValue string) *rollupConfig { newRollupConfig := func(rf rollupFunc, tagValue string) *rollupConfig {
return &rollupConfig{ return &rollupConfig{
TagValue: tagValue, TagValue: tagValue,
Func: rf, Func: rf,
Start: start, Start: start,
End: end, End: end,
Step: step, Step: step,
Window: window, Window: window,
MayAdjustWindow: !rollupFuncsCannotAdjustWindow[name], MayAdjustWindow: !rollupFuncsCannotAdjustWindow[name],
LookbackDelta: lookbackDelta, CanDropLastSample: name == "default_rollup",
Timestamps: sharedTimestamps, LookbackDelta: lookbackDelta,
Timestamps: sharedTimestamps,
} }
} }
appendRollupConfigs := func(dst []*rollupConfig) []*rollupConfig { appendRollupConfigs := func(dst []*rollupConfig) []*rollupConfig {
@ -370,6 +371,11 @@ type rollupConfig struct {
// when using window smaller than 2 x scrape_interval. // when using window smaller than 2 x scrape_interval.
MayAdjustWindow bool MayAdjustWindow bool
// Whether the last sample can be dropped during rollup calculations.
// The last sample can be dropped for `default_rollup()` function only.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/748 .
CanDropLastSample bool
Timestamps []int64 Timestamps []int64
// LoookbackDelta is the analog to `-query.lookback-delta` from Prometheus world. // LoookbackDelta is the analog to `-query.lookback-delta` from Prometheus world.
@ -501,6 +507,7 @@ func (rc *rollupConfig) doInternal(dstValues []float64, tsm *timeseriesMap, valu
ni := 0 ni := 0
nj := 0 nj := 0
stalenessInterval := int64(float64(scrapeInterval) * 0.9) stalenessInterval := int64(float64(scrapeInterval) * 0.9)
canDropLastSample := rc.CanDropLastSample
for _, tEnd := range rc.Timestamps { for _, tEnd := range rc.Timestamps {
tStart := tEnd - window tStart := tEnd - window
ni = seekFirstTimestampIdxAfter(timestamps[i:], tStart, ni) ni = seekFirstTimestampIdxAfter(timestamps[i:], tStart, ni)
@ -519,7 +526,7 @@ func (rc *rollupConfig) doInternal(dstValues []float64, tsm *timeseriesMap, valu
} }
rfa.values = values[i:j] rfa.values = values[i:j]
rfa.timestamps = timestamps[i:j] rfa.timestamps = timestamps[i:j]
if j == len(timestamps) && j > 0 && (tEnd-timestamps[j-1] > stalenessInterval || i == j && len(timestamps) == 1) && rc.End-tEnd >= 2*rc.Step { if canDropLastSample && j == len(timestamps) && j > 0 && (tEnd-timestamps[j-1] > stalenessInterval || i == j && len(timestamps) == 1) && rc.End-tEnd >= 2*rc.Step {
// Drop trailing data points in the following cases: // Drop trailing data points in the following cases:
// - if the distance between the last raw sample and tEnd exceeds stalenessInterval // - if the distance between the last raw sample and tEnd exceeds stalenessInterval
// - if time series contains only a single raw sample // - if time series contains only a single raw sample