From 6d76f09c586f635216728c573a4008cb22453354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0tibran=C3=BD?= Date: Sat, 29 Jan 2022 23:55:27 +0100 Subject: [PATCH] Extract interface from ActivityQueryTracker and allows passing custom implementation (#10071) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Extract interface from ActivityQueryTracker and allows passing custom implementation. Signed-off-by: Peter Štibraný --- promql/engine.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index 4800f1bfbd..3eb7503c42 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -208,13 +208,32 @@ func contextErr(err error, env string) error { } } +// QueryTracker provides access to two features: +// +// 1) Tracking of active query. If PromQL engine crashes while executing any query, such query should be present +// in the tracker on restart, hence logged. After the logging on restart, the tracker gets emptied. +// +// 2) Enforcement of the maximum number of concurrent queries. +type QueryTracker interface { + // GetMaxConcurrent returns maximum number of concurrent queries that are allowed by this tracker. + GetMaxConcurrent() int + + // Insert inserts query into query tracker. This call must block if maximum number of queries is already running. + // If Insert doesn't return error then returned integer value should be used in subsequent Delete call. + // Insert should return error if context is finished before query can proceed, and integer value returned in this case should be ignored by caller. + Insert(ctx context.Context, query string) (int, error) + + // Delete removes query from activity tracker. InsertIndex is value returned by Insert call. + Delete(insertIndex int) +} + // EngineOpts contains configuration options used when creating a new Engine. type EngineOpts struct { Logger log.Logger Reg prometheus.Registerer MaxSamples int Timeout time.Duration - ActiveQueryTracker *ActiveQueryTracker + ActiveQueryTracker QueryTracker // LookbackDelta determines the time since the last sample after which a time // series is considered stale. LookbackDelta time.Duration @@ -244,7 +263,7 @@ type Engine struct { metrics *engineMetrics timeout time.Duration maxSamplesPerQuery int - activeQueryTracker *ActiveQueryTracker + activeQueryTracker QueryTracker queryLogger QueryLogger queryLoggerLock sync.RWMutex lookbackDelta time.Duration