/
timer_queue_active_task_executor.go
681 lines (608 loc) · 22.2 KB
/
timer_queue_active_task_executor.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
// 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"
"github.com/pborman/uuid"
"google.golang.org/protobuf/types/known/durationpb"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/api/matchingservice/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/backoff"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/failure"
"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/primitives/timestamp"
"go.temporal.io/server/common/resource"
"go.temporal.io/server/common/worker_versioning"
"go.temporal.io/server/service/history/configs"
"go.temporal.io/server/service/history/consts"
"go.temporal.io/server/service/history/deletemanager"
"go.temporal.io/server/service/history/queues"
"go.temporal.io/server/service/history/shard"
"go.temporal.io/server/service/history/tasks"
"go.temporal.io/server/service/history/vclock"
"go.temporal.io/server/service/history/workflow"
wcache "go.temporal.io/server/service/history/workflow/cache"
)
type (
timerQueueActiveTaskExecutor struct {
*timerQueueTaskExecutorBase
}
)
func newTimerQueueActiveTaskExecutor(
shard shard.Context,
workflowCache wcache.Cache,
workflowDeleteManager deletemanager.DeleteManager,
logger log.Logger,
metricProvider metrics.Handler,
config *configs.Config,
matchingRawClient resource.MatchingRawClient,
) queues.Executor {
return &timerQueueActiveTaskExecutor{
timerQueueTaskExecutorBase: newTimerQueueTaskExecutorBase(
shard,
workflowCache,
workflowDeleteManager,
matchingRawClient,
logger,
metricProvider,
config,
),
}
}
func (t *timerQueueActiveTaskExecutor) Execute(
ctx context.Context,
executable queues.Executable,
) queues.ExecuteResponse {
taskTypeTagValue := queues.GetActiveTimerTaskTypeTagValue(executable)
namespaceTag, replicationState := getNamespaceTagAndReplicationStateByID(
t.shardContext.GetNamespaceRegistry(),
executable.GetNamespaceID(),
)
metricsTags := []metrics.Tag{
namespaceTag,
metrics.TaskTypeTag(taskTypeTagValue),
metrics.OperationTag(taskTypeTagValue), // for backward compatibility
}
if replicationState == enumspb.REPLICATION_STATE_HANDOVER {
// TODO: exclude task types here if we believe it's safe & necessary to execute
// them during namespace handover.
// TODO: move this logic to queues.Executable when metrics tag doesn't need to
// be returned from task executor
return queues.ExecuteResponse{
ExecutionMetricTags: metricsTags,
ExecutedAsActive: true,
ExecutionErr: consts.ErrNamespaceHandover,
}
}
var err error
switch task := executable.GetTask().(type) {
case *tasks.UserTimerTask:
err = t.executeUserTimerTimeoutTask(ctx, task)
case *tasks.ActivityTimeoutTask:
err = t.executeActivityTimeoutTask(ctx, task)
case *tasks.WorkflowTaskTimeoutTask:
err = t.executeWorkflowTaskTimeoutTask(ctx, task)
case *tasks.WorkflowTimeoutTask:
err = t.executeWorkflowTimeoutTask(ctx, task)
case *tasks.ActivityRetryTimerTask:
err = t.executeActivityRetryTimerTask(ctx, task)
case *tasks.WorkflowBackoffTimerTask:
err = t.executeWorkflowBackoffTimerTask(ctx, task)
case *tasks.DeleteHistoryEventTask:
err = t.executeDeleteHistoryEventTask(ctx, task)
default:
err = errUnknownTimerTask
}
return queues.ExecuteResponse{
ExecutionMetricTags: metricsTags,
ExecutedAsActive: true,
ExecutionErr: err,
}
}
func (t *timerQueueActiveTaskExecutor) executeUserTimerTimeoutTask(
ctx context.Context,
task *tasks.UserTimerTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil {
release(nil) // release(nil) so mutable state is not unloaded from cache
return consts.ErrWorkflowExecutionNotFound
}
timerSequence := t.getTimerSequence(mutableState)
referenceTime := t.shardContext.GetTimeSource().Now()
timerFired := false
Loop:
for _, timerSequenceID := range timerSequence.LoadAndSortUserTimers() {
timerInfo, ok := mutableState.GetUserTimerInfoByEventID(timerSequenceID.EventID)
if !ok {
errString := fmt.Sprintf("failed to find in user timer event ID: %v", timerSequenceID.EventID)
t.logger.Error(errString)
return serviceerror.NewInternal(errString)
}
if !queues.IsTimeExpired(referenceTime, timerSequenceID.Timestamp) {
// Timer sequence IDs are sorted; once we encounter a timer whose
// sequence ID has not expired, all subsequent timers will not have
// expired.
break Loop
}
if !mutableState.IsWorkflowExecutionRunning() {
release(nil) // so mutable state is not unloaded from cache
return consts.ErrWorkflowCompleted
}
if _, err := mutableState.AddTimerFiredEvent(timerInfo.GetTimerId()); err != nil {
return err
}
timerFired = true
}
if !timerFired {
release(nil) // so mutable state is not unloaded from cache
return errNoTimerFired
}
return t.updateWorkflowExecution(ctx, weContext, mutableState, timerFired)
}
func (t *timerQueueActiveTaskExecutor) executeActivityTimeoutTask(
ctx context.Context,
task *tasks.ActivityTimeoutTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil || !mutableState.IsWorkflowExecutionRunning() {
return nil
}
timerSequence := t.getTimerSequence(mutableState)
referenceTime := t.shardContext.GetTimeSource().Now()
updateMutableState := false
scheduleWorkflowTask := false
// Need to clear activity heartbeat timer task mask for new activity timer task creation.
// NOTE: LastHeartbeatTimeoutVisibilityInSeconds is for deduping heartbeat timer creation as it's possible
// one heartbeat task was persisted multiple times with different taskIDs due to the retry logic
// for updating workflow execution. In that case, only one new heartbeat timeout task should be
// created.
isHeartBeatTask := task.TimeoutType == enumspb.TIMEOUT_TYPE_HEARTBEAT
activityInfo, heartbeatTimeoutVis, ok := mutableState.GetActivityInfoWithTimerHeartbeat(task.EventID)
if isHeartBeatTask && ok && queues.IsTimeExpired(task.GetVisibilityTime(), heartbeatTimeoutVis) {
activityInfo.TimerTaskStatus = activityInfo.TimerTaskStatus &^ workflow.TimerTaskStatusCreatedHeartbeat
if err := mutableState.UpdateActivity(activityInfo); err != nil {
return err
}
updateMutableState = true
}
Loop:
for _, timerSequenceID := range timerSequence.LoadAndSortActivityTimers() {
activityInfo, ok := mutableState.GetActivityInfo(timerSequenceID.EventID)
if !ok || timerSequenceID.Attempt < activityInfo.Attempt {
// handle 2 cases:
// 1. !ok
// this case can happen since each activity can have 4 timers
// and one of those 4 timers may have fired in this loop
// 2. timerSequenceID.attempt < activityInfo.Attempt
// retry could update activity attempt, should not timeouts new attempt
continue Loop
}
if !queues.IsTimeExpired(referenceTime, timerSequenceID.Timestamp) {
// timer sequence IDs are sorted, once there is one timer
// sequence ID not expired, all after that wil not expired
break Loop
}
failureMsg := fmt.Sprintf("activity %v timeout", timerSequenceID.TimerType.String())
timeoutFailure := failure.NewTimeoutFailure(failureMsg, timerSequenceID.TimerType)
var retryState enumspb.RetryState
if retryState, err = mutableState.RetryActivity(
activityInfo,
timeoutFailure,
); err != nil {
return err
} else if retryState == enumspb.RETRY_STATE_IN_PROGRESS {
updateMutableState = true
continue Loop
}
timeoutFailure.GetTimeoutFailureInfo().LastHeartbeatDetails = activityInfo.LastHeartbeatDetails
// If retryState is Timeout then it means that expirationTime is expired.
// ExpirationTime is expired when ScheduleToClose timeout is expired.
if retryState == enumspb.RETRY_STATE_TIMEOUT {
timeoutFailure.GetTimeoutFailureInfo().TimeoutType = enumspb.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE
}
t.emitTimeoutMetricScopeWithNamespaceTag(
namespace.ID(mutableState.GetExecutionInfo().NamespaceId),
metrics.TimerActiveTaskActivityTimeoutScope,
timerSequenceID.TimerType,
)
if _, err := mutableState.AddActivityTaskTimedOutEvent(
activityInfo.ScheduledEventId,
activityInfo.StartedEventId,
timeoutFailure,
retryState,
); err != nil {
return err
}
updateMutableState = true
scheduleWorkflowTask = true
}
if !updateMutableState {
return nil
}
return t.updateWorkflowExecution(ctx, weContext, mutableState, scheduleWorkflowTask)
}
func (t *timerQueueActiveTaskExecutor) executeWorkflowTaskTimeoutTask(
ctx context.Context,
task *tasks.WorkflowTaskTimeoutTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil || !mutableState.IsWorkflowExecutionRunning() {
return nil
}
workflowTask := mutableState.GetWorkflowTaskByID(task.EventID)
if workflowTask == nil {
return nil
}
if workflowTask.Type == enumsspb.WORKFLOW_TASK_TYPE_SPECULATIVE {
// Check if mutable state still points to this task.
// Mutable state can lost speculative WT or even has another one there if, for example, workflow was evicted from cache.
if !mutableState.CheckSpeculativeWorkflowTaskTimeoutTask(task) {
return nil
}
} else {
err = CheckTaskVersion(t.shardContext, t.logger, mutableState.GetNamespaceEntry(), workflowTask.Version, task.Version, task)
if err != nil {
return err
}
if workflowTask.Attempt != task.ScheduleAttempt {
return nil
}
}
scheduleWorkflowTask := false
switch task.TimeoutType {
case enumspb.TIMEOUT_TYPE_START_TO_CLOSE:
t.emitTimeoutMetricScopeWithNamespaceTag(
namespace.ID(mutableState.GetExecutionInfo().NamespaceId),
metrics.TimerActiveTaskWorkflowTaskTimeoutScope,
enumspb.TIMEOUT_TYPE_START_TO_CLOSE,
)
if _, err := mutableState.AddWorkflowTaskTimedOutEvent(
workflowTask,
); err != nil {
return err
}
scheduleWorkflowTask = true
case enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START:
if workflowTask.StartedEventID != common.EmptyEventID {
// workflowTask has already started
return nil
}
t.emitTimeoutMetricScopeWithNamespaceTag(
namespace.ID(mutableState.GetExecutionInfo().NamespaceId),
metrics.TimerActiveTaskWorkflowTaskTimeoutScope,
enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START,
)
_, err := mutableState.AddWorkflowTaskScheduleToStartTimeoutEvent(workflowTask)
if err != nil {
return err
}
scheduleWorkflowTask = true
}
return t.updateWorkflowExecution(ctx, weContext, mutableState, scheduleWorkflowTask)
}
func (t *timerQueueActiveTaskExecutor) executeWorkflowBackoffTimerTask(
ctx context.Context,
task *tasks.WorkflowBackoffTimerTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil || !mutableState.IsWorkflowExecutionRunning() {
return nil
}
if task.WorkflowBackoffType == enumsspb.WORKFLOW_BACKOFF_TYPE_RETRY {
t.metricHandler.Counter(metrics.WorkflowRetryBackoffTimerCount.Name()).Record(
1,
metrics.OperationTag(metrics.TimerActiveTaskWorkflowBackoffTimerScope),
)
} else if task.WorkflowBackoffType == enumsspb.WORKFLOW_BACKOFF_TYPE_CRON {
t.metricHandler.Counter(metrics.WorkflowCronBackoffTimerCount.Name()).Record(
1,
metrics.OperationTag(metrics.TimerActiveTaskWorkflowBackoffTimerScope),
)
} else if task.WorkflowBackoffType == enumsspb.WORKFLOW_BACKOFF_TYPE_DELAY_START {
t.metricHandler.Counter(metrics.WorkflowDelayedStartBackoffTimerCount.Name()).Record(
1,
metrics.OperationTag(metrics.TimerActiveTaskWorkflowBackoffTimerScope),
)
}
if mutableState.HadOrHasWorkflowTask() {
// already has workflow task
return nil
}
// schedule first workflow task
return t.updateWorkflowExecution(ctx, weContext, mutableState, true)
}
func (t *timerQueueActiveTaskExecutor) executeActivityRetryTimerTask(
ctx context.Context,
task *tasks.ActivityRetryTimerTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil {
release(nil) // release(nil) so mutable state is not unloaded from cache
return consts.ErrWorkflowExecutionNotFound
}
// generate activity task
activityInfo, ok := mutableState.GetActivityInfo(task.EventID)
if !ok || task.Attempt < activityInfo.Attempt || activityInfo.StartedEventId != common.EmptyEventID {
if ok {
t.logger.Info("Duplicate activity retry timer task",
tag.WorkflowID(mutableState.GetExecutionInfo().WorkflowId),
tag.WorkflowRunID(mutableState.GetExecutionState().GetRunId()),
tag.WorkflowNamespaceID(mutableState.GetExecutionInfo().NamespaceId),
tag.WorkflowScheduledEventID(activityInfo.ScheduledEventId),
tag.Attempt(activityInfo.Attempt),
tag.FailoverVersion(activityInfo.Version),
tag.TimerTaskStatus(activityInfo.TimerTaskStatus),
tag.ScheduleAttempt(task.Attempt))
}
release(nil) // release(nil) so mutable state is not unloaded from cache
return consts.ErrActivityTaskNotFound
}
err = CheckTaskVersion(t.shardContext, t.logger, mutableState.GetNamespaceEntry(), activityInfo.Version, task.Version, task)
if err != nil {
return err
}
if !mutableState.IsWorkflowExecutionRunning() {
release(nil) // release(nil) so mutable state is not unloaded from cache
return consts.ErrWorkflowCompleted
}
taskQueue := &taskqueuepb.TaskQueue{
Name: activityInfo.TaskQueue,
Kind: enumspb.TASK_QUEUE_KIND_NORMAL,
}
scheduleToStartTimeout := timestamp.DurationValue(activityInfo.ScheduleToStartTimeout)
directive := worker_versioning.MakeDirectiveForActivityTask(mutableState.GetWorkerVersionStamp(), activityInfo.UseCompatibleVersion)
// NOTE: do not access anything related mutable state after this lock release
release(nil) // release earlier as we don't need the lock anymore
_, retError = t.matchingRawClient.AddActivityTask(ctx, &matchingservice.AddActivityTaskRequest{
NamespaceId: task.GetNamespaceID(),
Execution: &commonpb.WorkflowExecution{
WorkflowId: task.GetWorkflowID(),
RunId: task.GetRunID(),
},
TaskQueue: taskQueue,
ScheduledEventId: task.EventID,
ScheduleToStartTimeout: durationpb.New(scheduleToStartTimeout),
Clock: vclock.NewVectorClock(t.shardContext.GetClusterMetadata().GetClusterID(), t.shardContext.GetShardID(), task.TaskID),
VersionDirective: directive,
})
return retError
}
func (t *timerQueueActiveTaskExecutor) executeWorkflowTimeoutTask(
ctx context.Context,
task *tasks.WorkflowTimeoutTask,
) (retError error) {
ctx, cancel := context.WithTimeout(ctx, taskTimeout)
defer cancel()
weContext, release, err := getWorkflowExecutionContextForTask(ctx, t.shardContext, t.cache, task)
if err != nil {
return err
}
defer func() { release(retError) }()
mutableState, err := loadMutableStateForTimerTask(ctx, t.shardContext, weContext, task, t.metricHandler, t.logger)
if err != nil {
return err
}
if mutableState == nil || !mutableState.IsWorkflowExecutionRunning() {
return nil
}
startVersion, err := mutableState.GetStartVersion()
if err != nil {
return err
}
err = CheckTaskVersion(t.shardContext, t.logger, mutableState.GetNamespaceEntry(), startVersion, task.Version, task)
if err != nil {
return err
}
timeoutFailure := failure.NewTimeoutFailure("workflow timeout", enumspb.TIMEOUT_TYPE_START_TO_CLOSE)
backoffInterval := backoff.NoBackoff
retryState := enumspb.RETRY_STATE_TIMEOUT
initiator := enumspb.CONTINUE_AS_NEW_INITIATOR_UNSPECIFIED
wfExpTime := mutableState.GetExecutionInfo().WorkflowExecutionExpirationTime
if wfExpTime == nil || wfExpTime.AsTime().IsZero() || wfExpTime.AsTime().After(t.shardContext.GetTimeSource().Now()) {
backoffInterval, retryState = mutableState.GetRetryBackoffDuration(timeoutFailure)
if backoffInterval != backoff.NoBackoff {
// We have a retry policy and we should retry.
initiator = enumspb.CONTINUE_AS_NEW_INITIATOR_RETRY
} else if backoffInterval = mutableState.GetCronBackoffDuration(); backoffInterval != backoff.NoBackoff {
// We have a cron schedule.
initiator = enumspb.CONTINUE_AS_NEW_INITIATOR_CRON_SCHEDULE
}
}
var newRunID string
if initiator != enumspb.CONTINUE_AS_NEW_INITIATOR_UNSPECIFIED {
newRunID = uuid.New()
}
// First add timeout workflow event, no matter what we're doing next.
if err := workflow.TimeoutWorkflow(
mutableState,
retryState,
newRunID,
); err != nil {
return err
}
// No more retries, or workflow is expired.
if initiator == enumspb.CONTINUE_AS_NEW_INITIATOR_UNSPECIFIED {
// We apply the update to execution using optimistic concurrency. If it fails due to a conflict than reload
// the history and try the operation again.
return t.updateWorkflowExecution(ctx, weContext, mutableState, false)
}
startEvent, err := mutableState.GetStartEvent(ctx)
if err != nil {
return err
}
startAttr := startEvent.GetWorkflowExecutionStartedEventAttributes()
newMutableState := workflow.NewMutableState(
t.shardContext,
t.shardContext.GetEventsCache(),
t.shardContext.GetLogger(),
mutableState.GetNamespaceEntry(),
t.shardContext.GetTimeSource().Now(),
)
err = workflow.SetupNewWorkflowForRetryOrCron(
ctx,
mutableState,
newMutableState,
newRunID,
startAttr,
startAttr.LastCompletionResult,
timeoutFailure,
backoffInterval,
initiator,
)
if err != nil {
return err
}
err = newMutableState.SetHistoryTree(
ctx,
newMutableState.GetExecutionInfo().WorkflowExecutionTimeout,
newMutableState.GetExecutionInfo().WorkflowRunTimeout,
newRunID)
if err != nil {
return err
}
newExecutionInfo := newMutableState.GetExecutionInfo()
newExecutionState := newMutableState.GetExecutionState()
return weContext.UpdateWorkflowExecutionWithNewAsActive(
ctx,
t.shardContext,
workflow.NewContext(
t.shardContext.GetConfig(),
definition.NewWorkflowKey(
newExecutionInfo.NamespaceId,
newExecutionInfo.WorkflowId,
newExecutionState.RunId,
),
t.logger,
t.shardContext.GetThrottledLogger(),
t.shardContext.GetMetricsHandler(),
),
newMutableState,
)
}
func (t *timerQueueActiveTaskExecutor) getTimerSequence(
mutableState workflow.MutableState,
) workflow.TimerSequence {
return workflow.NewTimerSequence(mutableState)
}
func (t *timerQueueActiveTaskExecutor) updateWorkflowExecution(
ctx context.Context,
context workflow.Context,
mutableState workflow.MutableState,
scheduleNewWorkflowTask bool,
) error {
var err error
if scheduleNewWorkflowTask {
// Schedule a new workflow task.
err = workflow.ScheduleWorkflowTask(mutableState)
if err != nil {
return err
}
}
return context.UpdateWorkflowExecutionAsActive(ctx, t.shardContext)
}
func (t *timerQueueActiveTaskExecutor) emitTimeoutMetricScopeWithNamespaceTag(
namespaceID namespace.ID,
operation string,
timerType enumspb.TimeoutType,
) {
namespaceEntry, err := t.registry.GetNamespaceByID(namespaceID)
if err != nil {
return
}
metricsScope := t.metricHandler.WithTags(
metrics.OperationTag(operation),
metrics.NamespaceTag(namespaceEntry.Name().String()),
)
switch timerType {
case enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START:
metricsScope.Counter(metrics.ScheduleToStartTimeoutCounter.Name()).Record(1)
case enumspb.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE:
metricsScope.Counter(metrics.ScheduleToCloseTimeoutCounter.Name()).Record(1)
case enumspb.TIMEOUT_TYPE_START_TO_CLOSE:
metricsScope.Counter(metrics.StartToCloseTimeoutCounter.Name()).Record(1)
case enumspb.TIMEOUT_TYPE_HEARTBEAT:
metricsScope.Counter(metrics.HeartbeatTimeoutCounter.Name()).Record(1)
}
}