2016-04-13 14:08:22 +00:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
package promql
|
|
|
|
|
|
|
|
import (
|
2015-08-19 13:28:53 +00:00
|
|
|
"fmt"
|
2015-03-30 17:13:36 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2015-09-23 09:06:51 +00:00
|
|
|
func TestQueryConcurrency(t *testing.T) {
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := NewEngine(nil, nil)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-04-30 22:49:19 +00:00
|
|
|
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-08-10 12:21:24 +00:00
|
|
|
|
|
|
|
f := func(context.Context) error {
|
2015-04-30 22:49:19 +00:00
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return nil
|
2015-08-10 12:21:24 +00:00
|
|
|
}
|
2015-04-30 22:49:19 +00:00
|
|
|
|
2015-06-15 10:49:11 +00:00
|
|
|
for i := 0; i < DefaultEngineOptions.MaxConcurrentQueries; i++ {
|
2015-08-10 12:21:24 +00:00
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
go q.Exec(ctx)
|
2015-04-30 22:49:19 +00:00
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
q := engine.newTestQuery(f)
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
go q.Exec(ctx)
|
2015-04-30 22:49:19 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
2016-07-11 18:27:25 +00:00
|
|
|
t.Fatalf("Query above concurrency threshold being executed")
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
// Expected.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate a running query.
|
|
|
|
block <- struct{}{}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-processing:
|
|
|
|
// Expected.
|
2015-09-23 09:06:51 +00:00
|
|
|
case <-time.After(20 * time.Millisecond):
|
2015-04-30 22:49:19 +00:00
|
|
|
t.Fatalf("Query within concurrency threshold not being executed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate remaining queries.
|
2015-06-15 10:49:11 +00:00
|
|
|
for i := 0; i < DefaultEngineOptions.MaxConcurrentQueries; i++ {
|
2015-04-30 22:49:19 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
func TestQueryTimeout(t *testing.T) {
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := NewEngine(nil, &EngineOptions{
|
|
|
|
Timeout: 5 * time.Millisecond,
|
|
|
|
MaxConcurrentQueries: 20,
|
|
|
|
})
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query := engine.newTestQuery(func(ctx context.Context) error {
|
2015-09-14 10:28:27 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-08-10 12:21:24 +00:00
|
|
|
return contextDone(ctx, "test statement execution")
|
2015-03-30 17:13:36 +00:00
|
|
|
})
|
|
|
|
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res := query.Exec(ctx)
|
2015-03-30 17:13:36 +00:00
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected timeout error but got none")
|
|
|
|
}
|
|
|
|
if _, ok := res.Err.(ErrQueryTimeout); res.Err != nil && !ok {
|
|
|
|
t.Fatalf("expected timeout error but got: %s", res.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryCancel(t *testing.T) {
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := NewEngine(nil, nil)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
|
|
|
defer cancelCtx()
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
// Cancel a running query before it completes.
|
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query1 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
2015-04-29 09:08:56 +00:00
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
var res *Result
|
|
|
|
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query1.Exec(ctx)
|
2015-08-10 12:21:24 +00:00
|
|
|
processing <- struct{}{}
|
2015-03-30 17:13:36 +00:00
|
|
|
}()
|
2015-08-10 12:21:24 +00:00
|
|
|
|
|
|
|
<-processing
|
2015-03-30 17:13:36 +00:00
|
|
|
query1.Cancel()
|
2015-08-10 12:21:24 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
2015-03-30 17:13:36 +00:00
|
|
|
|
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected cancellation error for query1 but got none")
|
|
|
|
}
|
2015-08-10 12:21:24 +00:00
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
2015-08-26 00:04:01 +00:00
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
// Canceling a query before starting it must have no effect.
|
|
|
|
query2 := engine.newTestQuery(func(ctx context.Context) error {
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
})
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
query2.Cancel()
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query2.Exec(ctx)
|
2015-03-30 17:13:36 +00:00
|
|
|
if res.Err != nil {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
t.Fatalf("unexpected error on executing query2: %s", res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEngineShutdown(t *testing.T) {
|
2015-06-15 10:49:11 +00:00
|
|
|
engine := NewEngine(nil, nil)
|
2016-09-15 22:58:06 +00:00
|
|
|
ctx, cancelCtx := context.WithCancel(context.Background())
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
block := make(chan struct{})
|
|
|
|
processing := make(chan struct{})
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
// Shutdown engine on first handler execution. Should handler execution ever become
|
|
|
|
// concurrent this test has to be adjusted accordingly.
|
2015-08-10 12:21:24 +00:00
|
|
|
f := func(ctx context.Context) error {
|
|
|
|
processing <- struct{}{}
|
|
|
|
<-block
|
|
|
|
return contextDone(ctx, "test statement execution")
|
|
|
|
}
|
|
|
|
query1 := engine.newTestQuery(f)
|
2015-03-30 17:13:36 +00:00
|
|
|
|
2015-04-29 09:08:56 +00:00
|
|
|
// Stopping the engine must cancel the base context. While executing queries is
|
|
|
|
// still possible, their context is canceled from the beginning and execution should
|
2015-03-30 17:13:36 +00:00
|
|
|
// terminate immediately.
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
var res *Result
|
|
|
|
go func() {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res = query1.Exec(ctx)
|
2015-08-10 12:21:24 +00:00
|
|
|
processing <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-processing
|
2016-09-15 22:58:06 +00:00
|
|
|
cancelCtx()
|
2015-08-10 12:21:24 +00:00
|
|
|
block <- struct{}{}
|
|
|
|
<-processing
|
|
|
|
|
2015-03-30 17:13:36 +00:00
|
|
|
if res.Err == nil {
|
|
|
|
t.Fatalf("expected error on shutdown during query but got none")
|
|
|
|
}
|
2015-08-10 12:21:24 +00:00
|
|
|
if ee := ErrQueryCanceled("test statement execution"); res.Err != ee {
|
|
|
|
t.Fatalf("expected error %q, got %q", ee, res.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 12:21:24 +00:00
|
|
|
query2 := engine.newTestQuery(func(context.Context) error {
|
|
|
|
t.Fatalf("reached query execution unexpectedly")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// The second query is started after the engine shut down. It must
|
|
|
|
// be canceled immediately.
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
res2 := query2.Exec(ctx)
|
2015-03-30 17:13:36 +00:00
|
|
|
if res2.Err == nil {
|
promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
2016-09-15 11:52:50 +00:00
|
|
|
t.Fatalf("expected error on querying with canceled context but got none")
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
2015-08-10 12:21:24 +00:00
|
|
|
if _, ok := res2.Err.(ErrQueryCanceled); !ok {
|
|
|
|
t.Fatalf("expected cancelation error, got %q", res2.Err)
|
2015-03-30 17:13:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-19 13:28:53 +00:00
|
|
|
|
|
|
|
func TestRecoverEvaluatorRuntime(t *testing.T) {
|
|
|
|
var ev *evaluator
|
|
|
|
var err error
|
|
|
|
defer ev.recover(&err)
|
|
|
|
|
|
|
|
// Cause a runtime panic.
|
|
|
|
var a []int
|
|
|
|
a[123] = 1
|
|
|
|
|
|
|
|
if err.Error() != "unexpected error" {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, "unexpected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoverEvaluatorError(t *testing.T) {
|
|
|
|
var ev *evaluator
|
|
|
|
var err error
|
|
|
|
|
|
|
|
e := fmt.Errorf("custom error")
|
|
|
|
|
2015-08-26 00:04:01 +00:00
|
|
|
defer func() {
|
|
|
|
if err.Error() != e.Error() {
|
|
|
|
t.Fatalf("wrong error message: %q, expected %q", err, e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer ev.recover(&err)
|
|
|
|
|
|
|
|
panic(e)
|
2015-08-19 13:28:53 +00:00
|
|
|
}
|