Rename FindHealthyServer() to FindServer()

There is no guarantee the server coming back is healthy.  It's apt to be healthy by virtue of its place in the server list, but it's not guaranteed.
This commit is contained in:
Sean Chittenden 2016-02-24 22:05:05 -08:00
parent 18885e3214
commit 7faea986a0
3 changed files with 41 additions and 5 deletions

View File

@ -321,7 +321,7 @@ func (c *Client) localEvent(event serf.UserEvent) {
// RPC is used to forward an RPC call to a consul server, or fail if no servers
func (c *Client) RPC(method string, args interface{}, reply interface{}) error {
server := c.serverMgr.FindHealthyServer()
server := c.serverMgr.FindServer()
if server == nil {
c.logger.Printf("[ERR] consul: No healthy servers found in the server config")
return structs.ErrNoServers

View File

@ -141,13 +141,13 @@ func (sc *serverConfig) cycleServer() (servers []*server_details.ServerDetails)
return newServers
}
// FindHealthyServer takes out an internal "read lock" and searches through
// the list of servers to find a "healthy" server. If the server is actually
// FindServer takes out an internal "read lock" and searches through the list
// of servers to find a "healthy" server. If the server is actually
// unhealthy, we rely on Serf to detect this and remove the node from the
// 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 (sm *ServerManager) FindHealthyServer() *server_details.ServerDetails {
func (sm *ServerManager) FindServer() *server_details.ServerDetails {
serverCfg := sm.getServerConfig()
numServers := len(serverCfg.servers)
if numServers == 0 {

View File

@ -38,8 +38,44 @@ func mockServerManager() (logger *log.Logger, shutdownCh chan struct{}) {
// func (sm *ServerManager) AddServer(server *server_details.ServerDetails) {
// func (sm *ServerManager) CycleFailedServers() {
// func (sm *ServerManager) FindServer() (server *server_details.ServerDetails) {
func TestServerManager_FindServer(t *testing.T) {
sm := testServerManager()
// func (sm *ServerManager) FindHealthyServer() (server *server_details.ServerDetails) {
s1 := sm.FindServer()
if s1 == nil {
t.Fatalf("Expected non-nil server")
}
if s1.Name != "s1" {
t.Fatalf("Expected s1 server")
}
s1 = sm.FindServer()
if s1 == nil || s1.Name != "s1" {
t.Fatalf("Expected s1 server (still)")
}
sm.AddServer(&server_details.ServerDetails{Name: "s2"})
if sm.NumServers() != 2 {
t.Fatalf("Expected two servers")
}
s1 = sm.FindServer()
if s1 == nil || s1.Name != "s1" {
t.Fatalf("Expected s1 server (still)")
}
sm.NotifyFailedServer(s1)
s2 := sm.FindServer()
if s2 == nil || s2.Name != "s2" {
t.Fatalf("Expected s2 server")
}
sm.NotifyFailedServer(s2)
s1 = sm.FindServer()
if s1 == nil || s1.Name != "s1" {
t.Fatalf("Expected s1 server")
}
}
// func (sm *ServerManager) GetNumServers() (numServers int) {
func TestServerManager_GetNumServers(t *testing.T) {