Skip to content

Commit 3cd1cb1

Browse files
committed
Improve error messages for mismatch
1 parent 5815866 commit 3cd1cb1

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed

internal/command/command.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type CommandType int
1313
const (
1414
_ CommandType = iota
1515

16-
CommandType_ScheduleActivityTask
16+
CommandType_ScheduleActivity
1717

1818
CommandType_ScheduleSubWorkflow
1919
CommandType_CancelSubWorkflow
@@ -26,6 +26,27 @@ const (
2626
CommandType_CompleteWorkflow
2727
)
2828

29+
func (ct CommandType) String() string {
30+
switch ct {
31+
case CommandType_ScheduleActivity:
32+
return "ScheduleActivityTask"
33+
case CommandType_ScheduleSubWorkflow:
34+
return "ScheduleSubWorkflow"
35+
case CommandType_CancelSubWorkflow:
36+
return "CancelSubWorkflow"
37+
case CommandType_ScheduleTimer:
38+
return "ScheduleTimer"
39+
case CommandType_CancelTimer:
40+
return "CancelTimer"
41+
case CommandType_SideEffect:
42+
return "SideEffect"
43+
case CommandType_CompleteWorkflow:
44+
return "CompleteWorkflow"
45+
}
46+
47+
return ""
48+
}
49+
2950
type CommandState int
3051

3152
const (
@@ -52,7 +73,7 @@ type ScheduleActivityTaskCommandAttr struct {
5273
func NewScheduleActivityTaskCommand(id int64, name string, inputs []payload.Payload) Command {
5374
return Command{
5475
ID: id,
55-
Type: CommandType_ScheduleActivityTask,
76+
Type: CommandType_ScheduleActivity,
5677
Attr: &ScheduleActivityTaskCommandAttr{
5778
Name: name,
5879
Inputs: inputs,

internal/workflow/executor.go

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,20 @@ func (e *executor) handleWorkflowTaskStarted(event history.Event, a *history.Wor
262262

263263
func (e *executor) handleActivityScheduled(event history.Event, a *history.ActivityScheduledAttributes) error {
264264
c := e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
265-
if c != nil {
266-
// Ensure the same activity was scheduled again
267-
ca := c.Attr.(*command.ScheduleActivityTaskCommandAttr)
268-
if a.Name != ca.Name {
269-
return fmt.Errorf("previous workflow execution scheduled different type of activity: %s, %s", a.Name, ca.Name)
270-
}
265+
266+
// Ensure activity
267+
if c == nil {
268+
return fmt.Errorf("previous workflow execution scheduled an activity which could not be found")
269+
}
270+
271+
if c.Type != command.CommandType_ScheduleActivity {
272+
return fmt.Errorf("previous workflow execution scheduled an activity, this time: %v", c.Type)
273+
}
274+
275+
// Ensure the same activity was scheduled again
276+
ca := c.Attr.(*command.ScheduleActivityTaskCommandAttr)
277+
if a.Name != ca.Name {
278+
return fmt.Errorf("previous workflow execution scheduled different type of activity: %s, %s", a.Name, ca.Name)
271279
}
272280

273281
return nil
@@ -276,7 +284,7 @@ func (e *executor) handleActivityScheduled(event history.Event, a *history.Activ
276284
func (e *executor) handleActivityCompleted(event history.Event, a *history.ActivityCompletedAttributes) error {
277285
f, ok := e.workflowState.FutureByScheduleEventID(event.ScheduleEventID)
278286
if !ok {
279-
return nil
287+
return fmt.Errorf("could not find pending future for activity completion")
280288
}
281289

282290
e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
@@ -291,7 +299,7 @@ func (e *executor) handleActivityCompleted(event history.Event, a *history.Activ
291299
func (e *executor) handleActivityFailed(event history.Event, a *history.ActivityFailedAttributes) error {
292300
f, ok := e.workflowState.FutureByScheduleEventID(event.ScheduleEventID)
293301
if !ok {
294-
return errors.New("no pending future found for activity failed event")
302+
return errors.New("no pending future for activity failed event")
295303
}
296304

297305
e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
@@ -316,7 +324,14 @@ func (e *executor) handleTimerFired(event history.Event, a *history.TimerFiredAt
316324
return nil
317325
}
318326

319-
e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
327+
c := e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
328+
if c == nil {
329+
return fmt.Errorf("previous workflow execution scheduled a timer")
330+
}
331+
332+
if c.Type != command.CommandType_ScheduleTimer {
333+
return fmt.Errorf("previous workflow execution scheduled a timer, this time: %v", c.Type)
334+
}
320335

321336
if err := f(nil, nil); err != nil {
322337
return fmt.Errorf("setting result: %w", err)
@@ -343,27 +358,35 @@ func (e *executor) handleTimerCanceled(event history.Event, a *history.TimerCanc
343358

344359
func (e *executor) handleSubWorkflowScheduled(event history.Event, a *history.SubWorkflowScheduledAttributes) error {
345360
c := e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
346-
if c != nil {
347-
ca := c.Attr.(*command.ScheduleSubWorkflowCommandAttr)
348-
if a.Name != ca.Name {
349-
return errors.New("previous workflow execution scheduled a different sub workflow")
350-
}
361+
if c == nil {
362+
return fmt.Errorf("previous workflow execution scheduled a sub workflow")
363+
}
351364

352-
// Set correct InstanceID here.
353-
// TODO: see if we can provide better support for commands here and find a better place to store
354-
// this message.
355-
ca.Instance = a.SubWorkflowInstance
365+
if c.Type != command.CommandType_ScheduleSubWorkflow {
366+
return fmt.Errorf("previous workflow execution scheduled a sub workflow, this time: %v", c.Type)
356367
}
357368

358-
// TOOD: If the command cannot be found, raise error?
369+
ca := c.Attr.(*command.ScheduleSubWorkflowCommandAttr)
370+
if a.Name != ca.Name {
371+
return fmt.Errorf("previous workflow execution scheduled different type of sub workflow: %s, %s", a.Name, ca.Name)
372+
}
373+
374+
// Set correct InstanceID here.
375+
// TODO: see if we can provide better support for commands here and find a better place to store
376+
// this message.
377+
ca.Instance = a.SubWorkflowInstance
359378

360379
return nil
361380
}
362381

363382
func (e *executor) handleSubWorkflowCancellationRequest(event history.Event, a *history.SubWorkflowCancellationRequestedAttributes) error {
364383
c := e.workflowState.RemoveCommandByEventID(event.ScheduleEventID)
365-
if c != nil {
366-
return fmt.Errorf("expected to find a sub workflow cancellation event, instead got: %v", event.Type)
384+
if c == nil {
385+
return fmt.Errorf("previous workflow execution cancelled a sub-workflow execution")
386+
}
387+
388+
if c.Type != command.CommandType_CancelSubWorkflow {
389+
return fmt.Errorf("previous workflow execution cancelled a sub-workflow execution, this time: %v", c.Type)
367390
}
368391

369392
return e.workflow.Continue(e.workflowCtx)
@@ -441,7 +464,7 @@ func (e *executor) processCommands(ctx context.Context, t *task.Workflow) (bool,
441464
c.State = command.CommandState_Committed
442465

443466
switch c.Type {
444-
case command.CommandType_ScheduleActivityTask:
467+
case command.CommandType_ScheduleActivity:
445468
a := c.Attr.(*command.ScheduleActivityTaskCommandAttr)
446469

447470
scheduleActivityEvent := e.createNewEvent(

internal/workflow/executor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func Test_ExecuteWorkflowWithActivityCommand(t *testing.T) {
200200
require.Equal(t, command.Command{
201201
ID: 1,
202202
State: command.CommandState_Committed,
203-
Type: command.CommandType_ScheduleActivityTask,
203+
Type: command.CommandType_ScheduleActivity,
204204
Attr: &command.ScheduleActivityTaskCommandAttr{
205205
Name: "activity1",
206206
Inputs: []payload.Payload{inputs},
@@ -309,7 +309,7 @@ func Test_ExecuteWorkflowWithSelector(t *testing.T) {
309309
require.Len(t, e.workflowState.Commands(), 2)
310310

311311
require.Equal(t, command.CommandType_ScheduleTimer, e.workflowState.Commands()[0].Type)
312-
require.Equal(t, command.CommandType_ScheduleActivityTask, e.workflowState.Commands()[1].Type)
312+
require.Equal(t, command.CommandType_ScheduleActivity, e.workflowState.Commands()[1].Type)
313313
}
314314

315315
func Test_ExecuteNewEvents(t *testing.T) {

0 commit comments

Comments
 (0)