Skip to content

Commit

Permalink
promql.Engine: Add Close method
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
  • Loading branch information
aknuds1 committed May 9, 2024
1 parent aabe4d6 commit 161649a
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 91 deletions.
3 changes: 3 additions & 0 deletions cmd/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,14 @@ func main() {
listener, err := webHandler.Listener()
if err != nil {
level.Error(logger).Log("msg", "Unable to start web listener", "err", err)
_ = queryEngine.Close()
os.Exit(1)
}

err = toolkit_web.Validate(*webConfig)
if err != nil {
level.Error(logger).Log("msg", "Unable to validate web configuration file", "err", err)
_ = queryEngine.Close()
os.Exit(1)
}

Expand All @@ -969,6 +971,7 @@ func main() {
case <-cancel:
reloadReady.Close()
}
_ = queryEngine.Close()
return nil
},
func(err error) {
Expand Down
8 changes: 8 additions & 0 deletions promql/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql"
Expand Down Expand Up @@ -257,6 +259,9 @@ func BenchmarkRangeQuery(b *testing.B) {
Timeout: 100 * time.Second,
}
engine := promql.NewEngine(opts)
b.Cleanup(func() {
require.NoError(b, engine.Close())
})

const interval = 10000 // 10s interval.
// A day of data plus 10k steps.
Expand Down Expand Up @@ -340,6 +345,9 @@ func BenchmarkNativeHistograms(b *testing.B) {
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
ng := promql.NewEngine(opts)
b.Cleanup(func() {
require.NoError(b, ng.Close())
})
for i := 0; i < b.N; i++ {
qry, err := ng.NewRangeQuery(context.Background(), testStorage, nil, tc.query, start, end, step)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions promql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"errors"
"fmt"
"io"
"math"
"reflect"
"runtime"
Expand Down Expand Up @@ -271,6 +272,8 @@ func contextErr(err error, env string) error {
//
// 2) Enforcement of the maximum number of concurrent queries.
type QueryTracker interface {
io.Closer

// GetMaxConcurrent returns maximum number of concurrent queries that are allowed by this tracker.
GetMaxConcurrent() int

Expand Down Expand Up @@ -423,6 +426,11 @@ func NewEngine(opts EngineOpts) *Engine {
}
}

// Close closes ng.
func (ng *Engine) Close() error {
return ng.activeQueryTracker.Close()
}

// SetQueryLogger sets the query logger.
func (ng *Engine) SetQueryLogger(l QueryLogger) {
ng.queryLoggerLock.Lock()
Expand Down

0 comments on commit 161649a

Please sign in to comment.