Skip to content

Query Frontend For Logical Query Plan Support #6884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4451,6 +4451,11 @@ grpc_client_config:
# URL of downstream Prometheus.
# CLI flag: -frontend.downstream-url
[downstream_url: <string> | default = ""]

# Experimental: Enables distributed execution of queries by passing logical
# query plan fragments to downstream components.
# CLI flag: -frontend.distributed_exec_enabled
[distributed_exec_enabled: <boolean> | default = false]
```

### `query_range_config`
Expand Down
3 changes: 2 additions & 1 deletion pkg/cortex/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,13 @@ func (t *Cortex) initQueryFrontendTripperware() (serv services.Service, err erro
prometheusCodec,
shardedPrometheusCodec,
t.Cfg.Querier.LookbackDelta,
t.Cfg.Frontend.DistributedExecEnabled,
)
if err != nil {
return nil, err
}

instantQueryMiddlewares, err := instantquery.Middlewares(util_log.Logger, t.Overrides, instantQueryCodec, queryAnalyzer, t.Cfg.Querier.LookbackDelta)
instantQueryMiddlewares, err := instantquery.Middlewares(util_log.Logger, t.Overrides, instantQueryCodec, queryAnalyzer, t.Cfg.Querier.LookbackDelta, t.Cfg.Frontend.DistributedExecEnabled)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/frontend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type CombinedFrontendConfig struct {
FrontendV1 v1.Config `yaml:",inline"`
FrontendV2 v2.Config `yaml:",inline"`

DownstreamURL string `yaml:"downstream_url"`
DownstreamURL string `yaml:"downstream_url"`
DistributedExecEnabled bool `yaml:"distributed_exec_enabled"`
}

func (cfg *CombinedFrontendConfig) RegisterFlags(f *flag.FlagSet) {
Expand All @@ -29,6 +30,7 @@ func (cfg *CombinedFrontendConfig) RegisterFlags(f *flag.FlagSet) {
cfg.FrontendV2.RegisterFlags(f)

f.StringVar(&cfg.DownstreamURL, "frontend.downstream-url", "", "URL of downstream Prometheus.")
f.BoolVar(&cfg.DistributedExecEnabled, "frontend.distributed_exec_enabled", false, "Experimental: Enables distributed execution of queries by passing logical query plan fragments to downstream components.")
}

// InitFrontend initializes frontend (either V1 -- without scheduler, or V2 -- with scheduler) or no frontend at
Expand Down
26 changes: 24 additions & 2 deletions pkg/querier/tripperware/instantquery/instant_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/limiter"
"github.com/cortexproject/cortex/pkg/util/spanlogger"

"github.com/thanos-io/promql-engine/logicalplan"
)

var (
Expand Down Expand Up @@ -141,6 +143,19 @@ func (c instantQueryCodec) DecodeResponse(ctx context.Context, r *http.Response,
return &resp, nil
}

func (c instantQueryCodec) getSerializedBody(promReq *tripperware.PrometheusRequest) ([]byte, error) {
var byteLP []byte
var err error

if promReq.LogicalPlan != nil {
byteLP, err = logicalplan.Marshal(promReq.LogicalPlan.Root())
if err != nil {
return nil, err
}
}
return byteLP, nil
}

func (c instantQueryCodec) EncodeRequest(ctx context.Context, r tripperware.Request) (*http.Request, error) {
promReq, ok := r.(*tripperware.PrometheusRequest)
if !ok {
Expand Down Expand Up @@ -168,17 +183,24 @@ func (c instantQueryCodec) EncodeRequest(ctx context.Context, r tripperware.Requ
}
}

h.Add("Content-Type", "application/json")

isSourceRuler := strings.Contains(h.Get("User-Agent"), tripperware.RulerUserAgent)
if !isSourceRuler {
// When the source is the Ruler, skip set header
tripperware.SetRequestHeaders(h, c.defaultCodecType, c.compression)
}

byteBody, err := c.getSerializedBody(promReq)
if err != nil {
return nil, err
}

req := &http.Request{
Method: "GET",
Method: "POST",
RequestURI: u.String(), // This is what the httpgrpc code looks at.
URL: u,
Body: http.NoBody,
Body: io.NopCloser(bytes.NewReader(byteBody)),
Header: h,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ func Middlewares(
merger tripperware.Merger,
queryAnalyzer querysharding.Analyzer,
lookbackDelta time.Duration,
distributedExecEnabled bool,
) ([]tripperware.Middleware, error) {
m := []tripperware.Middleware{
NewLimitsMiddleware(limits, lookbackDelta),
tripperware.ShardByMiddleware(log, limits, merger, queryAnalyzer),
}

if distributedExecEnabled {
m = append(m,
tripperware.LogicalPlanGenMiddleware())
}

return m, nil
}
Loading