open-nomad/nomad/resources_endpoint.go

135 lines
3.4 KiB
Go
Raw Normal View History

package nomad
import (
"fmt"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
)
// truncateLimit is the maximum number of matches that will be returned for a
// prefix for a specific context
2017-08-04 22:18:49 +00:00
const (
truncateLimit = 20
)
// allContexts are the available contexts which searched to find matches for a
// given prefix
var (
allContexts = []string{"allocs", "nodes", "jobs", "evals"}
)
// Resource endpoint is used to lookup matches for a given prefix and context
type Resources struct {
srv *Server
}
// getMatches extracts matches for an iterator, and returns a list of ids for
// these matches.
2017-08-07 14:16:24 +00:00
func (r *Resources) getMatches(iter memdb.ResultIterator) ([]string, bool) {
var matches []string
isTruncated := false
for i := 0; i < truncateLimit; i++ {
raw := iter.Next()
if raw == nil {
break
}
2017-08-04 15:08:12 +00:00
var id string
2017-08-07 14:16:24 +00:00
switch t := raw.(type) {
2017-08-04 15:08:12 +00:00
case *structs.Job:
id = raw.(*structs.Job).ID
case *structs.Evaluation:
id = raw.(*structs.Evaluation).ID
case *structs.Allocation:
id = raw.(*structs.Allocation).ID
case *structs.Node:
id = raw.(*structs.Node).ID
default:
2017-08-07 14:16:24 +00:00
r.srv.logger.Printf("[ERR] nomad.resources: unexpected type for resources context; %T \n", t)
continue
}
matches = append(matches, id)
}
if iter.Next() != nil {
isTruncated = true
}
return matches, isTruncated
}
// getResourceIter takes a context and returns a memdb iterator specific to
// that context
func getResourceIter(context, prefix string, ws memdb.WatchSet, state *state.StateStore) (memdb.ResultIterator, error) {
switch context {
case "jobs":
return state.JobsByIDPrefix(ws, prefix)
case "evals":
return state.EvalsByIDPrefix(ws, prefix)
case "allocs":
return state.AllocsByIDPrefix(ws, prefix)
case "nodes":
return state.NodesByIDPrefix(ws, prefix)
default:
2017-08-04 22:18:49 +00:00
return nil, fmt.Errorf("context must be one of %v; got %q", allContexts, context)
}
}
// List is used to list the resouces registered in the system that matches the
// given prefix. Resources are jobs, evaluations, allocations, and/or nodes.
2017-08-04 22:18:49 +00:00
func (r *Resources) List(args *structs.ResourceListRequest,
reply *structs.ResourceListResponse) error {
reply.Matches = make(map[string][]string)
reply.Truncations = make(map[string]bool)
// Setup the blocking query
opts := blockingOptions{
queryMeta: &reply.QueryMeta,
queryOpts: &structs.QueryOptions{},
run: func(ws memdb.WatchSet, state *state.StateStore) error {
iters := make(map[string]memdb.ResultIterator)
2017-08-04 22:18:49 +00:00
contexts := allContexts
if args.Context != "" {
2017-08-04 22:18:49 +00:00
contexts = []string{args.Context}
}
for _, e := range contexts {
iter, err := getResourceIter(e, args.Prefix, ws, state)
if err != nil {
return err
}
2017-08-04 22:18:49 +00:00
iters[e] = iter
}
// Return matches for the given prefix
for k, v := range iters {
2017-08-07 14:16:24 +00:00
res, isTrunc := r.getMatches(v)
reply.Matches[k] = res
reply.Truncations[k] = isTrunc
}
2017-08-04 15:08:12 +00:00
// Set the index for the context. If the context has been specified, it
// will be used as the index of the response. Otherwise, the
// maximum index from all resources will be used.
reply.Index = 0
for _, e := range contexts {
index, err := state.Index(e)
if err != nil {
return err
}
if index > reply.Index {
reply.Index = index
}
}
r.srv.setQueryMeta(&reply.QueryMeta)
return nil
}}
return r.srv.blockingRPC(&opts)
}