-
Notifications
You must be signed in to change notification settings - Fork 2k
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
autocomplete api #2964
autocomplete api #2964
Conversation
requires further refactoring and logic for more contexts
makes context singular
adding nodes to resources list endpoint
set the index for a resources response
add test for when no prefixes are matched add test for no context at HTTP api
extract magic number into variable
add tests for edge cases
nomad/resources_endpoint.go
Outdated
iters := make(map[string]memdb.ResultIterator) | ||
|
||
if args.Context != "" { | ||
iter, err := getResourceIter(args.Context, args.Prefix, ws, state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Top of file
var (
allContexts = []string{"allocs", "nodes", "jobs", "evals"}
)
contexts := allContexts
if args.Context != "" {
contexts = []string{args.Context}
}
for _, e := range contexts {
iter, err := getResourceIter(e, args.Prefix, ws, state)
if err != nil {
return err
}
iters[e] = iter
}
nomad/resources_endpoint.go
Outdated
case *structs.Node: | ||
id = raw.(*structs.Node).ID | ||
default: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log an error here
nomad/resources_endpoint.go
Outdated
// is the only non-empty match set, and the index is set for it. | ||
// If the context was not specified, we set the index of the first | ||
// non-empty match set. | ||
for k, v := range reply.Matches { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go through each context and call state.Index on the context and then take the Max of all the returned indexes.
nomad/resources_endpoint.go
Outdated
case "nodes": | ||
return state.NodesByIDPrefix(ws, prefix) | ||
default: | ||
return nil, fmt.Errorf("invalid context") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt.Errorf("context must be one of %v; got %q", allContexts, context)
nomad/structs/structs.go
Outdated
@@ -231,6 +231,22 @@ type NodeSpecificRequest struct { | |||
QueryOptions | |||
} | |||
|
|||
// ResourcesResponse is used to return matches and information about whether | |||
// the match list is truncated specific to each type of context. | |||
type ResourcesResponse struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResourceListResponse
nomad/structs/structs.go
Outdated
// ResourcesResponse is used to return matches and information about whether | ||
// the match list is truncated specific to each type of context. | ||
type ResourcesResponse struct { | ||
Matches map[string][]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put a comment please
respW := httptest.NewRecorder() | ||
|
||
resp, err := s.Server.ResourcesRequest(respW, req) | ||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.Nil
|
||
res := resp.(structs.ResourcesResponse) | ||
|
||
assert.Equal(t, 1, len(res.Matches)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert := assert.New(t) and then use that
}) | ||
} | ||
|
||
func TestHTTP_Resources_NoContext(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably test more than just the job is returned
}) | ||
} | ||
|
||
func TestHTTP_ResoucesList_Evaluation(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allocs and nodes?
add http tests for remaining contexts
nomad/resources_endpoint.go
Outdated
case *structs.Node: | ||
id = raw.(*structs.Node).ID | ||
default: | ||
//s.logger.Printf("[ERR] nomad: unexpected type for resources context; not a job, allocation, node, or evaluation") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can either make it a method of Resources
or have it return an error. Also we should just use the variable so that if contexts get added it will automatically propagate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ERR] nomad.resources:
nomad/resources_endpoint.go
Outdated
// If the context was not specified, we set the index to be the max index | ||
// from available contexts | ||
reply.Index = 0 | ||
for k, v := range reply.Matches { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to iterate over the matches. I think you want to do it over the contexts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the max index be used even if the specified prefix does not have any matches for that particular context?
nomad/structs/structs.go
Outdated
// Prefix is what resources are matched to. I.e, if the given prefix were | ||
// "a", potential matches might be "abcd" or "aabb" | ||
Prefix string | ||
// Context is the resource that can be matched. A context can be a job, node, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
White space above
nomad/resources_endpoint_test.go
Outdated
assert.Equal(uint64(1000), resp.Index) | ||
} | ||
|
||
//// Tests that the top 20 matches are returned when no prefix is set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double comments?
nomad/resources_endpoint.go
Outdated
@@ -47,8 +47,7 @@ func getMatches(iter memdb.ResultIterator) ([]string, bool) { | |||
case *structs.Node: | |||
id = raw.(*structs.Node).ID | |||
default: | |||
//s.logger.Printf("[ERR] nomad: unexpected type for resources context; not a job, allocation, node, or evaluation") | |||
// TODO | |||
r.srv.logger.Printf("[ERR] nomad.resources: unexpected type for resources context; %T \n", t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change ;
to :
just for consistency and you also don't need the \n
. The logger adds it automatically.
nomad/resources_endpoint.go
Outdated
isTruncated = true | ||
} | ||
|
||
return matches, isTruncated |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An option that you don't have to take:
return matches, iter.Next() != nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks great, adding it.
nomad/resources_endpoint.go
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already initialized at zero
daef46f
to
440fea7
Compare
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
Adds an HTTP api which acceps a prefix and a context, which can be jobs, evaluations, allocations, or nodes. If a context is not set, all types are returned that match the given prefix.
This will be used to extend autocomplete functionality for the CLI.