2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
package resolver
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-09 22:46:58 +00:00
|
|
|
"fmt"
|
2020-09-09 20:37:43 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/resolver"
|
2020-10-13 22:45:17 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2021-12-06 15:55:54 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-09-09 20:37:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerResolverBuilder tracks the current server list and keeps any
|
|
|
|
// ServerResolvers updated when changes occur.
|
|
|
|
type ServerResolverBuilder struct {
|
2021-06-01 22:31:52 +00:00
|
|
|
cfg Config
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
// leaderResolver is used to track the address of the leader in the local DC.
|
|
|
|
leaderResolver leaderResolver
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
// servers is an index of Servers by area and Server.ID. The map contains server IDs
|
2020-10-13 22:45:17 +00:00
|
|
|
// for all datacenters.
|
2021-12-06 15:55:54 +00:00
|
|
|
servers map[types.AreaID]map[string]*metadata.Server
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// resolvers is an index of connections to the serverResolver which manages
|
|
|
|
// addresses of servers for that connection.
|
2023-05-11 16:08:57 +00:00
|
|
|
//
|
|
|
|
// this is only applicable for non-leader conn types
|
2020-09-09 22:46:58 +00:00
|
|
|
resolvers map[resolver.ClientConn]*serverResolver
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
// lock for all stateful fields (excludes config which is immutable).
|
2020-09-09 22:46:58 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2023-05-11 16:08:57 +00:00
|
|
|
// Datacenter is the datacenter of this agent.
|
|
|
|
Datacenter string
|
|
|
|
|
|
|
|
// AgentType is either 'server' or 'client' and is required.
|
|
|
|
AgentType string
|
|
|
|
|
2021-06-01 22:31:52 +00:00
|
|
|
// Authority used to query the server. Defaults to "". Used to support
|
|
|
|
// parallel testing because gRPC registers resolvers globally.
|
|
|
|
Authority string
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 20:16:44 +00:00
|
|
|
func NewServerResolverBuilder(cfg Config) *ServerResolverBuilder {
|
2023-05-11 16:08:57 +00:00
|
|
|
if cfg.Datacenter == "" {
|
|
|
|
panic("ServerResolverBuilder needs Config.Datacenter to be nonempty")
|
|
|
|
}
|
|
|
|
switch cfg.AgentType {
|
|
|
|
case "server", "client":
|
|
|
|
default:
|
|
|
|
panic("ServerResolverBuilder needs Config.AgentType to be either server or client")
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
return &ServerResolverBuilder{
|
2021-06-01 22:31:52 +00:00
|
|
|
cfg: cfg,
|
2021-12-06 15:55:54 +00:00
|
|
|
servers: make(map[types.AreaID]map[string]*metadata.Server),
|
2020-09-14 20:16:44 +00:00
|
|
|
resolvers: make(map[resolver.ClientConn]*serverResolver),
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
// ServerForGlobalAddr returns server metadata for a server with the specified globally unique address.
|
|
|
|
func (s *ServerResolverBuilder) ServerForGlobalAddr(globalAddr string) (*metadata.Server, error) {
|
2020-09-09 22:46:58 +00:00
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
for _, areaServers := range s.servers {
|
|
|
|
for _, server := range areaServers {
|
|
|
|
if DCPrefix(server.Datacenter, server.Addr.String()) == globalAddr {
|
|
|
|
return server, nil
|
|
|
|
}
|
2020-09-09 22:46:58 +00:00
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
2023-05-11 16:08:57 +00:00
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
return nil, fmt.Errorf("failed to find Consul server for global address %q", globalAddr)
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// Build returns a new serverResolver for the given ClientConn. The resolver
|
2020-09-09 20:37:43 +00:00
|
|
|
// will keep the ClientConn's state updated based on updates from Serf.
|
2022-03-23 16:10:03 +00:00
|
|
|
func (s *ServerResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
|
2020-09-09 20:37:43 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// If there's already a resolver for this connection, return it.
|
|
|
|
// TODO(streaming): how would this happen since we already cache connections in ClientConnPool?
|
2021-07-22 18:58:08 +00:00
|
|
|
if cc == s.leaderResolver.clientConn {
|
|
|
|
return s.leaderResolver, nil
|
|
|
|
}
|
2023-05-11 16:08:57 +00:00
|
|
|
if resolver, ok := s.resolvers[cc]; ok {
|
|
|
|
return resolver, nil
|
|
|
|
}
|
2021-07-22 18:58:08 +00:00
|
|
|
|
2022-11-07 16:34:30 +00:00
|
|
|
//nolint:staticcheck
|
2023-06-05 20:08:39 +00:00
|
|
|
serverType, datacenter, err := parseEndpoint(target.Endpoint())
|
2021-07-22 18:58:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if serverType == "leader" {
|
|
|
|
// TODO: is this safe? can we ever have multiple CC for the leader? Seems
|
|
|
|
// like we can only have one given the caching in ClientConnPool.Dial
|
|
|
|
s.leaderResolver.clientConn = cc
|
|
|
|
s.leaderResolver.updateClientConn()
|
|
|
|
return s.leaderResolver, nil
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
// Make a new resolver for the dc and add it to the list of active ones.
|
2020-09-09 22:46:58 +00:00
|
|
|
resolver := &serverResolver{
|
2020-09-09 20:37:43 +00:00
|
|
|
datacenter: datacenter,
|
|
|
|
clientConn: cc,
|
2020-09-09 22:46:58 +00:00
|
|
|
close: func() {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
delete(s.resolvers, cc)
|
|
|
|
},
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
resolver.updateAddrs(s.getDCAddrs(datacenter))
|
|
|
|
|
|
|
|
s.resolvers[cc] = resolver
|
|
|
|
return resolver, nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
// parseEndpoint parses a string, expecting a format of "serverType.datacenter"
|
|
|
|
func parseEndpoint(target string) (string, string, error) {
|
|
|
|
parts := strings.SplitN(target, ".", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", fmt.Errorf("unexpected endpoint address: %v", target)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 22:31:52 +00:00
|
|
|
func (s *ServerResolverBuilder) Authority() string {
|
|
|
|
return s.cfg.Authority
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
// AddServer updates the resolvers' states to include the new server's address.
|
2021-12-06 15:55:54 +00:00
|
|
|
func (s *ServerResolverBuilder) AddServer(areaID types.AreaID, server *metadata.Server) {
|
2023-05-11 16:08:57 +00:00
|
|
|
if s.shouldIgnoreServer(areaID, server) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:37:43 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
areaServers, ok := s.servers[areaID]
|
|
|
|
if !ok {
|
|
|
|
areaServers = make(map[string]*metadata.Server)
|
|
|
|
s.servers[areaID] = areaServers
|
|
|
|
}
|
|
|
|
|
|
|
|
areaServers[uniqueID(server)] = server
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
addrs := s.getDCAddrs(server.Datacenter)
|
|
|
|
for _, resolver := range s.resolvers {
|
|
|
|
if resolver.datacenter == server.Datacenter {
|
|
|
|
resolver.updateAddrs(addrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:45:17 +00:00
|
|
|
// uniqueID returns a unique identifier for the server which includes the
|
|
|
|
// Datacenter and the ID.
|
|
|
|
//
|
|
|
|
// In practice it is expected that the server.ID is already a globally unique
|
|
|
|
// UUID. This function is an extra safeguard in case that ever changes.
|
|
|
|
func uniqueID(server *metadata.Server) string {
|
|
|
|
return server.Datacenter + "-" + server.ID
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
// DCPrefix prefixes the given string with a datacenter for use in
|
|
|
|
// disambiguation.
|
|
|
|
func DCPrefix(datacenter, suffix string) string {
|
|
|
|
return datacenter + "-" + suffix
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:37:43 +00:00
|
|
|
// RemoveServer updates the resolvers' states with the given server removed.
|
2021-12-06 15:55:54 +00:00
|
|
|
func (s *ServerResolverBuilder) RemoveServer(areaID types.AreaID, server *metadata.Server) {
|
2023-05-11 16:08:57 +00:00
|
|
|
if s.shouldIgnoreServer(areaID, server) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:37:43 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
areaServers, ok := s.servers[areaID]
|
|
|
|
if !ok {
|
|
|
|
return // already gone
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(areaServers, uniqueID(server))
|
|
|
|
if len(areaServers) == 0 {
|
|
|
|
delete(s.servers, areaID)
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
|
|
|
|
addrs := s.getDCAddrs(server.Datacenter)
|
|
|
|
for _, resolver := range s.resolvers {
|
|
|
|
if resolver.datacenter == server.Datacenter {
|
|
|
|
resolver.updateAddrs(addrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
// shouldIgnoreServer is used to contextually decide if a particular kind of
|
|
|
|
// server should be accepted into a given area.
|
|
|
|
//
|
|
|
|
// On client agents it's pretty easy: clients only participate in the standard
|
|
|
|
// LAN, so we only accept servers from the LAN.
|
|
|
|
//
|
|
|
|
// On server agents it's a little less obvious. This resolver is ultimately
|
|
|
|
// used to have servers dial other servers. If a server is going to cross
|
|
|
|
// between datacenters (using traditional federation) then we want to use the
|
|
|
|
// WAN addresses for them, but if a server is going to dial a sibling server in
|
|
|
|
// the same datacenter we want it to use the LAN addresses always. To achieve
|
|
|
|
// that here we simply never allow WAN servers for our current datacenter to be
|
|
|
|
// added into the resolver, letting only the LAN instances through.
|
|
|
|
func (s *ServerResolverBuilder) shouldIgnoreServer(areaID types.AreaID, server *metadata.Server) bool {
|
|
|
|
if s.cfg.AgentType == "client" && areaID != types.AreaLAN {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.cfg.AgentType == "server" &&
|
|
|
|
server.Datacenter == s.cfg.Datacenter &&
|
|
|
|
areaID != types.AreaLAN {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:37:43 +00:00
|
|
|
// getDCAddrs returns a list of the server addresses for the given datacenter.
|
2020-09-09 22:46:58 +00:00
|
|
|
// This method requires that lock is held for reads.
|
2020-09-09 20:37:43 +00:00
|
|
|
func (s *ServerResolverBuilder) getDCAddrs(dc string) []resolver.Address {
|
2023-05-11 16:08:57 +00:00
|
|
|
lanRequest := (s.cfg.Datacenter == dc)
|
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
var (
|
|
|
|
addrs []resolver.Address
|
|
|
|
keptServerIDs = make(map[string]struct{})
|
|
|
|
)
|
2023-05-11 16:08:57 +00:00
|
|
|
for areaID, areaServers := range s.servers {
|
|
|
|
if (areaID == types.AreaLAN) != lanRequest {
|
|
|
|
// LAN requests only look at LAN data. WAN requests only look at
|
|
|
|
// WAN data.
|
|
|
|
continue
|
|
|
|
}
|
2021-12-06 15:55:54 +00:00
|
|
|
for _, server := range areaServers {
|
|
|
|
if server.Datacenter != dc {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2021-12-06 15:55:54 +00:00
|
|
|
// Servers may be part of multiple areas, so only include each one once.
|
|
|
|
if _, ok := keptServerIDs[server.ID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
keptServerIDs[server.ID] = struct{}{}
|
|
|
|
|
|
|
|
addrs = append(addrs, resolver.Address{
|
|
|
|
// NOTE: the address persisted here is only dialable using our custom dialer
|
|
|
|
Addr: DCPrefix(server.Datacenter, server.Addr.String()),
|
|
|
|
ServerName: server.Name,
|
|
|
|
})
|
|
|
|
}
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
// UpdateLeaderAddr updates the leader address in the local DC's resolver.
|
2021-08-24 21:28:44 +00:00
|
|
|
func (s *ServerResolverBuilder) UpdateLeaderAddr(datacenter, addr string) {
|
2021-07-22 18:58:08 +00:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
s.leaderResolver.globalAddr = DCPrefix(datacenter, addr)
|
2021-07-22 18:58:08 +00:00
|
|
|
s.leaderResolver.updateClientConn()
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// serverResolver is a grpc Resolver that will keep a grpc.ClientConn up to date
|
2020-09-09 20:37:43 +00:00
|
|
|
// on the list of server addresses to use.
|
2020-09-09 22:46:58 +00:00
|
|
|
type serverResolver struct {
|
|
|
|
// datacenter that can be reached by the clientConn. Used by ServerResolverBuilder
|
|
|
|
// to filter resolvers for those in a specific datacenter.
|
|
|
|
datacenter string
|
2020-09-09 20:37:43 +00:00
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// clientConn that this resolver is providing addresses for.
|
|
|
|
clientConn resolver.ClientConn
|
|
|
|
|
|
|
|
// close is used by ServerResolverBuilder to remove this resolver from the
|
|
|
|
// index of resolvers. It is called by grpc when the connection is closed.
|
|
|
|
close func()
|
|
|
|
|
|
|
|
// addrs stores the list of addresses passed to updateAddrs, so that they
|
|
|
|
// can be rebalanced periodically by ServerResolverBuilder.
|
|
|
|
addrs []resolver.Address
|
|
|
|
addrLock sync.Mutex
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
var _ resolver.Resolver = (*serverResolver)(nil)
|
|
|
|
|
|
|
|
// updateAddrs updates this serverResolver's ClientConn to use the given set of
|
2020-09-09 20:37:43 +00:00
|
|
|
// addrs.
|
2020-09-09 22:46:58 +00:00
|
|
|
func (r *serverResolver) updateAddrs(addrs []resolver.Address) {
|
2020-09-09 20:37:43 +00:00
|
|
|
r.addrLock.Lock()
|
|
|
|
defer r.addrLock.Unlock()
|
|
|
|
|
|
|
|
r.clientConn.UpdateState(resolver.State{Addresses: addrs})
|
2020-09-09 22:46:58 +00:00
|
|
|
r.addrs = addrs
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
func (r *serverResolver) Close() {
|
|
|
|
r.close()
|
2020-09-09 20:37:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 22:46:58 +00:00
|
|
|
// ResolveNow is not used
|
2022-03-23 16:10:03 +00:00
|
|
|
func (*serverResolver) ResolveNow(options resolver.ResolveNowOptions) {}
|
2021-07-22 18:58:08 +00:00
|
|
|
|
|
|
|
type leaderResolver struct {
|
2021-08-24 21:28:44 +00:00
|
|
|
globalAddr string
|
2021-07-22 18:58:08 +00:00
|
|
|
clientConn resolver.ClientConn
|
|
|
|
}
|
|
|
|
|
2022-03-23 16:10:03 +00:00
|
|
|
func (l leaderResolver) ResolveNow(resolver.ResolveNowOptions) {}
|
2021-07-22 18:58:08 +00:00
|
|
|
|
|
|
|
func (l leaderResolver) Close() {}
|
|
|
|
|
|
|
|
func (l leaderResolver) updateClientConn() {
|
2021-08-24 21:28:44 +00:00
|
|
|
if l.globalAddr == "" || l.clientConn == nil {
|
2021-07-22 18:58:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
addrs := []resolver.Address{
|
|
|
|
{
|
2021-08-24 21:28:44 +00:00
|
|
|
// NOTE: the address persisted here is only dialable using our custom dialer
|
|
|
|
Addr: l.globalAddr,
|
2021-07-22 18:58:08 +00:00
|
|
|
ServerName: "leader",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
l.clientConn.UpdateState(resolver.State{Addresses: addrs})
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ resolver.Resolver = (*leaderResolver)(nil)
|