2013-12-07 01:18:09 +00:00
|
|
|
package consul
|
2013-12-07 00:54:33 +00:00
|
|
|
|
2013-12-09 23:29:44 +00:00
|
|
|
import (
|
2017-08-14 14:36:07 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"path/filepath"
|
2014-02-19 20:36:27 +00:00
|
|
|
"strings"
|
2016-12-01 22:05:02 +00:00
|
|
|
"time"
|
2014-06-18 16:03:30 +00:00
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/wanfed"
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-08-14 14:36:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-10-08 19:02:19 +00:00
|
|
|
libserf "github.com/hashicorp/consul/lib/serf"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-03-09 20:59:02 +00:00
|
|
|
"github.com/hashicorp/memberlist"
|
2016-07-28 19:11:28 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2014-06-18 16:03:30 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-09 23:29:44 +00:00
|
|
|
)
|
|
|
|
|
2014-03-20 19:51:49 +00:00
|
|
|
const (
|
|
|
|
// StatusReap is used to update the status of a node if we
|
|
|
|
// are handling a EventMemberReap
|
|
|
|
StatusReap = serf.MemberStatus(-1)
|
2014-08-27 02:04:07 +00:00
|
|
|
|
|
|
|
// userEventPrefix is pre-pended to a user event to distinguish it
|
|
|
|
userEventPrefix = "consul:event:"
|
2016-12-01 22:05:02 +00:00
|
|
|
|
|
|
|
// maxPeerRetries limits how many invalidate attempts are made
|
|
|
|
maxPeerRetries = 6
|
2014-03-20 19:51:49 +00:00
|
|
|
)
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// setupSerf is used to setup and initialize a Serf
|
2017-09-01 00:39:46 +00:00
|
|
|
func (s *Server) setupSerf(conf *serf.Config, ch chan serf.Event, path string, wan bool, wanPort int,
|
|
|
|
segment string, listener net.Listener) (*serf.Serf, error) {
|
2017-08-14 14:36:07 +00:00
|
|
|
conf.Init()
|
|
|
|
|
|
|
|
if wan {
|
|
|
|
conf.NodeName = fmt.Sprintf("%s.%s", s.config.NodeName, s.config.Datacenter)
|
|
|
|
} else {
|
|
|
|
conf.NodeName = s.config.NodeName
|
2018-03-26 19:21:06 +00:00
|
|
|
if wanPort > 0 {
|
|
|
|
conf.Tags["wan_join_port"] = fmt.Sprintf("%d", wanPort)
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
conf.Tags["role"] = "consul"
|
|
|
|
conf.Tags["dc"] = s.config.Datacenter
|
|
|
|
conf.Tags["segment"] = segment
|
|
|
|
conf.Tags["id"] = string(s.config.NodeID)
|
|
|
|
conf.Tags["vsn"] = fmt.Sprintf("%d", s.config.ProtocolVersion)
|
|
|
|
conf.Tags["vsn_min"] = fmt.Sprintf("%d", ProtocolVersionMin)
|
|
|
|
conf.Tags["vsn_max"] = fmt.Sprintf("%d", ProtocolVersionMax)
|
|
|
|
conf.Tags["raft_vsn"] = fmt.Sprintf("%d", s.config.RaftConfig.ProtocolVersion)
|
|
|
|
conf.Tags["build"] = s.config.Build
|
2017-09-01 00:39:46 +00:00
|
|
|
addr := listener.Addr().(*net.TCPAddr)
|
2017-08-14 14:36:07 +00:00
|
|
|
conf.Tags["port"] = fmt.Sprintf("%d", addr.Port)
|
|
|
|
if s.config.Bootstrap {
|
|
|
|
conf.Tags["bootstrap"] = "1"
|
|
|
|
}
|
|
|
|
if s.config.BootstrapExpect != 0 {
|
|
|
|
conf.Tags["expect"] = fmt.Sprintf("%d", s.config.BootstrapExpect)
|
|
|
|
}
|
|
|
|
if s.config.NonVoter {
|
|
|
|
conf.Tags["nonvoter"] = "1"
|
|
|
|
}
|
|
|
|
if s.config.UseTLS {
|
|
|
|
conf.Tags["use_tls"] = "1"
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2018-10-31 20:00:46 +00:00
|
|
|
if s.acls.ACLsEnabled() {
|
2018-10-19 16:04:07 +00:00
|
|
|
// we start in legacy mode and allow upgrading later
|
|
|
|
conf.Tags["acls"] = string(structs.ACLModeLegacy)
|
|
|
|
} else {
|
|
|
|
conf.Tags["acls"] = string(structs.ACLModeDisabled)
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2020-06-04 21:05:27 +00:00
|
|
|
// feature flag: advertise support for federation states
|
|
|
|
conf.Tags["ft_fs"] = "1"
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// feature flag: advertise support for service-intentions
|
|
|
|
conf.Tags["ft_si"] = "1"
|
|
|
|
|
2020-02-05 15:52:43 +00:00
|
|
|
var subLoggerName string
|
|
|
|
if wan {
|
|
|
|
subLoggerName = logging.WAN
|
|
|
|
} else {
|
|
|
|
subLoggerName = logging.LAN
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
// Wrap hclog in a standard logger wrapper for serf and memberlist
|
|
|
|
// We use the Intercept variant here to ensure that serf and memberlist logs
|
|
|
|
// can be streamed via the monitor endpoint
|
|
|
|
serfLogger := s.logger.
|
|
|
|
NamedIntercept(logging.Serf).
|
2020-02-05 15:52:43 +00:00
|
|
|
NamedIntercept(subLoggerName).
|
2020-01-28 23:50:41 +00:00
|
|
|
StandardLoggerIntercept(&hclog.StandardLoggerOptions{InferLevels: true})
|
|
|
|
memberlistLogger := s.logger.
|
|
|
|
NamedIntercept(logging.Memberlist).
|
2020-02-05 15:52:43 +00:00
|
|
|
NamedIntercept(subLoggerName).
|
2020-01-28 23:50:41 +00:00
|
|
|
StandardLoggerIntercept(&hclog.StandardLoggerOptions{InferLevels: true})
|
|
|
|
|
|
|
|
conf.MemberlistConfig.Logger = memberlistLogger
|
|
|
|
conf.Logger = serfLogger
|
2017-08-14 14:36:07 +00:00
|
|
|
conf.EventCh = ch
|
|
|
|
conf.ProtocolVersion = protocolVersionMap[s.config.ProtocolVersion]
|
|
|
|
conf.RejoinAfterLeave = s.config.RejoinAfterLeave
|
|
|
|
if wan {
|
|
|
|
conf.Merge = &wanMergeDelegate{}
|
|
|
|
} else {
|
|
|
|
conf.Merge = &lanMergeDelegate{
|
|
|
|
dc: s.config.Datacenter,
|
|
|
|
nodeID: s.config.NodeID,
|
|
|
|
nodeName: s.config.NodeName,
|
|
|
|
segment: segment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if wan {
|
|
|
|
nt, err := memberlist.NewNetTransport(&memberlist.NetTransportConfig{
|
|
|
|
BindAddrs: []string{conf.MemberlistConfig.BindAddr},
|
|
|
|
BindPort: conf.MemberlistConfig.BindPort,
|
|
|
|
Logger: conf.MemberlistConfig.Logger,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.config.ConnectMeshGatewayWANFederationEnabled {
|
|
|
|
mgwTransport, err := wanfed.NewTransport(
|
|
|
|
s.tlsConfigurator,
|
|
|
|
nt,
|
|
|
|
s.config.Datacenter,
|
|
|
|
s.gatewayLocator.PickGateway,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.MemberlistConfig.Transport = mgwTransport
|
|
|
|
} else {
|
|
|
|
conf.MemberlistConfig.Transport = nt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// Until Consul supports this fully, we disable automatic resolution.
|
|
|
|
// When enabled, the Serf gossip may just turn off if we are the minority
|
|
|
|
// node which is rather unexpected.
|
|
|
|
conf.EnableNameConflictResolution = false
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if wan && s.config.ConnectMeshGatewayWANFederationEnabled {
|
|
|
|
conf.MemberlistConfig.RequireNodeNames = true
|
|
|
|
conf.MemberlistConfig.DisableTcpPingsForNode = func(nodeName string) bool {
|
|
|
|
_, dc, err := wanfed.SplitNodeName(nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return false // don't disable anything if we don't understand the node name
|
|
|
|
}
|
|
|
|
|
|
|
|
// If doing cross-dc we will be using TCP via the gateways so
|
|
|
|
// there's no need for an extra TCP request.
|
|
|
|
return s.config.Datacenter != dc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
if !s.config.DevMode {
|
|
|
|
conf.SnapshotPath = filepath.Join(s.config.DataDir, path)
|
|
|
|
}
|
|
|
|
if err := lib.EnsurePath(conf.SnapshotPath, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:02:19 +00:00
|
|
|
conf.ReconnectTimeoutOverride = libserf.NewReconnectOverride(s.logger)
|
|
|
|
|
2020-01-29 18:21:38 +00:00
|
|
|
s.addEnterpriseSerfTags(conf.Tags)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if s.config.OverrideInitialSerfTags != nil {
|
|
|
|
s.config.OverrideInitialSerfTags(conf.Tags)
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
return serf.Create(conf)
|
|
|
|
}
|
|
|
|
|
2014-08-27 01:50:03 +00:00
|
|
|
// userEventName computes the name of a user event
|
|
|
|
func userEventName(name string) string {
|
2014-08-27 02:04:07 +00:00
|
|
|
return userEventPrefix + name
|
|
|
|
}
|
|
|
|
|
|
|
|
// isUserEvent checks if a serf event is a user event
|
|
|
|
func isUserEvent(name string) bool {
|
|
|
|
return strings.HasPrefix(name, userEventPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rawUserEventName is used to get the raw user event name
|
|
|
|
func rawUserEventName(name string) string {
|
|
|
|
return strings.TrimPrefix(name, userEventPrefix)
|
2014-08-27 01:50:03 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 00:54:33 +00:00
|
|
|
// lanEventHandler is used to handle events from the lan Serf cluster
|
|
|
|
func (s *Server) lanEventHandler() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-s.eventChLAN:
|
2013-12-09 23:29:44 +00:00
|
|
|
switch e.EventType() {
|
|
|
|
case serf.EventMemberJoin:
|
2015-07-29 23:33:25 +00:00
|
|
|
s.lanNodeJoin(e.(serf.MemberEvent))
|
2014-05-27 22:16:07 +00:00
|
|
|
s.localMemberEvent(e.(serf.MemberEvent))
|
|
|
|
|
2019-03-04 14:19:35 +00:00
|
|
|
case serf.EventMemberLeave, serf.EventMemberFailed, serf.EventMemberReap:
|
2015-07-29 23:33:25 +00:00
|
|
|
s.lanNodeFailed(e.(serf.MemberEvent))
|
2014-05-27 22:16:07 +00:00
|
|
|
s.localMemberEvent(e.(serf.MemberEvent))
|
|
|
|
|
2013-12-09 23:29:44 +00:00
|
|
|
case serf.EventUser:
|
2014-02-19 20:36:27 +00:00
|
|
|
s.localEvent(e.(serf.UserEvent))
|
2017-03-21 23:36:44 +00:00
|
|
|
case serf.EventMemberUpdate:
|
2020-03-16 16:54:45 +00:00
|
|
|
s.lanNodeUpdate(e.(serf.MemberEvent))
|
2017-03-15 23:09:55 +00:00
|
|
|
s.localMemberEvent(e.(serf.MemberEvent))
|
2014-03-12 19:46:14 +00:00
|
|
|
case serf.EventQuery: // Ignore
|
2013-12-09 23:29:44 +00:00
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Unhandled LAN Serf Event", "event", e)
|
2013-12-09 23:29:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 00:54:33 +00:00
|
|
|
case <-s.shutdownCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 23:49:09 +00:00
|
|
|
// localMemberEvent is used to reconcile Serf events with the strongly
|
|
|
|
// consistent store if we are the current leader
|
|
|
|
func (s *Server) localMemberEvent(me serf.MemberEvent) {
|
|
|
|
// Do nothing if we are not the leader
|
|
|
|
if !s.IsLeader() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-20 19:51:49 +00:00
|
|
|
// Check if this is a reap event
|
|
|
|
isReap := me.EventType() == serf.EventMemberReap
|
|
|
|
|
2014-01-10 20:55:55 +00:00
|
|
|
// Queue the members for reconciliation
|
2014-01-09 23:49:09 +00:00
|
|
|
for _, m := range me.Members {
|
2014-03-20 19:51:49 +00:00
|
|
|
// Change the status if this is a reap event
|
|
|
|
if isReap {
|
|
|
|
m.Status = StatusReap
|
|
|
|
}
|
2014-01-10 20:55:55 +00:00
|
|
|
select {
|
|
|
|
case s.reconcileCh <- m:
|
|
|
|
default:
|
2013-12-10 00:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-09 23:29:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 20:36:27 +00:00
|
|
|
// localEvent is called when we receive an event on the local Serf
|
|
|
|
func (s *Server) localEvent(event serf.UserEvent) {
|
|
|
|
// Handle only consul events
|
|
|
|
if !strings.HasPrefix(event.Name, "consul:") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-27 02:04:07 +00:00
|
|
|
switch name := event.Name; {
|
|
|
|
case name == newLeaderEvent:
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("New leader elected", "payload", string(event.Payload))
|
2014-02-19 20:36:27 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if s.config.ServerUp != nil {
|
|
|
|
s.config.ServerUp()
|
|
|
|
}
|
2014-08-27 02:04:07 +00:00
|
|
|
case isUserEvent(name):
|
|
|
|
event.Name = rawUserEventName(name)
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Debug("User event", "event", event.Name)
|
2014-08-27 02:04:07 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if s.config.UserEventHandler != nil {
|
|
|
|
s.config.UserEventHandler(event)
|
|
|
|
}
|
2014-02-19 20:36:27 +00:00
|
|
|
default:
|
2018-05-24 14:36:42 +00:00
|
|
|
if !s.handleEnterpriseUserEvents(event) {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Unhandled local event", "event", event)
|
2018-05-24 14:36:42 +00:00
|
|
|
}
|
2014-02-19 20:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
// lanNodeJoin is used to handle join events on the LAN pool.
|
|
|
|
func (s *Server) lanNodeJoin(me serf.MemberEvent) {
|
2013-12-12 00:24:34 +00:00
|
|
|
for _, m := range me.Members {
|
2017-08-30 03:35:22 +00:00
|
|
|
ok, serverMeta := metadata.IsConsulServer(m)
|
2017-08-14 14:36:07 +00:00
|
|
|
if !ok || serverMeta.Segment != "" {
|
2013-12-12 00:24:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("Adding LAN server", "server", serverMeta.String())
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2017-08-30 03:35:22 +00:00
|
|
|
// Update server lookup
|
|
|
|
s.serverLookup.AddServer(serverMeta)
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2016-11-03 05:54:43 +00:00
|
|
|
// If we're still expecting to bootstrap, may need to handle this.
|
2015-07-29 23:33:25 +00:00
|
|
|
if s.config.BootstrapExpect != 0 {
|
|
|
|
s.maybeBootstrap()
|
|
|
|
}
|
2017-03-15 19:26:54 +00:00
|
|
|
|
2017-03-15 23:11:19 +00:00
|
|
|
// Kick the join flooders.
|
|
|
|
s.FloodNotify()
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:21:38 +00:00
|
|
|
func (s *Server) lanNodeUpdate(me serf.MemberEvent) {
|
|
|
|
for _, m := range me.Members {
|
|
|
|
ok, serverMeta := metadata.IsConsulServer(m)
|
|
|
|
if !ok || serverMeta.Segment != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.logger.Info("Updating LAN server", "server", serverMeta.String())
|
|
|
|
|
|
|
|
// Update server lookup
|
|
|
|
s.serverLookup.AddServer(serverMeta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:22:32 +00:00
|
|
|
// maybeBootstrap is used to handle bootstrapping when a new consul server joins.
|
2014-06-18 23:15:28 +00:00
|
|
|
func (s *Server) maybeBootstrap() {
|
2016-07-28 19:11:28 +00:00
|
|
|
// Bootstrap can only be done if there are no committed logs, remove our
|
|
|
|
// expectations of bootstrapping. This is slightly cheaper than the full
|
|
|
|
// check that BootstrapCluster will do, so this is a good pre-filter.
|
2014-06-18 23:15:28 +00:00
|
|
|
index, err := s.raftStore.LastIndex()
|
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Failed to read last raft index", "error", err)
|
2014-06-18 23:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if index != 0 {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("Raft data found, disabling bootstrap mode")
|
2014-06-20 00:08:48 +00:00
|
|
|
s.config.BootstrapExpect = 0
|
2014-06-18 23:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-28 19:11:28 +00:00
|
|
|
// Scan for all the known servers.
|
2014-06-18 23:15:28 +00:00
|
|
|
members := s.serfLAN.Members()
|
2017-07-06 10:48:37 +00:00
|
|
|
var servers []metadata.Server
|
2018-09-20 00:41:36 +00:00
|
|
|
voters := 0
|
2014-06-18 23:15:28 +00:00
|
|
|
for _, member := range members {
|
2017-07-06 10:48:37 +00:00
|
|
|
valid, p := metadata.IsConsulServer(member)
|
2014-06-18 23:15:28 +00:00
|
|
|
if !valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p.Datacenter != s.config.Datacenter {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("Member has a conflicting datacenter, ignoring", "member", member)
|
2014-06-18 23:15:28 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-06-20 00:08:48 +00:00
|
|
|
if p.Expect != 0 && p.Expect != s.config.BootstrapExpect {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Member has a conflicting expect value. All nodes should expect the same number.", "member", member)
|
2014-06-18 23:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if p.Bootstrap {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Member has bootstrap mode. Expect disabled.", "member", member)
|
2014-06-18 23:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
2018-09-20 00:41:36 +00:00
|
|
|
if !p.NonVoter {
|
|
|
|
voters++
|
|
|
|
}
|
2016-09-01 04:22:32 +00:00
|
|
|
servers = append(servers, *p)
|
2014-06-18 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 19:11:28 +00:00
|
|
|
// Skip if we haven't met the minimum expect count.
|
2018-09-20 00:41:36 +00:00
|
|
|
if voters < s.config.BootstrapExpect {
|
2014-06-18 23:15:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:22:32 +00:00
|
|
|
// Query each of the servers and make sure they report no Raft peers.
|
|
|
|
for _, server := range servers {
|
|
|
|
var peers []string
|
2016-12-01 22:05:02 +00:00
|
|
|
|
|
|
|
// Retry with exponential backoff to get peer status from this server
|
|
|
|
for attempt := uint(0); attempt < maxPeerRetries; attempt++ {
|
2020-05-28 07:48:34 +00:00
|
|
|
if err := s.connPool.RPC(s.config.Datacenter, server.ShortName, server.Addr,
|
2020-04-30 20:12:17 +00:00
|
|
|
"Status.Peers", &structs.DCSpecificRequest{Datacenter: s.config.Datacenter}, &peers); err != nil {
|
2020-04-16 17:35:28 +00:00
|
|
|
nextRetry := (1 << attempt) * time.Second
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Failed to confirm peer status for server (will retry).",
|
|
|
|
"server", server.Name,
|
|
|
|
"retry_interval", nextRetry.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2016-12-01 22:05:02 +00:00
|
|
|
time.Sleep(nextRetry)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2016-09-01 04:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Found a node with some Raft peers, stop bootstrap since there's
|
|
|
|
// evidence of an existing cluster. We should get folded in by the
|
2016-09-01 06:54:53 +00:00
|
|
|
// existing servers if that's the case, so it's cleaner to sit as a
|
2016-09-01 04:22:32 +00:00
|
|
|
// candidate with no peers so we don't cause spurious elections.
|
|
|
|
// It's OK this is racy, because even with an initial bootstrap
|
|
|
|
// as long as one peer runs bootstrap things will work, and if we
|
|
|
|
// have multiple peers bootstrap in the same way, that's OK. We
|
|
|
|
// just don't want a server added much later to do a live bootstrap
|
|
|
|
// and interfere with the cluster. This isn't required for Raft's
|
|
|
|
// correctness because no server in the existing cluster will vote
|
|
|
|
// for this server, but it makes things much more stable.
|
|
|
|
if len(peers) > 0 {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("Existing Raft peers reported by server, disabling bootstrap mode", "server", server.Name)
|
2016-09-01 04:22:32 +00:00
|
|
|
s.config.BootstrapExpect = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-28 19:11:28 +00:00
|
|
|
// Attempt a live bootstrap!
|
|
|
|
var configuration raft.Configuration
|
2016-09-01 04:22:32 +00:00
|
|
|
var addrs []string
|
2017-12-12 00:38:52 +00:00
|
|
|
minRaftVersion, err := s.autopilot.MinRaftProtocol()
|
2017-02-22 20:53:32 +00:00
|
|
|
if err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Failed to read server raft versions", "error", err)
|
2017-02-22 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 04:22:32 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
addr := server.Addr.String()
|
|
|
|
addrs = append(addrs, addr)
|
2017-02-22 20:53:32 +00:00
|
|
|
var id raft.ServerID
|
2017-02-24 04:32:13 +00:00
|
|
|
if minRaftVersion >= 3 {
|
2017-02-22 20:53:32 +00:00
|
|
|
id = raft.ServerID(server.ID)
|
|
|
|
} else {
|
|
|
|
id = raft.ServerID(addr)
|
|
|
|
}
|
2018-09-20 00:41:36 +00:00
|
|
|
suffrage := raft.Voter
|
|
|
|
if server.NonVoter {
|
|
|
|
suffrage = raft.Nonvoter
|
|
|
|
}
|
2016-09-01 04:22:32 +00:00
|
|
|
peer := raft.Server{
|
2018-09-20 00:41:36 +00:00
|
|
|
ID: id,
|
|
|
|
Address: raft.ServerAddress(addr),
|
|
|
|
Suffrage: suffrage,
|
2016-07-28 19:11:28 +00:00
|
|
|
}
|
2016-09-01 04:22:32 +00:00
|
|
|
configuration.Servers = append(configuration.Servers, peer)
|
2016-07-28 19:11:28 +00:00
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("Found expected number of peers, attempting bootstrap",
|
|
|
|
"peers", strings.Join(addrs, ","),
|
|
|
|
)
|
2016-07-28 19:11:28 +00:00
|
|
|
future := s.raft.BootstrapCluster(configuration)
|
|
|
|
if err := future.Error(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Error("Failed to bootstrap cluster", "error", err)
|
2014-06-18 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 04:22:32 +00:00
|
|
|
// Bootstrapping complete, or failed for some reason, don't enter this
|
|
|
|
// again.
|
2014-06-20 00:08:48 +00:00
|
|
|
s.config.BootstrapExpect = 0
|
2014-06-18 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
// lanNodeFailed is used to handle fail events on the LAN pool.
|
|
|
|
func (s *Server) lanNodeFailed(me serf.MemberEvent) {
|
|
|
|
for _, m := range me.Members {
|
2017-08-30 17:31:36 +00:00
|
|
|
ok, serverMeta := metadata.IsConsulServer(m)
|
2017-08-14 14:36:07 +00:00
|
|
|
if !ok || serverMeta.Segment != "" {
|
2015-07-29 23:33:25 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("Removing LAN server", "server", serverMeta.String())
|
2015-07-29 23:33:25 +00:00
|
|
|
|
2017-08-30 00:37:48 +00:00
|
|
|
// Update id to address map
|
2017-08-30 17:31:36 +00:00
|
|
|
s.serverLookup.RemoveServer(serverMeta)
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
}
|