Skip to content

Commit cbd1b5e

Browse files
authored
Fix DeleteWorkflowExecution API when delete non current execution (#2484)
1 parent 1d33c3e commit cbd1b5e

File tree

10 files changed

+105
-58
lines changed

10 files changed

+105
-58
lines changed

service/history/historyEngine.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func NewEngineWithShardContext(
158158
historyCache,
159159
config,
160160
archivalClient,
161+
shard.GetTimeSource(),
161162
)
162163

163164
historyEngImpl := &historyEngineImpl{
@@ -2267,31 +2268,23 @@ func (e *historyEngineImpl) TerminateWorkflowExecution(
22672268

22682269
func (e *historyEngineImpl) DeleteWorkflowExecution(
22692270
ctx context.Context,
2270-
deleteRequest *historyservice.DeleteWorkflowExecutionRequest,
2271-
) error {
2272-
2273-
return e.updateWorkflow(
2274-
ctx,
2275-
namespace.ID(deleteRequest.NamespaceId),
2276-
*deleteRequest.GetWorkflowExecution(),
2277-
func(weCtx workflow.Context, mutableState workflow.MutableState) (*updateWorkflowAction, error) {
2278-
if mutableState.IsWorkflowExecutionRunning() {
2279-
return nil, consts.ErrWorkflowNotCompleted // workflow is running, cannot be deleted
2280-
}
2281-
2282-
taskGenerator := workflow.NewTaskGenerator(
2283-
e.shard.GetNamespaceRegistry(),
2284-
e.logger,
2285-
mutableState,
2286-
)
2271+
request *historyservice.DeleteWorkflowExecutionRequest,
2272+
) (retError error) {
2273+
nsID := namespace.ID(request.GetNamespaceId())
22872274

2288-
err := taskGenerator.GenerateDeleteExecutionTask(e.timeSource.Now())
2289-
if err != nil {
2290-
return nil, err
2291-
}
2275+
wfCtx, err := e.loadWorkflow(ctx, nsID, request.GetWorkflowExecution().GetWorkflowId(), request.GetWorkflowExecution().GetRunId())
2276+
if err != nil {
2277+
return err
2278+
}
2279+
defer func() { wfCtx.getReleaseFn()(retError) }()
22922280

2293-
return updateWorkflowWithoutWorkflowTask, nil
2294-
})
2281+
return e.workflowDeleteManager.AddDeleteWorkflowExecutionTask(
2282+
nsID,
2283+
commonpb.WorkflowExecution{
2284+
WorkflowId: request.GetWorkflowExecution().GetWorkflowId(),
2285+
RunId: request.GetWorkflowExecution().GetRunId(),
2286+
},
2287+
wfCtx.getMutableState())
22952288
}
22962289

22972290
// RecordChildExecutionCompleted records the completion of child execution into parent execution history

service/history/nDCHistoryReplicator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func newNDCHistoryReplicator(
176176
logger,
177177
state,
178178
func(mutableState workflow.MutableState) workflow.TaskGenerator {
179-
return workflow.NewTaskGenerator(shard.GetNamespaceRegistry(), logger, mutableState)
179+
return workflow.NewTaskGenerator(shard.GetNamespaceRegistry(), mutableState)
180180
},
181181
)
182182
},

service/history/nDCStateRebuilder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (r *nDCStateRebuilderImpl) initializeBuilders(
213213
r.logger,
214214
resetMutableStateBuilder,
215215
func(mutableState workflow.MutableState) workflow.TaskGenerator {
216-
return workflow.NewTaskGenerator(r.shard.GetNamespaceRegistry(), r.logger, mutableState)
216+
return workflow.NewTaskGenerator(r.shard.GetNamespaceRegistry(), mutableState)
217217
},
218218
)
219219
return resetMutableStateBuilder, stateBuilder

service/history/workflow/delete_manager.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,24 @@ import (
3333
enumspb "go.temporal.io/api/enums/v1"
3434

3535
"go.temporal.io/server/common"
36+
"go.temporal.io/server/common/clock"
3637
"go.temporal.io/server/common/definition"
3738
"go.temporal.io/server/common/metrics"
3839
"go.temporal.io/server/common/namespace"
40+
"go.temporal.io/server/common/persistence"
3941
"go.temporal.io/server/common/searchattribute"
4042
"go.temporal.io/server/service/history/configs"
4143
"go.temporal.io/server/service/history/consts"
4244
"go.temporal.io/server/service/history/shard"
45+
"go.temporal.io/server/service/history/tasks"
4346
"go.temporal.io/server/service/worker/archiver"
4447
)
4548

4649
type (
4750
DeleteManager interface {
48-
DeleteWorkflowExecution(namespaceID namespace.ID, we commonpb.WorkflowExecution, weCtx Context, ms MutableState, sourceTaskVersion int64) error
49-
DeleteWorkflowExecutionByRetention(namespaceID namespace.ID, we commonpb.WorkflowExecution, weCtx Context, ms MutableState, sourceTaskVersion int64) error
51+
AddDeleteWorkflowExecutionTask(nsID namespace.ID, we commonpb.WorkflowExecution, ms MutableState) error
52+
DeleteWorkflowExecution(nsID namespace.ID, we commonpb.WorkflowExecution, weCtx Context, ms MutableState, sourceTaskVersion int64) error
53+
DeleteWorkflowExecutionByRetention(nsID namespace.ID, we commonpb.WorkflowExecution, weCtx Context, ms MutableState, sourceTaskVersion int64) error
5054
}
5155

5256
DeleteManagerImpl struct {
@@ -55,6 +59,7 @@ type (
5559
config *configs.Config
5660
metricsClient metrics.Client
5761
archivalClient archiver.Client
62+
timeSource clock.TimeSource
5863
}
5964
)
6065

@@ -65,20 +70,59 @@ func NewDeleteManager(
6570
cache Cache,
6671
config *configs.Config,
6772
archiverClient archiver.Client,
73+
timeSource clock.TimeSource,
6874
) *DeleteManagerImpl {
6975
deleteManager := &DeleteManagerImpl{
7076
shard: shard,
7177
historyCache: cache,
7278
metricsClient: shard.GetMetricsClient(),
7379
config: config,
7480
archivalClient: archiverClient,
81+
timeSource: timeSource,
7582
}
7683

7784
return deleteManager
7885
}
86+
func (m *DeleteManagerImpl) AddDeleteWorkflowExecutionTask(
87+
nsID namespace.ID,
88+
we commonpb.WorkflowExecution,
89+
ms MutableState,
90+
) error {
91+
92+
if ms.IsWorkflowExecutionRunning() {
93+
// Running workflow cannot be deleted. Close or terminate it first.
94+
return consts.ErrWorkflowNotCompleted
95+
}
96+
97+
taskGenerator := NewTaskGenerator(
98+
m.shard.GetNamespaceRegistry(),
99+
ms,
100+
)
101+
102+
deleteTask, err := taskGenerator.GenerateDeleteExecutionTask(m.timeSource.Now())
103+
if err != nil {
104+
return err
105+
}
106+
107+
err = m.shard.AddTasks(&persistence.AddTasksRequest{
108+
ShardID: m.shard.GetShardID(),
109+
// RangeID is set by shard
110+
NamespaceID: nsID.String(),
111+
WorkflowID: we.GetWorkflowId(),
112+
RunID: we.GetRunId(),
113+
Tasks: map[tasks.Category][]tasks.Task{
114+
tasks.CategoryTransfer: {deleteTask},
115+
},
116+
})
117+
if err != nil {
118+
return err
119+
}
120+
121+
return nil
122+
}
79123

80124
func (m *DeleteManagerImpl) DeleteWorkflowExecution(
81-
namespaceID namespace.ID,
125+
nsID namespace.ID,
82126
we commonpb.WorkflowExecution,
83127
weCtx Context,
84128
ms MutableState,
@@ -94,7 +138,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecution(
94138
}
95139

96140
err := m.deleteWorkflowExecutionInternal(
97-
namespaceID,
141+
nsID,
98142
we,
99143
weCtx,
100144
ms,
@@ -107,7 +151,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecution(
107151
}
108152

109153
func (m *DeleteManagerImpl) DeleteWorkflowExecutionByRetention(
110-
namespaceID namespace.ID,
154+
nsID namespace.ID,
111155
we commonpb.WorkflowExecution,
112156
weCtx Context,
113157
ms MutableState,
@@ -122,7 +166,7 @@ func (m *DeleteManagerImpl) DeleteWorkflowExecutionByRetention(
122166
}
123167

124168
err := m.deleteWorkflowExecutionInternal(
125-
namespaceID,
169+
nsID,
126170
we,
127171
weCtx,
128172
ms,

service/history/workflow/delete_manager_mock.go

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/history/workflow/delete_manager_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
persistencespb "go.temporal.io/server/api/persistence/v1"
4242
"go.temporal.io/server/common"
4343
carchiver "go.temporal.io/server/common/archiver"
44+
"go.temporal.io/server/common/clock"
4445
"go.temporal.io/server/common/definition"
4546
"go.temporal.io/server/common/metrics"
4647
"go.temporal.io/server/common/namespace"
@@ -59,6 +60,7 @@ type (
5960
mockCache *MockCache
6061
mockArchivalClient *archiver.MockClient
6162
mockShardContext *shard.MockContext
63+
mockClock *clock.EventTimeSource
6264

6365
deleteManager DeleteManager
6466
}
@@ -83,6 +85,7 @@ func (s *deleteManagerWorkflowSuite) SetupTest() {
8385
s.controller = gomock.NewController(s.T())
8486
s.mockCache = NewMockCache(s.controller)
8587
s.mockArchivalClient = archiver.NewMockClient(s.controller)
88+
s.mockClock = clock.NewEventTimeSource()
8689

8790
config := tests.NewDynamicConfig()
8891
s.mockShardContext = shard.NewMockContext(s.controller)
@@ -93,6 +96,7 @@ func (s *deleteManagerWorkflowSuite) SetupTest() {
9396
s.mockCache,
9497
config,
9598
s.mockArchivalClient,
99+
s.mockClock,
96100
)
97101
}
98102

service/history/workflow/mutable_state_impl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func NewMutableState(
260260
common.FirstEventID,
261261
s.bufferEventsInDB,
262262
)
263-
s.taskGenerator = NewTaskGenerator(shard.GetNamespaceRegistry(), s.logger, s)
263+
s.taskGenerator = NewTaskGenerator(shard.GetNamespaceRegistry(), s)
264264
s.workflowTaskManager = newWorkflowTaskStateMachine(s)
265265

266266
return s

service/history/workflow/task_generator.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636

3737
enumsspb "go.temporal.io/server/api/enums/v1"
3838
"go.temporal.io/server/common/clock"
39-
"go.temporal.io/server/common/log"
4039
"go.temporal.io/server/common/namespace"
4140
"go.temporal.io/server/common/persistence/versionhistory"
4241
"go.temporal.io/server/common/primitives/timestamp"
@@ -54,7 +53,7 @@ type (
5453
) error
5554
GenerateDeleteExecutionTask(
5655
now time.Time,
57-
) error
56+
) (*tasks.DeleteExecutionTask, error)
5857
GenerateRecordWorkflowStartedTasks(
5958
now time.Time,
6059
startEvent *historypb.HistoryEvent,
@@ -120,9 +119,7 @@ type (
120119

121120
TaskGeneratorImpl struct {
122121
namespaceRegistry namespace.Registry
123-
logger log.Logger
124-
125-
mutableState MutableState
122+
mutableState MutableState
126123
}
127124
)
128125

@@ -132,15 +129,12 @@ var _ TaskGenerator = (*TaskGeneratorImpl)(nil)
132129

133130
func NewTaskGenerator(
134131
namespaceRegistry namespace.Registry,
135-
logger log.Logger,
136132
mutableState MutableState,
137133
) *TaskGeneratorImpl {
138134

139135
mstg := &TaskGeneratorImpl{
140136
namespaceRegistry: namespaceRegistry,
141-
logger: logger,
142-
143-
mutableState: mutableState,
137+
mutableState: mutableState,
144138
}
145139

146140
return mstg
@@ -212,18 +206,16 @@ func (r *TaskGeneratorImpl) GenerateWorkflowCloseTasks(
212206

213207
func (r *TaskGeneratorImpl) GenerateDeleteExecutionTask(
214208
now time.Time,
215-
) error {
209+
) (*tasks.DeleteExecutionTask, error) {
216210

217211
currentVersion := r.mutableState.GetCurrentVersion()
218212

219-
r.mutableState.AddTasks(&tasks.DeleteExecutionTask{
213+
return &tasks.DeleteExecutionTask{
220214
// TaskID is set by shard
221215
WorkflowKey: r.mutableState.GetWorkflowKey(),
222216
VisibilityTimestamp: now,
223217
Version: currentVersion,
224-
})
225-
226-
return nil
218+
}, nil
227219
}
228220

229221
func (r *TaskGeneratorImpl) GenerateDelayedWorkflowTasks(

service/history/workflow/task_generator_mock.go

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/history/workflow/task_refresher.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (r *TaskRefresherImpl) RefreshTasks(
7777

7878
taskGenerator := NewTaskGenerator(
7979
r.namespaceRegistry,
80-
r.logger,
8180
mutableState,
8281
)
8382

0 commit comments

Comments
 (0)