/
admin_handler_deprecated.go
452 lines (409 loc) · 16.1 KB
/
admin_handler_deprecated.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// 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.
package frontend
import (
"context"
"fmt"
"github.com/pborman/uuid"
"google.golang.org/protobuf/types/known/timestamppb"
commonpb "go.temporal.io/api/common/v1"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/api/adminservice/v1"
enumsspb "go.temporal.io/server/api/enums/v1"
historyspb "go.temporal.io/server/api/history/v1"
"go.temporal.io/server/api/historyservice/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
tokenspb "go.temporal.io/server/api/token/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/versionhistory"
"go.temporal.io/server/common/persistence/visibility/store/standard/cassandra"
)
// DEPRECATED: DO NOT MODIFY UNLESS ALSO APPLIED TO ./service/history/api/getworkflowexecutionrawhistoryv2/api.go
func (adh *AdminHandler) getWorkflowExecutionRawHistoryV2(
ctx context.Context,
request *adminservice.GetWorkflowExecutionRawHistoryV2Request,
) (_ *adminservice.GetWorkflowExecutionRawHistoryV2Response, retError error) {
ns, err := adh.namespaceRegistry.GetNamespaceByID(namespace.ID(request.GetNamespaceId()))
if err != nil {
return nil, err
}
execution := request.Execution
var pageToken *tokenspb.RawHistoryContinuation
var targetVersionHistory *historyspb.VersionHistory
if request.NextPageToken == nil {
response, err := adh.historyClient.GetMutableState(ctx, &historyservice.GetMutableStateRequest{
NamespaceId: ns.ID().String(),
Execution: execution,
})
if err != nil {
return nil, err
}
targetVersionHistory, err = adh.setRequestDefaultValueAndGetTargetVersionHistory(
request,
response.GetVersionHistories(),
)
if err != nil {
return nil, err
}
pageToken = generatePaginationToken(request, response.GetVersionHistories())
} else {
pageToken, err = deserializeRawHistoryToken(request.NextPageToken)
if err != nil {
return nil, err
}
versionHistories := pageToken.GetVersionHistories()
if versionHistories == nil {
return nil, errInvalidVersionHistories
}
targetVersionHistory, err = adh.setRequestDefaultValueAndGetTargetVersionHistory(
request,
versionHistories,
)
if err != nil {
return nil, err
}
}
if err := validatePaginationToken(
request,
pageToken,
); err != nil {
return nil, err
}
if pageToken.GetStartEventId()+1 == pageToken.GetEndEventId() {
// API is exclusive-exclusive. Return empty response here.
return &adminservice.GetWorkflowExecutionRawHistoryV2Response{
HistoryBatches: []*commonpb.DataBlob{},
NextPageToken: nil, // no further pagination
VersionHistory: targetVersionHistory,
}, nil
}
pageSize := int(request.GetMaximumPageSize())
shardID := common.WorkflowIDToHistoryShard(
ns.ID().String(),
execution.GetWorkflowId(),
adh.numberOfHistoryShards,
)
rawHistoryResponse, err := adh.persistenceExecutionManager.ReadRawHistoryBranch(ctx, &persistence.ReadHistoryBranchRequest{
BranchToken: targetVersionHistory.GetBranchToken(),
// GetWorkflowExecutionRawHistoryV2 is exclusive exclusive.
// ReadRawHistoryBranch is inclusive exclusive.
MinEventID: pageToken.GetStartEventId() + 1,
MaxEventID: pageToken.GetEndEventId(),
PageSize: pageSize,
NextPageToken: pageToken.PersistenceToken,
ShardID: shardID,
})
if err != nil {
if _, isNotFound := err.(*serviceerror.NotFound); isNotFound {
// when no events can be returned from DB, DB layer will return
// EntityNotExistsError, this API shall return empty response
return &adminservice.GetWorkflowExecutionRawHistoryV2Response{
HistoryBatches: []*commonpb.DataBlob{},
NextPageToken: nil, // no further pagination
VersionHistory: targetVersionHistory,
}, nil
}
return nil, err
}
pageToken.PersistenceToken = rawHistoryResponse.NextPageToken
size := rawHistoryResponse.Size
adh.metricsHandler.Histogram(metrics.HistorySize.Name(), metrics.HistorySize.Unit()).Record(
int64(size),
metrics.NamespaceTag(ns.Name().String()),
metrics.OperationTag(metrics.AdminGetWorkflowExecutionRawHistoryV2Scope),
)
result := &adminservice.GetWorkflowExecutionRawHistoryV2Response{
HistoryBatches: rawHistoryResponse.HistoryEventBlobs,
VersionHistory: targetVersionHistory,
HistoryNodeIds: rawHistoryResponse.NodeIDs,
}
if len(pageToken.PersistenceToken) == 0 {
result.NextPageToken = nil
} else {
result.NextPageToken, err = serializeRawHistoryToken(pageToken)
if err != nil {
return nil, err
}
}
return result, nil
}
// DEPRECATED: DO NOT MODIFY UNLESS ALSO APPLIED TO ./service/history/api/forcedeleteworkflowexecution/api.go
func (adh *AdminHandler) deleteWorkflowExecution(
ctx context.Context,
request *adminservice.DeleteWorkflowExecutionRequest,
) (_ *adminservice.DeleteWorkflowExecutionResponse, err error) {
namespaceID, err := adh.namespaceRegistry.GetNamespaceID(namespace.Name(request.GetNamespace()))
if err != nil {
return nil, err
}
execution := request.Execution
shardID := common.WorkflowIDToHistoryShard(
namespaceID.String(),
execution.GetWorkflowId(),
adh.numberOfHistoryShards,
)
logger := log.With(adh.logger,
tag.WorkflowNamespace(request.Namespace),
tag.WorkflowID(execution.WorkflowId),
tag.WorkflowRunID(execution.RunId),
)
if execution.RunId == "" {
resp, err := adh.persistenceExecutionManager.GetCurrentExecution(ctx, &persistence.GetCurrentExecutionRequest{
ShardID: shardID,
NamespaceID: namespaceID.String(),
WorkflowID: execution.WorkflowId,
})
if err != nil {
return nil, err
}
execution.RunId = resp.RunID
}
var warnings []string
var branchTokens [][]byte
var startTime, closeTime *timestamppb.Timestamp
cassVisBackend := adh.visibilityMgr.HasStoreName(cassandra.CassandraPersistenceName)
resp, err := adh.persistenceExecutionManager.GetWorkflowExecution(ctx, &persistence.GetWorkflowExecutionRequest{
ShardID: shardID,
NamespaceID: namespaceID.String(),
WorkflowID: execution.WorkflowId,
RunID: execution.RunId,
})
if err != nil {
if common.IsContextCanceledErr(err) || common.IsContextDeadlineExceededErr(err) {
return nil, err
}
// continue to deletion
warnMsg := "Unable to load mutable state when deleting workflow execution, " +
"will skip deleting workflow history and cassandra visibility record"
logger.Warn(warnMsg, tag.Error(err))
warnings = append(warnings, fmt.Sprintf("%s. Error: %v", warnMsg, err.Error()))
} else {
// load necessary information from mutable state
executionInfo := resp.State.GetExecutionInfo()
histories := executionInfo.GetVersionHistories().GetHistories()
branchTokens = make([][]byte, 0, len(histories))
for _, historyItem := range histories {
branchTokens = append(branchTokens, historyItem.GetBranchToken())
}
if cassVisBackend {
if resp.State.ExecutionState.State != enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED {
startTime = executionInfo.GetStartTime()
} else if executionInfo.GetCloseTime() != nil {
closeTime = executionInfo.GetCloseTime()
} else {
completionEvent, err := adh.getWorkflowCompletionEvent(ctx, shardID, resp.State)
if err != nil {
warnMsg := "Unable to load workflow completion event, will skip deleting visibility record"
adh.logger.Warn(warnMsg, tag.Error(err))
warnings = append(warnings, fmt.Sprintf("%s. Error: %v", warnMsg, err.Error()))
} else {
closeTime = completionEvent.GetEventTime()
}
}
}
}
if !cassVisBackend || (startTime != nil || closeTime != nil) {
// if using cass visibility, then either start or close time should be non-nil
// NOTE: the deletion is best effort, for sql and cassandra visibility implementation,
// we can't guarantee there's no update or record close request for this workflow since
// visibility queue processing is async. Operator can call this api again to delete visibility
// record again if this happens.
if _, err := adh.historyClient.DeleteWorkflowVisibilityRecord(ctx, &historyservice.DeleteWorkflowVisibilityRecordRequest{
NamespaceId: namespaceID.String(),
Execution: execution,
WorkflowStartTime: startTime,
WorkflowCloseTime: closeTime,
}); err != nil {
return nil, err
}
}
if err := adh.persistenceExecutionManager.DeleteCurrentWorkflowExecution(ctx, &persistence.DeleteCurrentWorkflowExecutionRequest{
ShardID: shardID,
NamespaceID: namespaceID.String(),
WorkflowID: execution.WorkflowId,
RunID: execution.RunId,
}); err != nil {
return nil, err
}
if err := adh.persistenceExecutionManager.DeleteWorkflowExecution(ctx, &persistence.DeleteWorkflowExecutionRequest{
ShardID: shardID,
NamespaceID: namespaceID.String(),
WorkflowID: execution.WorkflowId,
RunID: execution.RunId,
}); err != nil {
return nil, err
}
for _, branchToken := range branchTokens {
if err := adh.persistenceExecutionManager.DeleteHistoryBranch(ctx, &persistence.DeleteHistoryBranchRequest{
ShardID: shardID,
BranchToken: branchToken,
}); err != nil {
warnMsg := "Failed to delete history branch, skip"
adh.logger.Warn(warnMsg, tag.WorkflowBranchID(string(branchToken)), tag.Error(err))
warnings = append(warnings, fmt.Sprintf("%s. BranchToken: %v, Error: %v", warnMsg, branchToken, err.Error()))
}
}
return &adminservice.DeleteWorkflowExecutionResponse{
Warnings: warnings,
}, nil
}
// DEPRECATED: DO NOT MODIFY UNLESS ALSO APPLIED TO ./service/history/api/getworkflowexecutionrawhistoryv2/api.go
func (adh *AdminHandler) validateGetWorkflowExecutionRawHistoryV2Request(
request *adminservice.GetWorkflowExecutionRawHistoryV2Request,
) error {
execution := request.Execution
if execution.GetWorkflowId() == "" {
return errWorkflowIDNotSet
}
// TODO currently, this API is only going to be used by re-send history events
// to remote cluster if kafka is lossy again, in the future, this API can be used
// by CLI and client, then empty runID (meaning the current workflow) should be allowed
if execution.GetRunId() == "" || uuid.Parse(execution.GetRunId()) == nil {
return errInvalidRunID
}
pageSize := int(request.GetMaximumPageSize())
if pageSize <= 0 {
return errInvalidPageSize
}
if request.GetStartEventId() == common.EmptyEventID &&
request.GetStartEventVersion() == common.EmptyVersion &&
request.GetEndEventId() == common.EmptyEventID &&
request.GetEndEventVersion() == common.EmptyVersion {
return errInvalidEventQueryRange
}
return nil
}
// DEPRECATED: DO NOT MODIFY UNLESS ALSO APPLIED TO ./service/history/api/getworkflowexecutionrawhistoryv2/api.go
func (adh *AdminHandler) setRequestDefaultValueAndGetTargetVersionHistory(
request *adminservice.GetWorkflowExecutionRawHistoryV2Request,
versionHistories *historyspb.VersionHistories,
) (*historyspb.VersionHistory, error) {
targetBranch, err := versionhistory.GetCurrentVersionHistory(versionHistories)
if err != nil {
return nil, err
}
firstItem, err := versionhistory.GetFirstVersionHistoryItem(targetBranch)
if err != nil {
return nil, err
}
lastItem, err := versionhistory.GetLastVersionHistoryItem(targetBranch)
if err != nil {
return nil, err
}
if request.GetStartEventId() == common.EmptyEventID || request.GetStartEventVersion() == common.EmptyVersion {
// If start event is not set, get the events from the first event
// As the API is exclusive-exclusive, use first event id - 1 here
request.StartEventId = common.FirstEventID - 1
request.StartEventVersion = firstItem.GetVersion()
}
if request.GetEndEventId() == common.EmptyEventID || request.GetEndEventVersion() == common.EmptyVersion {
// If end event is not set, get the events until the end event
// As the API is exclusive-exclusive, use end event id + 1 here
request.EndEventId = lastItem.GetEventId() + 1
request.EndEventVersion = lastItem.GetVersion()
}
if request.GetStartEventId() < 0 {
return nil, errInvalidFirstNextEventCombination
}
// get branch based on the end event if end event is defined in the request
if request.GetEndEventId() == lastItem.GetEventId()+1 &&
request.GetEndEventVersion() == lastItem.GetVersion() {
// this is a special case, target branch remains the same
} else {
endItem := versionhistory.NewVersionHistoryItem(request.GetEndEventId(), request.GetEndEventVersion())
idx, err := versionhistory.FindFirstVersionHistoryIndexByVersionHistoryItem(versionHistories, endItem)
if err != nil {
return nil, err
}
targetBranch, err = versionhistory.GetVersionHistory(versionHistories, idx)
if err != nil {
return nil, err
}
}
startItem := versionhistory.NewVersionHistoryItem(request.GetStartEventId(), request.GetStartEventVersion())
// If the request start event is defined. The start event may be on a different branch as current branch.
// We need to find the LCA of the start event and the current branch.
if request.GetStartEventId() == common.FirstEventID-1 &&
request.GetStartEventVersion() == firstItem.GetVersion() {
// this is a special case, start event is on the same branch as target branch
} else {
if !versionhistory.ContainsVersionHistoryItem(targetBranch, startItem) {
idx, err := versionhistory.FindFirstVersionHistoryIndexByVersionHistoryItem(versionHistories, startItem)
if err != nil {
return nil, err
}
startBranch, err := versionhistory.GetVersionHistory(versionHistories, idx)
if err != nil {
return nil, err
}
startItem, err = versionhistory.FindLCAVersionHistoryItem(targetBranch, startBranch)
if err != nil {
return nil, err
}
request.StartEventId = startItem.GetEventId()
request.StartEventVersion = startItem.GetVersion()
}
}
return targetBranch, nil
}
// DEPRECATED: DO NOT MODIFY UNLESS ALSO APPLIED TO ./service/history/api/forcedeleteworkflowexecution/api.go
func (adh *AdminHandler) getWorkflowCompletionEvent(
ctx context.Context,
shardID int32,
mutableState *persistencespb.WorkflowMutableState,
) (*historypb.HistoryEvent, error) {
executionInfo := mutableState.GetExecutionInfo()
completionEventID := mutableState.GetNextEventId() - 1
currentVersionHistory, err := versionhistory.GetCurrentVersionHistory(executionInfo.VersionHistories)
if err != nil {
return nil, err
}
version, err := versionhistory.GetVersionHistoryEventVersion(currentVersionHistory, completionEventID)
if err != nil {
return nil, err
}
resp, err := adh.persistenceExecutionManager.ReadHistoryBranch(ctx, &persistence.ReadHistoryBranchRequest{
ShardID: shardID,
BranchToken: currentVersionHistory.GetBranchToken(),
MinEventID: executionInfo.CompletionEventBatchId,
MaxEventID: completionEventID + 1,
PageSize: 1,
})
if err != nil {
return nil, err
}
// find history event from batch and return back single event to caller
for _, e := range resp.HistoryEvents {
if e.EventId == completionEventID && e.Version == version {
return e, nil
}
}
return nil, serviceerror.NewInternal("Unable to find closed event for workflow")
}