Rename server_details package to agent

This commit is contained in:
Sean Chittenden 2016-03-29 17:39:19 -07:00
parent 80f9f54bb3
commit 7cdc056ca5
12 changed files with 118 additions and 114 deletions

View File

@ -1,4 +1,9 @@
package server_details
// Package agent provides a logical endpoint for Consul agents in the
// network. agent data originates from Serf gossip and is primarily used to
// communicate Consul server information. Gossiped information that ends up
// in Server contains the necessary metadata required for servers.Manager to
// select which server an RPC request should be routed to.
package agent
import (
"fmt"
@ -18,8 +23,8 @@ func (k *Key) Equal(x *Key) bool {
return k.name == x.name
}
// ServerDetails is used to return details of a consul server
type ServerDetails struct {
// Server is used to return details of a consul server
type Server struct {
Name string
Datacenter string
Port int
@ -30,14 +35,14 @@ type ServerDetails struct {
}
// Key returns the corresponding Key
func (s *ServerDetails) Key() *Key {
func (s *Server) Key() *Key {
return &Key{
name: s.Name,
}
}
// String returns a string representation of ServerDetails
func (s *ServerDetails) String() string {
// String returns a string representation of Server
func (s *Server) String() string {
var addrStr, networkStr string
if s.Addr != nil {
addrStr = s.Addr.String()
@ -47,9 +52,9 @@ func (s *ServerDetails) String() string {
return fmt.Sprintf("%s (Addr: %s/%s) (DC: %s)", s.Name, networkStr, addrStr, s.Datacenter)
}
// IsConsulServer returns true if a serf member is a consul server. Returns a
// bool and a pointer to the ServerDetails.
func IsConsulServer(m serf.Member) (bool, *ServerDetails) {
// IsConsulServer returns true if a serf member is a consul server
// agent. Returns a bool and a pointer to the Server.
func IsConsulServer(m serf.Member) (bool, *Server) {
if m.Tags["role"] != "consul" {
return false, nil
}
@ -81,7 +86,7 @@ func IsConsulServer(m serf.Member) (bool, *ServerDetails) {
addr := &net.TCPAddr{IP: m.Addr, Port: port}
parts := &ServerDetails{
parts := &Server{
Name: m.Name,
Datacenter: datacenter,
Port: port,

View File

@ -1,10 +1,10 @@
package server_details
package agent
import (
"testing"
)
func TestServerDetails_Key_Equal(t *testing.T) {
func TestServer_Key_Equal(t *testing.T) {
tests := []struct {
name string
k1 *Key
@ -47,16 +47,16 @@ func TestServerDetails_Key_Equal(t *testing.T) {
}
}
func TestServerDetails_Key(t *testing.T) {
func TestServer_Key(t *testing.T) {
tests := []struct {
name string
sd *ServerDetails
sd *Server
k *Key
equal bool
}{
{
name: "Key equality",
sd: &ServerDetails{
sd: &Server{
Name: "s1",
},
k: &Key{
@ -66,7 +66,7 @@ func TestServerDetails_Key(t *testing.T) {
},
{
name: "Key inequality",
sd: &ServerDetails{
sd: &Server{
Name: "s1",
},
k: &Key{

View File

@ -1,32 +1,32 @@
package server_details_test
package agent_test
import (
"net"
"testing"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/serf/serf"
)
func TestServerDetails_Key_params(t *testing.T) {
func TestServer_Key_params(t *testing.T) {
ipv4a := net.ParseIP("127.0.0.1")
ipv4b := net.ParseIP("1.2.3.4")
tests := []struct {
name string
sd1 *server_details.ServerDetails
sd2 *server_details.ServerDetails
sd1 *agent.Server
sd2 *agent.Server
equal bool
}{
{
name: "Addr inequality",
sd1: &server_details.ServerDetails{
sd1: &agent.Server{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
Addr: &net.IPAddr{IP: ipv4a},
},
sd2: &server_details.ServerDetails{
sd2: &agent.Server{
Name: "s1",
Datacenter: "dc1",
Port: 8300,
@ -42,7 +42,7 @@ func TestServerDetails_Key_params(t *testing.T) {
}
// Test Key to make sure it actually works as a key
m := make(map[server_details.Key]bool)
m := make(map[agent.Key]bool)
m[*test.sd1.Key()] = true
if _, found := m[*test.sd2.Key()]; found != test.equal {
t.Errorf("Expected a %v result from map test %s", test.equal, test.name)
@ -61,7 +61,7 @@ func TestIsConsulServer(t *testing.T) {
"vsn": "1",
},
}
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok || parts.Datacenter != "east-aws" || parts.Port != 10000 {
t.Fatalf("bad: %v %v", ok, parts)
}
@ -76,7 +76,7 @@ func TestIsConsulServer(t *testing.T) {
}
m.Tags["bootstrap"] = "1"
m.Tags["disabled"] = "1"
ok, parts = server_details.IsConsulServer(m)
ok, parts = agent.IsConsulServer(m)
if !ok {
t.Fatalf("expected a valid consul server")
}
@ -92,7 +92,7 @@ func TestIsConsulServer(t *testing.T) {
m.Tags["expect"] = "3"
delete(m.Tags, "bootstrap")
delete(m.Tags, "disabled")
ok, parts = server_details.IsConsulServer(m)
ok, parts = agent.IsConsulServer(m)
if !ok || parts.Expect != 3 {
t.Fatalf("bad: %v", parts.Expect)
}
@ -101,7 +101,7 @@ func TestIsConsulServer(t *testing.T) {
}
delete(m.Tags, "role")
ok, parts = server_details.IsConsulServer(m)
ok, parts = agent.IsConsulServer(m)
if ok {
t.Fatalf("unexpected ok server")
}

View File

@ -10,7 +10,7 @@ import (
"sync"
"time"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/consul/servers"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/serf/coordinate"
@ -261,7 +261,7 @@ func (c *Client) lanEventHandler() {
// nodeJoin is used to handle join events on the serf cluster
func (c *Client) nodeJoin(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
continue
}
@ -283,7 +283,7 @@ func (c *Client) nodeJoin(me serf.MemberEvent) {
// nodeFail is used to handle fail events on the serf cluster
func (c *Client) nodeFail(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
continue
}

View File

@ -8,7 +8,7 @@ import (
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/serf"
@ -350,7 +350,7 @@ func (s *Server) shouldHandleMember(member serf.Member) bool {
if valid, dc := isConsulNode(member); valid && dc == s.config.Datacenter {
return true
}
if valid, parts := server_details.IsConsulServer(member); valid && parts.Datacenter == s.config.Datacenter {
if valid, parts := agent.IsConsulServer(member); valid && parts.Datacenter == s.config.Datacenter {
return true
}
return false
@ -361,7 +361,7 @@ func (s *Server) shouldHandleMember(member serf.Member) bool {
func (s *Server) handleAliveMember(member serf.Member) error {
// Register consul service if a server
var service *structs.NodeService
if valid, parts := server_details.IsConsulServer(member); valid {
if valid, parts := agent.IsConsulServer(member); valid {
service = &structs.NodeService{
ID: ConsulServiceID,
Service: ConsulServiceName,
@ -497,7 +497,7 @@ func (s *Server) handleDeregisterMember(reason string, member serf.Member) error
}
// Remove from Raft peers if this was a server
if valid, parts := server_details.IsConsulServer(member); valid {
if valid, parts := agent.IsConsulServer(member); valid {
if err := s.removeConsulServer(member, parts.Port); err != nil {
return err
}
@ -524,7 +524,7 @@ func (s *Server) handleDeregisterMember(reason string, member serf.Member) error
}
// joinConsulServer is used to try to join another consul server
func (s *Server) joinConsulServer(m serf.Member, parts *server_details.ServerDetails) error {
func (s *Server) joinConsulServer(m serf.Member, parts *agent.Server) error {
// Do not join ourself
if m.Name == s.config.NodeName {
return nil
@ -534,7 +534,7 @@ func (s *Server) joinConsulServer(m serf.Member, parts *server_details.ServerDet
if parts.Bootstrap {
members := s.serfLAN.Members()
for _, member := range members {
valid, p := server_details.IsConsulServer(member)
valid, p := agent.IsConsulServer(member)
if valid && member.Name != m.Name && p.Bootstrap {
s.logger.Printf("[ERR] consul: '%v' and '%v' are both in bootstrap mode. Only one node should be in bootstrap mode, not adding Raft peer.", m.Name, member.Name)
return nil

View File

@ -3,7 +3,7 @@ package consul
import (
"fmt"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/serf/serf"
)
@ -25,7 +25,7 @@ func (md *lanMergeDelegate) NotifyMerge(members []*serf.Member) error {
continue
}
ok, parts := server_details.IsConsulServer(*m)
ok, parts := agent.IsConsulServer(*m)
if ok && parts.Datacenter != md.dc {
return fmt.Errorf("Member '%s' part of wrong datacenter '%s'",
m.Name, parts.Datacenter)
@ -42,7 +42,7 @@ type wanMergeDelegate struct {
func (md *wanMergeDelegate) NotifyMerge(members []*serf.Member) error {
for _, m := range members {
ok, _ := server_details.IsConsulServer(*m)
ok, _ := agent.IsConsulServer(*m)
if !ok {
return fmt.Errorf("Member '%s' is not a server", m.Name)
}

View File

@ -10,7 +10,7 @@ import (
"sync/atomic"
"time"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/tlsutil"
"github.com/hashicorp/net-rpc-msgpackrpc"
"github.com/hashicorp/yamux"
@ -408,7 +408,7 @@ func (p *ConnPool) RPC(dc string, addr net.Addr, version int, method string, arg
// PingConsulServer sends a Status.Ping message to the specified server and
// returns true if healthy, false if an error occurred
func (p *ConnPool) PingConsulServer(s *server_details.ServerDetails) (bool, error) {
func (p *ConnPool) PingConsulServer(s *agent.Server) (bool, error) {
// Get a usable client
conn, sc, err := p.getClient(s.Datacenter, s.Addr, s.Version)
if err != nil {

View File

@ -4,7 +4,7 @@ import (
"net"
"strings"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/serf/serf"
)
@ -141,7 +141,7 @@ func (s *Server) localEvent(event serf.UserEvent) {
// lanNodeJoin is used to handle join events on the LAN pool.
func (s *Server) lanNodeJoin(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
continue
}
@ -164,7 +164,7 @@ func (s *Server) lanNodeJoin(me serf.MemberEvent) {
// wanNodeJoin is used to handle join events on the WAN pool.
func (s *Server) wanNodeJoin(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
s.logger.Printf("[WARN] consul: non-server in WAN pool: %s", m.Name)
continue
@ -210,7 +210,7 @@ func (s *Server) maybeBootstrap() {
members := s.serfLAN.Members()
addrs := make([]string, 0)
for _, member := range members {
valid, p := server_details.IsConsulServer(member)
valid, p := agent.IsConsulServer(member)
if !valid {
continue
}
@ -248,7 +248,7 @@ func (s *Server) maybeBootstrap() {
// lanNodeFailed is used to handle fail events on the LAN pool.
func (s *Server) lanNodeFailed(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
continue
}
@ -263,7 +263,7 @@ func (s *Server) lanNodeFailed(me serf.MemberEvent) {
// wanNodeFailed is used to handle fail events on the WAN pool.
func (s *Server) wanNodeFailed(me serf.MemberEvent) {
for _, m := range me.Members {
ok, parts := server_details.IsConsulServer(m)
ok, parts := agent.IsConsulServer(m)
if !ok {
continue
}

View File

@ -15,7 +15,7 @@ import (
"time"
"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/consul/state"
"github.com/hashicorp/consul/tlsutil"
"github.com/hashicorp/raft"
@ -98,7 +98,7 @@ type Server struct {
// localConsuls is used to track the known consuls
// in the local datacenter. Used to do leader forwarding.
localConsuls map[string]*server_details.ServerDetails
localConsuls map[string]*agent.Server
localLock sync.RWMutex
// Logger uses the provided LogOutput
@ -120,7 +120,7 @@ type Server struct {
// remoteConsuls is used to track the known consuls in
// remote datacenters. Used to do DC forwarding.
remoteConsuls map[string][]*server_details.ServerDetails
remoteConsuls map[string][]*agent.Server
remoteLock sync.RWMutex
// rpcListener is used to listen for incoming connections
@ -217,10 +217,10 @@ func NewServer(config *Config) (*Server, error) {
connPool: NewPool(config.LogOutput, serverRPCCache, serverMaxStreams, tlsWrap),
eventChLAN: make(chan serf.Event, 256),
eventChWAN: make(chan serf.Event, 256),
localConsuls: make(map[string]*server_details.ServerDetails),
localConsuls: make(map[string]*agent.Server),
logger: logger,
reconcileCh: make(chan serf.Member, 32),
remoteConsuls: make(map[string][]*server_details.ServerDetails),
remoteConsuls: make(map[string][]*agent.Server),
rpcServer: rpc.NewServer(),
rpcTLS: incomingTLS,
tombstoneGC: gc,

View File

@ -1,8 +1,8 @@
// Package servers provides a Manager interface for Manager managed
// server_details.ServerDetails objects. The servers package manages servers
// from a Consul client's perspective (i.e. a list of servers that a client
// talks with for RPCs). The servers package does not provide any API
// guarantees and should be called only by `hashicorp/consul`.
// agent.Server objects. The servers package manages servers from a Consul
// client's perspective (i.e. a list of servers that a client talks with for
// RPCs). The servers package does not provide any API guarantees and should
// be called only by `hashicorp/consul`.
package servers
import (
@ -12,7 +12,7 @@ import (
"sync/atomic"
"time"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/lib"
)
@ -61,7 +61,7 @@ type ConsulClusterInfo interface {
// Pinger is an interface wrapping client.ConnPool to prevent a
// cyclic import dependency
type Pinger interface {
PingConsulServer(server *server_details.ServerDetails) (bool, error)
PingConsulServer(s *agent.Server) (bool, error)
}
// serverList is a local copy of the struct used to maintain the list of
@ -72,7 +72,7 @@ type Pinger interface {
type serverList struct {
// servers tracks the locally known servers. List membership is
// maintained by Serf.
servers []*server_details.ServerDetails
servers []*agent.Server
}
type Manager struct {
@ -108,7 +108,7 @@ type Manager struct {
// begin seeing use after the rebalance timer fires or enough servers fail
// organically. If the server is already known, merge the new server
// details.
func (m *Manager) AddServer(server *server_details.ServerDetails) {
func (m *Manager) AddServer(s *agent.Server) {
m.listLock.Lock()
defer m.listLock.Unlock()
l := m.getServerList()
@ -116,13 +116,13 @@ func (m *Manager) AddServer(server *server_details.ServerDetails) {
// Check if this server is known
found := false
for idx, existing := range l.servers {
if existing.Name == server.Name {
newServers := make([]*server_details.ServerDetails, len(l.servers))
if existing.Name == s.Name {
newServers := make([]*agent.Server, len(l.servers))
copy(newServers, l.servers)
// Overwrite the existing server details in order to
// possibly update metadata (e.g. server version)
newServers[idx] = server
newServers[idx] = s
l.servers = newServers
found = true
@ -132,9 +132,9 @@ func (m *Manager) AddServer(server *server_details.ServerDetails) {
// Add to the list if not known
if !found {
newServers := make([]*server_details.ServerDetails, len(l.servers), len(l.servers)+1)
newServers := make([]*agent.Server, len(l.servers), len(l.servers)+1)
copy(newServers, l.servers)
newServers = append(newServers, server)
newServers = append(newServers, s)
l.servers = newServers
}
@ -149,13 +149,13 @@ func (m *Manager) AddServer(server *server_details.ServerDetails) {
// less desirable than just returning the next server in the firing line. If
// the next server fails, it will fail fast enough and cycleServer will be
// called again.
func (l *serverList) cycleServer() (servers []*server_details.ServerDetails) {
func (l *serverList) cycleServer() (servers []*agent.Server) {
numServers := len(l.servers)
if numServers < 2 {
return servers // No action required
}
newServers := make([]*server_details.ServerDetails, 0, numServers)
newServers := make([]*agent.Server, 0, numServers)
newServers = append(newServers, l.servers[1:]...)
newServers = append(newServers, l.servers[0])
@ -163,7 +163,7 @@ func (l *serverList) cycleServer() (servers []*server_details.ServerDetails) {
}
// removeServerByKey performs an inline removal of the first matching server
func (l *serverList) removeServerByKey(targetKey *server_details.Key) {
func (l *serverList) removeServerByKey(targetKey *agent.Key) {
for i, s := range l.servers {
if targetKey.Equal(s.Key()) {
copy(l.servers[i:], l.servers[i+1:])
@ -188,7 +188,7 @@ func (l *serverList) shuffleServers() {
// server list. If the server at the front of the list has failed or fails
// during an RPC call, it is rotated to the end of the list. If there are no
// servers available, return nil.
func (m *Manager) FindServer() *server_details.ServerDetails {
func (m *Manager) FindServer() *agent.Server {
l := m.getServerList()
numServers := len(l.servers)
if numServers == 0 {
@ -225,14 +225,14 @@ func New(logger *log.Logger, shutdownCh chan struct{}, clusterInfo ConsulCluster
m.shutdownCh = shutdownCh
l := serverList{}
l.servers = make([]*server_details.ServerDetails, 0)
l.servers = make([]*agent.Server, 0)
m.saveServerList(l)
return m
}
// NotifyFailedServer marks the passed in server as "failed" by rotating it
// to the end of the server list.
func (m *Manager) NotifyFailedServer(server *server_details.ServerDetails) {
func (m *Manager) NotifyFailedServer(s *agent.Server) {
l := m.getServerList()
// If the server being failed is not the first server on the list,
@ -241,7 +241,7 @@ func (m *Manager) NotifyFailedServer(server *server_details.ServerDetails) {
// the server to the end of the list.
// Only rotate the server list when there is more than one server
if len(l.servers) > 1 && l.servers[0] == server &&
if len(l.servers) > 1 && l.servers[0] == s &&
// Use atomic.CAS to emulate a TryLock().
atomic.CompareAndSwapInt32(&m.notifyFailedBarrier, 0, 1) {
defer atomic.StoreInt32(&m.notifyFailedBarrier, 0)
@ -252,7 +252,7 @@ func (m *Manager) NotifyFailedServer(server *server_details.ServerDetails) {
defer m.listLock.Unlock()
l = m.getServerList()
if len(l.servers) > 1 && l.servers[0] == server {
if len(l.servers) > 1 && l.servers[0] == s {
l.servers = l.cycleServer()
m.saveServerList(l)
}
@ -261,10 +261,9 @@ func (m *Manager) NotifyFailedServer(server *server_details.ServerDetails) {
// NumServers takes out an internal "read lock" and returns the number of
// servers. numServers includes both healthy and unhealthy servers.
func (m *Manager) NumServers() (numServers int) {
func (m *Manager) NumServers() int {
l := m.getServerList()
numServers = len(l.servers)
return numServers
return len(l.servers)
}
// RebalanceServers shuffles the list of servers on this agent. The server
@ -355,14 +354,14 @@ func (m *Manager) reconcileServerList(l *serverList) bool {
}
type targetServer struct {
server *server_details.ServerDetails
server *agent.Server
// 'b' == both
// 'o' == original
// 'n' == new
state byte
}
mergedList := make(map[server_details.Key]*targetServer, len(l.servers))
mergedList := make(map[agent.Key]*targetServer, len(l.servers))
for _, s := range l.servers {
mergedList[*s.Key()] = &targetServer{server: s, state: 'o'}
}
@ -404,15 +403,15 @@ func (m *Manager) reconcileServerList(l *serverList) bool {
// RemoveServer takes out an internal write lock and removes a server from
// the server list.
func (m *Manager) RemoveServer(server *server_details.ServerDetails) {
func (m *Manager) RemoveServer(s *agent.Server) {
m.listLock.Lock()
defer m.listLock.Unlock()
l := m.getServerList()
// Remove the server if known
for i, _ := range l.servers {
if l.servers[i].Name == server.Name {
newServers := make([]*server_details.ServerDetails, 0, len(l.servers)-1)
if l.servers[i].Name == s.Name {
newServers := make([]*agent.Server, 0, len(l.servers)-1)
newServers = append(newServers, l.servers[:i]...)
newServers = append(newServers, l.servers[i+1:]...)
l.servers = newServers

View File

@ -9,7 +9,7 @@ import (
"testing"
"time"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
)
var (
@ -31,7 +31,7 @@ type fauxConnPool struct {
failPct float64
}
func (cp *fauxConnPool) PingConsulServer(server *server_details.ServerDetails) (bool, error) {
func (cp *fauxConnPool) PingConsulServer(server *agent.Server) (bool, error) {
var success bool
successProb := rand.Float64()
if successProb > cp.failPct {
@ -63,14 +63,14 @@ func testManagerFailProb(failPct float64) (m *Manager) {
return m
}
// func (l *serverList) cycleServer() (servers []*server_details.ServerDetails) {
// func (l *serverList) cycleServer() (servers []*agent.Server) {
func TestManagerInternal_cycleServer(t *testing.T) {
m := testManager()
l := m.getServerList()
server0 := &server_details.ServerDetails{Name: "server1"}
server1 := &server_details.ServerDetails{Name: "server2"}
server2 := &server_details.ServerDetails{Name: "server3"}
server0 := &agent.Server{Name: "server1"}
server1 := &agent.Server{Name: "server2"}
server2 := &agent.Server{Name: "server3"}
l.servers = append(l.servers, server0, server1, server2)
m.saveServerList(l)
@ -166,11 +166,11 @@ func test_reconcileServerList(maxServers int) (bool, error) {
const failPct = 0.5
m := testManagerFailProb(failPct)
var failedServers, healthyServers []*server_details.ServerDetails
var failedServers, healthyServers []*agent.Server
for i := 0; i < maxServers; i++ {
nodeName := fmt.Sprintf("s%02d", i)
node := &server_details.ServerDetails{Name: nodeName}
node := &agent.Server{Name: nodeName}
// Add 66% of servers to Manager
if rand.Float64() > 0.33 {
m.AddServer(node)
@ -230,7 +230,7 @@ func test_reconcileServerList(maxServers int) (bool, error) {
return true, nil
}
resultingServerMap := make(map[server_details.Key]bool)
resultingServerMap := make(map[agent.Key]bool)
for _, s := range m.getServerList().servers {
resultingServerMap[*s.Key()] = true
}
@ -302,7 +302,7 @@ func TestManagerInternal_refreshServerRebalanceTimer(t *testing.T) {
m := New(logger, shutdownCh, &fauxSerf{numNodes: s.numNodes}, &fauxConnPool{})
for i := 0; i < s.numServers; i++ {
nodeName := fmt.Sprintf("s%02d", i)
m.AddServer(&server_details.ServerDetails{Name: nodeName})
m.AddServer(&agent.Server{Name: nodeName})
}
d := m.refreshServerRebalanceTimer()
@ -323,7 +323,7 @@ func TestManagerInternal_saveServerList(t *testing.T) {
t.Fatalf("Manager.saveServerList failed to load init config")
}
newServer := new(server_details.ServerDetails)
newServer := new(agent.Server)
l.servers = append(l.servers, newServer)
m.saveServerList(l)
}()
@ -339,7 +339,7 @@ func TestManagerInternal_saveServerList(t *testing.T) {
// Verify mutation w/o a save doesn't alter the original
func() {
newServer := new(server_details.ServerDetails)
newServer := new(agent.Server)
l := m.getServerList()
l.servers = append(l.servers, newServer)

View File

@ -9,7 +9,7 @@ import (
"strings"
"testing"
"github.com/hashicorp/consul/consul/server_details"
"github.com/hashicorp/consul/consul/agent"
"github.com/hashicorp/consul/consul/servers"
)
@ -32,7 +32,7 @@ type fauxConnPool struct {
failPct float64
}
func (cp *fauxConnPool) PingConsulServer(server *server_details.ServerDetails) (bool, error) {
func (cp *fauxConnPool) PingConsulServer(server *agent.Server) (bool, error) {
var success bool
successProb := rand.Float64()
if successProb > cp.failPct {
@ -64,7 +64,7 @@ func testManagerFailProb(failPct float64) (m *servers.Manager) {
return m
}
// func (m *Manager) AddServer(server *server_details.ServerDetails) {
// func (m *Manager) AddServer(server *agent.Server) {
func TestServers_AddServer(t *testing.T) {
m := testManager()
var num int
@ -73,7 +73,7 @@ func TestServers_AddServer(t *testing.T) {
t.Fatalf("Expected zero servers to start")
}
s1 := &server_details.ServerDetails{Name: "s1"}
s1 := &agent.Server{Name: "s1"}
m.AddServer(s1)
num = m.NumServers()
if num != 1 {
@ -86,7 +86,7 @@ func TestServers_AddServer(t *testing.T) {
t.Fatalf("Expected one server (still)")
}
s2 := &server_details.ServerDetails{Name: "s2"}
s2 := &agent.Server{Name: "s2"}
m.AddServer(s2)
num = m.NumServers()
if num != 2 {
@ -94,7 +94,7 @@ func TestServers_AddServer(t *testing.T) {
}
}
// func (m *Manager) FindServer() (server *server_details.ServerDetails) {
// func (m *Manager) FindServer() (server *agent.Server) {
func TestServers_FindServer(t *testing.T) {
m := testManager()
@ -102,7 +102,7 @@ func TestServers_FindServer(t *testing.T) {
t.Fatalf("Expected nil return")
}
m.AddServer(&server_details.ServerDetails{Name: "s1"})
m.AddServer(&agent.Server{Name: "s1"})
if m.NumServers() != 1 {
t.Fatalf("Expected one server")
}
@ -120,7 +120,7 @@ func TestServers_FindServer(t *testing.T) {
t.Fatalf("Expected s1 server (still)")
}
m.AddServer(&server_details.ServerDetails{Name: "s2"})
m.AddServer(&agent.Server{Name: "s2"})
if m.NumServers() != 2 {
t.Fatalf("Expected two servers")
}
@ -153,7 +153,7 @@ func TestServers_New(t *testing.T) {
}
}
// func (m *Manager) NotifyFailedServer(server *server_details.ServerDetails) {
// func (m *Manager) NotifyFailedServer(server *agent.Server) {
func TestServers_NotifyFailedServer(t *testing.T) {
m := testManager()
@ -161,8 +161,8 @@ func TestServers_NotifyFailedServer(t *testing.T) {
t.Fatalf("Expected zero servers to start")
}
s1 := &server_details.ServerDetails{Name: "s1"}
s2 := &server_details.ServerDetails{Name: "s2"}
s1 := &agent.Server{Name: "s1"}
s2 := &agent.Server{Name: "s2"}
// Try notifying for a server that is not managed by Manager
m.NotifyFailedServer(s1)
@ -215,7 +215,7 @@ func TestServers_NumServers(t *testing.T) {
t.Fatalf("Expected zero servers to start")
}
s := &server_details.ServerDetails{}
s := &agent.Server{}
m.AddServer(s)
num = m.NumServers()
if num != 1 {
@ -234,7 +234,7 @@ func TestServers_RebalanceServers(t *testing.T) {
// Make a huge list of nodes.
for i := 0; i < maxServers; i++ {
nodeName := fmt.Sprintf("s%02d", i)
m.AddServer(&server_details.ServerDetails{Name: nodeName})
m.AddServer(&agent.Server{Name: nodeName})
}
// Keep track of how many unique shuffles we get.
@ -260,7 +260,7 @@ func TestServers_RebalanceServers(t *testing.T) {
}
}
// func (m *Manager) RemoveServer(server *server_details.ServerDetails) {
// func (m *Manager) RemoveServer(server *agent.Server) {
func TestManager_RemoveServer(t *testing.T) {
const nodeNameFmt = "s%02d"
m := testManager()
@ -271,21 +271,21 @@ func TestManager_RemoveServer(t *testing.T) {
// Test removing server before its added
nodeName := fmt.Sprintf(nodeNameFmt, 1)
s1 := &server_details.ServerDetails{Name: nodeName}
s1 := &agent.Server{Name: nodeName}
m.RemoveServer(s1)
m.AddServer(s1)
nodeName = fmt.Sprintf(nodeNameFmt, 2)
s2 := &server_details.ServerDetails{Name: nodeName}
s2 := &agent.Server{Name: nodeName}
m.RemoveServer(s2)
m.AddServer(s2)
const maxServers = 19
servers := make([]*server_details.ServerDetails, maxServers)
servers := make([]*agent.Server, maxServers)
// Already added two servers above
for i := maxServers; i > 2; i-- {
nodeName := fmt.Sprintf(nodeNameFmt, i)
server := &server_details.ServerDetails{Name: nodeName}
server := &agent.Server{Name: nodeName}
servers = append(servers, server)
m.AddServer(server)
}
@ -299,7 +299,7 @@ func TestManager_RemoveServer(t *testing.T) {
t.Fatalf("Expected %d servers, received %d", maxServers, m.NumServers())
}
findServer := func(server *server_details.ServerDetails) bool {
findServer := func(server *agent.Server) bool {
for i := m.NumServers(); i > 0; i-- {
s := m.FindServer()
if s == server {
@ -310,7 +310,7 @@ func TestManager_RemoveServer(t *testing.T) {
}
expectedNumServers := maxServers
removedServers := make([]*server_details.ServerDetails, 0, maxServers)
removedServers := make([]*agent.Server, 0, maxServers)
// Remove servers from the front of the list
for i := 3; i > 0; i-- {