Skip to content

Commit

Permalink
Skip regex compile if it is match all
Browse files Browse the repository at this point in the history
  • Loading branch information
justinjung04 committed Nov 1, 2023
1 parent e8bd7d0 commit 280dbab
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
4 changes: 0 additions & 4 deletions pkg/frontend/transport/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package transport
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -39,12 +37,10 @@ func (b *buffer) Bytes() []byte {
}

func (a *grpcRoundTripperAdapter) RoundTrip(r *http.Request) (*http.Response, error) {
regexp.MustCompile("str")
req, err := server.HTTPRequest(r)
if err != nil {
return nil, err
}
fmt.Println(r.URL.Path)

var (
resp *httpgrpc.HTTPResponse
Expand Down
2 changes: 1 addition & 1 deletion pkg/scheduler/queue/user_queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Limits interface {
// of outstanding requests per tenant per request queue.
MaxOutstandingPerTenant(user string) int

// QueryPriority returns query priority config for the tenant, including different priorities,
// QueryPriority returns query priority config for the tenant, including priority level,
// their attributes, and how many reserved queriers each priority has.
QueryPriority(user string) validation.QueryPriority
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/query/priority.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetPriority(requestParams url.Values, now time.Time, queryPriority validati
for _, attribute := range priority.QueryAttributes {
compiledRegex := attribute.CompiledRegex

if compiledRegex == nil || !compiledRegex.MatchString(queryParam) {
if compiledRegex != nil && !compiledRegex.MatchString(queryParam) {
continue
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/util/validation/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ type DisabledRuleGroup struct {
type DisabledRuleGroups []DisabledRuleGroup

type QueryPriority struct {
Enabled bool `yaml:"enabled" doc:"nocli|description=Whether queries are assigned with priorities."`
DefaultPriority int64 `yaml:"default_priority" doc:"nocli|description=Priority assigned to all queries by default. Use this as a baseline to make certain queries higher/lower priority.|default=1"`
Enabled bool `yaml:"enabled" doc:"nocli|description=Whether queries are assigned with priorities.|default=false"`
DefaultPriority int64 `yaml:"default_priority" doc:"nocli|description=Priority assigned to all queries by default. Must be a unique value. Use this as a baseline to make certain queries higher/lower priority.|default=1"`
Priorities []PriorityDef `yaml:"priorities" doc:"nocli|description=List of priority definitions."`
RegexCompiled bool `yaml:"-" doc:"nocli"`
}

type PriorityDef struct {
Priority int64 `yaml:"priority" doc:"nocli|description=Priority level."`
ReservedQueriers float64 `yaml:"reserved_queriers" doc:"nocli|description=Number of reserved queriers to handle this priority only. Value between 0 and 1 will be used as a percentage."`
Priority int64 `yaml:"priority" doc:"nocli|description=Priority level. Must be a unique value.|default=2"`
ReservedQueriers float64 `yaml:"reserved_queriers" doc:"nocli|description=Number of reserved queriers to handle this priority only. Value between 0 and 1 will be used as a percentage.|default=0"`
QueryAttributes []QueryAttribute `yaml:"query_attributes" doc:"nocli|description=List of query attributes to assign the priority."`
}

type QueryAttribute struct {
Regex string `yaml:"regex" doc:"nocli|description=Query string regex. If evaluated true (on top of meeting all other criteria), query is treated as a high priority."`
Regex string `yaml:"regex" doc:"nocli|description=Query string regex. If evaluated true (on top of meeting all other criteria), query is treated as a high priority.|default=.*"`
CompiledRegex *regexp.Regexp `yaml:"-" doc:"nocli"`
StartTime time.Duration `yaml:"start_time" doc:"nocli|description=If query range falls between the start_time and end_time (on top of meeting all other criteria), query is treated as a high priority.|default=0s"`
EndTime time.Duration `yaml:"end_time" doc:"nocli|description=If query range falls between the start_time and end_time (on top of meeting all other criteria), query is treated as a high priority.|default=0s"`
Expand Down Expand Up @@ -520,6 +520,9 @@ func (o *Overrides) QueryPriority(userID string) QueryPriority {
priorities := o.GetOverridesForUser(userID).QueryPriority.Priorities
for i, priority := range priorities {
for j, attributes := range priority.QueryAttributes {
if attributes.Regex == "" || attributes.Regex == ".*" || attributes.Regex == ".+" {
continue // don't use regex at all, if it is match all
}
o.GetOverridesForUser(userID).QueryPriority.Priorities[i].QueryAttributes[j].CompiledRegex, _ = regexp.Compile(attributes.Regex)
}
}
Expand Down

0 comments on commit 280dbab

Please sign in to comment.