-
Notifications
You must be signed in to change notification settings - Fork 762
/
workflowTaskHandlerCallbacks.go
788 lines (701 loc) · 28.3 KB
/
workflowTaskHandlerCallbacks.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// 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 history
import (
"context"
"fmt"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
historypb "go.temporal.io/api/history/v1"
querypb "go.temporal.io/api/query/v1"
"go.temporal.io/api/serviceerror"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/searchattribute"
historyspb "go.temporal.io/server/api/history/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/clock"
"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/payloads"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/primitives/timestamp"
serviceerrors "go.temporal.io/server/common/serviceerror"
"go.temporal.io/server/service/history/configs"
"go.temporal.io/server/service/history/consts"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/workflow"
)
type (
// workflow task business logic handler
workflowTaskHandlerCallbacks interface {
handleWorkflowTaskScheduled(context.Context, *historyservice.ScheduleWorkflowTaskRequest) error
handleWorkflowTaskStarted(context.Context,
*historyservice.RecordWorkflowTaskStartedRequest) (*historyservice.RecordWorkflowTaskStartedResponse, error)
handleWorkflowTaskFailed(context.Context,
*historyservice.RespondWorkflowTaskFailedRequest) error
handleWorkflowTaskCompleted(context.Context,
*historyservice.RespondWorkflowTaskCompletedRequest) (*historyservice.RespondWorkflowTaskCompletedResponse, error)
// TODO also include the handle of workflow task timeout here
}
workflowTaskHandlerCallbacksImpl struct {
currentClusterName string
config *configs.Config
shard shard.Context
timeSource clock.TimeSource
historyEngine *historyEngineImpl
namespaceRegistry namespace.Registry
historyCache workflow.Cache
tokenSerializer common.TaskTokenSerializer
metricsClient metrics.Client
logger log.Logger
throttledLogger log.Logger
commandAttrValidator *commandAttrValidator
searchAttributesMapper searchattribute.Mapper
}
)
func newWorkflowTaskHandlerCallback(historyEngine *historyEngineImpl) *workflowTaskHandlerCallbacksImpl {
return &workflowTaskHandlerCallbacksImpl{
currentClusterName: historyEngine.currentClusterName,
config: historyEngine.config,
shard: historyEngine.shard,
timeSource: historyEngine.shard.GetTimeSource(),
historyEngine: historyEngine,
namespaceRegistry: historyEngine.shard.GetNamespaceRegistry(),
historyCache: historyEngine.historyCache,
tokenSerializer: historyEngine.tokenSerializer,
metricsClient: historyEngine.metricsClient,
logger: historyEngine.logger,
throttledLogger: historyEngine.throttledLogger,
commandAttrValidator: newCommandAttrValidator(
historyEngine.shard.GetNamespaceRegistry(),
historyEngine.config,
historyEngine.searchAttributesValidator,
),
searchAttributesMapper: historyEngine.shard.GetSearchAttributesMapper(),
}
}
func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskScheduled(
ctx context.Context,
req *historyservice.ScheduleWorkflowTaskRequest,
) error {
namespaceEntry, err := handler.historyEngine.getActiveNamespaceEntry(namespace.ID(req.GetNamespaceId()))
if err != nil {
return err
}
namespaceID := namespaceEntry.ID()
execution := commonpb.WorkflowExecution{
WorkflowId: req.WorkflowExecution.WorkflowId,
RunId: req.WorkflowExecution.RunId,
}
return handler.historyEngine.updateWorkflowExecution(
ctx,
namespaceID,
execution,
func(workflowContext workflowContext) (*updateWorkflowAction, error) {
mutableState := workflowContext.getMutableState()
if !mutableState.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
if mutableState.HasProcessedOrPendingWorkflowTask() {
return &updateWorkflowAction{
noop: true,
}, nil
}
startEvent, err := mutableState.GetStartEvent(ctx)
if err != nil {
return nil, err
}
if err := mutableState.AddFirstWorkflowTaskScheduled(
startEvent,
); err != nil {
return nil, err
}
return &updateWorkflowAction{}, nil
})
}
func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskStarted(
ctx context.Context,
req *historyservice.RecordWorkflowTaskStartedRequest,
) (*historyservice.RecordWorkflowTaskStartedResponse, error) {
namespaceEntry, err := handler.historyEngine.getActiveNamespaceEntry(namespace.ID(req.GetNamespaceId()))
if err != nil {
return nil, err
}
namespaceID := namespaceEntry.ID()
execution := commonpb.WorkflowExecution{
WorkflowId: req.WorkflowExecution.WorkflowId,
RunId: req.WorkflowExecution.RunId,
}
scheduleID := req.GetScheduleId()
requestID := req.GetRequestId()
var resp *historyservice.RecordWorkflowTaskStartedResponse
err = handler.historyEngine.updateWorkflowExecution(
ctx,
namespaceID,
execution,
func(workflowContext workflowContext) (*updateWorkflowAction, error) {
mutableState := workflowContext.getMutableState()
if !mutableState.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
workflowTask, isRunning := mutableState.GetWorkflowTaskInfo(scheduleID)
metricsScope := handler.metricsClient.Scope(metrics.HistoryRecordWorkflowTaskStartedScope)
// First check to see if cache needs to be refreshed as we could potentially have stale workflow execution in
// some extreme cassandra failure cases.
if !isRunning && scheduleID >= mutableState.GetNextEventID() {
metricsScope.IncCounter(metrics.StaleMutableStateCounter)
// Reload workflow execution history
// ErrStaleState will trigger updateWorkflowExecution function to reload the mutable state
return nil, consts.ErrStaleState
}
// Check execution state to make sure task is in the list of outstanding tasks and it is not yet started. If
// task is not outstanding than it is most probably a duplicate and complete the task.
if !isRunning {
// Looks like WorkflowTask already completed as a result of another call.
// It is OK to drop the task at this point.
return nil, serviceerror.NewNotFound("Workflow task not found.")
}
updateAction := &updateWorkflowAction{}
if workflowTask.StartedID != common.EmptyEventID {
// If workflow task is started as part of the current request scope then return a positive response
if workflowTask.RequestID == requestID {
resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowTask, req.PollRequest.GetIdentity())
if err != nil {
return nil, err
}
updateAction.noop = true
return updateAction, nil
}
// Looks like WorkflowTask already started as a result of another call.
// It is OK to drop the task at this point.
return nil, serviceerrors.NewTaskAlreadyStarted("Workflow")
}
_, workflowTask, err = mutableState.AddWorkflowTaskStartedEvent(
scheduleID,
requestID,
req.PollRequest.TaskQueue,
req.PollRequest.Identity,
)
if err != nil {
// Unable to add WorkflowTaskStarted event to history
return nil, err
}
workflowScheduleToStartLatency := workflowTask.StartedTime.Sub(*workflowTask.ScheduledTime)
namespaceName := namespaceEntry.Name()
taskQueue := workflowTask.TaskQueue
metrics.GetPerTaskQueueScope(
metricsScope,
namespaceName.String(),
taskQueue.GetName(),
taskQueue.GetKind(),
).Tagged(metrics.TaskQueueTypeTag(enumspb.TASK_QUEUE_TYPE_WORKFLOW)).
RecordTimer(metrics.TaskScheduleToStartLatency, workflowScheduleToStartLatency)
resp, err = handler.createRecordWorkflowTaskStartedResponse(mutableState, workflowTask, req.PollRequest.GetIdentity())
if err != nil {
return nil, err
}
return updateAction, nil
})
if err != nil {
return nil, err
}
return resp, nil
}
func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskFailed(
ctx context.Context,
req *historyservice.RespondWorkflowTaskFailedRequest,
) (retError error) {
namespaceEntry, err := handler.historyEngine.getActiveNamespaceEntry(namespace.ID(req.GetNamespaceId()))
if err != nil {
return err
}
namespaceID := namespaceEntry.ID()
request := req.FailedRequest
token, err := handler.tokenSerializer.Deserialize(request.TaskToken)
if err != nil {
return consts.ErrDeserializingToken
}
workflowExecution := commonpb.WorkflowExecution{
WorkflowId: token.GetWorkflowId(),
RunId: token.GetRunId(),
}
return handler.historyEngine.updateWorkflowExecution(
ctx,
namespaceID,
workflowExecution,
func(workflowContext workflowContext) (*updateWorkflowAction, error) {
mutableState := workflowContext.getMutableState()
if !mutableState.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
scheduleID := token.GetScheduleId()
workflowTask, isRunning := mutableState.GetWorkflowTaskInfo(scheduleID)
if !isRunning || workflowTask.Attempt != token.ScheduleAttempt || workflowTask.StartedID == common.EmptyEventID {
return nil, serviceerror.NewNotFound("Workflow task not found.")
}
_, err := mutableState.AddWorkflowTaskFailedEvent(workflowTask.ScheduleID, workflowTask.StartedID, request.GetCause(), request.GetFailure(),
request.GetIdentity(), request.GetBinaryChecksum(), "", "", 0)
if err != nil {
return nil, err
}
return &updateWorkflowAction{
noop: false,
createWorkflowTask: true,
}, nil
})
}
func (handler *workflowTaskHandlerCallbacksImpl) handleWorkflowTaskCompleted(
ctx context.Context,
req *historyservice.RespondWorkflowTaskCompletedRequest,
) (resp *historyservice.RespondWorkflowTaskCompletedResponse, retError error) {
namespaceEntry, err := handler.historyEngine.getActiveNamespaceEntry(namespace.ID(req.GetNamespaceId()))
if err != nil {
return nil, err
}
namespaceID := namespaceEntry.ID()
request := req.CompleteRequest
token, err0 := handler.tokenSerializer.Deserialize(request.TaskToken)
if err0 != nil {
return nil, consts.ErrDeserializingToken
}
scheduleID := token.GetScheduleId()
workflowExecution := commonpb.WorkflowExecution{
WorkflowId: token.GetWorkflowId(),
RunId: token.GetRunId(),
}
weContext, release, err := handler.historyCache.GetOrCreateWorkflowExecution(
ctx,
namespaceID,
workflowExecution,
workflow.CallerTypeAPI,
)
if err != nil {
return nil, err
}
defer func() { release(retError) }()
var msBuilder workflow.MutableState
var currentWorkflowTask *workflow.WorkflowTaskInfo
var currentWorkflowTaskRunning bool
for attempt := 1; ; attempt++ {
msBuilder, err = weContext.LoadWorkflowExecution(ctx)
if err != nil {
return nil, err
}
if !msBuilder.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
currentWorkflowTask, currentWorkflowTaskRunning = msBuilder.GetWorkflowTaskInfo(scheduleID)
// First check to see if cache is still up-to-date as it could potentially have stale workflow execution in
// some extreme Cassandra failure cases.
if currentWorkflowTaskRunning || scheduleID < msBuilder.GetNextEventID() {
break
}
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.StaleMutableStateCounter)
if attempt == conditionalRetryCount {
return nil, consts.ErrMaxAttemptsExceeded
}
// Reload workflow execution history
weContext.Clear()
}
executionInfo := msBuilder.GetExecutionInfo()
executionStats, err := weContext.LoadExecutionStats(ctx)
if err != nil {
return nil, err
}
if !msBuilder.IsWorkflowExecutionRunning() || !currentWorkflowTaskRunning || currentWorkflowTask.Attempt != token.ScheduleAttempt ||
currentWorkflowTask.StartedID == common.EmptyEventID {
return nil, serviceerror.NewNotFound("Workflow task not found.")
}
startedID := currentWorkflowTask.StartedID
maxResetPoints := handler.config.MaxAutoResetPoints(namespaceEntry.Name().String())
if msBuilder.GetExecutionInfo().AutoResetPoints != nil && maxResetPoints == len(msBuilder.GetExecutionInfo().AutoResetPoints.Points) {
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.AutoResetPointsLimitExceededCounter)
}
workflowTaskHeartbeating := request.GetForceCreateNewWorkflowTask() && len(request.Commands) == 0
var workflowTaskHeartbeatTimeout bool
var completedEvent *historypb.HistoryEvent
if workflowTaskHeartbeating {
namespace := namespaceEntry.Name()
timeout := handler.config.WorkflowTaskHeartbeatTimeout(namespace.String())
origSchedTime := timestamp.TimeValue(currentWorkflowTask.OriginalScheduledTime)
if origSchedTime.UnixNano() > 0 && handler.timeSource.Now().After(origSchedTime.Add(timeout)) {
workflowTaskHeartbeatTimeout = true
scope := handler.metricsClient.Scope(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.NamespaceTag(namespace.String()))
scope.IncCounter(metrics.WorkflowTaskHeartbeatTimeoutCounter)
completedEvent, err = msBuilder.AddWorkflowTaskTimedOutEvent(currentWorkflowTask.ScheduleID, currentWorkflowTask.StartedID)
if err != nil {
return nil, err
}
msBuilder.ClearStickyness()
} else {
completedEvent, err = msBuilder.AddWorkflowTaskCompletedEvent(scheduleID, startedID, request, maxResetPoints)
if err != nil {
return nil, err
}
}
} else {
completedEvent, err = msBuilder.AddWorkflowTaskCompletedEvent(scheduleID, startedID, request, maxResetPoints)
if err != nil {
return nil, err
}
}
var (
wtFailedCause *workflowTaskFailedCause
activityNotStartedCancelled bool
newStateBuilder workflow.MutableState
hasUnhandledEvents bool
responseMutations []workflowTaskResponseMutation
)
hasUnhandledEvents = msBuilder.HasBufferedEvents()
if request.StickyAttributes == nil || request.StickyAttributes.WorkerTaskQueue == nil {
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.CompleteWorkflowTaskWithStickyDisabledCounter)
executionInfo.StickyTaskQueue = ""
executionInfo.StickyScheduleToStartTimeout = timestamp.DurationFromSeconds(0)
} else {
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.CompleteWorkflowTaskWithStickyEnabledCounter)
executionInfo.StickyTaskQueue = request.StickyAttributes.WorkerTaskQueue.GetName()
executionInfo.StickyScheduleToStartTimeout = request.StickyAttributes.GetScheduleToStartTimeout()
}
binChecksum := request.GetBinaryChecksum()
if err := namespaceEntry.VerifyBinaryChecksum(binChecksum); err != nil {
wtFailedCause = NewWorkflowTaskFailedCause(enumspb.WORKFLOW_TASK_FAILED_CAUSE_BAD_BINARY, serviceerror.NewInvalidArgument(fmt.Sprintf("binary %v is already marked as bad deployment", binChecksum)))
} else {
namespace := namespaceEntry.Name()
workflowSizeChecker := newWorkflowSizeChecker(
handler.config.BlobSizeLimitWarn(namespace.String()),
handler.config.BlobSizeLimitError(namespace.String()),
handler.config.MemoSizeLimitWarn(namespace.String()),
handler.config.MemoSizeLimitError(namespace.String()),
handler.config.HistorySizeLimitWarn(namespace.String()),
handler.config.HistorySizeLimitError(namespace.String()),
handler.config.HistoryCountLimitWarn(namespace.String()),
handler.config.HistoryCountLimitError(namespace.String()),
completedEvent.GetEventId(),
msBuilder,
handler.historyEngine.searchAttributesValidator,
executionStats,
handler.metricsClient.Scope(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.NamespaceTag(namespace.String())),
handler.throttledLogger,
)
workflowTaskHandler := newWorkflowTaskHandler(
request.GetIdentity(),
completedEvent.GetEventId(),
msBuilder,
handler.commandAttrValidator,
workflowSizeChecker,
handler.logger,
handler.namespaceRegistry,
handler.metricsClient,
handler.config,
handler.shard,
handler.searchAttributesMapper,
)
if responseMutations, err = workflowTaskHandler.handleCommands(
ctx,
request.Commands,
); err != nil {
return nil, err
}
// set the vars used by following logic
// further refactor should also clean up the vars used below
wtFailedCause = workflowTaskHandler.workflowTaskFailedCause
// failMessage is not used by workflowTaskHandlerCallbacks
activityNotStartedCancelled = workflowTaskHandler.activityNotStartedCancelled
// continueAsNewTimerTasks is not used by workflowTaskHandlerCallbacks
newStateBuilder = workflowTaskHandler.newStateBuilder
hasUnhandledEvents = workflowTaskHandler.hasBufferedEvents
}
if wtFailedCause != nil {
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.FailedWorkflowTasksCounter)
handler.logger.Info("Failing the workflow task.",
tag.Value(wtFailedCause.Message()),
tag.WorkflowID(token.GetWorkflowId()),
tag.WorkflowRunID(token.GetRunId()),
tag.WorkflowNamespaceID(namespaceID.String()))
if currentWorkflowTask.Attempt > 1 {
// drop this workflow task if it keeps failing. This will cause the workflow task to timeout and get retried after timeout.
return nil, serviceerror.NewInvalidArgument(wtFailedCause.Message())
}
msBuilder, err = handler.historyEngine.failWorkflowTask(ctx, weContext, scheduleID, startedID, wtFailedCause, request)
if err != nil {
return nil, err
}
hasUnhandledEvents = true
newStateBuilder = nil
}
createNewWorkflowTask := msBuilder.IsWorkflowExecutionRunning() && (hasUnhandledEvents || request.GetForceCreateNewWorkflowTask() || activityNotStartedCancelled)
var newWorkflowTaskScheduledID int64
if createNewWorkflowTask {
bypassTaskGeneration := request.GetReturnNewWorkflowTask() && wtFailedCause == nil
var newWorkflowTask *workflow.WorkflowTaskInfo
var err error
if workflowTaskHeartbeating && !workflowTaskHeartbeatTimeout {
newWorkflowTask, err = msBuilder.AddWorkflowTaskScheduledEventAsHeartbeat(
bypassTaskGeneration,
currentWorkflowTask.OriginalScheduledTime,
)
} else {
newWorkflowTask, err = msBuilder.AddWorkflowTaskScheduledEvent(bypassTaskGeneration)
}
if err != nil {
return nil, err
}
newWorkflowTaskScheduledID = newWorkflowTask.ScheduleID
// skip transfer task for workflow task if request asking to return new workflow task
if bypassTaskGeneration {
// start the new workflow task if request asked to do so
// TODO: replace the poll request
_, _, err := msBuilder.AddWorkflowTaskStartedEvent(
newWorkflowTask.ScheduleID,
"request-from-RespondWorkflowTaskCompleted",
newWorkflowTask.TaskQueue,
request.Identity,
)
if err != nil {
return nil, err
}
}
}
var updateErr error
if newStateBuilder != nil {
newWorkflowExecutionInfo := newStateBuilder.GetExecutionInfo()
newWorkflowExecutionState := newStateBuilder.GetExecutionState()
updateErr = weContext.UpdateWorkflowExecutionWithNewAsActive(
ctx,
handler.shard.GetTimeSource().Now(),
workflow.NewContext(
handler.shard,
definition.NewWorkflowKey(
newWorkflowExecutionInfo.NamespaceId,
newWorkflowExecutionInfo.WorkflowId,
newWorkflowExecutionState.RunId,
),
handler.logger,
),
newStateBuilder,
)
} else {
updateErr = weContext.UpdateWorkflowExecutionAsActive(ctx, handler.shard.GetTimeSource().Now())
}
if updateErr != nil {
if updateErr == consts.ErrConflict {
handler.metricsClient.IncCounter(metrics.HistoryRespondWorkflowTaskCompletedScope, metrics.ConcurrencyUpdateFailureCounter)
}
// if updateErr resulted in TransactionSizeLimitError then fail workflow
switch updateErr.(type) {
case *persistence.TransactionSizeLimitError:
// must reload mutable state because the first call to updateWorkflowExecutionWithContext or continueAsNewWorkflowExecution
// clears mutable state if error is returned
msBuilder, err = weContext.LoadWorkflowExecution(ctx)
if err != nil {
return nil, err
}
eventBatchFirstEventID := msBuilder.GetNextEventID()
if err := workflow.TerminateWorkflow(
msBuilder,
eventBatchFirstEventID,
common.FailureReasonTransactionSizeExceedsLimit,
payloads.EncodeString(updateErr.Error()),
consts.IdentityHistoryService,
); err != nil {
return nil, err
}
if err := weContext.UpdateWorkflowExecutionAsActive(
ctx,
handler.shard.GetTimeSource().Now(),
); err != nil {
return nil, err
}
}
return nil, updateErr
}
handler.handleBufferedQueries(msBuilder, req.GetCompleteRequest().GetQueryResults(), createNewWorkflowTask, namespaceEntry, workflowTaskHeartbeating)
if workflowTaskHeartbeatTimeout {
// at this point, update is successful, but we still return an error to client so that the worker will give up this workflow
return nil, serviceerror.NewNotFound("workflow task heartbeat timeout")
}
if wtFailedCause != nil {
return nil, serviceerror.NewInvalidArgument(wtFailedCause.Message())
}
resp = &historyservice.RespondWorkflowTaskCompletedResponse{}
if request.GetReturnNewWorkflowTask() && createNewWorkflowTask {
workflowTask, _ := msBuilder.GetWorkflowTaskInfo(newWorkflowTaskScheduledID)
resp.StartedResponse, err = handler.createRecordWorkflowTaskStartedResponse(msBuilder, workflowTask, request.GetIdentity())
if err != nil {
return nil, err
}
// sticky is always enabled when worker request for new workflow task from RespondWorkflowTaskCompleted
resp.StartedResponse.StickyExecutionEnabled = true
}
for _, mutation := range responseMutations {
if err := mutation(resp); err != nil {
return nil, err
}
}
return resp, nil
}
func (handler *workflowTaskHandlerCallbacksImpl) createRecordWorkflowTaskStartedResponse(
msBuilder workflow.MutableState,
workflowTask *workflow.WorkflowTaskInfo,
identity string,
) (*historyservice.RecordWorkflowTaskStartedResponse, error) {
response := &historyservice.RecordWorkflowTaskStartedResponse{}
response.WorkflowType = msBuilder.GetWorkflowType()
executionInfo := msBuilder.GetExecutionInfo()
if executionInfo.LastWorkflowTaskStartId != common.EmptyEventID {
response.PreviousStartedEventId = executionInfo.LastWorkflowTaskStartId
}
// Starting workflowTask could result in different scheduleID if workflowTask was transient and new new events came in
// before it was started.
response.ScheduledEventId = workflowTask.ScheduleID
response.StartedEventId = workflowTask.StartedID
response.StickyExecutionEnabled = msBuilder.IsStickyTaskQueueEnabled()
response.NextEventId = msBuilder.GetNextEventID()
response.Attempt = workflowTask.Attempt
response.WorkflowExecutionTaskQueue = &taskqueuepb.TaskQueue{
Name: executionInfo.TaskQueue,
Kind: enumspb.TASK_QUEUE_KIND_NORMAL,
}
response.ScheduledTime = workflowTask.ScheduledTime
response.StartedTime = workflowTask.StartedTime
if workflowTask.Attempt > 1 {
// This workflowTask is retried from mutable state
// Also return schedule and started which are not written to history yet
scheduledEvent, startedEvent := msBuilder.CreateTransientWorkflowTaskEvents(workflowTask, identity)
response.WorkflowTaskInfo = &historyspb.TransientWorkflowTaskInfo{}
response.WorkflowTaskInfo.ScheduledEvent = scheduledEvent
response.WorkflowTaskInfo.StartedEvent = startedEvent
}
currentBranchToken, err := msBuilder.GetCurrentBranchToken()
if err != nil {
return nil, err
}
response.BranchToken = currentBranchToken
qr := msBuilder.GetQueryRegistry()
buffered := qr.GetBufferedIDs()
queries := make(map[string]*querypb.WorkflowQuery)
for _, id := range buffered {
input, err := qr.GetQueryInput(id)
if err != nil {
continue
}
queries[id] = input
}
response.Queries = queries
return response, nil
}
func (handler *workflowTaskHandlerCallbacksImpl) handleBufferedQueries(msBuilder workflow.MutableState, queryResults map[string]*querypb.WorkflowQueryResult, createNewWorkflowTask bool, namespaceEntry *namespace.Namespace, workflowTaskHeartbeating bool) {
queryRegistry := msBuilder.GetQueryRegistry()
if !queryRegistry.HasBufferedQuery() {
return
}
namespaceName := namespaceEntry.Name()
workflowID := msBuilder.GetExecutionInfo().WorkflowId
runID := msBuilder.GetExecutionState().GetRunId()
scope := handler.metricsClient.Scope(
metrics.HistoryRespondWorkflowTaskCompletedScope,
metrics.NamespaceTag(namespaceEntry.Name().String()),
metrics.CommandTypeTag("ConsistentQuery"))
// if its a heartbeat workflow task it means local activities may still be running on the worker
// which were started by an external event which happened before the query
if workflowTaskHeartbeating {
return
}
sizeLimitError := handler.config.BlobSizeLimitError(namespaceName.String())
sizeLimitWarn := handler.config.BlobSizeLimitWarn(namespaceName.String())
// Complete or fail all queries we have results for
for id, result := range queryResults {
if err := common.CheckEventBlobSizeLimit(
result.GetAnswer().Size(),
sizeLimitWarn,
sizeLimitError,
namespaceName.String(),
workflowID,
runID,
scope,
handler.throttledLogger,
tag.BlobSizeViolationOperation("ConsistentQuery"),
); err != nil {
handler.logger.Info("failing query because query result size is too large",
tag.WorkflowNamespace(namespaceName.String()),
tag.WorkflowID(workflowID),
tag.WorkflowRunID(runID),
tag.QueryID(id),
tag.Error(err))
failedTerminationState := &workflow.QueryTerminationState{
QueryTerminationType: workflow.QueryTerminationTypeFailed,
Failure: err,
}
if err := queryRegistry.SetTerminationState(id, failedTerminationState); err != nil {
handler.logger.Error(
"failed to set query termination state to failed",
tag.WorkflowNamespace(namespaceName.String()),
tag.WorkflowID(workflowID),
tag.WorkflowRunID(runID),
tag.QueryID(id),
tag.Error(err))
scope.IncCounter(metrics.QueryRegistryInvalidStateCount)
}
} else {
completedTerminationState := &workflow.QueryTerminationState{
QueryTerminationType: workflow.QueryTerminationTypeCompleted,
QueryResult: result,
}
if err := queryRegistry.SetTerminationState(id, completedTerminationState); err != nil {
handler.logger.Error(
"failed to set query termination state to completed",
tag.WorkflowNamespace(namespaceName.String()),
tag.WorkflowID(workflowID),
tag.WorkflowRunID(runID),
tag.QueryID(id),
tag.Error(err))
scope.IncCounter(metrics.QueryRegistryInvalidStateCount)
}
}
}
// If no workflow task was created then it means no buffered events came in during this workflow task's handling.
// This means all unanswered buffered queries can be dispatched directly through matching at this point.
if !createNewWorkflowTask {
buffered := queryRegistry.GetBufferedIDs()
for _, id := range buffered {
unblockTerminationState := &workflow.QueryTerminationState{
QueryTerminationType: workflow.QueryTerminationTypeUnblocked,
}
if err := queryRegistry.SetTerminationState(id, unblockTerminationState); err != nil {
handler.logger.Error(
"failed to set query termination state to unblocked",
tag.WorkflowNamespace(namespaceName.String()),
tag.WorkflowID(workflowID),
tag.WorkflowRunID(runID),
tag.QueryID(id),
tag.Error(err))
scope.IncCounter(metrics.QueryRegistryInvalidStateCount)
}
}
}
}