From 045f346293832dc5bf4a4e65f126716103339acc Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Mon, 4 Dec 2017 12:35:41 -0800 Subject: [PATCH] Use region instead of datacenter for version checking --- nomad/leader.go | 4 +-- nomad/leader_test.go | 2 +- nomad/serf.go | 4 +-- nomad/util.go | 6 ++--- nomad/util_test.go | 64 ++++++++++++++++++++++---------------------- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/nomad/leader.go b/nomad/leader.go index 3910f5f58..b7be18c06 100644 --- a/nomad/leader.go +++ b/nomad/leader.go @@ -776,7 +776,7 @@ func (s *Server) addRaftPeer(m serf.Member, parts *serverParts) error { // but we want to avoid doing that if possible to prevent useless Raft // log entries. If the address is the same but the ID changed, remove the // old server before adding the new one. - minRaftProtocol, err := MinRaftProtocol(s.config.Datacenter, members) + minRaftProtocol, err := MinRaftProtocol(s.config.Region, members) if err != nil { return err } @@ -848,7 +848,7 @@ func (s *Server) removeRaftPeer(m serf.Member, parts *serverParts) error { return err } - minRaftProtocol, err := MinRaftProtocol(s.config.Datacenter, s.serf.Members()) + minRaftProtocol, err := MinRaftProtocol(s.config.Region, s.serf.Members()) if err != nil { return err } diff --git a/nomad/leader_test.go b/nomad/leader_test.go index 05d534ad2..765f2638f 100644 --- a/nomad/leader_test.go +++ b/nomad/leader_test.go @@ -854,7 +854,7 @@ func TestLeader_UpgradeRaftVersion(t *testing.T) { } for _, s := range []*Server{s1, s3} { - minVer, err := MinRaftProtocol("dc1", s.Members()) + minVer, err := MinRaftProtocol(s1.config.Region, s.Members()) if err != nil { t.Fatal(err) } diff --git a/nomad/serf.go b/nomad/serf.go index 4d8aa90bd..011d7b358 100644 --- a/nomad/serf.go +++ b/nomad/serf.go @@ -184,9 +184,9 @@ func (s *Server) maybeBootstrap() { // Attempt a live bootstrap! var configuration raft.Configuration var addrs []string - minRaftVersion, err := MinRaftProtocol(s.config.Datacenter, members) + minRaftVersion, err := MinRaftProtocol(s.config.Region, members) if err != nil { - s.logger.Printf("[ERR] consul: Failed to read server raft versions: %v", err) + s.logger.Printf("[ERR] nomad: Failed to read server raft versions: %v", err) } for _, server := range servers { diff --git a/nomad/util.go b/nomad/util.go index 5cae76309..8f789a969 100644 --- a/nomad/util.go +++ b/nomad/util.go @@ -149,11 +149,11 @@ func ServersMeetMinimumVersion(members []serf.Member, minVersion *version.Versio } // MinRaftProtocol returns the lowest supported Raft protocol among alive servers -// in the given datacenter. -func MinRaftProtocol(datacenter string, members []serf.Member) (int, error) { +// in the given region. +func MinRaftProtocol(region string, members []serf.Member) (int, error) { minVersion := -1 for _, m := range members { - if m.Tags["role"] != "nomad" || m.Tags["dc"] != datacenter || m.Status != serf.StatusAlive { + if m.Tags["role"] != "nomad" || m.Tags["region"] != region || m.Status != serf.StatusAlive { continue } diff --git a/nomad/util_test.go b/nomad/util_test.go index cf28a2b22..82e939491 100644 --- a/nomad/util_test.go +++ b/nomad/util_test.go @@ -154,14 +154,14 @@ func TestServersMeetMinimumVersion(t *testing.T) { func TestMinRaftProtocol(t *testing.T) { t.Parallel() - makeMember := func(version, datacenter string) serf.Member { + makeMember := func(version, region string) serf.Member { return serf.Member{ Name: "foo", Addr: net.IP([]byte{127, 0, 0, 1}), Tags: map[string]string{ "role": "nomad", - "region": "aws", - "dc": datacenter, + "region": region, + "dc": "dc1", "port": "10000", "vsn": "1", "raft_vsn": version, @@ -171,10 +171,10 @@ func TestMinRaftProtocol(t *testing.T) { } cases := []struct { - members []serf.Member - datacenter string - expected int - err error + members []serf.Member + region string + expected int + err error }{ // No servers, error { @@ -185,61 +185,61 @@ func TestMinRaftProtocol(t *testing.T) { // One server { members: []serf.Member{ - makeMember("1", "dc1"), + makeMember("1", "global"), }, - datacenter: "dc1", - expected: 1, + region: "global", + expected: 1, }, // One server, bad version formatting { members: []serf.Member{ - makeMember("asdf", "dc1"), + makeMember("asdf", "global"), }, - datacenter: "dc1", - expected: -1, - err: errors.New(`strconv.Atoi: parsing "asdf": invalid syntax`), + region: "global", + expected: -1, + err: errors.New(`strconv.Atoi: parsing "asdf": invalid syntax`), }, // One server, wrong datacenter { members: []serf.Member{ - makeMember("1", "dc1"), + makeMember("1", "global"), }, - datacenter: "dc2", - expected: -1, - err: errors.New("no servers found"), + region: "nope", + expected: -1, + err: errors.New("no servers found"), }, // Multiple servers, different versions { members: []serf.Member{ - makeMember("1", "dc1"), - makeMember("2", "dc1"), + makeMember("1", "global"), + makeMember("2", "global"), }, - datacenter: "dc1", - expected: 1, + region: "global", + expected: 1, }, // Multiple servers, same version { members: []serf.Member{ - makeMember("2", "dc1"), - makeMember("2", "dc1"), + makeMember("2", "global"), + makeMember("2", "global"), }, - datacenter: "dc1", - expected: 2, + region: "global", + expected: 2, }, // Multiple servers, multiple datacenters { members: []serf.Member{ - makeMember("3", "dc1"), - makeMember("2", "dc1"), - makeMember("1", "dc2"), + makeMember("3", "r1"), + makeMember("2", "r1"), + makeMember("1", "r2"), }, - datacenter: "dc1", - expected: 2, + region: "r1", + expected: 2, }, } for _, tc := range cases { - result, err := MinRaftProtocol(tc.datacenter, tc.members) + result, err := MinRaftProtocol(tc.region, tc.members) if result != tc.expected { t.Fatalf("bad: %v, %v, %v", result, tc.expected, tc) }