Moves disable checks down into the sort routine.

This commit is contained in:
James Phillips 2015-07-24 12:53:50 -07:00
parent 54ef97b268
commit 78b2c2d7ac
2 changed files with 21 additions and 13 deletions

View File

@ -105,13 +105,11 @@ func (c *Catalog) ListDatacenters(args *struct{}, reply *[]string) error {
dcs = append(dcs, dc) dcs = append(dcs, dc)
} }
// Sort the DCs // Sort the DCs by name first, then apply a stable sort by distance.
sort.Strings(dcs) sort.Strings(dcs)
if !c.srv.config.DisableCoordinates {
if err := c.srv.sortDatacentersByDistance(dcs); err != nil { if err := c.srv.sortDatacentersByDistance(dcs); err != nil {
return err return err
} }
}
// Return // Return
*reply = dcs *reply = dcs
@ -137,9 +135,6 @@ func (c *Catalog) ListNodes(args *structs.DCSpecificRequest, reply *structs.Inde
} }
reply.Index, reply.Nodes = index, nodes reply.Index, reply.Nodes = index, nodes
if c.srv.config.DisableCoordinates {
return nil
}
return c.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes) return c.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
}) })
} }
@ -200,9 +195,6 @@ func (c *Catalog) ServiceNodes(args *structs.ServiceSpecificRequest, reply *stru
if err := c.srv.filterACL(args.Token, reply); err != nil { if err := c.srv.filterACL(args.Token, reply); err != nil {
return err return err
} }
if c.srv.config.DisableCoordinates {
return nil
}
return c.srv.sortNodesByDistanceFrom(args.Source, reply.ServiceNodes) return c.srv.sortNodesByDistanceFrom(args.Source, reply.ServiceNodes)
}) })

View File

@ -113,8 +113,15 @@ func (s *Server) newSorterByDistanceFrom(c *coordinate.Coordinate, subj interfac
// sortNodesByDistanceFrom is used to sort results from our service catalog based // sortNodesByDistanceFrom is used to sort results from our service catalog based
// on the round trip time from the given source node. Nodes with missing coordinates // on the round trip time from the given source node. Nodes with missing coordinates
// will get stable sorted at the end of the list. // will get stable sorted at the end of the list.
//
// If coordinates are disabled this will be a no-op.
func (s *Server) sortNodesByDistanceFrom(source structs.QuerySource, subj interface{}) error { func (s *Server) sortNodesByDistanceFrom(source structs.QuerySource, subj interface{}) error {
// Make it safe to call this without having to check if coordinates are
// disabled first.
if s.config.DisableCoordinates {
return nil
}
// We can't compare coordinates across DCs. // We can't compare coordinates across DCs.
if source.Datacenter != s.config.Datacenter { if source.Datacenter != s.config.Datacenter {
return nil return nil
@ -131,7 +138,7 @@ func (s *Server) sortNodesByDistanceFrom(source structs.QuerySource, subj interf
return nil return nil
} }
// Do the Dew! // Do the sort!
sorter, err := s.newSorterByDistanceFrom(coord, subj) sorter, err := s.newSorterByDistanceFrom(coord, subj)
if err != nil { if err != nil {
return err return err
@ -182,7 +189,16 @@ func (s *serverSerfer) GetNodesForDatacenter(dc string) []string {
// sortDatacentersByDistance will sort the given list of DCs based on the // sortDatacentersByDistance will sort the given list of DCs based on the
// median RTT to all nodes we know about from the WAN gossip pool). DCs with // median RTT to all nodes we know about from the WAN gossip pool). DCs with
// missing coordinates will be stable sorted to the end of the list. // missing coordinates will be stable sorted to the end of the list.
//
// If coordinates are disabled this will be a no-op.
func (s *Server) sortDatacentersByDistance(dcs []string) error { func (s *Server) sortDatacentersByDistance(dcs []string) error {
// Make it safe to call this without having to check if coordinates are
// disabled first.
if s.config.DisableCoordinates {
return nil
}
// Do the sort!
serfer := serverSerfer{s} serfer := serverSerfer{s}
return sortDatacentersByDistance(&serfer, dcs) return sortDatacentersByDistance(&serfer, dcs)
} }