mirror of https://github.com/status-im/consul.git
Moves sorting up into coordinate endpoint HTTP handlers.
This commit is contained in:
parent
d45fc23abf
commit
6289764ea2
|
@ -3,6 +3,7 @@ package agent
|
||||||
import (
|
import (
|
||||||
"github.com/hashicorp/consul/consul/structs"
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// coordinateDisabled handles all the endpoints when coordinates are not enabled,
|
// coordinateDisabled handles all the endpoints when coordinates are not enabled,
|
||||||
|
@ -13,11 +14,35 @@ func coordinateDisabled(resp http.ResponseWriter, req *http.Request) (interface{
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sorter wraps a coordinate list and implements the sort.Interface to sort by
|
||||||
|
// node name.
|
||||||
|
type sorter struct {
|
||||||
|
coordinates []structs.Coordinate
|
||||||
|
}
|
||||||
|
|
||||||
|
// See sort.Interface.
|
||||||
|
func (s *sorter) Len() int {
|
||||||
|
return len(s.coordinates)
|
||||||
|
}
|
||||||
|
|
||||||
|
// See sort.Interface.
|
||||||
|
func (s *sorter) Swap(i, j int) {
|
||||||
|
s.coordinates[i], s.coordinates[j] = s.coordinates[j], s.coordinates[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// See sort.Interface.
|
||||||
|
func (s *sorter) Less(i, j int) bool {
|
||||||
|
return s.coordinates[i].Node < s.coordinates[j].Node
|
||||||
|
}
|
||||||
|
|
||||||
// CoordinateDatacenters returns the WAN nodes in each datacenter, along with
|
// CoordinateDatacenters returns the WAN nodes in each datacenter, along with
|
||||||
// raw network coordinates.
|
// raw network coordinates.
|
||||||
func (s *HTTPServer) CoordinateDatacenters(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
func (s *HTTPServer) CoordinateDatacenters(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
var out []structs.DatacenterMap
|
var out []structs.DatacenterMap
|
||||||
if err := s.agent.RPC("Coordinate.ListDatacenters", struct{}{}, &out); err != nil {
|
if err := s.agent.RPC("Coordinate.ListDatacenters", struct{}{}, &out); err != nil {
|
||||||
|
for i := range out {
|
||||||
|
sort.Sort(&sorter{out[i].Coordinates})
|
||||||
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out, nil
|
return out, nil
|
||||||
|
@ -34,6 +59,7 @@ func (s *HTTPServer) CoordinateNodes(resp http.ResponseWriter, req *http.Request
|
||||||
var out structs.IndexedCoordinates
|
var out structs.IndexedCoordinates
|
||||||
defer setMeta(resp, &out.QueryMeta)
|
defer setMeta(resp, &out.QueryMeta)
|
||||||
if err := s.agent.RPC("Coordinate.ListNodes", &args, &out); err != nil {
|
if err := s.agent.RPC("Coordinate.ListNodes", &args, &out); err != nil {
|
||||||
|
sort.Sort(&sorter{out.Coordinates})
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return out.Coordinates, nil
|
return out.Coordinates, nil
|
||||||
|
|
|
@ -70,7 +70,7 @@ func TestCoordinate_Nodes(t *testing.T) {
|
||||||
}
|
}
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
// Query back and check the nodes are present.
|
// Query back and check the nodes are present and sorted correctly.
|
||||||
req, err := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
|
req, err := http.NewRequest("GET", "/v1/coordinate/nodes?dc=dc1", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
|
|
|
@ -135,27 +135,6 @@ func (c *Coordinate) Get(args *structs.NodeSpecificRequest,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// sorter wraps a coordinate list and implements the sort.Interface to sort by
|
|
||||||
// node name.
|
|
||||||
type sorter struct {
|
|
||||||
coordinates []structs.Coordinate
|
|
||||||
}
|
|
||||||
|
|
||||||
// See sort.Interface.
|
|
||||||
func (s *sorter) Len() int {
|
|
||||||
return len(s.coordinates)
|
|
||||||
}
|
|
||||||
|
|
||||||
// See sort.Interface.
|
|
||||||
func (s *sorter) Swap(i, j int) {
|
|
||||||
s.coordinates[i], s.coordinates[j] = s.coordinates[j], s.coordinates[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// See sort.Interface.
|
|
||||||
func (s *sorter) Less(i, j int) bool {
|
|
||||||
return s.coordinates[i].Node < s.coordinates[j].Node
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListDatacenters returns the list of datacenters and their respective nodes
|
// ListDatacenters returns the list of datacenters and their respective nodes
|
||||||
// and the raw coordinates of those nodes (if no coordinates are available for
|
// and the raw coordinates of those nodes (if no coordinates are available for
|
||||||
// any of the nodes, the node list may be empty).
|
// any of the nodes, the node list may be empty).
|
||||||
|
@ -172,16 +151,13 @@ func (c *Coordinate) ListDatacenters(args *struct{}, reply *[]structs.Datacenter
|
||||||
sort.Strings(dcs)
|
sort.Strings(dcs)
|
||||||
maps := c.srv.getDatacenterMaps(dcs)
|
maps := c.srv.getDatacenterMaps(dcs)
|
||||||
|
|
||||||
// Strip the datacenter suffixes from all the node names and then sort
|
// Strip the datacenter suffixes from all the node names.
|
||||||
// the sub-lists.
|
|
||||||
for i := range maps {
|
for i := range maps {
|
||||||
suffix := fmt.Sprintf(".%s", maps[i].Datacenter)
|
suffix := fmt.Sprintf(".%s", maps[i].Datacenter)
|
||||||
for j := range maps[i].Coordinates {
|
for j := range maps[i].Coordinates {
|
||||||
node := maps[i].Coordinates[j].Node
|
node := maps[i].Coordinates[j].Node
|
||||||
maps[i].Coordinates[j].Node = strings.TrimSuffix(node, suffix)
|
maps[i].Coordinates[j].Node = strings.TrimSuffix(node, suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(&sorter{maps[i].Coordinates})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*reply = maps
|
*reply = maps
|
||||||
|
@ -202,7 +178,6 @@ func (c *Coordinate) ListNodes(args *structs.DCSpecificRequest, reply *structs.I
|
||||||
func() error {
|
func() error {
|
||||||
var err error
|
var err error
|
||||||
reply.Index, reply.Coordinates, err = state.Coordinates()
|
reply.Index, reply.Coordinates, err = state.Coordinates()
|
||||||
sort.Sort(&sorter{reply.Coordinates})
|
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -270,8 +270,7 @@ func TestCoordinate_ListNodes(t *testing.T) {
|
||||||
}
|
}
|
||||||
time.Sleep(2 * s1.config.CoordinateUpdatePeriod)
|
time.Sleep(2 * s1.config.CoordinateUpdatePeriod)
|
||||||
|
|
||||||
// Now query back for all the nodes and make sure they are sorted
|
// Now query back for all the nodes.
|
||||||
// properly.
|
|
||||||
arg := structs.DCSpecificRequest{
|
arg := structs.DCSpecificRequest{
|
||||||
Datacenter: "dc1",
|
Datacenter: "dc1",
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue