2017-08-14 14:36:07 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-05-19 19:07:24 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
|
2017-08-14 14:36:07 +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"
|
2020-08-27 15:23:52 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2017-08-14 14:36:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
conf.Init()
|
|
|
|
|
|
|
|
conf.NodeName = c.config.NodeName
|
|
|
|
conf.Tags["role"] = "node"
|
|
|
|
conf.Tags["dc"] = c.config.Datacenter
|
|
|
|
conf.Tags["segment"] = c.config.Segment
|
|
|
|
conf.Tags["id"] = string(c.config.NodeID)
|
|
|
|
conf.Tags["vsn"] = fmt.Sprintf("%d", c.config.ProtocolVersion)
|
|
|
|
conf.Tags["vsn_min"] = fmt.Sprintf("%d", ProtocolVersionMin)
|
|
|
|
conf.Tags["vsn_max"] = fmt.Sprintf("%d", ProtocolVersionMax)
|
|
|
|
conf.Tags["build"] = c.config.Build
|
2020-10-08 19:02:19 +00:00
|
|
|
if c.config.AdvertiseReconnectTimeout != 0 {
|
|
|
|
conf.Tags[libserf.ReconnectTimeoutTag] = c.config.AdvertiseReconnectTimeout.String()
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
if c.acls.ACLsEnabled() {
|
|
|
|
// we start in legacy mode and then transition to normal
|
|
|
|
// mode once we know the cluster can handle it.
|
|
|
|
conf.Tags["acls"] = string(structs.ACLModeLegacy)
|
|
|
|
} else {
|
|
|
|
conf.Tags["acls"] = string(structs.ACLModeDisabled)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
// We use the Intercept variant here to ensure that serf and memberlist logs
|
|
|
|
// can be streamed via the monitor endpoint
|
|
|
|
serfLogger := c.logger.
|
|
|
|
NamedIntercept(logging.Serf).
|
2020-02-05 15:52:43 +00:00
|
|
|
NamedIntercept(logging.LAN).
|
2020-01-28 23:50:41 +00:00
|
|
|
StandardLoggerIntercept(&hclog.StandardLoggerOptions{InferLevels: true})
|
|
|
|
memberlistLogger := c.logger.
|
|
|
|
NamedIntercept(logging.Memberlist).
|
2020-02-05 15:52:43 +00:00
|
|
|
NamedIntercept(logging.LAN).
|
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[c.config.ProtocolVersion]
|
|
|
|
conf.RejoinAfterLeave = c.config.RejoinAfterLeave
|
|
|
|
conf.Merge = &lanMergeDelegate{
|
|
|
|
dc: c.config.Datacenter,
|
|
|
|
nodeID: c.config.NodeID,
|
|
|
|
nodeName: c.config.NodeName,
|
|
|
|
segment: c.config.Segment,
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.SnapshotPath = filepath.Join(c.config.DataDir, path)
|
|
|
|
if err := lib.EnsurePath(conf.SnapshotPath, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:07:24 +00:00
|
|
|
addEnterpriseSerfTags(conf.Tags)
|
2020-01-29 18:21:38 +00:00
|
|
|
|
2020-10-08 19:02:19 +00:00
|
|
|
conf.ReconnectTimeoutOverride = libserf.NewReconnectOverride(c.logger)
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
return serf.Create(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// lanEventHandler is used to handle events from the lan Serf cluster
|
|
|
|
func (c *Client) lanEventHandler() {
|
|
|
|
var numQueuedEvents int
|
|
|
|
for {
|
|
|
|
numQueuedEvents = len(c.eventCh)
|
|
|
|
if numQueuedEvents > serfEventBacklogWarning {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("number of queued serf events above warning threshold",
|
|
|
|
"queued_events", numQueuedEvents,
|
|
|
|
"warning_threshold", serfEventBacklogWarning,
|
|
|
|
)
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e := <-c.eventCh:
|
|
|
|
switch e.EventType() {
|
|
|
|
case serf.EventMemberJoin:
|
|
|
|
c.nodeJoin(e.(serf.MemberEvent))
|
2019-03-04 14:19:35 +00:00
|
|
|
case serf.EventMemberLeave, serf.EventMemberFailed, serf.EventMemberReap:
|
2017-08-14 14:36:07 +00:00
|
|
|
c.nodeFail(e.(serf.MemberEvent))
|
|
|
|
case serf.EventUser:
|
|
|
|
c.localEvent(e.(serf.UserEvent))
|
|
|
|
case serf.EventMemberUpdate: // Ignore
|
2020-01-29 18:21:38 +00:00
|
|
|
c.nodeUpdate(e.(serf.MemberEvent))
|
2017-08-14 14:36:07 +00:00
|
|
|
case serf.EventQuery: // Ignore
|
|
|
|
default:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("unhandled LAN Serf Event", "event", e)
|
2017-08-14 14:36:07 +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 {
|
|
|
|
ok, parts := metadata.IsConsulServer(m)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parts.Datacenter != c.config.Datacenter {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("server has joined the wrong cluster: wrong datacenter",
|
|
|
|
"server", m.Name,
|
|
|
|
"datacenter", parts.Datacenter,
|
|
|
|
)
|
2017-08-14 14:36:07 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("adding server", "server", parts)
|
2020-08-27 15:23:52 +00:00
|
|
|
c.router.AddServer(types.AreaLAN, parts)
|
2017-08-14 14:36:07 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if c.config.ServerUp != nil {
|
|
|
|
c.config.ServerUp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:21:38 +00:00
|
|
|
// nodeUpdate is used to handle update events on the serf cluster
|
|
|
|
func (c *Client) nodeUpdate(me serf.MemberEvent) {
|
|
|
|
for _, m := range me.Members {
|
|
|
|
ok, parts := metadata.IsConsulServer(m)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if parts.Datacenter != c.config.Datacenter {
|
|
|
|
c.logger.Warn("server has joined the wrong cluster: wrong datacenter",
|
|
|
|
"server", m.Name,
|
|
|
|
"datacenter", parts.Datacenter,
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.logger.Info("updating server", "server", parts.String())
|
2020-08-27 15:23:52 +00:00
|
|
|
c.router.AddServer(types.AreaLAN, parts)
|
2020-01-29 18:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +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 {
|
|
|
|
ok, parts := metadata.IsConsulServer(m)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("removing server", "server", parts.String())
|
2020-08-27 15:23:52 +00:00
|
|
|
c.router.RemoveServer(types.AreaLAN, parts)
|
2017-08-14 14:36:07 +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 name := event.Name; {
|
|
|
|
case name == newLeaderEvent:
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Info("New leader elected", "payload", string(event.Payload))
|
2017-08-14 14:36:07 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if c.config.ServerUp != nil {
|
|
|
|
c.config.ServerUp()
|
|
|
|
}
|
|
|
|
case isUserEvent(name):
|
|
|
|
event.Name = rawUserEventName(name)
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Debug("user event", "name", event.Name)
|
2017-08-14 14:36:07 +00:00
|
|
|
|
|
|
|
// Trigger the callback
|
|
|
|
if c.config.UserEventHandler != nil {
|
|
|
|
c.config.UserEventHandler(event)
|
|
|
|
}
|
|
|
|
default:
|
2018-05-24 14:36:42 +00:00
|
|
|
if !c.handleEnterpriseUserEvents(event) {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("Unhandled local event", "event", event)
|
2018-05-24 14:36:42 +00:00
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
}
|