-
Notifications
You must be signed in to change notification settings - Fork 777
/
replicationTaskExecutor.go
296 lines (266 loc) · 11.3 KB
/
replicationTaskExecutor.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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.
//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination replicationTaskHandler_mock.go
package namespace
import (
enumspb "go.temporal.io/api/enums/v1"
replicationpb "go.temporal.io/api/replication/v1"
"go.temporal.io/api/serviceerror"
enumsspb "go.temporal.io/server/api/enums/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
replicationspb "go.temporal.io/server/api/replication/v1"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/persistence"
)
var (
// ErrEmptyNamespaceReplicationTask is the error to indicate empty replication task
ErrEmptyNamespaceReplicationTask = serviceerror.NewInvalidArgument("empty namespace replication task")
// ErrInvalidNamespaceOperation is the error to indicate empty namespace operation attribute
ErrInvalidNamespaceOperation = serviceerror.NewInvalidArgument("invalid namespace operation attribute")
// ErrInvalidNamespaceID is the error to indicate empty rID attribute
ErrInvalidNamespaceID = serviceerror.NewInvalidArgument("invalid namespace ID attribute")
// ErrInvalidNamespaceInfo is the error to indicate empty info attribute
ErrInvalidNamespaceInfo = serviceerror.NewInvalidArgument("invalid namespace info attribute")
// ErrInvalidNamespaceConfig is the error to indicate empty config attribute
ErrInvalidNamespaceConfig = serviceerror.NewInvalidArgument("invalid namespace config attribute")
// ErrInvalidNamespaceReplicationConfig is the error to indicate empty replication config attribute
ErrInvalidNamespaceReplicationConfig = serviceerror.NewInvalidArgument("invalid namespace replication config attribute")
// ErrInvalidNamespaceConfigVersion is the error to indicate empty config version attribute
ErrInvalidNamespaceConfigVersion = serviceerror.NewInvalidArgument("invalid namespace config version attribute")
// ErrInvalidNamespaceFailoverVersion is the error to indicate empty failover version attribute
ErrInvalidNamespaceFailoverVersion = serviceerror.NewInvalidArgument("invalid namespace failover version attribute")
// ErrInvalidNamespaceState is the error to indicate invalid namespace state
ErrInvalidNamespaceState = serviceerror.NewInvalidArgument("invalid namespace state attribute")
// ErrNameUUIDCollision is the error to indicate namespace name / UUID collision
ErrNameUUIDCollision = serviceerror.NewInvalidArgument("namespace replication encounter name / UUID collision")
)
// NOTE: the counterpart of namespace replication transmission logic is in service/fropntend package
type (
// ReplicationTaskExecutor is the interface which is to execute namespace replication task
ReplicationTaskExecutor interface {
Execute(task *replicationspb.NamespaceTaskAttributes) error
}
namespaceReplicationTaskExecutorImpl struct {
metadataManagerV2 persistence.MetadataManager
logger log.Logger
}
)
// NewReplicationTaskExecutor create a new instance of namespace replicator
func NewReplicationTaskExecutor(
metadataManagerV2 persistence.MetadataManager,
logger log.Logger,
) ReplicationTaskExecutor {
return &namespaceReplicationTaskExecutorImpl{
metadataManagerV2: metadataManagerV2,
logger: logger,
}
}
// Execute handles receiving of the namespace replication task
func (h *namespaceReplicationTaskExecutorImpl) Execute(task *replicationspb.NamespaceTaskAttributes) error {
if err := h.validateNamespaceReplicationTask(task); err != nil {
return err
}
switch task.GetNamespaceOperation() {
case enumsspb.NAMESPACE_OPERATION_CREATE:
return h.handleNamespaceCreationReplicationTask(task)
case enumsspb.NAMESPACE_OPERATION_UPDATE:
return h.handleNamespaceUpdateReplicationTask(task)
default:
return ErrInvalidNamespaceOperation
}
}
// handleNamespaceCreationReplicationTask handles the namespace creation replication task
func (h *namespaceReplicationTaskExecutorImpl) handleNamespaceCreationReplicationTask(task *replicationspb.NamespaceTaskAttributes) error {
// task already validated
err := h.validateNamespaceStatus(task.Info.State)
if err != nil {
return err
}
request := &persistence.CreateNamespaceRequest{
Namespace: &persistencespb.NamespaceDetail{
Info: &persistencespb.NamespaceInfo{
Id: task.GetId(),
Name: task.Info.GetName(),
State: task.Info.GetState(),
Description: task.Info.GetDescription(),
Owner: task.Info.GetOwnerEmail(),
Data: task.Info.Data,
},
Config: &persistencespb.NamespaceConfig{
Retention: task.Config.GetWorkflowExecutionRetentionTtl(),
HistoryArchivalState: task.Config.GetHistoryArchivalState(),
HistoryArchivalUri: task.Config.GetHistoryArchivalUri(),
VisibilityArchivalState: task.Config.GetVisibilityArchivalState(),
VisibilityArchivalUri: task.Config.GetVisibilityArchivalUri(),
},
ReplicationConfig: &persistencespb.NamespaceReplicationConfig{
ActiveClusterName: task.ReplicationConfig.GetActiveClusterName(),
Clusters: h.convertClusterReplicationConfigFromProto(task.ReplicationConfig.Clusters),
},
ConfigVersion: task.GetConfigVersion(),
FailoverVersion: task.GetFailoverVersion(),
},
IsGlobalNamespace: true, // local namespace will not be replicated
}
_, err = h.metadataManagerV2.CreateNamespace(request)
if err != nil {
// SQL and Cassandra handle namespace UUID collision differently
// here, whenever seeing a error replicating a namespace
// do a check if there is a name / UUID collision
recordExists := true
resp, getErr := h.metadataManagerV2.GetNamespace(&persistence.GetNamespaceRequest{
Name: task.Info.GetName(),
})
switch getErr.(type) {
case nil:
if resp.Namespace.Info.Id != task.GetId() {
return ErrNameUUIDCollision
}
case *serviceerror.NotFound:
// no check is necessary
recordExists = false
default:
// return the original err
return err
}
resp, getErr = h.metadataManagerV2.GetNamespace(&persistence.GetNamespaceRequest{
ID: task.GetId(),
})
switch getErr.(type) {
case nil:
if resp.Namespace.Info.Name != task.Info.GetName() {
return ErrNameUUIDCollision
}
case *serviceerror.NotFound:
// no check is necessary
recordExists = false
default:
// return the original err
return err
}
if recordExists {
// name -> id & id -> name check pass, this is duplication request
return nil
}
return err
}
return err
}
// handleNamespaceUpdateReplicationTask handles the namespace update replication task
func (h *namespaceReplicationTaskExecutorImpl) handleNamespaceUpdateReplicationTask(task *replicationspb.NamespaceTaskAttributes) error {
// task already validated
err := h.validateNamespaceStatus(task.Info.State)
if err != nil {
return err
}
// first we need to get the current notification version since we need to it for conditional update
metadata, err := h.metadataManagerV2.GetMetadata()
if err != nil {
return err
}
notificationVersion := metadata.NotificationVersion
// plus, we need to check whether the config version is <= the config version set in the input
// plus, we need to check whether the failover version is <= the failover version set in the input
resp, err := h.metadataManagerV2.GetNamespace(&persistence.GetNamespaceRequest{
Name: task.Info.GetName(),
})
if err != nil {
if _, ok := err.(*serviceerror.NotFound); ok {
// this can happen if the create namespace replication task is to processed.
// e.g. new cluster which does not have anything
return h.handleNamespaceCreationReplicationTask(task)
}
return err
}
recordUpdated := false
request := &persistence.UpdateNamespaceRequest{
Namespace: resp.Namespace,
NotificationVersion: notificationVersion,
}
if resp.Namespace.ConfigVersion < task.GetConfigVersion() {
recordUpdated = true
request.Namespace.Info = &persistencespb.NamespaceInfo{
Id: task.GetId(),
Name: task.Info.GetName(),
State: task.Info.GetState(),
Description: task.Info.GetDescription(),
Owner: task.Info.GetOwnerEmail(),
Data: task.Info.Data,
}
request.Namespace.Config = &persistencespb.NamespaceConfig{
Retention: task.Config.GetWorkflowExecutionRetentionTtl(),
HistoryArchivalState: task.Config.GetHistoryArchivalState(),
HistoryArchivalUri: task.Config.GetHistoryArchivalUri(),
VisibilityArchivalState: task.Config.GetVisibilityArchivalState(),
VisibilityArchivalUri: task.Config.GetVisibilityArchivalUri(),
}
if task.Config.GetBadBinaries() != nil {
request.Namespace.Config.BadBinaries = task.Config.GetBadBinaries()
}
request.Namespace.ReplicationConfig.Clusters = h.convertClusterReplicationConfigFromProto(task.ReplicationConfig.Clusters)
request.Namespace.ConfigVersion = task.GetConfigVersion()
}
if resp.Namespace.FailoverVersion < task.GetFailoverVersion() {
recordUpdated = true
request.Namespace.ReplicationConfig.ActiveClusterName = task.ReplicationConfig.GetActiveClusterName()
request.Namespace.FailoverVersion = task.GetFailoverVersion()
request.Namespace.FailoverNotificationVersion = notificationVersion
}
if !recordUpdated {
return nil
}
return h.metadataManagerV2.UpdateNamespace(request)
}
func (h *namespaceReplicationTaskExecutorImpl) validateNamespaceReplicationTask(task *replicationspb.NamespaceTaskAttributes) error {
if task == nil {
return ErrEmptyNamespaceReplicationTask
}
if task.Id == "" {
return ErrInvalidNamespaceID
} else if task.Info == nil {
return ErrInvalidNamespaceInfo
} else if task.Config == nil {
return ErrInvalidNamespaceConfig
} else if task.ReplicationConfig == nil {
return ErrInvalidNamespaceReplicationConfig
}
return nil
}
func (h *namespaceReplicationTaskExecutorImpl) convertClusterReplicationConfigFromProto(
input []*replicationpb.ClusterReplicationConfig) []string {
output := []string{}
for _, cluster := range input {
clusterName := cluster.GetClusterName()
output = append(output, clusterName)
}
return output
}
func (h *namespaceReplicationTaskExecutorImpl) validateNamespaceStatus(input enumspb.NamespaceState) error {
switch input {
case enumspb.NAMESPACE_STATE_REGISTERED, enumspb.NAMESPACE_STATE_DEPRECATED:
return nil
default:
return ErrInvalidNamespaceState
}
}