forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomainArchivalConfigStateMachine.go
241 lines (219 loc) · 7.32 KB
/
domainArchivalConfigStateMachine.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) 2017 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 frontend
import (
"github.com/uber/cadence/.gen/go/shared"
)
// domainArchivalConfigStateMachine is only used by domainHandler.
// It is simply meant to simplify the logic around archival domain state changes.
// Logically this class can be thought of as part of domainHandler.
type (
// archivalState represents the state of archival config
// the only invalid state is {URI="", status=enabled}
// once URI is set it is immutable
archivalState struct {
status shared.ArchivalStatus
URI string
}
// archivalEvent represents a change request to archival config state
// the only restriction placed on events is that defaultURI is not empty
// status can be nil, enabled, or disabled (nil indicates no update by user is being attempted)
archivalEvent struct {
defaultURI string
URI string
status *shared.ArchivalStatus
}
)
// the following errors represents impossible code states that should never occur
var (
errInvalidState = &shared.BadRequestError{Message: "Encountered illegal state: archival is enabled but URI is not set (should be impossible)"}
errInvalidEvent = &shared.BadRequestError{Message: "Encountered illegal event: default URI is not set (should be impossible)"}
errCannotHandleStateChange = &shared.BadRequestError{Message: "Encountered current state and event that cannot be handled (should be impossible)"}
)
// the following errors represents bad user input
var (
errURIUpdate = &shared.BadRequestError{Message: "Cannot update existing archival URI"}
)
func neverEnabledState() *archivalState {
return &archivalState{
URI: "",
status: shared.ArchivalStatusDisabled,
}
}
func (e *archivalEvent) validate() error {
if len(e.defaultURI) == 0 {
return errInvalidEvent
}
return nil
}
func (s *archivalState) validate() error {
if s.status == shared.ArchivalStatusEnabled && len(s.URI) == 0 {
return errInvalidState
}
return nil
}
func (s *archivalState) getNextState(
e *archivalEvent,
URIValidationFunc func(URI string) error,
) (nextState *archivalState, changed bool, err error) {
defer func() {
// ensure that any existing URI name was not mutated
if nextState != nil && len(s.URI) != 0 && s.URI != nextState.URI {
nextState = nil
changed = false
err = errCannotHandleStateChange
return
}
// ensure that next state is valid
if nextState != nil {
if nextStateErr := nextState.validate(); nextStateErr != nil {
nextState = nil
changed = false
err = nextStateErr
return
}
}
if nextState != nil && nextState.URI != "" {
if validateURIErr := URIValidationFunc(nextState.URI); validateURIErr != nil {
nextState = nil
changed = false
err = validateURIErr
return
}
}
}()
if s == nil || e == nil {
return nil, false, errCannotHandleStateChange
}
if err := s.validate(); err != nil {
return nil, false, err
}
if err := e.validate(); err != nil {
return nil, false, err
}
/**
At this point state and event are both non-nil and valid.
State can be any one of the following:
{status=enabled, URI="foo"}
{status=disabled, URI="foo"}
{status=disabled, URI=""}
Event can be any one of the following:
{status=enabled, URI="foo", defaultURI="bar"}
{status=enabled, URI="", defaultURI="bar"}
{status=disabled, URI="foo", defaultURI="bar"}
{status=disabled, URI="", defaultURI="bar"}
{status=nil, URI="foo", defaultURI="bar"}
{status=nil, URI="", defaultURI="bar"}
*/
stateURISet := len(s.URI) != 0
eventURISet := len(e.URI) != 0
// factor this case out to ensure that URI is immutable
if stateURISet && eventURISet && s.URI != e.URI {
return nil, false, errURIUpdate
}
// state 1
if s.status == shared.ArchivalStatusEnabled && stateURISet {
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && eventURISet {
return s, false, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && !eventURISet {
return s, false, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && eventURISet {
return &archivalState{
status: shared.ArchivalStatusDisabled,
URI: s.URI,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && !eventURISet {
return &archivalState{
status: shared.ArchivalStatusDisabled,
URI: s.URI,
}, true, nil
}
if e.status == nil && eventURISet {
return s, false, nil
}
if e.status == nil && !eventURISet {
return s, false, nil
}
}
// state 2
if s.status == shared.ArchivalStatusDisabled && stateURISet {
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && eventURISet {
return &archivalState{
URI: s.URI,
status: shared.ArchivalStatusEnabled,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && !eventURISet {
return &archivalState{
status: shared.ArchivalStatusEnabled,
URI: s.URI,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && eventURISet {
return s, false, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && !eventURISet {
return s, false, nil
}
if e.status == nil && eventURISet {
return s, false, nil
}
if e.status == nil && !eventURISet {
return s, false, nil
}
}
// state 3
if s.status == shared.ArchivalStatusDisabled && !stateURISet {
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && eventURISet {
return &archivalState{
status: shared.ArchivalStatusEnabled,
URI: e.URI,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusEnabled && !eventURISet {
return &archivalState{
status: shared.ArchivalStatusEnabled,
URI: e.defaultURI,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && eventURISet {
return &archivalState{
status: shared.ArchivalStatusDisabled,
URI: e.URI,
}, true, nil
}
if e.status != nil && *e.status == shared.ArchivalStatusDisabled && !eventURISet {
return s, false, nil
}
if e.status == nil && eventURISet {
return &archivalState{
status: shared.ArchivalStatusDisabled,
URI: e.URI,
}, true, nil
}
if e.status == nil && !eventURISet {
return s, false, nil
}
}
return nil, false, errCannotHandleStateChange
}