/
adminTimers.go
314 lines (271 loc) · 7.77 KB
/
adminTimers.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
// The MIT License (MIT)
//
// Copyright (c) 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 cli
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"github.com/urfave/cli"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/backoff"
"github.com/uber/cadence/common/persistence"
)
// LoadCloser loads timer task information
type LoadCloser interface {
Load() []*persistence.TimerTaskInfo
Close()
}
// Printer prints timer task information
type Printer interface {
Print(timers []*persistence.TimerTaskInfo) error
}
// Reporter wraps LoadCloser, Printer and a filter on time task type and domainID
type Reporter struct {
domainID string
timerTypes []int
loader LoadCloser
printer Printer
}
type dbLoadCloser struct {
ctx *cli.Context
executionManager persistence.ExecutionManager
}
type fileLoadCloser struct {
file *os.File
}
type histogramPrinter struct {
ctx *cli.Context
timeFormat string
}
type jsonPrinter struct {
ctx *cli.Context
}
// NewDBLoadCloser creates a new LoadCloser to load timer task information from database
func NewDBLoadCloser(c *cli.Context) LoadCloser {
shardID := getRequiredIntOption(c, FlagShardID)
executionManager := initializeExecutionStore(c, shardID)
return &dbLoadCloser{
ctx: c,
executionManager: executionManager,
}
}
// NewFileLoadCloser creates a new LoadCloser to load timer task information from file
func NewFileLoadCloser(c *cli.Context) LoadCloser {
file, err := os.Open(c.String(FlagInputFile))
if err != nil {
ErrorAndExit("cannot open file", err)
}
return &fileLoadCloser{
file: file,
}
}
// NewReporter creates a new Reporter
func NewReporter(domain string, timerTypes []int, loader LoadCloser, printer Printer) *Reporter {
return &Reporter{
timerTypes: timerTypes,
domainID: domain,
loader: loader,
printer: printer,
}
}
// NewHistogramPrinter creates a new Printer to display timer task information in a histogram
func NewHistogramPrinter(c *cli.Context, timeFormat string) Printer {
return &histogramPrinter{
ctx: c,
timeFormat: timeFormat,
}
}
// NewJSONPrinter creates a new Printer to display timer task information in a JSON format
func NewJSONPrinter(c *cli.Context) Printer {
return &jsonPrinter{
ctx: c,
}
}
func (r *Reporter) filter(timers []*persistence.TimerTaskInfo) []*persistence.TimerTaskInfo {
taskTypes := intSliceToSet(r.timerTypes)
for i, t := range timers {
if len(r.domainID) > 0 && t.DomainID != r.domainID {
timers[i] = nil
continue
}
if _, ok := taskTypes[t.TaskType]; !ok {
timers[i] = nil
continue
}
}
return timers
}
// Report loads, filters and prints timer tasks
func (r *Reporter) Report() error {
return r.printer.Print(r.filter(r.loader.Load()))
}
// AdminTimers is used to list scheduled timers.
func AdminTimers(c *cli.Context) {
timerTypes := c.IntSlice(FlagTimerType)
if !c.IsSet(FlagTimerType) || (len(timerTypes) == 1 && timerTypes[0] == -1) {
timerTypes = []int{
persistence.TaskTypeDecisionTimeout,
persistence.TaskTypeActivityTimeout,
persistence.TaskTypeUserTimer,
persistence.TaskTypeWorkflowTimeout,
persistence.TaskTypeDeleteHistoryEvent,
persistence.TaskTypeActivityRetryTimer,
persistence.TaskTypeWorkflowBackoffTimer,
}
}
// setup loader
var loader LoadCloser
if !c.IsSet(FlagInputFile) {
loader = NewDBLoadCloser(c)
} else {
loader = NewFileLoadCloser(c)
}
defer loader.Close()
// setup printer
var printer Printer
if !c.Bool(FlagPrintJSON) {
var timerFormat string
if c.IsSet(FlagDateFormat) {
timerFormat = c.String(FlagDateFormat)
} else {
switch c.String(FlagBucketSize) {
case "day":
timerFormat = "2006-01-02"
case "hour":
timerFormat = "2006-01-02T15"
case "minute":
timerFormat = "2006-01-02T15:04"
case "second":
timerFormat = "2006-01-02T15:04:05"
default:
ErrorAndExit("unknown bucket size: "+c.String(FlagBucketSize), nil)
}
}
printer = NewHistogramPrinter(c, timerFormat)
} else {
printer = NewJSONPrinter(c)
}
reporter := NewReporter(c.String(FlagDomainID), timerTypes, loader, printer)
if err := reporter.Report(); err != nil {
ErrorAndExit("Reporter failed", err)
}
}
func (jp *jsonPrinter) Print(timers []*persistence.TimerTaskInfo) error {
for _, t := range timers {
if t == nil {
continue
}
data, err := json.Marshal(t)
if err != nil {
if !jp.ctx.Bool(FlagSkipErrorMode) {
ErrorAndExit("cannot marshal timer to json", err)
}
fmt.Println(err.Error())
} else {
fmt.Println(string(data))
}
}
return nil
}
func (cl *dbLoadCloser) Load() []*persistence.TimerTaskInfo {
batchSize := cl.ctx.Int(FlagBatchSize)
startDate := cl.ctx.String(FlagStartDate)
endDate := cl.ctx.String(FlagEndDate)
st, err := parseSingleTs(startDate)
if err != nil {
ErrorAndExit("wrong date format for "+FlagStartDate, err)
}
et, err := parseSingleTs(endDate)
if err != nil {
ErrorAndExit("wrong date format for "+FlagEndDate, err)
}
var timers []*persistence.TimerTaskInfo
isRetryable := func(err error) bool {
return persistence.IsTransientError(err) || common.IsContextTimeoutError(err)
}
throttleRetry := backoff.NewThrottleRetry(
backoff.WithRetryPolicy(common.CreatePersistenceRetryPolicy()),
backoff.WithRetryableError(isRetryable),
)
var token []byte
isFirstIteration := true
for isFirstIteration || len(token) != 0 {
isFirstIteration = false
req := persistence.GetTimerIndexTasksRequest{
MinTimestamp: st,
MaxTimestamp: et,
BatchSize: batchSize,
NextPageToken: token,
}
resp := &persistence.GetTimerIndexTasksResponse{}
op := func() error {
ctx, cancel := newContext(cl.ctx)
defer cancel()
var err error
resp, err = cl.executionManager.GetTimerIndexTasks(ctx, &req)
return err
}
err = throttleRetry.Do(context.Background(), op)
if err != nil {
ErrorAndExit("cannot get timer tasks for shard", err)
}
token = resp.NextPageToken
timers = append(timers, resp.Timers...)
}
return timers
}
func (cl *dbLoadCloser) Close() {
if cl.executionManager != nil {
cl.executionManager.Close()
}
}
func (fl *fileLoadCloser) Load() []*persistence.TimerTaskInfo {
var data []*persistence.TimerTaskInfo
dec := json.NewDecoder(fl.file)
for {
var timer persistence.TimerTaskInfo
if err := dec.Decode(&timer); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
}
data = append(data, &timer)
}
return data
}
func (fl *fileLoadCloser) Close() {
if fl.file != nil {
fl.file.Close()
}
}
func (hp *histogramPrinter) Print(timers []*persistence.TimerTaskInfo) error {
h := NewHistogram()
for _, t := range timers {
if t == nil {
continue
}
h.Add(t.VisibilityTimestamp.Format(hp.timeFormat))
}
return h.Print(hp.ctx.Int(FlagShardMultiplier))
}