From bf5cb7522f220ebfe3ff2e416ab4af9422723b41 Mon Sep 17 00:00:00 2001 From: Derek Chiang Date: Sat, 18 Apr 2015 17:05:29 -0400 Subject: [PATCH] Use IndexedCoordinate instead --- command/agent/agent.go | 6 ++---- consul/coordinate_endpoint.go | 18 ++++++++++-------- consul/coordinate_endpoint_test.go | 14 ++++++-------- consul/structs/structs.go | 26 +++++++++++++++++++++++--- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 86f0bc7a2b..1981bd368d 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -576,10 +576,8 @@ func (a *Agent) SendCoordinates(shutdownCh chan struct{}) { c = a.client.GetCoordinate() } req := structs.CoordinateUpdateRequest{ - NodeSpecificRequest: structs.NodeSpecificRequest{ - Datacenter: a.config.Datacenter, - Node: a.config.NodeName, - }, + Datacenter: a.config.Datacenter, + Node: a.config.NodeName, Op: structs.CoordinateSet, Coord: c, WriteRequest: structs.WriteRequest{Token: a.config.ACLToken}, diff --git a/consul/coordinate_endpoint.go b/consul/coordinate_endpoint.go index 5df381d224..dda85e9cd7 100644 --- a/consul/coordinate_endpoint.go +++ b/consul/coordinate_endpoint.go @@ -13,19 +13,21 @@ type Coordinate struct { // If the node is in the same datacenter, then the LAN coordinate of the node is // returned. If the node is in a remote DC, then the WAN coordinate of the node // is returned. -func (c *Coordinate) Get(args *structs.NodeSpecificRequest, reply *structs.Coordinate) error { +func (c *Coordinate) Get(args *structs.CoordinateGetRequest, reply *structs.IndexedCoordinate) error { if done, err := c.srv.forward("Coordinate.Get", args, args, reply); done { return err } state := c.srv.fsm.State() - _, coord, err := state.CoordinateGet(args.Node) - if err != nil { - return err - } - *reply = *coord - - return nil + return c.srv.blockingRPC(&args.QueryOptions, + &reply.QueryMeta, + state.QueryTables("Coordinates"), + func() error { + idx, coord, err := state.CoordinateGet(args.Node) + reply.Index = idx + reply.Coord = coord.Coord + return err + }) } func (c *Coordinate) Update(args *structs.CoordinateUpdateRequest, reply *struct{}) error { diff --git a/consul/coordinate_endpoint_test.go b/consul/coordinate_endpoint_test.go index 7589d84e77..b08bab7999 100644 --- a/consul/coordinate_endpoint_test.go +++ b/consul/coordinate_endpoint_test.go @@ -51,12 +51,10 @@ func TestCoordinate(t *testing.T) { testutil.WaitForLeader(t, client.Call, "dc1") arg := structs.CoordinateUpdateRequest{ - NodeSpecificRequest: structs.NodeSpecificRequest{ - Datacenter: "dc1", - Node: "node1", - }, - Op: structs.CoordinateSet, - Coord: getRandomCoordinate(), + Datacenter: "dc1", + Node: "node1", + Op: structs.CoordinateSet, + Coord: getRandomCoordinate(), } var out struct{} @@ -75,8 +73,8 @@ func TestCoordinate(t *testing.T) { } // Get via RPC - var out2 *structs.Coordinate - arg2 := structs.NodeSpecificRequest{ + var out2 *structs.IndexedCoordinate + arg2 := structs.CoordinateGetRequest{ Datacenter: "dc1", Node: "node1", } diff --git a/consul/structs/structs.go b/consul/structs/structs.go index 7e7ebeacff..6ac93503ea 100644 --- a/consul/structs/structs.go +++ b/consul/structs/structs.go @@ -626,20 +626,40 @@ type Coordinate struct { Coord *coordinate.Coordinate } +type IndexedCoordinate struct { + Coord *coordinate.Coordinate + QueryMeta +} + type CoordinateOp string const ( CoordinateSet CoordinateOp = "set" ) +type CoordinateGetRequest struct { + Datacenter string + Node string + QueryOptions +} + +func (c *CoordinateGetRequest) RequestDatacenter() string { + return c.Datacenter +} + // CoordinateUpdateRequest is used to update the network coordinate of a given node type CoordinateUpdateRequest struct { - NodeSpecificRequest - Op CoordinateOp - Coord *coordinate.Coordinate + Datacenter string + Node string + Op CoordinateOp + Coord *coordinate.Coordinate WriteRequest } +func (c *CoordinateUpdateRequest) RequestDatacenter() string { + return c.Datacenter +} + // EventFireRequest is used to ask a server to fire // a Serf event. It is a bit odd, since it doesn't depend on // the catalog or leader. Any node can respond, so it's not quite