forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
326 lines (289 loc) · 9.26 KB
/
example_test.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// 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 mapq
import (
"context"
"fmt"
"testing"
"time"
"github.com/uber/cadence/common/log/loggerimpl"
"github.com/uber/cadence/common/mapq/types"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
)
func TestExample(t *testing.T) {
persister := &InMemoryPersister{}
logger, err := loggerimpl.NewDevelopment()
if err != nil {
panic(err)
}
scope := metrics.NoopScope(0)
cl, err := New(
logger,
scope,
WithConsumerFactory(&NoOpConsumerFactory{}),
WithPersister(persister),
WithPartitions([]string{"type", "sub-type", "domain"}),
WithPolicies([]types.NodePolicy{
// level 0: default policy for root (splitted by type)
{
Path: "*",
SplitPolicy: &types.SplitPolicy{
PredefinedSplits: []any{"timer", "transfer"},
},
},
// level 1: default policy (splitted by sub-type)
{
Path: "*/.",
SplitPolicy: &types.SplitPolicy{
PredefinedSplits: []any{},
},
},
// level 1: timer node
{
Path: "*/timer",
SplitPolicy: &types.SplitPolicy{
PredefinedSplits: []any{
persistence.TaskTypeDeleteHistoryEvent,
},
},
},
// level 1: transfer node
{
Path: "*/transfer",
SplitPolicy: &types.SplitPolicy{
PredefinedSplits: []any{
persistence.TransferTaskTypeStartChildExecution,
},
},
},
// level 2: nodes per <type, sub-type> pairs
// - default 1000 RPS for per sub-type node
// - split by domain. predefined split for d3 domain
{
Path: "*/./.",
SplitPolicy: &types.SplitPolicy{
PredefinedSplits: []any{"d3"},
},
},
// override for timer delete history event:
// - only allow 50 RPS
// - disable split policy
{
Path: "*/timer/4",
DispatchPolicy: &types.DispatchPolicy{DispatchRPS: 50},
SplitPolicy: &types.SplitPolicy{Disabled: true},
},
// override for start child execution
// - only allow 10 RPS
// - disable split policy
{
Path: "*/transfer/4",
DispatchPolicy: &types.DispatchPolicy{DispatchRPS: 10},
SplitPolicy: &types.SplitPolicy{Disabled: true},
},
// level 3: default policy for all nodes at this level. nodes per <type, sub-type, domain> pairs
// - only allow 100 rps
// - disable split policy
{
Path: "*/././.",
DispatchPolicy: &types.DispatchPolicy{DispatchRPS: 100},
SplitPolicy: &types.SplitPolicy{Disabled: true},
},
// level 3: override policy for catch-all nodes at this level (all domains that don't have a specific node)
// this policy will override the 100 RPS policy defined above to give more RPS to catch-all nodes
{
Path: "*/././*",
DispatchPolicy: &types.DispatchPolicy{DispatchRPS: 1000},
SplitPolicy: &types.SplitPolicy{Disabled: true},
},
}),
)
if err != nil {
panic(err)
}
ctx := context.Background()
if err := cl.Start(ctx); err != nil {
panic(err)
}
defer cl.Stop(ctx)
_, err = cl.Enqueue(context.Background(), []types.Item{
newTimerItem("d1", time.Now(), persistence.TaskTypeDecisionTimeout),
newTimerItem("d1", time.Now(), persistence.TaskTypeActivityTimeout),
newTimerItem("d1", time.Now(), persistence.TaskTypeUserTimer),
newTimerItem("d3", time.Now(), persistence.TaskTypeUserTimer),
newTimerItem("d3", time.Now(), persistence.TaskTypeUserTimer),
})
if err != nil {
panic(err)
}
if len(persister.items) != 5 {
panic(fmt.Errorf("expected 5 items in persister, got %v", len(persister.items)))
}
_, err = cl.Enqueue(context.Background(), []types.Item{
newTransferItem("d2", 1, persistence.TransferTaskTypeDecisionTask),
newTransferItem("d2", 2, persistence.TransferTaskTypeActivityTask),
newTransferItem("d2", 3, persistence.TransferTaskTypeStartChildExecution),
newTransferItem("d2", 4, persistence.TransferTaskTypeStartChildExecution),
newTransferItem("d2", 5, persistence.TransferTaskTypeStartChildExecution),
})
if err != nil {
panic(err)
}
if len(persister.items) != 10 {
panic(fmt.Errorf("expected 10 items in persister, got %v", len(persister.items)))
}
}
var _ types.ConsumerFactory = (*NoOpConsumerFactory)(nil)
type NoOpConsumerFactory struct{}
func (f *NoOpConsumerFactory) New(types.ItemPartitions) (types.Consumer, error) {
return &NoOpConsumer{}, nil
}
func (f *NoOpConsumerFactory) Stop(context.Context) error {
return nil
}
type NoOpConsumer struct{}
func (c *NoOpConsumer) Start(context.Context) error {
return nil
}
func (c *NoOpConsumer) Stop(context.Context) error {
return nil
}
func (c *NoOpConsumer) Process(ctx context.Context, item types.Item) error {
fmt.Printf("processing item: %v\n", item)
return nil
}
type InMemoryPersister struct {
items []types.ItemToPersist
offsets *types.Offsets
}
func (p *InMemoryPersister) Persist(ctx context.Context, items []types.ItemToPersist) error {
fmt.Printf("persisting %v items\n", len(items))
for _, item := range items {
partitionsKV := map[string]any{}
actualKV := map[string]any{}
for _, k := range item.GetPartitionKeys() {
partitionsKV[k] = item.GetPartitionValue(k)
actualKV[k] = item.GetAttribute(k)
}
fmt.Printf("item attributes: %v, partitions: %v\n", actualKV, partitionsKV)
}
p.items = append(p.items, items...)
return nil
}
func (p *InMemoryPersister) GetOffsets(context.Context) (*types.Offsets, error) {
return p.offsets, nil
}
func (p *InMemoryPersister) CommitOffsets(ctx context.Context, offsets *types.Offsets) error {
fmt.Printf("committing offsets: %v\n", offsets)
p.offsets = offsets
return nil
}
// Fetch(ctx context.Context, partitions ItemPartitions, pageInfo PageInfo) ([]Item, error)
func (p *InMemoryPersister) Fetch(ctx context.Context, partitions types.ItemPartitions, pageInfo types.PageInfo) ([]types.Item, error) {
return nil, nil
}
func newTimerItem(domain string, t time.Time, timerType int) types.Item {
switch timerType {
case persistence.TaskTypeDecisionTimeout:
case persistence.TaskTypeActivityTimeout:
case persistence.TaskTypeUserTimer:
case persistence.TaskTypeWorkflowTimeout:
case persistence.TaskTypeDeleteHistoryEvent:
case persistence.TaskTypeActivityRetryTimer:
case persistence.TaskTypeWorkflowBackoffTimer:
default:
panic(fmt.Errorf("unknown timer type: %v", timerType))
}
return &timerItem{
t: t,
timerType: timerType,
domain: domain,
}
}
type timerItem struct {
t time.Time
timerType int
domain string
}
func (t *timerItem) String() string {
return fmt.Sprintf("timerItem{timerType: %v, time: %v}", t.timerType, t.t)
}
func (t *timerItem) Offset() int64 {
return t.t.UnixNano()
}
func (t *timerItem) GetAttribute(key string) any {
switch key {
case "type":
return "timer"
case "sub-type":
return t.timerType
case "domain":
return t.domain
default:
panic(fmt.Errorf("unknown key: %v", key))
}
}
func newTransferItem(domain string, taskID int64, transferType int) types.Item {
switch transferType {
case persistence.TransferTaskTypeActivityTask:
case persistence.TransferTaskTypeDecisionTask:
case persistence.TransferTaskTypeCloseExecution:
case persistence.TransferTaskTypeCancelExecution:
case persistence.TransferTaskTypeSignalExecution:
case persistence.TransferTaskTypeStartChildExecution:
case persistence.TransferTaskTypeRecordWorkflowStarted:
case persistence.TransferTaskTypeResetWorkflow:
case persistence.TransferTaskTypeUpsertWorkflowSearchAttributes:
case persistence.TransferTaskTypeRecordWorkflowClosed:
case persistence.TransferTaskTypeRecordChildExecutionCompleted:
case persistence.TransferTaskTypeApplyParentClosePolicy:
default:
panic(fmt.Errorf("unknown transfer type: %v", transferType))
}
return &transferItem{
domain: domain,
taskID: taskID,
transferType: transferType,
}
}
type transferItem struct {
taskID int64
transferType int
domain string
}
func (t *transferItem) String() string {
return fmt.Sprintf("transferItem{transferType: %v, taskID: %v}", t.transferType, t.taskID)
}
func (t *transferItem) Offset() int64 {
return t.taskID
}
func (t *transferItem) GetAttribute(key string) any {
switch key {
case "type":
return "transfer"
case "sub-type":
return t.transferType
case "domain":
return t.domain
default:
panic(fmt.Errorf("unknown key: %v", key))
}
}