2013-12-12 19:07:14 +00:00
|
|
|
package consul
|
|
|
|
|
2013-12-19 22:18:55 +00:00
|
|
|
import (
|
2014-04-29 17:55:42 +00:00
|
|
|
"runtime"
|
2013-12-19 22:37:54 +00:00
|
|
|
"strconv"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
2017-04-13 00:09:57 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2014-06-16 21:36:12 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2021-09-22 23:34:14 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2013-12-19 22:18:55 +00:00
|
|
|
)
|
|
|
|
|
2015-10-27 21:30:29 +00:00
|
|
|
// CanServersUnderstandProtocol checks to see if all the servers in the given
|
2015-10-27 22:56:36 +00:00
|
|
|
// list understand the given protocol version. If there are no servers in the
|
|
|
|
// list then this will return false.
|
2015-10-27 21:30:29 +00:00
|
|
|
func CanServersUnderstandProtocol(members []serf.Member, version uint8) (bool, error) {
|
|
|
|
numServers, numWhoGrok := 0, 0
|
|
|
|
for _, m := range members {
|
|
|
|
if m.Tags["role"] != "consul" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numServers++
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
vsnMin, err := strconv.Atoi(m.Tags["vsn_min"])
|
2015-10-27 21:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
vsnMax, err := strconv.Atoi(m.Tags["vsn_max"])
|
2015-10-27 22:56:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := int(version)
|
2018-01-28 18:40:13 +00:00
|
|
|
if (v >= vsnMin) && (v <= vsnMax) {
|
2015-10-27 21:30:29 +00:00
|
|
|
numWhoGrok++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (numServers > 0) && (numWhoGrok == numServers), nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 22:36:59 +00:00
|
|
|
// Returns if a member is a consul node. Returns a bool,
|
2015-02-19 22:45:47 +00:00
|
|
|
// and the datacenter.
|
2014-01-09 23:45:14 +00:00
|
|
|
func isConsulNode(m serf.Member) (bool, string) {
|
2014-01-30 21:13:29 +00:00
|
|
|
if m.Tags["role"] != "node" {
|
2014-01-09 23:45:14 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
2014-01-30 21:13:29 +00:00
|
|
|
return true, m.Tags["dc"]
|
2014-01-09 23:45:14 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 17:55:42 +00:00
|
|
|
// runtimeStats is used to return various runtime information
|
|
|
|
func runtimeStats() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"os": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
"version": runtime.Version(),
|
|
|
|
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
|
|
|
|
"goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
|
|
|
|
"cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10),
|
|
|
|
}
|
|
|
|
}
|
2017-04-13 00:09:57 +00:00
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
// checkServersProvider exists so that we can unit tests the requirements checking functions
|
|
|
|
// without having to spin up a whole agent/server.
|
|
|
|
type checkServersProvider interface {
|
|
|
|
CheckServers(datacenter string, fn func(*metadata.Server) bool)
|
2019-07-26 19:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
// serverRequirementsFn should inspect the given metadata.Server struct
|
|
|
|
// and return two booleans. The first indicates whether the given requirements
|
|
|
|
// are met. The second indicates whether this server should be considered filtered.
|
|
|
|
//
|
|
|
|
// The reason for the two booleans is so that a requirement function could "filter"
|
|
|
|
// out the left server members if we only want to consider things which are still
|
|
|
|
// around or likely to come back (failed state).
|
|
|
|
type serverRequirementFn func(*metadata.Server) (ok bool, filtered bool)
|
|
|
|
|
|
|
|
type serversMeetRequirementsState struct {
|
|
|
|
// meetsRequirements is the callback to actual check for some specific requirement
|
|
|
|
meetsRequirements serverRequirementFn
|
|
|
|
|
|
|
|
// ok indicates whether all unfiltered servers meet the desired requirements
|
|
|
|
ok bool
|
|
|
|
|
|
|
|
// found is a boolean indicating that the meetsRequirement function accepted at
|
|
|
|
// least one unfiltered server.
|
|
|
|
found bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *serversMeetRequirementsState) update(srv *metadata.Server) bool {
|
|
|
|
ok, filtered := s.meetsRequirements(srv)
|
|
|
|
|
|
|
|
if filtered {
|
|
|
|
// keep going but don't update any of the internal state as this server
|
|
|
|
// was filtered by the requirements function
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark that at least one server processed was not filtered
|
|
|
|
s.found = true
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
// mark that at least one server does not meet the requirements
|
|
|
|
s.ok = false
|
|
|
|
|
|
|
|
// prevent continuing server evaluation
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// this should already be set but this will prevent accidentally reusing
|
|
|
|
// the state object from causing false-negatives.
|
|
|
|
s.ok = true
|
|
|
|
|
|
|
|
// continue evaluating servers
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServersInDCMeetRequirements returns whether the given server members meet the requirements as defined by the
|
|
|
|
// callback function and whether at least one server remains unfiltered by the requirements function.
|
|
|
|
func ServersInDCMeetRequirements(provider checkServersProvider, datacenter string, meetsRequirements serverRequirementFn) (ok bool, found bool) {
|
|
|
|
state := serversMeetRequirementsState{meetsRequirements: meetsRequirements, found: false, ok: true}
|
|
|
|
|
|
|
|
provider.CheckServers(datacenter, state.update)
|
|
|
|
|
|
|
|
return state.ok, state.found
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServersInDCMeetMinimumVersion returns whether the given alive servers from a particular
|
|
|
|
// datacenter are at least on the given Consul version. This also returns whether any
|
|
|
|
// alive or failed servers are known in that datacenter (ignoring left and leaving ones)
|
|
|
|
func ServersInDCMeetMinimumVersion(provider checkServersProvider, datacenter string, minVersion *version.Version) (ok bool, found bool) {
|
|
|
|
return ServersInDCMeetRequirements(provider, datacenter, func(srv *metadata.Server) (bool, bool) {
|
|
|
|
if srv.Status != serf.StatusAlive && srv.Status != serf.StatusFailed {
|
|
|
|
// filter out the left servers as those should not be factored into our requirements
|
|
|
|
return true, true
|
2019-07-26 19:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 16:31:43 +00:00
|
|
|
return !srv.Build.LessThan(minVersion), false
|
2019-07-26 19:57:57 +00:00
|
|
|
})
|
2020-03-27 16:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckServers implements the checkServersProvider interface for the Server
|
|
|
|
func (s *Server) CheckServers(datacenter string, fn func(*metadata.Server) bool) {
|
|
|
|
if datacenter == s.config.Datacenter {
|
|
|
|
// use the ServerLookup type for the local DC
|
|
|
|
s.serverLookup.CheckServers(fn)
|
|
|
|
} else {
|
|
|
|
// use the router for all non-local DCs
|
|
|
|
s.router.CheckServers(datacenter, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 15:54:27 +00:00
|
|
|
// CheckServers implements the checkServersProvider interface for the Client
|
|
|
|
func (c *Client) CheckServers(datacenter string, fn func(*metadata.Server) bool) {
|
|
|
|
if datacenter != c.config.Datacenter {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:23:52 +00:00
|
|
|
c.router.CheckServers(datacenter, fn)
|
2020-04-14 15:54:27 +00:00
|
|
|
}
|