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

Allow lookups based on short identifiers #575

Merged
merged 17 commits into from
Jan 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
var allocs []string
if !short {
// Query the node allocations
nodeAllocs, _, err := client.Nodes().Allocations(nodeID, nil)
nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
return 1
Expand Down
68 changes: 60 additions & 8 deletions nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,26 @@ func (s *StateStore) UpdateNodeDrain(index uint64, nodeID string, drain bool) er
func (s *StateStore) NodeByID(nodeID string) (*structs.Node, error) {
txn := s.db.Txn(false)

existing, err := txn.First("nodes", "id", nodeID)
existing, err := txn.Find("nodes", "id", nodeID)
if err != nil {
return nil, fmt.Errorf("node lookup failed: %v", err)
}

if existing != nil {
return existing.(*structs.Node), nil
// Return exact match directly
Copy link
Contributor

Choose a reason for hiding this comment

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

So it is best if we revert all these to their original state and then we can add additional methods such as NodeByIDPrefix. These methods can return a memdb.ResultIterator and use memdb.Txn().Get() and use the "_prefix" operator.

We can then add HTTP endpoints that would take a prefix and would call these new methods. And then they could return back a fixed set to the client. This would then let the user refine the search if multiple are returned or if just one is returned, the cli could just call with the full ID.

if len(existing) == 1 {
return existing[0].(*structs.Node), nil
}

// The results were ambiguous for the given node identifier. Return
// an error with possible options so that the user can try again with
// a more specific identifier.
var nodes []string
for _, result := range existing {
node := result.(*structs.Node)
nodes = append(nodes, node.ID)
}
return nil, fmt.Errorf("Ambiguous identifier: %v", nodes)
}
return nil, nil
}
Expand Down Expand Up @@ -336,13 +349,26 @@ func (s *StateStore) DeleteJob(index uint64, jobID string) error {
func (s *StateStore) JobByID(id string) (*structs.Job, error) {
txn := s.db.Txn(false)

existing, err := txn.First("jobs", "id", id)
existing, err := txn.Find("jobs", "id", id)
if err != nil {
return nil, fmt.Errorf("job lookup failed: %v", err)
}

if existing != nil {
return existing.(*structs.Job), nil
// Return exact match directly
if len(existing) == 1 {
return existing[0].(*structs.Job), nil
}

// The results were ambiguous for the given job identifier. Return
// an error with possible options so that the user can try again with
// a more specific identifier.
var jobs []string
for _, result := range existing {
job := result.(*structs.Job)
jobs = append(jobs, job.ID)
}
return nil, fmt.Errorf("Ambiguous identifier: %v", jobs)
}
return nil, nil
}
Expand Down Expand Up @@ -477,13 +503,26 @@ func (s *StateStore) DeleteEval(index uint64, evals []string, allocs []string) e
func (s *StateStore) EvalByID(id string) (*structs.Evaluation, error) {
txn := s.db.Txn(false)

existing, err := txn.First("evals", "id", id)
existing, err := txn.Find("evals", "id", id)
if err != nil {
return nil, fmt.Errorf("eval lookup failed: %v", err)
}

if existing != nil {
return existing.(*structs.Evaluation), nil
// Return exact match directly
if len(existing) == 1 {
return existing[0].(*structs.Evaluation), nil
}

// The results were ambiguous for the given eval identifier. Return
// an error with possible options so that the user can try again with
// a more specific identifier.
var evals []string
for _, result := range existing {
eval := result.(*structs.Evaluation)
evals = append(evals, eval.ID)
}
return nil, fmt.Errorf("Ambiguous identifier: %v", evals)
}
return nil, nil
}
Expand Down Expand Up @@ -625,13 +664,26 @@ func (s *StateStore) UpsertAllocs(index uint64, allocs []*structs.Allocation) er
func (s *StateStore) AllocByID(id string) (*structs.Allocation, error) {
txn := s.db.Txn(false)

existing, err := txn.First("allocs", "id", id)
existing, err := txn.Find("allocs", "id", id)
if err != nil {
return nil, fmt.Errorf("alloc lookup failed: %v", err)
}

if existing != nil {
return existing.(*structs.Allocation), nil
// Return exact match directly
if len(existing) == 1 {
return existing[0].(*structs.Allocation), nil
}

// The results were ambiguous for the given job identifier. Return
// an error with possible options so that the user can try again with
// a more specific identifier.
var allocs []string
for _, result := range existing {
alloc := result.(*structs.Allocation)
allocs = append(allocs, alloc.ID)
}
return nil, fmt.Errorf("Ambiguous identifier: %v", allocs)
}
return nil, nil
}
Expand Down