|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package host |
| 26 | + |
| 27 | +import ( |
| 28 | + "bytes" |
| 29 | + "encoding/binary" |
| 30 | + "strconv" |
| 31 | + "time" |
| 32 | + |
| 33 | + "github.com/pborman/uuid" |
| 34 | + commandpb "go.temporal.io/api/command/v1" |
| 35 | + commonpb "go.temporal.io/api/common/v1" |
| 36 | + enumspb "go.temporal.io/api/enums/v1" |
| 37 | + historypb "go.temporal.io/api/history/v1" |
| 38 | + taskqueuepb "go.temporal.io/api/taskqueue/v1" |
| 39 | + "go.temporal.io/api/workflowservice/v1" |
| 40 | + |
| 41 | + "go.temporal.io/server/common/log/tag" |
| 42 | + "go.temporal.io/server/common/payloads" |
| 43 | + "go.temporal.io/server/common/primitives/timestamp" |
| 44 | +) |
| 45 | + |
| 46 | +func (s *integrationSuite) TestResetWorkflow() { |
| 47 | + id := "integration-reset-workflow-test" |
| 48 | + wt := "integration-reset-workflow-test-type" |
| 49 | + tq := "integration-reset-workflow-test-taskqueue" |
| 50 | + identity := "worker1" |
| 51 | + |
| 52 | + workflowType := &commonpb.WorkflowType{Name: wt} |
| 53 | + |
| 54 | + taskQueue := &taskqueuepb.TaskQueue{Name: tq} |
| 55 | + |
| 56 | + // Start workflow execution |
| 57 | + request := &workflowservice.StartWorkflowExecutionRequest{ |
| 58 | + RequestId: uuid.New(), |
| 59 | + Namespace: s.namespace, |
| 60 | + WorkflowId: id, |
| 61 | + WorkflowType: workflowType, |
| 62 | + TaskQueue: taskQueue, |
| 63 | + Input: nil, |
| 64 | + WorkflowRunTimeout: timestamp.DurationPtr(100 * time.Second), |
| 65 | + WorkflowTaskTimeout: timestamp.DurationPtr(1 * time.Second), |
| 66 | + Identity: identity, |
| 67 | + } |
| 68 | + |
| 69 | + we, err0 := s.engine.StartWorkflowExecution(NewContext(), request) |
| 70 | + s.NoError(err0) |
| 71 | + |
| 72 | + s.Logger.Info("StartWorkflowExecution", tag.WorkflowRunID(we.RunId)) |
| 73 | + |
| 74 | + // workflow logic |
| 75 | + workflowComplete := false |
| 76 | + activityData := int32(1) |
| 77 | + activityCount := 3 |
| 78 | + isFirstTaskProcessed := false |
| 79 | + isSecondTaskProcessed := false |
| 80 | + var firstActivityCompletionEvent *historypb.HistoryEvent |
| 81 | + wtHandler := func(execution *commonpb.WorkflowExecution, wt *commonpb.WorkflowType, |
| 82 | + previousStartedEventID, startedEventID int64, history *historypb.History) ([]*commandpb.Command, error) { |
| 83 | + |
| 84 | + if !isFirstTaskProcessed { |
| 85 | + // Schedule 3 activities on first workflow task |
| 86 | + isFirstTaskProcessed = true |
| 87 | + buf := new(bytes.Buffer) |
| 88 | + s.Nil(binary.Write(buf, binary.LittleEndian, activityData)) |
| 89 | + |
| 90 | + var scheduleActivityCommands []*commandpb.Command |
| 91 | + for i := 1; i <= activityCount; i++ { |
| 92 | + scheduleActivityCommands = append(scheduleActivityCommands, &commandpb.Command{ |
| 93 | + CommandType: enumspb.COMMAND_TYPE_SCHEDULE_ACTIVITY_TASK, |
| 94 | + Attributes: &commandpb.Command_ScheduleActivityTaskCommandAttributes{ScheduleActivityTaskCommandAttributes: &commandpb.ScheduleActivityTaskCommandAttributes{ |
| 95 | + ActivityId: strconv.Itoa(i), |
| 96 | + ActivityType: &commonpb.ActivityType{Name: "ResetActivity"}, |
| 97 | + TaskQueue: &taskqueuepb.TaskQueue{Name: tq}, |
| 98 | + Input: payloads.EncodeBytes(buf.Bytes()), |
| 99 | + ScheduleToCloseTimeout: timestamp.DurationPtr(100 * time.Second), |
| 100 | + ScheduleToStartTimeout: timestamp.DurationPtr(2 * time.Second), |
| 101 | + StartToCloseTimeout: timestamp.DurationPtr(50 * time.Second), |
| 102 | + HeartbeatTimeout: timestamp.DurationPtr(5 * time.Second), |
| 103 | + }}, |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + return scheduleActivityCommands, nil |
| 108 | + } else if !isSecondTaskProcessed { |
| 109 | + // Confirm one activity completion on second workflow task |
| 110 | + isSecondTaskProcessed = true |
| 111 | + for _, event := range history.Events[previousStartedEventID:] { |
| 112 | + if event.GetEventType() == enumspb.EVENT_TYPE_ACTIVITY_TASK_COMPLETED { |
| 113 | + firstActivityCompletionEvent = event |
| 114 | + return []*commandpb.Command{}, nil |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Complete workflow after reset |
| 120 | + workflowComplete = true |
| 121 | + return []*commandpb.Command{{ |
| 122 | + CommandType: enumspb.COMMAND_TYPE_COMPLETE_WORKFLOW_EXECUTION, |
| 123 | + Attributes: &commandpb.Command_CompleteWorkflowExecutionCommandAttributes{CompleteWorkflowExecutionCommandAttributes: &commandpb.CompleteWorkflowExecutionCommandAttributes{ |
| 124 | + Result: payloads.EncodeString("Done"), |
| 125 | + }}, |
| 126 | + }}, nil |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + // activity handler |
| 131 | + atHandler := func(execution *commonpb.WorkflowExecution, activityType *commonpb.ActivityType, |
| 132 | + activityID string, input *commonpb.Payloads, taskToken []byte) (*commonpb.Payloads, bool, error) { |
| 133 | + |
| 134 | + return payloads.EncodeString("Activity Result"), false, nil |
| 135 | + } |
| 136 | + |
| 137 | + poller := &TaskPoller{ |
| 138 | + Engine: s.engine, |
| 139 | + Namespace: s.namespace, |
| 140 | + TaskQueue: taskQueue, |
| 141 | + Identity: identity, |
| 142 | + WorkflowTaskHandler: wtHandler, |
| 143 | + ActivityTaskHandler: atHandler, |
| 144 | + Logger: s.Logger, |
| 145 | + T: s.T(), |
| 146 | + } |
| 147 | + |
| 148 | + // Process first workflow task to schedule activities |
| 149 | + _, err := poller.PollAndProcessWorkflowTask(false, false) |
| 150 | + s.Logger.Info("PollAndProcessWorkflowTask", tag.Error(err)) |
| 151 | + s.NoError(err) |
| 152 | + |
| 153 | + // Process one activity task which also creates second workflow task |
| 154 | + err = poller.PollAndProcessActivityTask(false) |
| 155 | + s.Logger.Info("Poll and process first activity", tag.Error(err)) |
| 156 | + s.NoError(err) |
| 157 | + |
| 158 | + // Process second workflow task which checks activity completion |
| 159 | + _, err = poller.PollAndProcessWorkflowTask(false, false) |
| 160 | + s.Logger.Info("Poll and process second workflow task", tag.Error(err)) |
| 161 | + s.NoError(err) |
| 162 | + |
| 163 | + // Find reset point (last completed workflow task) |
| 164 | + events := s.getHistory(s.namespace, &commonpb.WorkflowExecution{ |
| 165 | + WorkflowId: id, |
| 166 | + RunId: we.GetRunId(), |
| 167 | + }) |
| 168 | + var lastWorkflowTask *historypb.HistoryEvent |
| 169 | + for _, event := range events { |
| 170 | + if event.GetEventType() == enumspb.EVENT_TYPE_WORKFLOW_TASK_COMPLETED { |
| 171 | + lastWorkflowTask = event |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Reset workflow execution |
| 176 | + _, err = s.engine.ResetWorkflowExecution(NewContext(), &workflowservice.ResetWorkflowExecutionRequest{ |
| 177 | + Namespace: s.namespace, |
| 178 | + WorkflowExecution: &commonpb.WorkflowExecution{ |
| 179 | + WorkflowId: id, |
| 180 | + RunId: we.RunId, |
| 181 | + }, |
| 182 | + Reason: "reset execution from test", |
| 183 | + WorkflowTaskFinishEventId: lastWorkflowTask.GetEventId(), |
| 184 | + RequestId: uuid.New(), |
| 185 | + }) |
| 186 | + s.NoError(err) |
| 187 | + |
| 188 | + err = poller.PollAndProcessActivityTask(false) |
| 189 | + s.Logger.Info("Poll and process second activity", tag.Error(err)) |
| 190 | + s.NoError(err) |
| 191 | + |
| 192 | + err = poller.PollAndProcessActivityTask(false) |
| 193 | + s.Logger.Info("Poll and process third activity", tag.Error(err)) |
| 194 | + s.NoError(err) |
| 195 | + |
| 196 | + _, err = poller.PollAndProcessWorkflowTask(false, false) |
| 197 | + s.Logger.Info("Poll and process final workflow task", tag.Error(err)) |
| 198 | + s.NoError(err) |
| 199 | + |
| 200 | + s.NotNil(firstActivityCompletionEvent) |
| 201 | + s.True(workflowComplete) |
| 202 | +} |
0 commit comments