Files
Long TranandGitHub 1224e4d0cd Propagate backlinks on Signal and Signal-with-Start responses (#9897)
## What changed?

### **High level**

With https://github.com/temporalio/api/pull/761 to add the linking on
the Signal and Signal-with-Start responses, This PR adds logic from the
server that:
* Adds `requestID` from Signal and Signal-with-Start requests to the
CHASM workflow tree under a new map field `IncomingSignals`, and event
store, so these requestIDs stay in buffer
* Return a backlink in the response that references the `requestID`
* On buffer flush to the DB transaction, attach these `requestID` to a
concrete `eventID`, which would allow users to later know which event
correlated w/ this request. We will wire the concrete event ID to the
signal request IDs stored in the workflow component CHASM tree
(`IncomingSignals` map)

> [!NOTE]
> Feature is gated behind a new dynamicconfig
`EnableCHASMSignalBacklinks`, which implicitly is only checked if
`EnableChasm` is enabled.

## Why?

This will enable the caller of the signal to have a backlink to the
cross-namespace signal invoked, which will become more relevant for
Nexus SDK ergonomics.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [ ] covered by existing tests
- [ ] added new unit test(s)
- [x] added new functional test(s)

In functional tests, I augmented existing tests for Signal and
Signal-with-Start to:
* Ensure that backlink is returned via the responses
* Later use `DescribeWorkflow` to ensure that we get a concrete EventID
(mapped when buffer flushed)
* Multiple signals with the same `requestID` gets de-duped

```
$ go test ./tests/ -run TestLinksTestSuite
ok      go.temporal.io/server/tests     1.486s
```

```
$ go test ./tests/ -run 'TestNexusWorkflowTestSuite' -count=1
ok      go.temporal.io/server/tests     4.714s
```

## Potential risks

Need to test end-to-end to see that the link shows up correctly in the
Web UI.

Feature is gated behind dynamicconfig since it requires CHASM-based
workflow to be enabled.
2026-05-15 13:05:55 -04:00

125 lines
3.6 KiB
Go

package signalworkflow
import (
"context"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/service/history/api"
"go.temporal.io/server/service/history/consts"
historyi "go.temporal.io/server/service/history/interfaces"
)
func Invoke(
ctx context.Context,
req *historyservice.SignalWorkflowExecutionRequest,
shard historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
) (resp *historyservice.SignalWorkflowExecutionResponse, retError error) {
namespaceEntry, err := api.GetActiveNamespace(shard, namespace.ID(req.GetNamespaceId()), req.SignalRequest.WorkflowExecution.WorkflowId)
if err != nil {
return nil, err
}
namespaceID := namespaceEntry.ID()
request := req.SignalRequest
externalWorkflowExecution := req.ExternalWorkflowExecution
childWorkflowOnly := req.GetChildWorkflowOnly()
// Capture the currently-running workflow's runID from mutableState via our closure,
// in case the caller didn't pin the signal to a specific runID.
var runID string
err = api.GetAndUpdateWorkflowWithNew(
ctx,
nil,
definition.NewWorkflowKey(
namespaceID.String(),
request.WorkflowExecution.WorkflowId,
request.WorkflowExecution.RunId,
),
func(workflowLease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
mutableState := workflowLease.GetMutableState()
runID = mutableState.GetExecutionState().GetRunId()
if request.GetRequestId() != "" && mutableState.IsSignalRequested(request.GetRequestId()) {
return &api.UpdateWorkflowAction{
Noop: true,
CreateWorkflowTask: false,
}, nil
}
releaseFn := workflowLease.GetReleaseFn()
if !mutableState.IsWorkflowExecutionRunning() {
// in-memory mutable state is still clean, release the lock with nil error to prevent
// clearing and reloading mutable state
releaseFn(nil)
return nil, consts.ErrWorkflowCompleted
}
if err := api.ValidateSignal(
ctx,
shard,
mutableState,
request.GetInput().Size(),
request.GetHeader().Size(),
"SignalWorkflowExecution",
); err != nil {
releaseFn(nil)
return nil, err
}
executionInfo := mutableState.GetExecutionInfo()
// Do not create workflow task when the workflow has first workflow task backoff and execution is not started yet
createWorkflowTask := !mutableState.IsWorkflowPendingOnWorkflowTaskBackoff()
if childWorkflowOnly {
parentWorkflowID := executionInfo.ParentWorkflowId
parentRunID := executionInfo.ParentRunId
if externalWorkflowExecution.GetWorkflowId() != parentWorkflowID ||
externalWorkflowExecution.GetRunId() != parentRunID {
releaseFn(nil)
return nil, consts.ErrWorkflowParent
}
}
if request.GetRequestId() != "" {
mutableState.AddSignalRequested(request.GetRequestId())
}
_, err := mutableState.AddWorkflowExecutionSignaledEvent(
request.GetSignalName(),
request.GetInput(),
request.GetIdentity(),
request.GetHeader(),
externalWorkflowExecution,
request.GetRequestId(),
request.GetLinks(),
)
if err != nil {
return nil, err
}
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: createWorkflowTask,
}, nil
},
nil,
shard,
workflowConsistencyChecker,
)
if err != nil {
return nil, err
}
return &historyservice.SignalWorkflowExecutionResponse{
Link: api.GenerateRequestIDRefLink(
request.GetNamespace(),
request.GetWorkflowExecution().GetWorkflowId(),
runID,
request.GetRequestId(),
enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED,
),
}, nil
}