2013-12-19 22:48:14 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-19 23:08:55 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2013-12-19 22:48:14 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
"log"
|
2013-12-19 23:08:55 +00:00
|
|
|
"math/rand"
|
2013-12-19 22:48:14 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-02-24 00:37:33 +00:00
|
|
|
"strconv"
|
2014-02-19 20:36:27 +00:00
|
|
|
"strings"
|
2013-12-19 22:48:14 +00:00
|
|
|
"sync"
|
2013-12-19 23:42:17 +00:00
|
|
|
"time"
|
2013-12-19 22:48:14 +00:00
|
|
|
)
|
|
|
|
|
2014-02-03 19:53:04 +00:00
|
|
|
var (
|
|
|
|
clientRPCCache = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
2013-12-19 23:20:10 +00:00
|
|
|
// Interface is used to provide either a Client or Server,
|
|
|
|
// both of which can be used to perform certain common
|
|
|
|
// Consul methods
|
|
|
|
type Interface interface {
|
2013-12-19 23:18:25 +00:00
|
|
|
RPC(method string, args interface{}, reply interface{}) error
|
2014-01-21 19:52:01 +00:00
|
|
|
LANMembers() []serf.Member
|
2013-12-19 23:18:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 22:48:14 +00:00
|
|
|
// Client is Consul client which uses RPC to communicate with the
|
|
|
|
// services for service discovery, health checking, and DC forwarding.
|
|
|
|
type Client struct {
|
|
|
|
config *Config
|
|
|
|
|
|
|
|
// Connection pool to consul servers
|
|
|
|
connPool *ConnPool
|
|
|
|
|
|
|
|
// consuls tracks the locally known servers
|
|
|
|
consuls []net.Addr
|
|
|
|
consulLock sync.RWMutex
|
|
|
|
|
|
|
|
// eventCh is used to receive events from the
|
|
|
|
// serf cluster in the datacenter
|
|
|
|
eventCh chan serf.Event
|
|
|
|
|
2014-02-03 19:53:04 +00:00
|
|
|
// lastServer is the last server we made an RPC call to,
|
|
|
|
// this is used to re-use the last connection
|
|
|
|
lastServer net.Addr
|
|
|
|
lastRPCTime time.Time
|
|
|
|
|
2013-12-19 22:48:14 +00:00
|
|
|
// Logger uses the provided LogOutput
|
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
// serf is the Serf cluster maintained inside the DC
|
|
|
|
// which contains all the DC nodes
|
|
|
|
serf *serf.Serf
|
|
|
|
|
|
|
|
shutdown bool
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
shutdownLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient is used to construct a new Consul client from the
|
|
|
|
// configuration, potentially returning an error
|
|
|
|
func NewClient(config *Config) (*Client, error) {
|
|
|
|
// Check for a data directory!
|
|
|
|
if config.DataDir == "" {
|
|
|
|
return nil, fmt.Errorf("Config must provide a DataDir")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have a log output
|
|
|
|
if config.LogOutput == nil {
|
|
|
|
config.LogOutput = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a logger
|
|
|
|
logger := log.New(config.LogOutput, "", log.LstdFlags)
|
|
|
|
|
|
|
|
// Create server
|
|
|
|
c := &Client{
|
|
|
|
config: config,
|
2014-02-06 00:19:05 +00:00
|
|
|
connPool: NewPool(clientRPCCache),
|
2013-12-19 22:48:14 +00:00
|
|
|
eventCh: make(chan serf.Event, 256),
|
|
|
|
logger: logger,
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the Serf listeners to prevent a deadlock
|
|
|
|
go c.lanEventHandler()
|
|
|
|
|
|
|
|
// Initialize the lan Serf
|
|
|
|
var err error
|
|
|
|
c.serf, err = c.setupSerf(config.SerfLANConfig,
|
|
|
|
c.eventCh, serfLANSnapshot)
|
|
|
|
if err != nil {
|
|
|
|
c.Shutdown()
|
|
|
|
return nil, fmt.Errorf("Failed to start lan serf: %v", err)
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupSerf is used to setup and initialize a Serf
|
|
|
|
func (c *Client) setupSerf(conf *serf.Config, ch chan serf.Event, path string) (*serf.Serf, error) {
|
2014-01-30 21:13:29 +00:00
|
|
|
conf.Init()
|
2013-12-19 22:48:14 +00:00
|
|
|
conf.NodeName = c.config.NodeName
|
2014-01-30 21:13:29 +00:00
|
|
|
conf.Tags["role"] = "node"
|
|
|
|
conf.Tags["dc"] = c.config.Datacenter
|
2013-12-19 22:48:14 +00:00
|
|
|
conf.MemberlistConfig.LogOutput = c.config.LogOutput
|
|
|
|
conf.LogOutput = c.config.LogOutput
|
|
|
|
conf.EventCh = ch
|
|
|
|
conf.SnapshotPath = filepath.Join(c.config.DataDir, path)
|
2014-03-05 23:32:59 +00:00
|
|
|
conf.ProtocolVersion = 3 // TODO: Support version 4
|
2013-12-19 22:48:14 +00:00
|
|
|
if err := ensurePath(conf.SnapshotPath, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serf.Create(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown is used to shutdown the client
|
|
|
|
func (c *Client) Shutdown() error {
|
2014-01-10 19:06:11 +00:00
|
|
|
c.logger.Printf("[INFO] consul: shutting down client")
|
2013-12-19 22:48:14 +00:00
|
|
|
c.shutdownLock.Lock()
|
|
|
|
defer c.shutdownLock.Unlock()
|
|
|
|
|
|
|
|
if c.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c.shutdown = true
|
|
|
|
close(c.shutdownCh)
|
|
|
|
|
|
|
|
if c.serf != nil {
|
|
|
|
c.serf.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the connection pool
|
|
|
|
c.connPool.Shutdown()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave is used to prepare for a graceful shutdown
|
|
|
|
func (c *Client) Leave() error {
|
2014-01-10 19:06:11 +00:00
|
|
|
c.logger.Printf("[INFO] consul: client starting leave")
|
2013-12-19 22:48:14 +00:00
|
|
|
|
|
|
|
// Leave the LAN pool
|
|
|
|
if c.serf != nil {
|
|
|
|
if err := c.serf.Leave(); err != nil {
|
2014-01-10 19:06:11 +00:00
|
|
|
c.logger.Printf("[ERR] consul: Failed to leave LAN Serf cluster: %v", err)
|
2013-12-19 22:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinLAN is used to have Consul client join the inner-DC pool
|
|
|
|
// The target address should be another node inside the DC
|
|
|
|
// listening on the Serf LAN address
|
2013-12-30 20:20:17 +00:00
|
|
|
func (c *Client) JoinLAN(addrs []string) (int, error) {
|
2014-02-21 00:27:03 +00:00
|
|
|
return c.serf.Join(addrs, true)
|
2013-12-19 22:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LANMembers is used to return the members of the LAN cluster
|
|
|
|
func (c *Client) LANMembers() []serf.Member {
|
|
|
|
return c.serf.Members()
|
|
|
|
}
|
|
|
|
|
2013-12-30 22:42:23 +00:00
|
|
|
// RemoveFailedNode is used to remove a failed node from the cluster
|
|
|
|
func (c *Client) RemoveFailedNode(node string) error {
|
|
|
|
return c.serf.RemoveFailedNode(node)
|
|
|
|
}
|
|
|
|
|
2013-12-19 22:48:14 +00:00
|
|
|
// lanEventHandler is used to handle events from the lan Serf cluster
|
|
|
|
func (c *Client) lanEventHandler() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-c.eventCh:
|
|
|
|
switch e.EventType() {
|
|
|
|
case serf.EventMemberJoin:
|
|
|
|
c.nodeJoin(e.(serf.MemberEvent))
|
|
|
|
case serf.EventMemberLeave:
|
|
|
|
fallthrough
|
|
|
|
case serf.EventMemberFailed:
|
|
|
|
c.nodeFail(e.(serf.MemberEvent))
|
|
|
|
case serf.EventUser:
|
2014-02-19 20:36:27 +00:00
|
|
|
c.localEvent(e.(serf.UserEvent))
|
2013-12-19 22:48:14 +00:00
|
|
|
default:
|
2014-01-10 19:06:11 +00:00
|
|
|
c.logger.Printf("[WARN] consul: unhandled LAN Serf Event: %#v", e)
|
2013-12-19 22:48:14 +00:00
|
|
|
}
|
|
|
|
case <-c.shutdownCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeJoin is used to handle join events on the serf cluster
|
|
|
|
func (c *Client) nodeJoin(me serf.MemberEvent) {
|
|
|
|
for _, m := range me.Members {
|
2014-01-20 23:39:07 +00:00
|
|
|
ok, parts := isConsulServer(m)
|
2013-12-19 22:48:14 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2014-01-20 23:39:07 +00:00
|
|
|
if parts.Datacenter != c.config.Datacenter {
|
2014-01-10 19:06:11 +00:00
|
|
|
c.logger.Printf("[WARN] consul: server %s for datacenter %s has joined wrong cluster",
|
2014-01-20 23:39:07 +00:00
|
|
|
m.Name, parts.Datacenter)
|
2013-12-19 22:48:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-01-20 23:39:07 +00:00
|
|
|
var addr net.Addr = &net.TCPAddr{IP: m.Addr, Port: parts.Port}
|
|
|
|
c.logger.Printf("[INFO] consul: adding server for datacenter: %s, addr: %s", parts.Datacenter, addr)
|
2013-12-19 22:48:14 +00:00
|
|
|
|
|
|
|
// Check if this server is known
|
|
|
|
found := false
|
|
|
|
c.consulLock.Lock()
|
|
|
|
for _, c := range c.consuls {
|
|
|
|
if c.String() == addr.String() {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to the list if not known
|
|
|
|
if !found {
|
|
|
|
c.consuls = append(c.consuls, addr)
|
|
|
|
}
|
|
|
|
c.consulLock.Unlock()
|
2014-02-07 20:11:34 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if c.config.ServerUp != nil {
|
|
|
|
c.config.ServerUp()
|
|
|
|
}
|
2013-12-19 22:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeFail is used to handle fail events on the serf cluster
|
|
|
|
func (c *Client) nodeFail(me serf.MemberEvent) {
|
|
|
|
for _, m := range me.Members {
|
2014-01-20 23:39:07 +00:00
|
|
|
ok, parts := isConsulServer(m)
|
2013-12-19 22:48:14 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2014-01-20 23:39:07 +00:00
|
|
|
var addr net.Addr = &net.TCPAddr{IP: m.Addr, Port: parts.Port}
|
|
|
|
c.logger.Printf("[INFO] consul: removing server for datacenter: %s, addr: %s", parts.Datacenter, addr)
|
2013-12-19 22:48:14 +00:00
|
|
|
|
|
|
|
// Remove the server if known
|
|
|
|
c.consulLock.Lock()
|
|
|
|
n := len(c.consuls)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if c.consuls[i].String() == addr.String() {
|
|
|
|
c.consuls[i], c.consuls[n-1] = c.consuls[n-1], nil
|
|
|
|
c.consuls = c.consuls[:n-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.consulLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 23:08:55 +00:00
|
|
|
|
2014-02-19 20:36:27 +00:00
|
|
|
// localEvent is called when we receive an event on the local Serf
|
|
|
|
func (c *Client) localEvent(event serf.UserEvent) {
|
|
|
|
// Handle only consul events
|
|
|
|
if !strings.HasPrefix(event.Name, "consul:") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch event.Name {
|
|
|
|
case newLeaderEvent:
|
|
|
|
c.logger.Printf("[INFO] consul: New leader elected: %s", event.Payload)
|
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if c.config.ServerUp != nil {
|
|
|
|
c.config.ServerUp()
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
c.logger.Printf("[WARN] consul: Unhandled local event: %v", event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:08:55 +00:00
|
|
|
// 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 {
|
2014-02-03 19:53:04 +00:00
|
|
|
// Check the last rpc time
|
|
|
|
var server net.Addr
|
|
|
|
if time.Now().Sub(c.lastRPCTime) < clientRPCCache {
|
|
|
|
server = c.lastServer
|
|
|
|
if server != nil {
|
|
|
|
goto TRY_RPC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:08:55 +00:00
|
|
|
// Bail if we can't find any servers
|
|
|
|
c.consulLock.RLock()
|
|
|
|
if len(c.consuls) == 0 {
|
|
|
|
c.consulLock.RUnlock()
|
|
|
|
return structs.ErrNoServers
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select a random addr
|
2014-02-03 19:53:04 +00:00
|
|
|
server = c.consuls[rand.Int31()%int32(len(c.consuls))]
|
2013-12-19 23:08:55 +00:00
|
|
|
c.consulLock.RUnlock()
|
|
|
|
|
|
|
|
// Forward to remote Consul
|
2014-02-03 19:53:04 +00:00
|
|
|
TRY_RPC:
|
|
|
|
if err := c.connPool.RPC(server, method, args, reply); err != nil {
|
|
|
|
c.lastServer = nil
|
|
|
|
c.lastRPCTime = time.Time{}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the last server
|
|
|
|
c.lastServer = server
|
|
|
|
c.lastRPCTime = time.Now()
|
|
|
|
return nil
|
2013-12-19 23:08:55 +00:00
|
|
|
}
|
2014-02-24 00:37:33 +00:00
|
|
|
|
|
|
|
// Stats is used to return statistics for debugging and insight
|
|
|
|
// for various sub-systems
|
|
|
|
func (c *Client) Stats() map[string]map[string]string {
|
|
|
|
toString := func(v uint64) string {
|
|
|
|
return strconv.FormatUint(v, 10)
|
|
|
|
}
|
|
|
|
stats := map[string]map[string]string{
|
|
|
|
"consul": map[string]string{
|
2014-02-24 02:08:58 +00:00
|
|
|
"server": "false",
|
|
|
|
"known_servers": toString(uint64(len(c.consuls))),
|
2014-02-24 00:37:33 +00:00
|
|
|
},
|
2014-02-24 02:08:58 +00:00
|
|
|
"serf-lan": c.serf.Stats(),
|
2014-02-24 00:37:33 +00:00
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|