Skip to content

Commit

Permalink
server: initialize mgw-wanfed to use local gateways more on startup (#…
Browse files Browse the repository at this point in the history
…9528)

Fixes #9342
  • Loading branch information
rboyer authored and hashicorp-ci committed Jan 25, 2021
1 parent 17e16f7 commit 685c38a
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 132 deletions.
3 changes: 3 additions & 0 deletions .changelog/9528.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
server: When wan federating via mesh gateways after initial federation default to using the local mesh gateways unless the heuristic indicates a bypass is required.
```
2 changes: 2 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4449,6 +4449,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
}
# wanfed
primary_gateways = ["` + gwAddr + `"]
retry_interval_wan = "250ms"
connect {
enabled = true
enable_mesh_gateway_wan_federation = true
Expand All @@ -4474,6 +4475,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
}
# wanfed
primary_gateways = ["` + gwAddr + `"]
retry_interval_wan = "250ms"
connect {
enabled = true
enable_mesh_gateway_wan_federation = true
Expand Down
29 changes: 22 additions & 7 deletions agent/consul/gateway_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ type GatewayLocator struct {
primaryDatacenter string

// these ONLY contain ones that have the wanfed:1 meta
gatewaysLock sync.Mutex
primaryGateways []string // WAN addrs
localGateways []string // LAN addrs
gatewaysLock sync.Mutex
primaryGateways []string // WAN addrs
localGateways []string // LAN addrs
populatedGateways bool

// primaryMeshGatewayDiscoveredAddresses is the current fallback addresses
// for the mesh gateways in the primary datacenter.
Expand Down Expand Up @@ -205,6 +206,10 @@ func (g *GatewayLocator) listGateways(primary bool) []string {
g.gatewaysLock.Lock()
defer g.gatewaysLock.Unlock()

if !g.populatedGateways {
return nil // don't even do anything yet
}

var addrs []string
if primary {
if g.datacenter == g.primaryDatacenter {
Expand Down Expand Up @@ -267,6 +272,7 @@ type serverDelegate interface {
blockingQuery(queryOpts structs.QueryOptionsCompat, queryMeta structs.QueryMetaCompat, fn queryFn) error
IsLeader() bool
LeaderLastContact() time.Time
setDatacenterSupportsFederationStates()
}

func NewGatewayLocator(
Expand All @@ -283,6 +289,8 @@ func NewGatewayLocator(
primaryGatewaysReadyCh: make(chan struct{}),
}
g.logPrimaryDialingMessage(g.DialPrimaryThroughLocalGateway())
// initialize
g.SetLastFederationStateReplicationError(nil, false)
return g
}

Expand All @@ -292,17 +300,18 @@ func (g *GatewayLocator) Run(ctx context.Context) {
var lastFetchIndex uint64
retryLoopBackoff(ctx, func() error {
idx, err := g.runOnce(lastFetchIndex)
if err != nil {
if errors.Is(err, errGatewayLocalStateNotInitialized) {
// don't do exponential backoff for something that's not broken
return nil
} else if err != nil {
return err
}

lastFetchIndex = idx

return nil
}, func(err error) {
if !errors.Is(err, errGatewayLocalStateNotInitialized) {
g.logger.Error("error tracking primary and local mesh gateways", "error", err)
}
g.logger.Error("error tracking primary and local mesh gateways", "error", err)
})
}

Expand Down Expand Up @@ -367,6 +376,10 @@ func (g *GatewayLocator) checkLocalStateIsReady() error {
}

func (g *GatewayLocator) updateFromState(results []*structs.FederationState) {
if len(results) > 0 {
g.srv.setDatacenterSupportsFederationStates()
}

var (
local structs.CheckServiceNodes
primary structs.CheckServiceNodes
Expand All @@ -388,6 +401,8 @@ func (g *GatewayLocator) updateFromState(results []*structs.FederationState) {
g.gatewaysLock.Lock()
defer g.gatewaysLock.Unlock()

g.populatedGateways = true

changed := false
primaryReady := false
if !stringslice.Equal(g.primaryGateways, primaryAddrs) {
Expand Down
Loading

0 comments on commit 685c38a

Please sign in to comment.