-
Notifications
You must be signed in to change notification settings - Fork 774
/
util.go
156 lines (144 loc) · 4.7 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// The MIT License (MIT)
//
// Copyright (c) 2017-2020 Uber Technologies Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package invariant
import (
"context"
"github.com/uber/cadence/common/cache"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/reconciliation/entity"
)
func checkBeforeFix(
ctx context.Context,
invariant Invariant,
execution interface{},
) (*FixResult, *CheckResult) {
checkResult := invariant.Check(ctx, execution)
if checkResult.CheckResultType == CheckResultTypeHealthy {
return &FixResult{
FixResultType: FixResultTypeSkipped,
InvariantName: invariant.Name(),
CheckResult: checkResult,
Info: "skipped fix because execution was healthy",
}, nil
}
if checkResult.CheckResultType == CheckResultTypeFailed {
return &FixResult{
FixResultType: FixResultTypeFailed,
InvariantName: invariant.Name(),
CheckResult: checkResult,
Info: "failed fix because check failed",
}, nil
}
return nil, &checkResult
}
// Open returns true if workflow state is open false if workflow is closed
func Open(state int) bool {
return state == persistence.WorkflowStateCreated || state == persistence.WorkflowStateRunning
}
// ExecutionOpen returns true if execution state is open false if workflow is closed
func ExecutionOpen(execution interface{}) bool {
return Open(getExecution(execution).State)
}
// getExecution returns base Execution
func getExecution(execution interface{}) *entity.Execution {
switch e := execution.(type) {
case *entity.CurrentExecution:
return &e.Execution
case *entity.ConcreteExecution:
return &e.Execution
default:
panic("unexpected execution type")
}
}
// DeleteExecution deletes concrete execution and
// current execution conditionally on matching runID.
func DeleteExecution(
ctx context.Context,
exec interface{},
pr persistence.Retryer,
dc cache.DomainCache,
) *FixResult {
execution := getExecution(exec)
domainName, errorDomainName := dc.GetDomainName(execution.DomainID)
if errorDomainName != nil {
return &FixResult{
FixResultType: FixResultTypeFailed,
Info: "failed to fetch domainName",
InfoDetails: errorDomainName.Error(),
}
}
if err := pr.DeleteWorkflowExecution(ctx, &persistence.DeleteWorkflowExecutionRequest{
DomainID: execution.DomainID,
WorkflowID: execution.WorkflowID,
RunID: execution.RunID,
DomainName: domainName,
}); err != nil {
return &FixResult{
FixResultType: FixResultTypeFailed,
Info: "failed to delete concrete workflow execution",
InfoDetails: err.Error(),
}
}
if err := pr.DeleteCurrentWorkflowExecution(ctx, &persistence.DeleteCurrentWorkflowExecutionRequest{
DomainID: execution.DomainID,
WorkflowID: execution.WorkflowID,
RunID: execution.RunID,
DomainName: domainName,
}); err != nil {
return &FixResult{
FixResultType: FixResultTypeFailed,
Info: "failed to delete current workflow execution",
InfoDetails: err.Error(),
}
}
return &FixResult{
FixResultType: FixResultTypeFixed,
}
}
func validateCheckContext(
ctx context.Context,
invariantName Name,
) *CheckResult {
if ctxErr := ctx.Err(); ctxErr != nil {
return &CheckResult{
CheckResultType: CheckResultTypeFailed,
InvariantName: invariantName,
Info: "failed to check: context expired or cancelled",
InfoDetails: ctxErr.Error(),
}
}
return nil
}
func validateFixContext(
ctx context.Context,
invariantName Name,
) *FixResult {
if ctxErr := ctx.Err(); ctxErr != nil {
return &FixResult{
FixResultType: FixResultTypeFailed,
InvariantName: invariantName,
Info: "failed to check: context expired or cancelled",
InfoDetails: ctxErr.Error(),
}
}
return nil
}