Skip to content

Commit

Permalink
connect: fix failover through a mesh gateway to a remote datacenter
Browse files Browse the repository at this point in the history
Failover is pushed entirely down to the data plane by creating envoy
clusters and putting each successive destination in a different load
assignment priority band. For example this shows that normally requests
go to 1.2.3.4:8080 but when that fails they go to 6.7.8.9:8080:

- name: foo
  load_assignment:
    cluster_name: foo
    policy:
      overprovisioning_factor: 100000
    endpoints:
    - priority: 0
      lb_endpoints:
      - endpoint:
          address:
            socket_address:
              address: 1.2.3.4
              port_value: 8080
    - priority: 1
      lb_endpoints:
      - endpoint:
          address:
            socket_address:
              address: 6.7.8.9
              port_value: 8080

Mesh gateways route requests based solely on the SNI header tacked onto
the TLS layer. Envoy currently only lets you configure the outbound SNI
header at the cluster layer.

If you try to failover through a mesh gateway you ideally would
configure the SNI value per endpoint, but that's not possible in envoy
today.

This PR introduces a simpler way around the problem for now:

1. We identify any target of failover that will use mesh gateway mode local or
   remote and then further isolate any resolver node in the compiled discovery
   chain that has a failover destination set to one of those targets.

2. For each of these resolvers we will perform a small measurement of
   comparative healths of the endpoints that come back from the health API for the
   set of primary target and serial failover targets. We walk the list of targets
   in order and if any endpoint is healthy we return that target, otherwise we
   move on to the next target.

3. The CDS and EDS endpoints both perform the measurements in (2) for the
   affected resolver nodes.

4. For CDS this measurement selects which TLS SNI field to use for the cluster
   (note the cluster is always going to be named for the primary target)

5. For EDS this measurement selects which set of endpoints will populate the
   cluster. Priority tiered failover is ignored.

One of the big downsides to this approach to failover is that the failover
detection and correction is going to be controlled by consul rather than
deferring that entirely to the data plane as with the prior version. This also
means that we are bound to only failover using official health signals and
cannot make use of data plane signals like outlier detection to affect
failover.

In this specific scenario the lack of data plane signals is ok because the
effectiveness is already muted by the fact that the ultimate destination
endpoints will have their data plane signals scrambled when they pass through
the mesh gateway wrapper anyway so we're not losing much.
  • Loading branch information
rboyer committed Aug 1, 2019
1 parent 7bddbec commit 7955d77
Show file tree
Hide file tree
Showing 48 changed files with 1,127 additions and 72 deletions.
2 changes: 1 addition & 1 deletion agent/cache-types/discovery_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestCompiledDiscoveryChain(t *testing.T) {

// just do the default chain
entries := structs.NewDiscoveryChainConfigEntries()
chain := discoverychain.TestCompileConfigEntries(t, "web", "default", "dc1", nil)
chain := discoverychain.TestCompileConfigEntries(t, "web", "default", "dc1", "dc1", nil)

// Expect the proper RPC call. This also sets the expected value
// since that is return-by-pointer in the arguments.
Expand Down
1 change: 1 addition & 0 deletions agent/consul/discovery_chain_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (c *DiscoveryChain) Get(args *structs.DiscoveryChainRequest, reply *structs
ServiceName: args.Name,
CurrentNamespace: evalNS,
CurrentDatacenter: evalDC,
UseInDatacenter: c.srv.config.Datacenter,
OverrideMeshGateway: args.OverrideMeshGateway,
OverrideProtocol: args.OverrideProtocol,
OverrideConnectTimeout: args.OverrideConnectTimeout,
Expand Down
25 changes: 23 additions & 2 deletions agent/consul/discoverychain/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (

type CompileRequest struct {
ServiceName string
CurrentNamespace string
CurrentDatacenter string
CurrentNamespace string // TODO(rb): rename to EvaluateInDatacenter
CurrentDatacenter string // TODO(rb): rename to EvaluateInNamespace
UseInDatacenter string // where the results will be used from

// OverrideMeshGateway allows for the setting to be overridden for any
// resolver in the compiled chain.
Expand Down Expand Up @@ -55,6 +56,7 @@ func Compile(req CompileRequest) (*structs.CompiledDiscoveryChain, error) {
serviceName = req.ServiceName
currentNamespace = req.CurrentNamespace
currentDatacenter = req.CurrentDatacenter
useInDatacenter = req.UseInDatacenter
entries = req.Entries
)
if serviceName == "" {
Expand All @@ -66,6 +68,9 @@ func Compile(req CompileRequest) (*structs.CompiledDiscoveryChain, error) {
if currentDatacenter == "" {
return nil, fmt.Errorf("currentDatacenter is required")
}
if useInDatacenter == "" {
return nil, fmt.Errorf("useInDatacenter is required")
}
if entries == nil {
return nil, fmt.Errorf("entries is required")
}
Expand All @@ -74,6 +79,7 @@ func Compile(req CompileRequest) (*structs.CompiledDiscoveryChain, error) {
serviceName: serviceName,
currentNamespace: currentNamespace,
currentDatacenter: currentDatacenter,
useInDatacenter: useInDatacenter,
overrideMeshGateway: req.OverrideMeshGateway,
overrideProtocol: req.OverrideProtocol,
overrideConnectTimeout: req.OverrideConnectTimeout,
Expand Down Expand Up @@ -108,6 +114,7 @@ type compiler struct {
serviceName string
currentNamespace string
currentDatacenter string
useInDatacenter string
overrideMeshGateway structs.MeshGatewayConfig
overrideProtocol string
overrideConnectTimeout time.Duration
Expand Down Expand Up @@ -252,6 +259,20 @@ func (c *compiler) compile() (*structs.CompiledDiscoveryChain, error) {
return nil, err
}

// Flip mesh gateway modes back to none if sharing a datacenter.
// TODO (mesh-gateway)- maybe allow using a gateway within a datacenter at some point
for target, targetConfig := range c.targets {
meshGateway := structs.MeshGatewayModeDefault
if target.Datacenter != c.useInDatacenter {
meshGateway = targetConfig.MeshGateway.Mode
}

if meshGateway != targetConfig.MeshGateway.Mode {
targetConfig.MeshGateway.Mode = meshGateway
c.targets[target] = targetConfig
}
}

if !enableAdvancedRoutingForProtocol(c.protocol) && c.usesAdvancedRoutingFeatures {
return nil, &structs.ConfigEntryGraphError{
Message: fmt.Sprintf(
Expand Down
70 changes: 58 additions & 12 deletions agent/consul/discoverychain/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func TestCompile(t *testing.T) {
"service redirect": testcase_ServiceRedirect(),
"service and subset redirect": testcase_ServiceAndSubsetRedirect(),
"datacenter redirect": testcase_DatacenterRedirect(),
"datacenter redirect with mesh gateways": testcase_DatacenterRedirect_WithMeshGateways(),
"service failover": testcase_ServiceFailover(),
"service and subset failover": testcase_ServiceAndSubsetFailover(),
"datacenter failover": testcase_DatacenterFailover(),
"service failover with mesh gateways": testcase_ServiceFailover_WithMeshGateways(),
"datacenter failover with mesh gateways": testcase_DatacenterFailover_WithMeshGateways(),
"noop split to resolver with default subset": testcase_NoopSplit_WithDefaultSubset(),
"resolver with default subset": testcase_Resolve_WithDefaultSubset(),
"resolver with no entries and inferring defaults": testcase_DefaultResolver(),
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestCompile(t *testing.T) {
ServiceName: "main",
CurrentNamespace: "default",
CurrentDatacenter: "dc1",
UseInDatacenter: "dc1",
Entries: tc.entries,
}
if tc.setup != nil {
Expand Down Expand Up @@ -984,6 +986,52 @@ func testcase_DatacenterRedirect() compileTestCase {
return compileTestCase{entries: entries, expect: expect}
}

func testcase_DatacenterRedirect_WithMeshGateways() compileTestCase {
entries := newEntries()
entries.GlobalProxy = &structs.ProxyConfigEntry{
Kind: structs.ProxyDefaults,
Name: structs.ProxyConfigGlobal,
MeshGateway: structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
},
}
entries.AddResolvers(
&structs.ServiceResolverConfigEntry{
Kind: "service-resolver",
Name: "main",
Redirect: &structs.ServiceResolverRedirect{
Datacenter: "dc9",
},
},
)

resolver := entries.GetResolver("main")

expect := &structs.CompiledDiscoveryChain{
Protocol: "tcp",
StartNode: "resolver:main,,,dc9",
Nodes: map[string]*structs.DiscoveryGraphNode{
"resolver:main,,,dc9": &structs.DiscoveryGraphNode{
Type: structs.DiscoveryGraphNodeTypeResolver,
Name: "main,,,dc9",
Resolver: &structs.DiscoveryResolver{
Definition: resolver,
ConnectTimeout: 5 * time.Second,
Target: newTarget("main", "", "default", "dc9"),
},
},
},
Targets: map[structs.DiscoveryTarget]structs.DiscoveryTargetConfig{
newTarget("main", "", "default", "dc9"): structs.DiscoveryTargetConfig{
MeshGateway: structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
},
},
},
}
return compileTestCase{entries: entries, expect: expect}
}

func testcase_ServiceFailover() compileTestCase {
entries := newEntries()
entries.AddResolvers(
Expand Down Expand Up @@ -1125,7 +1173,7 @@ func testcase_DatacenterFailover() compileTestCase {
return compileTestCase{entries: entries, expect: expect}
}

func testcase_ServiceFailover_WithMeshGateways() compileTestCase {
func testcase_DatacenterFailover_WithMeshGateways() compileTestCase {
entries := newEntries()
entries.GlobalProxy = &structs.ProxyConfigEntry{
Kind: structs.ProxyDefaults,
Expand All @@ -1139,13 +1187,12 @@ func testcase_ServiceFailover_WithMeshGateways() compileTestCase {
Kind: "service-resolver",
Name: "main",
Failover: map[string]structs.ServiceResolverFailover{
"*": {Service: "backup"},
"*": {Datacenters: []string{"dc2", "dc4"}},
},
},
)

resolverMain := entries.GetResolver("main")

wildFail := resolverMain.Failover["*"]

expect := &structs.CompiledDiscoveryChain{
Expand All @@ -1162,19 +1209,21 @@ func testcase_ServiceFailover_WithMeshGateways() compileTestCase {
Failover: &structs.DiscoveryFailover{
Definition: &wildFail,
Targets: []structs.DiscoveryTarget{
newTarget("backup", "", "default", "dc1"),
newTarget("main", "", "default", "dc2"),
newTarget("main", "", "default", "dc4"),
},
},
},
},
},
Targets: map[structs.DiscoveryTarget]structs.DiscoveryTargetConfig{
newTarget("backup", "", "default", "dc1"): structs.DiscoveryTargetConfig{
newTarget("main", "", "default", "dc1"): structs.DiscoveryTargetConfig{},
newTarget("main", "", "default", "dc2"): structs.DiscoveryTargetConfig{
MeshGateway: structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
},
},
newTarget("main", "", "default", "dc1"): structs.DiscoveryTargetConfig{
newTarget("main", "", "default", "dc4"): structs.DiscoveryTargetConfig{
MeshGateway: structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
},
Expand Down Expand Up @@ -1304,11 +1353,8 @@ func testcase_DefaultResolver_WithProxyDefaults() compileTestCase {
},
},
Targets: map[structs.DiscoveryTarget]structs.DiscoveryTargetConfig{
newTarget("main", "", "default", "dc1"): structs.DiscoveryTargetConfig{
MeshGateway: structs.MeshGatewayConfig{
Mode: structs.MeshGatewayModeRemote,
},
},
// mesh gateway mode is stripped because we are sharing a dc
newTarget("main", "", "default", "dc1"): structs.DiscoveryTargetConfig{},
},
}
return compileTestCase{entries: entries, expect: expect, expectIsDefault: true}
Expand Down
2 changes: 2 additions & 0 deletions agent/consul/discoverychain/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestCompileConfigEntries(
serviceName string,
currentNamespace string,
currentDatacenter string,
useInDatacenter string,
setup func(req *CompileRequest),
entries ...structs.ConfigEntry,
) *structs.CompiledDiscoveryChain {
Expand All @@ -22,6 +23,7 @@ func TestCompileConfigEntries(
ServiceName: serviceName,
CurrentNamespace: currentNamespace,
CurrentDatacenter: currentDatacenter,
UseInDatacenter: useInDatacenter,
Entries: set,
}
if setup != nil {
Expand Down
1 change: 1 addition & 0 deletions agent/consul/state/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func (s *Store) testCompileDiscoveryChain(
ServiceName: chainName,
CurrentNamespace: "default",
CurrentDatacenter: "dc1",
UseInDatacenter: "dc1",
Entries: speculativeEntries,
}
_, err = discoverychain.Compile(req)
Expand Down
4 changes: 2 additions & 2 deletions agent/proxycfg/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestManager_BasicLifecycle(t *testing.T) {
Datacenter: "dc1",
}
dbDefaultChain := func() *structs.CompiledDiscoveryChain {
return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1",
return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1", "dc1",
func(req *discoverychain.CompileRequest) {
// This is because structs.TestUpstreams uses an opaque config
// to override connect timeouts.
Expand All @@ -76,7 +76,7 @@ func TestManager_BasicLifecycle(t *testing.T) {
)
}
dbSplitChain := func() *structs.CompiledDiscoveryChain {
return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1",
return discoverychain.TestCompileConfigEntries(t, "db", "default", "dc1", "dc1",
func(req *discoverychain.CompileRequest) {
// This is because structs.TestUpstreams uses an opaque config
// to override connect timeouts.
Expand Down
8 changes: 1 addition & 7 deletions agent/proxycfg/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,7 @@ func (s *state) resetWatchesFromChain(

ctx, cancel := context.WithCancel(s.ctx)

// TODO (mesh-gateway)- maybe allow using a gateway within a datacenter at some point
meshGateway := structs.MeshGatewayModeDefault
if target.Datacenter != s.source.Datacenter {
meshGateway = targetConfig.MeshGateway.Mode
}

// if the default mode
meshGateway := targetConfig.MeshGateway.Mode
if meshGateway == structs.MeshGatewayModeDefault {
meshGateway = structs.MeshGatewayModeNone
}
Expand Down
10 changes: 5 additions & 5 deletions agent/proxycfg/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func TestState_WatchesAndUpdates(t *testing.T) {
cache.UpdateEvent{
CorrelationID: "discovery-chain:api",
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t, "api", "default", "dc1",
Chain: discoverychain.TestCompileConfigEntries(t, "api", "default", "dc1", "dc1",
func(req *discoverychain.CompileRequest) {
req.OverrideMeshGateway.Mode = meshGatewayProxyConfigValue
}),
Expand All @@ -430,7 +430,7 @@ func TestState_WatchesAndUpdates(t *testing.T) {
cache.UpdateEvent{
CorrelationID: "discovery-chain:api-failover-remote?dc=dc2",
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-remote", "default", "dc2",
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-remote", "default", "dc2", "dc1",
func(req *discoverychain.CompileRequest) {
req.OverrideMeshGateway.Mode = structs.MeshGatewayModeRemote
}),
Expand All @@ -440,7 +440,7 @@ func TestState_WatchesAndUpdates(t *testing.T) {
cache.UpdateEvent{
CorrelationID: "discovery-chain:api-failover-local?dc=dc2",
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-local", "default", "dc2",
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-local", "default", "dc2", "dc1",
func(req *discoverychain.CompileRequest) {
req.OverrideMeshGateway.Mode = structs.MeshGatewayModeLocal
}),
Expand All @@ -450,7 +450,7 @@ func TestState_WatchesAndUpdates(t *testing.T) {
cache.UpdateEvent{
CorrelationID: "discovery-chain:api-failover-direct?dc=dc2",
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-direct", "default", "dc2",
Chain: discoverychain.TestCompileConfigEntries(t, "api-failover-direct", "default", "dc2", "dc1",
func(req *discoverychain.CompileRequest) {
req.OverrideMeshGateway.Mode = structs.MeshGatewayModeNone
}),
Expand All @@ -460,7 +460,7 @@ func TestState_WatchesAndUpdates(t *testing.T) {
cache.UpdateEvent{
CorrelationID: "discovery-chain:api-dc2",
Result: &structs.DiscoveryChainResponse{
Chain: discoverychain.TestCompileConfigEntries(t, "api-dc2", "default", "dc1",
Chain: discoverychain.TestCompileConfigEntries(t, "api-dc2", "default", "dc1", "dc1",
func(req *discoverychain.CompileRequest) {
req.OverrideMeshGateway.Mode = meshGatewayProxyConfigValue
},
Expand Down
Loading

0 comments on commit 7955d77

Please sign in to comment.