2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-05-27 16:31:10 +00:00
|
|
|
"errors"
|
2021-01-25 23:30:38 +00:00
|
|
|
"sync/atomic"
|
2020-03-09 20:59:02 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-23 19:21:37 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-05-23 21:29:10 +00:00
|
|
|
"github.com/hashicorp/consul/agent/blockingquery"
|
2020-03-09 20:59:02 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGatewayLocator(t *testing.T) {
|
2020-10-23 19:21:37 +00:00
|
|
|
state := state.NewStateStore(nil)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
serverRoles := []string{"leader", "follower"}
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
dc1 := &structs.FederationState{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
MeshGateways: []structs.CheckServiceNode{
|
|
|
|
newTestMeshGatewayNode(
|
|
|
|
"dc1", "gateway1", "1.2.3.4", 5555, map[string]string{structs.MetaWANFederationKey: "1"}, api.HealthPassing,
|
|
|
|
),
|
|
|
|
newTestMeshGatewayNode(
|
|
|
|
"dc1", "gateway2", "4.3.2.1", 9999, map[string]string{structs.MetaWANFederationKey: "1"}, api.HealthPassing,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
dc2 := &structs.FederationState{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
MeshGateways: []structs.CheckServiceNode{
|
|
|
|
newTestMeshGatewayNode(
|
|
|
|
"dc2", "gateway1", "5.6.7.8", 5555, map[string]string{structs.MetaWANFederationKey: "1"}, api.HealthPassing,
|
|
|
|
),
|
|
|
|
newTestMeshGatewayNode(
|
|
|
|
"dc2", "gateway2", "8.7.6.5", 9999, map[string]string{structs.MetaWANFederationKey: "1"}, api.HealthPassing,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
UpdatedAt: time.Now().UTC(),
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
t.Run("primary - no data", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
for _, role := range serverRoles {
|
|
|
|
t.Run(role, func(t *testing.T) {
|
|
|
|
isLeader := role == "leader"
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: isLeader}
|
|
|
|
if !isLeader {
|
|
|
|
tsd.lastContact = now
|
|
|
|
}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc1",
|
|
|
|
"dc1",
|
|
|
|
)
|
|
|
|
g.SetUseReplicationSignal(isLeader)
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway()) // not important
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true))
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway()) // not important
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true))
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates()) // no results, so we don't flip the bit yet
|
|
|
|
})
|
2021-01-22 16:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - no data", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
for _, role := range serverRoles {
|
|
|
|
t.Run(role, func(t *testing.T) {
|
|
|
|
isLeader := role == "leader"
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: isLeader}
|
|
|
|
if !isLeader {
|
|
|
|
tsd.lastContact = now
|
|
|
|
}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
|
|
|
g.SetUseReplicationSignal(isLeader)
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true))
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true))
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates()) // no results, so we don't flip the bit yet
|
|
|
|
})
|
2021-01-22 16:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - just fallback", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
for _, role := range serverRoles {
|
|
|
|
t.Run(role, func(t *testing.T) {
|
|
|
|
isLeader := role == "leader"
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: isLeader}
|
|
|
|
if !isLeader {
|
|
|
|
tsd.lastContact = now
|
|
|
|
}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
|
|
|
g.SetUseReplicationSignal(isLeader)
|
|
|
|
g.RefreshPrimaryGatewayFallbackAddresses([]string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
})
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(1), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates()) // no results, so we don't flip the bit yet
|
|
|
|
})
|
2021-01-22 16:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// Insert data for the dcs
|
|
|
|
require.NoError(t, state.FederationStateSet(1, dc1))
|
|
|
|
require.NoError(t, state.FederationStateSet(2, dc2))
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
t.Run("primary - with data", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
for _, role := range serverRoles {
|
|
|
|
t.Run(role, func(t *testing.T) {
|
|
|
|
isLeader := role == "leader"
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: isLeader}
|
|
|
|
if !isLeader {
|
|
|
|
tsd.lastContact = now
|
|
|
|
}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc1",
|
|
|
|
"dc1",
|
|
|
|
)
|
|
|
|
g.SetUseReplicationSignal(isLeader)
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway()) // not important
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway()) // not important
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"1.2.3.4:5555",
|
|
|
|
"4.3.2.1:9999",
|
|
|
|
}, g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"1.2.3.4:5555",
|
|
|
|
"4.3.2.1:9999",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
2021-01-22 16:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - with data", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
for _, role := range serverRoles {
|
|
|
|
t.Run(role, func(t *testing.T) {
|
|
|
|
isLeader := role == "leader"
|
|
|
|
|
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: isLeader}
|
|
|
|
if !isLeader {
|
|
|
|
tsd.lastContact = now
|
|
|
|
}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
|
|
|
g.SetUseReplicationSignal(isLeader)
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
2021-01-22 16:03:24 +00:00
|
|
|
assert.Equal(t, []string{
|
2021-01-25 23:30:38 +00:00
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(false))
|
2021-01-22 16:03:24 +00:00
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(true))
|
2021-01-25 23:30:38 +00:00
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - with data and fallback - repl ok", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
// Only run for the leader.
|
2020-03-09 20:59:02 +00:00
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: true}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetUseReplicationSignal(true)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
g.RefreshPrimaryGatewayFallbackAddresses([]string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetLastFederationStateReplicationError(nil, true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
2020-05-27 16:31:10 +00:00
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway())
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - with data and fallback - repl ok then failed 2 times", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
// Only run for the leader.
|
2020-05-27 16:31:10 +00:00
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: true}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetUseReplicationSignal(true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
|
|
|
g.RefreshPrimaryGatewayFallbackAddresses([]string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetLastFederationStateReplicationError(nil, true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // defaults to sure!
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway())
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - with data and fallback - repl ok then failed 3 times", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
// Only run for the leader.
|
2020-05-27 16:31:10 +00:00
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: true}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetUseReplicationSignal(true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
|
|
|
g.RefreshPrimaryGatewayFallbackAddresses([]string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetLastFederationStateReplicationError(nil, true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway()) // too many errors
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.False(t, g.DialPrimaryThroughLocalGateway())
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"1.2.3.4:5555",
|
|
|
|
"4.3.2.1:9999",
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
2020-05-27 16:31:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("secondary - with data and fallback - repl ok then failed 3 times then ok again", func(t *testing.T) {
|
2021-01-22 16:03:24 +00:00
|
|
|
// Only run for the leader.
|
2020-05-27 16:31:10 +00:00
|
|
|
logger := testutil.Logger(t)
|
|
|
|
tsd := &testServerDelegate{State: state, isLeader: true}
|
|
|
|
g := NewGatewayLocator(
|
|
|
|
logger,
|
|
|
|
tsd,
|
|
|
|
"dc2",
|
|
|
|
"dc1",
|
|
|
|
)
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetUseReplicationSignal(true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
|
|
|
g.RefreshPrimaryGatewayFallbackAddresses([]string{
|
|
|
|
"7.7.7.7:7777",
|
|
|
|
"8.8.8.8:8888",
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:03:24 +00:00
|
|
|
g.SetLastFederationStateReplicationError(nil, true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(errors.New("fake"), true)
|
|
|
|
g.SetLastFederationStateReplicationError(nil, true)
|
2020-05-27 16:31:10 +00:00
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
t.Run("before first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // all better again
|
|
|
|
assert.Len(t, tsd.Calls, 0)
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(false))
|
|
|
|
assert.Equal(t, []string(nil), g.listGateways(true)) // don't return any data until we initialize
|
|
|
|
assert.False(t, tsd.datacenterSupportsFederationStates())
|
|
|
|
})
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
idx, err := g.runOnce(0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, uint64(2), idx)
|
2021-01-25 23:30:38 +00:00
|
|
|
|
|
|
|
t.Run("after first run", func(t *testing.T) {
|
|
|
|
assert.True(t, g.DialPrimaryThroughLocalGateway()) // all better again
|
|
|
|
assert.Len(t, tsd.Calls, 1)
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(false))
|
|
|
|
assert.Equal(t, []string{
|
|
|
|
"5.6.7.8:5555",
|
|
|
|
"8.7.6.5:9999",
|
|
|
|
}, g.listGateways(true))
|
|
|
|
assert.True(t, tsd.datacenterSupportsFederationStates()) // have results, so we flip the bit
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-23 21:29:10 +00:00
|
|
|
var _ serverDelegate = (*testServerDelegate)(nil)
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
type testServerDelegate struct {
|
2021-01-25 23:30:38 +00:00
|
|
|
dcSupportsFederationStates int32 // atomically accessed, at start to prevent alignment issues
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
State *state.Store
|
|
|
|
|
2020-05-27 16:31:10 +00:00
|
|
|
Calls []uint64
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
isLeader bool
|
|
|
|
lastContact time.Time
|
|
|
|
}
|
|
|
|
|
2021-01-25 23:30:38 +00:00
|
|
|
func (d *testServerDelegate) setDatacenterSupportsFederationStates() {
|
|
|
|
atomic.StoreInt32(&d.dcSupportsFederationStates, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testServerDelegate) datacenterSupportsFederationStates() bool {
|
|
|
|
return atomic.LoadInt32(&d.dcSupportsFederationStates) != 0
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// This is just enough to exercise the logic.
|
|
|
|
func (d *testServerDelegate) blockingQuery(
|
2023-05-23 21:29:10 +00:00
|
|
|
queryOpts blockingquery.RequestOptions,
|
|
|
|
queryMeta blockingquery.ResponseMeta,
|
|
|
|
fn blockingquery.QueryFn,
|
2020-03-09 20:59:02 +00:00
|
|
|
) error {
|
|
|
|
minQueryIndex := queryOpts.GetMinQueryIndex()
|
|
|
|
|
|
|
|
d.Calls = append(d.Calls, minQueryIndex)
|
|
|
|
|
|
|
|
var ws memdb.WatchSet
|
|
|
|
|
|
|
|
err := fn(ws, d.State)
|
|
|
|
if err == nil && queryMeta.GetIndex() < 1 {
|
|
|
|
queryMeta.SetIndex(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testServerDelegate) IsLeader() bool {
|
|
|
|
return d.isLeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testServerDelegate) LeaderLastContact() time.Time {
|
|
|
|
return d.lastContact
|
|
|
|
}
|