|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Uber Technologies, Inc. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +package deleteworkflow |
| 26 | + |
| 27 | +import ( |
| 28 | + "context" |
| 29 | + |
| 30 | + commonpb "go.temporal.io/api/common/v1" |
| 31 | + |
| 32 | + "go.temporal.io/server/api/historyservice/v1" |
| 33 | + "go.temporal.io/server/common/definition" |
| 34 | + "go.temporal.io/server/common/namespace" |
| 35 | + "go.temporal.io/server/service/history/api" |
| 36 | + "go.temporal.io/server/service/history/consts" |
| 37 | + "go.temporal.io/server/service/history/shard" |
| 38 | + "go.temporal.io/server/service/history/workflow" |
| 39 | +) |
| 40 | + |
| 41 | +func Invoke( |
| 42 | + ctx context.Context, |
| 43 | + request *historyservice.DeleteWorkflowExecutionRequest, |
| 44 | + shard shard.Context, |
| 45 | + workflowConsistencyChecker api.WorkflowConsistencyChecker, |
| 46 | + workflowDeleteManager workflow.DeleteManager, |
| 47 | +) (_ *historyservice.DeleteWorkflowExecutionResponse, retError error) { |
| 48 | + weCtx, err := workflowConsistencyChecker.GetWorkflowContext( |
| 49 | + ctx, |
| 50 | + nil, |
| 51 | + api.BypassMutableStateConsistencyPredicate, |
| 52 | + definition.NewWorkflowKey( |
| 53 | + request.NamespaceId, |
| 54 | + request.WorkflowExecution.WorkflowId, |
| 55 | + request.WorkflowExecution.RunId, |
| 56 | + ), |
| 57 | + ) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + defer func() { weCtx.GetReleaseFn()(retError) }() |
| 62 | + |
| 63 | + // Open and Close workflow executions are deleted differently. |
| 64 | + // Open workflow execution is deleted by terminating with special flag `deleteAfterTerminate` set to true. |
| 65 | + // This flag will be carried over with CloseExecutionTask and workflow will be deleted as the last step while processing the task. |
| 66 | + // |
| 67 | + // Close workflow execution is deleted using DeleteExecutionTask. |
| 68 | + // |
| 69 | + // DeleteWorkflowExecution is not replicated automatically. Workflow executions must be deleted separately in each cluster. |
| 70 | + // Although running workflows in active cluster are terminated first and the termination event might be replicated. |
| 71 | + // In passive cluster, workflow executions are just deleted in regardless of its state. |
| 72 | + |
| 73 | + if weCtx.GetMutableState().IsWorkflowExecutionRunning() { |
| 74 | + if request.GetClosedWorkflowOnly() { |
| 75 | + // skip delete open workflow |
| 76 | + return &historyservice.DeleteWorkflowExecutionResponse{}, nil |
| 77 | + } |
| 78 | + ns, err := shard.GetNamespaceRegistry().GetNamespaceByID(namespace.ID(request.GetNamespaceId())) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + if ns.ActiveInCluster(shard.GetClusterMetadata().GetCurrentClusterName()) { |
| 83 | + // If workflow execution is running and in active cluster. |
| 84 | + if err := api.UpdateWorkflowWithNew( |
| 85 | + shard, |
| 86 | + ctx, |
| 87 | + weCtx, |
| 88 | + func(workflowContext api.WorkflowContext) (*api.UpdateWorkflowAction, error) { |
| 89 | + mutableState := workflowContext.GetMutableState() |
| 90 | + eventBatchFirstEventID := mutableState.GetNextEventID() |
| 91 | + |
| 92 | + return api.UpdateWorkflowWithoutWorkflowTask, workflow.TerminateWorkflow( |
| 93 | + mutableState, |
| 94 | + eventBatchFirstEventID, |
| 95 | + "Delete workflow execution", |
| 96 | + nil, |
| 97 | + consts.IdentityHistoryService, |
| 98 | + true, |
| 99 | + ) |
| 100 | + }, |
| 101 | + nil, |
| 102 | + ); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + return &historyservice.DeleteWorkflowExecutionResponse{}, nil |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // If workflow execution is closed or in passive cluster. |
| 110 | + if err := workflowDeleteManager.AddDeleteWorkflowExecutionTask( |
| 111 | + ctx, |
| 112 | + namespace.ID(request.GetNamespaceId()), |
| 113 | + commonpb.WorkflowExecution{ |
| 114 | + WorkflowId: request.GetWorkflowExecution().GetWorkflowId(), |
| 115 | + RunId: request.GetWorkflowExecution().GetRunId(), |
| 116 | + }, |
| 117 | + weCtx.GetMutableState(), |
| 118 | + request.GetWorkflowVersion(), |
| 119 | + ); err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + return &historyservice.DeleteWorkflowExecutionResponse{}, nil |
| 123 | +} |
0 commit comments