Skip to content

Commit

Permalink
use state store index if possible when filtering list results
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Feb 11, 2022
1 parent 92280bd commit e1c5dbd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
24 changes: 24 additions & 0 deletions nomad/deployment_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,30 @@ func (d *Deployment) List(args *structs.DeploymentListRequest, reply *structs.De
return structs.ErrIncompatibleFiltering
}

// Use state store index if possible.
useIndex := false
matches := listFilterSimpleEqRegex.FindStringSubmatch(args.Filter)

if len(matches) == 3 {
prop := matches[1]
value := matches[2]

switch prop {
case "ID":
useIndex = true
args.Prefix = value
case "Namespace":
useIndex = true
args.Namespace = value
}
}

if useIndex {
args.Filter = ""
} else {
// Use wildcards namespace to apply filter to all evals.
args.Namespace = "*"
}
// Use wildcards namespace to apply filter to all deployments.
args.Namespace = "*"
}
Expand Down
26 changes: 24 additions & 2 deletions nomad/eval_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,30 @@ func (e *Eval) List(args *structs.EvalListRequest,
return structs.ErrIncompatibleFiltering
}

// Use wildcards namespace to apply filter to all evals.
args.Namespace = "*"
// Use state store index if possible.
useIndex := false
matches := listFilterSimpleEqRegex.FindStringSubmatch(args.Filter)

if len(matches) == 3 {
prop := matches[1]
value := matches[2]

switch prop {
case "ID":
useIndex = true
args.Prefix = value
case "Namespace":
useIndex = true
args.Namespace = value
}
}

if useIndex {
args.Filter = ""
} else {
// Use wildcards namespace to apply filter to all evals.
args.Namespace = "*"
}
}

// Setup the blocking query
Expand Down
6 changes: 6 additions & 0 deletions nomad/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
"regexp"
"strconv"

memdb "github.com/hashicorp/go-memdb"
Expand All @@ -20,6 +21,11 @@ import (
// in ApplyPlanResultsRequest
var MinVersionPlanNormalization = version.Must(version.NewVersion("0.9.2"))

// listFilterSimpleEqRegex matches go-bexpr filters with a single equality
// check of a string value. It is used to determine if a state store index can
// be used rather than filtering elements individually.
var listFilterSimpleEqRegex = regexp.MustCompile(`^(.+)\s+==\s+"(.+)"$`)

// ensurePath is used to make sure a path exists
func ensurePath(path string, dir bool) error {
if !dir {
Expand Down

0 comments on commit e1c5dbd

Please sign in to comment.