@@ -30,6 +30,7 @@ import (
3030 "context"
3131
3232 commonpb "go.temporal.io/api/common/v1"
33+ "go.temporal.io/api/serviceerror"
3334
3435 enumsspb "go.temporal.io/server/api/enums/v1"
3536 "go.temporal.io/server/api/historyservice/v1"
@@ -41,6 +42,7 @@ import (
4142 serviceerrors "go.temporal.io/server/common/serviceerror"
4243 "go.temporal.io/server/common/xdc"
4344 "go.temporal.io/server/service/history/shard"
45+ "go.temporal.io/server/service/history/workflow"
4446)
4547
4648type (
@@ -54,9 +56,10 @@ type (
5456 namespaceRegistry namespace.Registry
5557 nDCHistoryResender xdc.NDCHistoryResender
5658 historyEngine shard.Engine
57-
58- metricsClient metrics.Client
59- logger log.Logger
59+ deleteManager workflow.DeleteManager
60+ workflowCache workflow.Cache
61+ metricsClient metrics.Client
62+ logger log.Logger
6063 }
6164)
6265
@@ -67,6 +70,8 @@ func newReplicationTaskExecutor(
6770 namespaceRegistry namespace.Registry ,
6871 nDCHistoryResender xdc.NDCHistoryResender ,
6972 historyEngine shard.Engine ,
73+ deleteManager workflow.DeleteManager ,
74+ workflowCache workflow.Cache ,
7075 metricsClient metrics.Client ,
7176 logger log.Logger ,
7277) replicationTaskExecutor {
@@ -76,6 +81,8 @@ func newReplicationTaskExecutor(
7681 namespaceRegistry : namespaceRegistry ,
7782 nDCHistoryResender : nDCHistoryResender ,
7883 historyEngine : historyEngine ,
84+ deleteManager : deleteManager ,
85+ workflowCache : workflowCache ,
7986 metricsClient : metricsClient ,
8087 logger : logger ,
8188 }
@@ -153,17 +160,23 @@ func (e *replicationTaskExecutorImpl) handleActivityTask(
153160 stopwatch := e .metricsClient .StartTimer (metrics .HistoryRereplicationByActivityReplicationScope , metrics .ClientLatency )
154161 defer stopwatch .Stop ()
155162
156- if resendErr := e .nDCHistoryResender .SendSingleWorkflowHistory (
163+ resendErr := e .nDCHistoryResender .SendSingleWorkflowHistory (
157164 namespace .ID (retryErr .NamespaceId ),
158165 retryErr .WorkflowId ,
159166 retryErr .RunId ,
160167 retryErr .StartEventId ,
161168 retryErr .StartEventVersion ,
162169 retryErr .EndEventId ,
163170 retryErr .EndEventVersion ,
164- ); resendErr != nil {
165- e .logger .Error ("error resend history for sync activity" , tag .Error (resendErr ))
166- // should return the replication error, not the resending error
171+ )
172+ switch resendErr .(type ) {
173+ case * serviceerror.NotFound :
174+ // workflow is not found in source cluster, cleanup workflow in target cluster
175+ return e .cleanupWorkflowExecution (ctx , retryErr .NamespaceId , retryErr .WorkflowId , retryErr .RunId )
176+ case nil :
177+ //no-op
178+ default :
179+ e .logger .Error ("error resend history for history event" , tag .Error (resendErr ))
167180 return err
168181 }
169182 return e .historyEngine .SyncActivity (ctx , request )
@@ -211,17 +224,23 @@ func (e *replicationTaskExecutorImpl) handleHistoryReplicationTask(
211224 resendStopWatch := e .metricsClient .StartTimer (metrics .HistoryRereplicationByHistoryReplicationScope , metrics .ClientLatency )
212225 defer resendStopWatch .Stop ()
213226
214- if resendErr := e .nDCHistoryResender .SendSingleWorkflowHistory (
227+ resendErr := e .nDCHistoryResender .SendSingleWorkflowHistory (
215228 namespace .ID (retryErr .NamespaceId ),
216229 retryErr .WorkflowId ,
217230 retryErr .RunId ,
218231 retryErr .StartEventId ,
219232 retryErr .StartEventVersion ,
220233 retryErr .EndEventId ,
221234 retryErr .EndEventVersion ,
222- ); resendErr != nil {
235+ )
236+ switch resendErr .(type ) {
237+ case * serviceerror.NotFound :
238+ // workflow is not found in source cluster, cleanup workflow in target cluster
239+ return e .cleanupWorkflowExecution (ctx , retryErr .NamespaceId , retryErr .WorkflowId , retryErr .RunId )
240+ case nil :
241+ //no-op
242+ default :
223243 e .logger .Error ("error resend history for history event" , tag .Error (resendErr ))
224- // should return the replication error, not the resending error
225244 return err
226245 }
227246
@@ -256,3 +275,32 @@ FilterLoop:
256275 }
257276 return shouldProcessTask , nil
258277}
278+
279+ func (e * replicationTaskExecutorImpl ) cleanupWorkflowExecution (ctx context.Context , namespaceID string , workflowID string , runID string ) (retErr error ) {
280+ nsID := namespace .ID (namespaceID )
281+ ex := commonpb.WorkflowExecution {
282+ WorkflowId : workflowID ,
283+ RunId : runID ,
284+ }
285+ wfCtx , releaseFn , err := e .workflowCache .GetOrCreateWorkflowExecution (ctx , nsID , ex , workflow .CallerTypeTask )
286+ if err != nil {
287+ return err
288+ }
289+ defer func () { releaseFn (retErr ) }()
290+ mutableState , err := wfCtx .LoadWorkflowExecution (ctx )
291+ if err != nil {
292+ return err
293+ }
294+ lastWriteVersion , err := mutableState .GetLastWriteVersion ()
295+ if err != nil {
296+ return err
297+ }
298+ return e .deleteManager .DeleteWorkflowExecutionByReplication (
299+ ctx ,
300+ nsID ,
301+ ex ,
302+ wfCtx ,
303+ mutableState ,
304+ lastWriteVersion ,
305+ )
306+ }
0 commit comments