Skip to content
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

Merged
merged 27 commits into from
Aug 7, 2017
Merged

autocomplete api #2964

merged 27 commits into from
Aug 7, 2017

Conversation

chelseakomlo
Copy link
Contributor

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.

iters := make(map[string]memdb.ResultIterator)

if args.Context != "" {
iter, err := getResourceIter(args.Context, args.Prefix, ws, state)
Copy link
Contributor

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
}

case *structs.Node:
id = raw.(*structs.Node).ID
default:
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log an error here

// 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 {
Copy link
Contributor

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.

case "nodes":
return state.NodesByIDPrefix(ws, prefix)
default:
return nil, fmt.Errorf("invalid context")
Copy link
Contributor

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)

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResourceListResponse

// 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
Copy link
Contributor

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 {
Copy link
Contributor

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))
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allocs and nodes?

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")
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ERR] nomad.resources:

// 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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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?

// 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

White space above

assert.Equal(uint64(1000), resp.Index)
}

//// Tests that the top 20 matches are returned when no prefix is set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double comments?

@@ -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)
Copy link
Contributor

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.

isTruncated = true
}

return matches, isTruncated
Copy link
Contributor

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

Copy link
Contributor Author

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.

// 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
Copy link
Contributor

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

@chelseakomlo chelseakomlo merged commit a768eca into master Aug 7, 2017
@chelseakomlo chelseakomlo deleted the f-autocomplete-api branch August 7, 2017 19:40
@github-actions
Copy link

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.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants