forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcRedirectionPolicy.go
185 lines (161 loc) · 7.55 KB
/
dcRedirectionPolicy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package frontend
import (
"context"
"fmt"
"github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/service/config"
"go.uber.org/yarpc"
)
const (
// DCRedirectionPolicyDefault means no redirection
DCRedirectionPolicyDefault = ""
// DCRedirectionPolicyNoop means no redirection
DCRedirectionPolicyNoop = "noop"
// DCRedirectionPolicySelectedAPIsForwarding means forwarding the following APIs based domain
// 1. StartWorkflowExecution
// 2. SignalWithStartWorkflowExecution
// 3. SignalWorkflowExecution
// 4. RequestCancelWorkflowExecution
// 5. TerminateWorkflowExecution
// please also reference selectedAPIsForwardingRedirectionPolicyWhitelistedAPIs
DCRedirectionPolicySelectedAPIsForwarding = "selected-apis-forwarding"
)
type (
// DCRedirectionPolicy is a DC redirection policy interface
DCRedirectionPolicy interface {
WithDomainIDRedirect(ctx context.Context, domainID string, apiName string, call func(string) error) error
WithDomainNameRedirect(ctx context.Context, domainName string, apiName string, call func(string) error) error
}
// NoopRedirectionPolicy is DC redirection policy which does nothing
NoopRedirectionPolicy struct {
currentClusterName string
}
// SelectedAPIsForwardingRedirectionPolicy is a DC redirection policy
// which (based on domain) forwards selected APIs calls to active cluster
SelectedAPIsForwardingRedirectionPolicy struct {
currentClusterName string
config *Config
domainCache cache.DomainCache
}
)
// selectedAPIsForwardingRedirectionPolicyWhitelistedAPIs contains a list of APIs which can be redirected
var selectedAPIsForwardingRedirectionPolicyWhitelistedAPIs = map[string]struct{}{
"StartWorkflowExecution": {},
"SignalWithStartWorkflowExecution": {},
"SignalWorkflowExecution": {},
"RequestCancelWorkflowExecution": {},
"TerminateWorkflowExecution": {},
}
// RedirectionPolicyGenerator generate corresponding redirection policy
func RedirectionPolicyGenerator(clusterMetadata cluster.Metadata, config *Config,
domainCache cache.DomainCache, policy config.DCRedirectionPolicy) DCRedirectionPolicy {
switch policy.Policy {
case DCRedirectionPolicyDefault:
// default policy, noop
return NewNoopRedirectionPolicy(clusterMetadata.GetCurrentClusterName())
case DCRedirectionPolicyNoop:
return NewNoopRedirectionPolicy(clusterMetadata.GetCurrentClusterName())
case DCRedirectionPolicySelectedAPIsForwarding:
currentClusterName := clusterMetadata.GetCurrentClusterName()
return NewSelectedAPIsForwardingPolicy(currentClusterName, config, domainCache)
default:
panic(fmt.Sprintf("Unknown DC redirection policy %v", policy.Policy))
}
}
// NewNoopRedirectionPolicy is DC redirection policy which does nothing
func NewNoopRedirectionPolicy(currentClusterName string) *NoopRedirectionPolicy {
return &NoopRedirectionPolicy{
currentClusterName: currentClusterName,
}
}
// WithDomainIDRedirect redirect the API call based on domain ID
func (policy *NoopRedirectionPolicy) WithDomainIDRedirect(ctx context.Context, domainID string, apiName string, call func(string) error) error {
return call(policy.currentClusterName)
}
// WithDomainNameRedirect redirect the API call based on domain name
func (policy *NoopRedirectionPolicy) WithDomainNameRedirect(ctx context.Context, domainName string, apiName string, call func(string) error) error {
return call(policy.currentClusterName)
}
// NewSelectedAPIsForwardingPolicy creates a forwarding policy for selected APIs based on domain
func NewSelectedAPIsForwardingPolicy(currentClusterName string, config *Config, domainCache cache.DomainCache) *SelectedAPIsForwardingRedirectionPolicy {
return &SelectedAPIsForwardingRedirectionPolicy{
currentClusterName: currentClusterName,
config: config,
domainCache: domainCache,
}
}
// WithDomainIDRedirect redirect the API call based on domain ID
func (policy *SelectedAPIsForwardingRedirectionPolicy) WithDomainIDRedirect(ctx context.Context, domainID string, apiName string, call func(string) error) error {
domainEntry, err := policy.domainCache.GetDomainByID(domainID)
if err != nil {
return err
}
return policy.withRedirect(ctx, domainEntry, apiName, call)
}
// WithDomainNameRedirect redirect the API call based on domain name
func (policy *SelectedAPIsForwardingRedirectionPolicy) WithDomainNameRedirect(ctx context.Context, domainName string, apiName string, call func(string) error) error {
domainEntry, err := policy.domainCache.GetDomain(domainName)
if err != nil {
return err
}
return policy.withRedirect(ctx, domainEntry, apiName, call)
}
func (policy *SelectedAPIsForwardingRedirectionPolicy) withRedirect(ctx context.Context, domainEntry *cache.DomainCacheEntry, apiName string, call func(string) error) error {
targetDC, enableDomainNotActiveForwarding := policy.getTargetClusterAndIsDomainNotActiveAutoForwarding(ctx, domainEntry, apiName)
err := call(targetDC)
targetDC, ok := policy.isDomainNotActiveError(err)
if !ok || !enableDomainNotActiveForwarding {
return err
}
return call(targetDC)
}
func (policy *SelectedAPIsForwardingRedirectionPolicy) isDomainNotActiveError(err error) (string, bool) {
domainNotActiveErr, ok := err.(*shared.DomainNotActiveError)
if !ok {
return "", false
}
return domainNotActiveErr.ActiveCluster, true
}
func (policy *SelectedAPIsForwardingRedirectionPolicy) getTargetClusterAndIsDomainNotActiveAutoForwarding(ctx context.Context, domainEntry *cache.DomainCacheEntry, apiName string) (string, bool) {
if !domainEntry.IsGlobalDomain() {
return policy.currentClusterName, false
}
if len(domainEntry.GetReplicationConfig().Clusters) == 1 {
// do not do dc redirection if domain is only targeting at 1 dc (effectively local domain)
return policy.currentClusterName, false
}
call := yarpc.CallFromContext(ctx)
enforceDCRedirection := call.Header(common.EnforceDCRedirection)
if !policy.config.EnableDomainNotActiveAutoForwarding(domainEntry.GetInfo().Name) && enforceDCRedirection != "true" {
// do not do dc redirection if auto-forwarding dynamic config and EnforceDCRedirection context flag is not enabled
return policy.currentClusterName, false
}
_, ok := selectedAPIsForwardingRedirectionPolicyWhitelistedAPIs[apiName]
if !ok {
// do not do dc redirection if API is not whitelisted
return policy.currentClusterName, false
}
return domainEntry.GetReplicationConfig().ActiveClusterName, true
}