Skip to content

Commit 4a2368d

Browse files
authored
Clarify semantic of cancel wf commands (#849)
1 parent b207660 commit 4a2368d

File tree

5 files changed

+17
-50
lines changed

5 files changed

+17
-50
lines changed

core/src/worker/workflow/machines/workflow_machines.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ use temporal_sdk_core_protos::{
5858
workflow_activation::{
5959
workflow_activation_job, NotifyHasPatch, UpdateRandomSeed, WorkflowActivation,
6060
},
61-
workflow_commands::{
62-
request_cancel_external_workflow_execution as cancel_we, ContinueAsNewWorkflowExecution,
63-
},
61+
workflow_commands::ContinueAsNewWorkflowExecution,
6462
},
6563
temporal::api::{
6664
command::v1::{command::Attributes as ProtoCmdAttrs, Command as ProtoCommand},
@@ -1397,28 +1395,17 @@ impl WorkflowMachines {
13971395
CommandID::ChildWorkflowStart(attrs.child_workflow_seq),
13981396
)?,
13991397
WFCommand::RequestCancelExternalWorkflow(attrs) => {
1400-
let (we, only_child) = match attrs.target {
1401-
None => {
1402-
return Err(WFMachinesError::Fatal(
1403-
"Cancel external workflow command had empty target field"
1404-
.to_string(),
1405-
))
1406-
}
1407-
Some(cancel_we::Target::ChildWorkflowId(wfid)) => (
1408-
NamespacedWorkflowExecution {
1409-
namespace: self.worker_config.namespace.clone(),
1410-
workflow_id: wfid,
1411-
run_id: "".to_string(),
1412-
},
1413-
true,
1414-
),
1415-
Some(cancel_we::Target::WorkflowExecution(we)) => (we, false),
1416-
};
1398+
let we = attrs.workflow_execution.ok_or_else(|| {
1399+
WFMachinesError::Fatal(
1400+
"Cancel external workflow command had no workflow_execution field"
1401+
.to_string(),
1402+
)
1403+
})?;
14171404
self.add_cmd_to_wf_task(
14181405
new_external_cancel(
14191406
attrs.seq,
14201407
we,
1421-
only_child,
1408+
false,
14221409
format!("Cancel requested by workflow with run id {}", self.run_id),
14231410
),
14241411
CommandID::CancelExternal(attrs.seq).into(),

sdk-core-protos/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,13 @@ message CancelChildWorkflowExecution {
269269
uint32 child_workflow_seq = 1;
270270
}
271271

272-
// Request cancellation of an external workflow execution (which may be a started child)
272+
// Request cancellation of an external workflow execution. For cancellation of a child workflow,
273+
// prefer `CancelChildWorkflowExecution` instead, as it guards against cancel-before-start issues.
273274
message RequestCancelExternalWorkflowExecution {
274275
// Lang's incremental sequence number, used as the operation identifier
275276
uint32 seq = 1;
276-
// What workflow is being targeted
277-
oneof target {
278-
// A specific workflow instance
279-
common.NamespacedWorkflowExecution workflow_execution = 2;
280-
// The desired target must be a child of the issuing workflow, and this is its workflow id
281-
string child_workflow_id = 3;
282-
}
277+
// The workflow instance being targeted
278+
common.NamespacedWorkflowExecution workflow_execution = 2;
283279
}
284280

285281
// Send a signal to an external or child workflow

sdk/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,6 @@ pub enum CancellableID {
716716
seqnum: u32,
717717
/// Identifying information about the workflow to be cancelled
718718
execution: NamespacedWorkflowExecution,
719-
/// Set to true if this workflow is a child of the issuing workflow
720-
only_child: bool,
721719
},
722720
}
723721

sdk/src/workflow_context.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use temporal_sdk_core_protos::{
3434
common::NamespacedWorkflowExecution,
3535
workflow_activation::resolve_child_workflow_execution_start::Status as ChildWorkflowStartStatus,
3636
workflow_commands::{
37-
request_cancel_external_workflow_execution as cancel_we,
3837
signal_external_workflow_execution as sig_we, workflow_command,
3938
CancelChildWorkflowExecution, ModifyWorkflowProperties,
4039
RequestCancelExternalWorkflowExecution, SetPatchMarker,
@@ -311,14 +310,13 @@ impl WfContext {
311310
&self,
312311
target: NamespacedWorkflowExecution,
313312
) -> impl Future<Output = CancelExternalWfResult> {
314-
let target = cancel_we::Target::WorkflowExecution(target);
315313
let seq = self.seq_nums.write().next_cancel_external_wf_seq();
316314
let (cmd, unblocker) = WFCommandFut::new();
317315
self.send(
318316
CommandCreateRequest {
319317
cmd: RequestCancelExternalWorkflowExecution {
320318
seq,
321-
target: Some(target),
319+
workflow_execution: Some(target),
322320
}
323321
.into(),
324322
unblocker,
@@ -700,7 +698,6 @@ impl ChildWorkflow {
700698
workflow_id: self.opts.workflow_id.clone(),
701699
..Default::default()
702700
},
703-
only_child: true,
704701
});
705702
cx.send(
706703
CommandSubscribeChildWorkflowCompletion {

sdk/src/workflow_future.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ use temporal_sdk_core_protos::{
2222
WorkflowActivationJob,
2323
},
2424
workflow_commands::{
25-
request_cancel_external_workflow_execution as cancel_we, update_response,
26-
workflow_command, CancelChildWorkflowExecution, CancelSignalWorkflow, CancelTimer,
27-
CancelWorkflowExecution, CompleteWorkflowExecution, FailWorkflowExecution,
25+
update_response, workflow_command, CancelChildWorkflowExecution, CancelSignalWorkflow,
26+
CancelTimer, CancelWorkflowExecution, CompleteWorkflowExecution, FailWorkflowExecution,
2827
RequestCancelActivity, RequestCancelExternalWorkflowExecution,
2928
RequestCancelLocalActivity, ScheduleActivity, ScheduleLocalActivity,
3029
StartChildWorkflowExecution, StartTimer, UpdateResponse,
@@ -514,22 +513,12 @@ impl WorkflowFuture {
514513
CancelSignalWorkflow { seq },
515514
));
516515
}
517-
CancellableID::ExternalWorkflow {
518-
seqnum,
519-
execution,
520-
only_child,
521-
} => {
516+
CancellableID::ExternalWorkflow { seqnum, execution } => {
522517
activation_cmds.push(
523518
workflow_command::Variant::RequestCancelExternalWorkflowExecution(
524519
RequestCancelExternalWorkflowExecution {
525520
seq: seqnum,
526-
target: Some(if only_child {
527-
cancel_we::Target::ChildWorkflowId(
528-
execution.workflow_id,
529-
)
530-
} else {
531-
cancel_we::Target::WorkflowExecution(execution)
532-
}),
521+
workflow_execution: Some(execution),
533522
},
534523
),
535524
);

0 commit comments

Comments
 (0)