consul: support PreferLocal in PQ's

This commit is contained in:
Ryan Uber 2016-06-20 14:07:08 -07:00
parent 055b6973f6
commit 1fef85cd2e
2 changed files with 20 additions and 0 deletions

View File

@ -372,6 +372,17 @@ func (p *PreparedQuery) Execute(args *structs.PreparedQueryExecuteRequest,
return err return err
} }
// Prefer the local service if it exists and is in the set.
if query.Service.PreferLocal {
for i, node := range reply.Nodes {
if node.Node.Node == args.Origin {
remote := append(reply.Nodes[:i], reply.Nodes[i+1:]...)
reply.Nodes = append([]structs.CheckServiceNode{node}, remote...)
break
}
}
}
// Apply the limit if given. // Apply the limit if given.
if args.Limit > 0 && len(reply.Nodes) > args.Limit { if args.Limit > 0 && len(reply.Nodes) > args.Limit {
reply.Nodes = reply.Nodes[:args.Limit] reply.Nodes = reply.Nodes[:args.Limit]

View File

@ -34,6 +34,11 @@ type ServiceQuery struct {
// discarded) // discarded)
OnlyPassing bool OnlyPassing bool
// If PreferLocal is true, the local agent will be checked for a local
// copy of the service before continuing to remote machines. This is
// useful to prefer colocated services but fall back when unavailable.
PreferLocal bool
// Tags are a set of required and/or disallowed tags. If a tag is in // Tags are a set of required and/or disallowed tags. If a tag is in
// this list it must be present. If the tag is preceded with "!" then // this list it must be present. If the tag is preceded with "!" then
// it is disallowed. // it is disallowed.
@ -163,6 +168,10 @@ func (q *PreparedQuerySpecificRequest) RequestDatacenter() string {
// PreparedQueryExecuteRequest is used to execute a prepared query. // PreparedQueryExecuteRequest is used to execute a prepared query.
type PreparedQueryExecuteRequest struct { type PreparedQueryExecuteRequest struct {
// Origin is used to carry around a reference to the node which
// is executing the query on behalf of the client.
Origin string
// Datacenter is the target this request is intended for. // Datacenter is the target this request is intended for.
Datacenter string Datacenter string