Skip to content

Commit

Permalink
Fix start workflow execution expiration time (cadence-workflow#3371)
Browse files Browse the repository at this point in the history
  • Loading branch information
yux0 authored Jul 6, 2020
1 parent 6a238a7 commit f8c60c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
8 changes: 5 additions & 3 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,14 @@ func CreateHistoryStartWorkflowRequest(
DomainUUID: StringPtr(domainID),
StartRequest: startRequest,
}
firstDecisionTaskBackoffSeconds := backoff.GetBackoffForNextScheduleInSeconds(startRequest.GetCronSchedule(), now, now)
if startRequest.RetryPolicy != nil && startRequest.RetryPolicy.GetExpirationIntervalInSeconds() > 0 {
expirationInSeconds := startRequest.RetryPolicy.GetExpirationIntervalInSeconds()
deadline := now.Add(time.Second * time.Duration(expirationInSeconds))
expirationInSeconds := startRequest.RetryPolicy.GetExpirationIntervalInSeconds() + firstDecisionTaskBackoffSeconds
// expirationTime calculates from first decision task schedule to the end of the workflow
deadline := now.Add(time.Duration(expirationInSeconds) * time.Second)
histRequest.ExpirationTimestamp = Int64Ptr(deadline.Round(time.Millisecond).UnixNano())
}
histRequest.FirstDecisionTaskBackoffSeconds = Int32Ptr(backoff.GetBackoffForNextScheduleInSeconds(startRequest.GetCronSchedule(), now, now))
histRequest.FirstDecisionTaskBackoffSeconds = Int32Ptr(firstDecisionTaskBackoffSeconds)
return histRequest
}

Expand Down
38 changes: 38 additions & 0 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import (
"testing"
"time"

"github.com/pborman/uuid"
"github.com/stretchr/testify/require"

workflow "github.com/uber/cadence/.gen/go/shared"
)

func TestIsServiceTransientError_ContextTimeout(t *testing.T) {
Expand Down Expand Up @@ -58,3 +61,38 @@ func TestConvertDynamicConfigMapPropertyToIntMap(t *testing.T) {
require.Equal(t, i, intMap[i])
}
}

func TestCreateHistoryStartWorkflowRequest_ExpirationTimeWithCron(t *testing.T) {
domainId := uuid.New()
request := &workflow.StartWorkflowExecutionRequest{
RetryPolicy: &workflow.RetryPolicy{
InitialIntervalInSeconds: Int32Ptr(60),
ExpirationIntervalInSeconds: Int32Ptr(60),
},
CronSchedule: StringPtr("@every 300s"),
}
now := time.Now()
startRequest := CreateHistoryStartWorkflowRequest(domainId, request)

expirationTime := startRequest.GetExpirationTimestamp()
require.NotNil(t, expirationTime)
require.True(t, time.Unix(0, expirationTime).Sub(now) > 60 * time.Second)
}

func TestCreateHistoryStartWorkflowRequest_ExpirationTimeWithoutCron(t *testing.T) {
domainId := uuid.New()
request := &workflow.StartWorkflowExecutionRequest{
RetryPolicy: &workflow.RetryPolicy{
InitialIntervalInSeconds: Int32Ptr(60),
ExpirationIntervalInSeconds: Int32Ptr(60),
},
}
now := time.Now()
startRequest := CreateHistoryStartWorkflowRequest(domainId, request)

expirationTime := startRequest.GetExpirationTimestamp()
require.NotNil(t, expirationTime)
delta := time.Unix(0, expirationTime).Sub(now)
require.True(t, delta > 58 * time.Second)
require.True(t, delta < 62 * time.Second)
}
4 changes: 2 additions & 2 deletions service/history/task/timer_active_task_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func (t *timerActiveTaskExecutor) executeWorkflowTimeoutTask(
}

startAttributes := startEvent.WorkflowExecutionStartedEventAttributes
continueAsnewAttributes := &workflow.ContinueAsNewWorkflowExecutionDecisionAttributes{
continueAsNewAttributes := &workflow.ContinueAsNewWorkflowExecutionDecisionAttributes{
WorkflowType: startAttributes.WorkflowType,
TaskList: startAttributes.TaskList,
Input: startAttributes.Input,
Expand All @@ -515,7 +515,7 @@ func (t *timerActiveTaskExecutor) executeWorkflowTimeoutTask(
mutableState,
eventBatchFirstEventID,
startAttributes.GetParentWorkflowDomain(),
continueAsnewAttributes,
continueAsNewAttributes,
)
if err != nil {
return err
Expand Down

0 comments on commit f8c60c1

Please sign in to comment.