Skip to content

Commit

Permalink
nomad: include datacenter names in regions reply
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Nov 24, 2015
1 parent 0c017e3 commit e392174
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (s *HTTPServer) RegionListRequest(resp http.ResponseWriter, req *http.Reque
return nil, nil
}

var regions []string
regions := make(map[string][]string)
if err := s.agent.RPC("Region.List", &args, &regions); err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions command/agent/region_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

Expand All @@ -21,9 +22,13 @@ func TestHTTP_RegionList(t *testing.T) {
t.Fatalf("err: %v", err)
}

out := obj.([]string)
if len(out) != 1 || out[0] != "global" {
t.Fatalf("unexpected regions: %#v", out)
expect := map[string][]string{
"global": []string{"dc1"},
}

out := obj.(map[string][]string)
if !reflect.DeepEqual(out, expect) {
t.Fatalf("expect: %v, got: %v", expect, out)
}
})
}
8 changes: 4 additions & 4 deletions nomad/regions_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ type Region struct {
srv *Server
}

// List is used to list all of the known regions. No leader forwarding is
// required for this endpoint because memberlist is used to populate the
// peers list we read from.
func (r *Region) List(args *structs.GenericRequest, reply *[]string) error {
// List is used to list all of the known regions and datacenters. No leader
// forwarding is required for this endpoint because memberlist is used to
// populate the peers list we read from.
func (r *Region) List(args *structs.GenericRequest, reply *map[string][]string) error {
*reply = r.srv.Regions()
return nil
}
12 changes: 9 additions & 3 deletions nomad/regions_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nomad

import (
"fmt"
"reflect"
"testing"

"github.com/hashicorp/net-rpc-msgpackrpc"
Expand Down Expand Up @@ -29,15 +30,20 @@ func TestRegionList(t *testing.T) {
t.Fatalf("Failed joining: %v (%d joined)", err, n)
}

expect := map[string][]string{
"region1": []string{"dc1"},
"region2": []string{"dc1"},
}

// Query the regions list
testutil.WaitForResult(func() (bool, error) {
var arg structs.GenericRequest
var out []string
var out map[string][]string
if err := msgpackrpc.CallWithCodec(codec, "Region.List", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
}
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" {
t.Fatalf("unexpected regions: %v", out)
if !reflect.DeepEqual(out, expect) {
t.Fatalf("expect: %v, got: %v", expect, out)
}
return true, nil
}, func(err error) {
Expand Down
22 changes: 17 additions & 5 deletions nomad/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,26 @@ func (s *Server) State() *state.StateStore {
return s.fsm.State()
}

// Regions returns the known regions in the cluster.
func (s *Server) Regions() []string {
// Regions returns the known regions in the cluster along with a list
// of the datacenters within each region.
func (s *Server) Regions() map[string][]string {
s.peerLock.RLock()
defer s.peerLock.RUnlock()

regions := make([]string, 0, len(s.peers))
for region, _ := range s.peers {
regions = append(regions, region)
regions := make(map[string][]string, len(s.peers))
for region, servers := range s.peers {
// Collect all the DC's
var dcs []string
seen := make(map[string]struct{})
for _, srv := range servers {
if _, ok := seen[srv.Datacenter]; !ok {
dcs = append(dcs, srv.Datacenter)
}
seen[srv.Datacenter] = struct{}{}
}

// Add the region
regions[region] = dcs
}
return regions
}
Expand Down
10 changes: 8 additions & 2 deletions nomad/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"net"
"reflect"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -108,11 +109,16 @@ func TestServer_Regions(t *testing.T) {
t.Fatalf("Failed joining: %v (%d joined)", err, n)
}

expect := map[string][]string{
"region1": []string{"dc1"},
"region2": []string{"dc1"},
}

// Try listing the regions
testutil.WaitForResult(func() (bool, error) {
out := s1.Regions()
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" {
return false, fmt.Errorf("unexpected regions: %v", out)
if !reflect.DeepEqual(out, expect) {
return false, fmt.Errorf("expect: %v, got: %v", expect, out)
}
return true, nil
}, func(err error) {
Expand Down
13 changes: 10 additions & 3 deletions website/source/docs/http/regions.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ layout: "http"
page_title: "HTTP API: /v1/regions"
sidebar_current: "docs-http-regions"
description: >
The '/v1/regions' endpoint lists the known cluster regions.
The '/v1/regions' endpoint lists the known cluster regions and their
datacenters.
---

# /v1/regions
Expand All @@ -13,7 +14,7 @@ description: >
<dl>
<dt>Description</dt>
<dd>
Returns the known region names.
Returns a summary of the known regions, including the datacenter names.
</dd>

<dt>Method</dt>
Expand All @@ -31,7 +32,13 @@ description: >
<dd>

```javascript
["region1","region2"]
{
"global": [
"dc1",
"dc2"
],
...
}
```

</dd>
Expand Down

0 comments on commit e392174

Please sign in to comment.