From e55205220b5b7512b2f04a88cd8c8feb60efa8ec Mon Sep 17 00:00:00 2001
From: Aliaksandr Valialkin <valyala@gmail.com>
Date: Tue, 19 Jan 2021 22:55:46 +0200
Subject: [PATCH] app/vmselect: add `-search.maxStepForPointsAdjustment`
 command-line flag, which can be used for disabling adjustment for points
 returned from `/api/v1/query_range` handler if they have timestamps closer
 than `-search.latencyOffset` to the current time

---
 app/vmselect/prometheus/prometheus.go | 10 +++++++---
 app/vmselect/promql/rollup.go         |  2 +-
 docs/CHANGELOG.md                     |  4 +++-
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go
index 2fb28d29f1..7fad450331 100644
--- a/app/vmselect/prometheus/prometheus.go
+++ b/app/vmselect/prometheus/prometheus.go
@@ -42,6 +42,8 @@ var (
 		"By default it is automatically calculated from the median interval between samples. This flag could be useful for tuning "+
 		"Prometheus data model closer to Influx-style data model. See https://prometheus.io/docs/prometheus/latest/querying/basics/#staleness for details. "+
 		"See also '-search.maxLookback' flag, which has the same meaning due to historical reasons")
+	maxStepForPointsAdjustment = flag.Duration("search.maxStepForPointsAdjustment", time.Minute, "The maximum step when /api/v1/query_range handler adjusts "+
+		"points with timestamps closer than -search.latencyOffset to the current time. The adjustment is needed because such points may contain incomplete data")
 	selectNodes = flagutil.NewArray("selectNode", "Addresses of vmselect nodes; usage: -selectNode=vmselect-host1:8481 -selectNode=vmselect-host2:8481")
 )
 
@@ -1208,9 +1210,11 @@ func queryRangeHandler(startTime time.Time, at *auth.Token, w http.ResponseWrite
 	if err != nil {
 		return fmt.Errorf("cannot execute query: %w", err)
 	}
-	queryOffset := getLatencyOffsetMilliseconds()
-	if ct-queryOffset < end {
-		result = adjustLastPoints(result, ct-queryOffset, ct+step)
+	if step < maxStepForPointsAdjustment.Milliseconds() {
+		queryOffset := getLatencyOffsetMilliseconds()
+		if ct-queryOffset < end {
+			result = adjustLastPoints(result, ct-queryOffset, ct+step)
+		}
 	}
 
 	// Remove NaN values as Prometheus does.
diff --git a/app/vmselect/promql/rollup.go b/app/vmselect/promql/rollup.go
index 41cf242fa7..3c62fbdc71 100644
--- a/app/vmselect/promql/rollup.go
+++ b/app/vmselect/promql/rollup.go
@@ -391,7 +391,7 @@ type rollupConfig struct {
 	// Whether window may be adjusted to 2 x interval between data points.
 	// This is needed for functions which have dt in the denominator
 	// such as rate, deriv, etc.
-	// Without the adjustement their value would jump in unexpected directions
+	// Without the adjustment their value would jump in unexpected directions
 	// when using window smaller than 2 x scrape_interval.
 	MayAdjustWindow bool
 
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 43d0c23d68..37cf62e9d0 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -2,6 +2,8 @@
 
 # tip
 
+* FEATURE: added `search.maxStepForPointsAdjustment` command-line flag, which can be used for disabling adjustment for points returned `/api/v1/query_range` handler if such points have timestamps closer than `-search.latencyOffset` to the current time. Such points may contain incomplete data, so they are substituted by the previous values for `step` query args smaller than one minute by default.
+
 
 # [v1.52.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.52.0)
 
@@ -306,7 +308,7 @@
 * BUGFIX: properly apply `-search.maxStalenessInterval` command-line flag value. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/784 .
 * BUGFIX: fix displaying data in Grafana tables. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/720 .
 * BUGFIX: do not adjust the number of detected CPU cores found at `/sys/devices/system/cpu/online`.
-  The adjustement was increasing the resulting GOMAXPROC by 1, which looked confusing to users.
+  The adjustment was increasing the resulting GOMAXPROC by 1, which looked confusing to users.
   See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/685#issuecomment-698595309 .
 * BUGFIX: vmagent: do not show `-remoteWrite.url` in initial logs if `-remoteWrite.showURL` isn't set. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/773 .
 * BUGFIX: properly handle case when [/metrics/find](https://victoriametrics.github.io/#graphite-metrics-api-usage) finds both a leaf and a node for the given `query=prefix.*`.