forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivities.go
454 lines (418 loc) · 15.7 KB
/
activities.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// 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 shardscanner
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"go.uber.org/cadence"
"go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/activity"
c "github.com/uber/cadence/common"
"github.com/uber/cadence/common/log/tag"
"github.com/uber/cadence/common/metrics"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/reconciliation/store"
)
const (
// ActivityScannerEmitMetrics is the activity name for scannerEmitMetricsActivity
ActivityScannerEmitMetrics = "cadence-sys-shardscanner-emit-metrics-activity"
// ActivityScannerConfig is the activity name scannerConfigActivity
ActivityScannerConfig = "cadence-sys-shardscanner-config-activity"
// ActivityFixerConfig is the activity name fixerConfigActivity
ActivityFixerConfig = "cadence-sys-shardscanner-fixer-config-activity"
// ActivityScanShard is the activity name for scanShardActivity
ActivityScanShard = "cadence-sys-shardscanner-scanshard-activity"
// ActivityFixerCorruptedKeys is the activity name for fixerCorruptedKeysActivity
ActivityFixerCorruptedKeys = "cadence-sys-shardscanner-corruptedkeys-activity"
// ActivityFixShard is the activity name for fixShardActivity
ActivityFixShard = "cadence-sys-shardscanner-fixshard-activity"
// ShardCorruptKeysQuery is the query name for the query used to get all completed shards with at least one corruption
ShardCorruptKeysQuery = "shard_corrupt_keys"
)
// scannerConfigActivity will read dynamic config, apply overwrites and return a resolved config.
func scannerConfigActivity(
activityCtx context.Context,
params ScannerConfigActivityParams,
) (ResolvedScannerWorkflowConfig, error) {
ctx, err := GetScannerContext(activityCtx)
if err != nil {
return ResolvedScannerWorkflowConfig{}, err
}
dc := ctx.Config.DynamicParams
result := ResolvedScannerWorkflowConfig{
GenericScannerConfig: GenericScannerConfig{
Enabled: dc.ScannerEnabled(),
Concurrency: dc.Concurrency(),
PageSize: dc.PageSize(),
BlobstoreFlushThreshold: dc.BlobstoreFlushThreshold(),
ActivityBatchSize: dc.ActivityBatchSize(),
},
}
if ctx.Hooks != nil && ctx.Hooks.GetScannerConfig != nil {
result.CustomScannerConfig = ctx.Hooks.GetScannerConfig(ctx)
}
overwrites := params.Overwrites.GenericScannerConfig
if overwrites.Enabled != nil {
result.GenericScannerConfig.Enabled = *overwrites.Enabled
}
if overwrites.Concurrency != nil {
result.GenericScannerConfig.Concurrency = *overwrites.Concurrency
}
if overwrites.PageSize != nil {
result.GenericScannerConfig.PageSize = *overwrites.PageSize
}
if overwrites.BlobstoreFlushThreshold != nil {
result.GenericScannerConfig.BlobstoreFlushThreshold = *overwrites.BlobstoreFlushThreshold
}
if overwrites.ActivityBatchSize != nil {
result.GenericScannerConfig.ActivityBatchSize = *overwrites.ActivityBatchSize
}
if params.Overwrites.CustomScannerConfig != nil {
result.CustomScannerConfig = *params.Overwrites.CustomScannerConfig
}
return result, nil
}
// scanShardActivity will scan a collection of shards for invariant violations.
func scanShardActivity(
activityCtx context.Context,
params ScanShardActivityParams,
) ([]ScanReport, error) {
heartbeatDetails := ScanShardHeartbeatDetails{
LastShardIndexHandled: -1,
Reports: nil,
}
ctx, err := GetScannerContext(activityCtx)
if err != nil {
return nil, err
}
if activity.HasHeartbeatDetails(activityCtx) {
if err := activity.GetHeartbeatDetails(activityCtx, &heartbeatDetails); err != nil {
ctx.Logger.Error("getting heartbeat details", tag.Error(err))
return nil, err
}
}
for i := heartbeatDetails.LastShardIndexHandled + 1; i < len(params.Shards); i++ {
currentShardID := params.Shards[i]
shardReport, err := scanShard(activityCtx, params, currentShardID, heartbeatDetails)
if err != nil {
ctx.Logger.Error("scanning shard", tag.Error(err))
return nil, err
}
heartbeatDetails = ScanShardHeartbeatDetails{
LastShardIndexHandled: i,
Reports: append(heartbeatDetails.Reports, *shardReport),
}
}
return heartbeatDetails.Reports, nil
}
func scanShard(
activityCtx context.Context,
params ScanShardActivityParams,
shardID int,
heartbeatDetails ScanShardHeartbeatDetails,
) (*ScanReport, error) {
ctx, err := GetScannerContext(activityCtx)
if err != nil {
return nil, err
}
info := activity.GetInfo(activityCtx)
scope := ctx.Scope.Tagged(
metrics.ActivityTypeTag(ActivityScanShard),
metrics.WorkflowTypeTag(info.WorkflowType.Name),
metrics.DomainTag(c.SystemLocalDomainName),
)
sw := scope.StartTimer(metrics.CadenceLatency)
defer sw.Stop()
if ctx.Hooks == nil {
return nil, cadence.NewCustomError(ErrMissingHooks)
}
resources := ctx.Resource
execManager, err := resources.GetExecutionManager(shardID)
if err != nil {
scope.IncCounter(metrics.CadenceFailures)
return nil, err
}
pr := persistence.NewPersistenceRetryer(execManager, resources.GetHistoryManager(), c.CreatePersistenceRetryPolicy())
scanner := NewScanner(
shardID,
ctx.Hooks.Iterator(activityCtx, pr, params),
resources.GetBlobstoreClient(),
params.BlobstoreFlushThreshold,
ctx.Hooks.Manager(activityCtx, pr, params, resources.GetDomainCache()),
func() { activity.RecordHeartbeat(activityCtx, heartbeatDetails) },
scope,
resources.GetDomainCache(),
)
report := scanner.Scan(activityCtx)
if report.Result.ControlFlowFailure != nil {
scope.IncCounter(metrics.CadenceFailures)
}
return &report, nil
}
// fixerCorruptedKeysActivity will fetch the keys of blobs from shards with corruptions from a completed scan workflow.
// If scan workflow is not closed or if query fails activity will return an error.
// Accepts as input the shard to start query at and returns a next page token, therefore this activity can
// be used to do pagination.
func fixerCorruptedKeysActivity(
activityCtx context.Context,
params FixerCorruptedKeysActivityParams,
) (*FixerCorruptedKeysActivityResult, error) {
ctx, err := GetFixerContext(activityCtx)
if err != nil {
return nil, err
}
client := ctx.Resource.GetSDKClient()
if params.ScannerWorkflowRunID == "" {
listResp, err := client.ListClosedWorkflowExecutions(activityCtx, &shared.ListClosedWorkflowExecutionsRequest{
Domain: c.StringPtr(c.SystemLocalDomainName),
MaximumPageSize: c.Int32Ptr(10),
NextPageToken: nil,
StartTimeFilter: &shared.StartTimeFilter{
EarliestTime: c.Int64Ptr(0),
LatestTime: c.Int64Ptr(time.Now().UnixNano()),
},
ExecutionFilter: &shared.WorkflowExecutionFilter{
WorkflowId: c.StringPtr(params.ScannerWorkflowWorkflowID),
},
})
if err != nil {
return nil, err
}
if len(listResp.Executions) > 10 {
return nil, errors.New("got unexpected number of executions back from list")
}
// ListClosedWorkflowExecutions API doesn't support querying by workflow ID and status filter at the same time,
// and we want to avoid using a scan result with Terminated state.
for _, executionInfo := range listResp.Executions {
if *executionInfo.CloseStatus == shared.WorkflowExecutionCloseStatusContinuedAsNew {
params.ScannerWorkflowRunID = *executionInfo.Execution.RunId
break
}
}
if len(params.ScannerWorkflowRunID) == 0 {
return nil, errors.New("failed to find a recent scanner workflow execution with ContinuedAsNew status")
}
}
descResp, err := client.DescribeWorkflowExecution(activityCtx, &shared.DescribeWorkflowExecutionRequest{
Domain: c.StringPtr(c.SystemLocalDomainName),
Execution: &shared.WorkflowExecution{
WorkflowId: c.StringPtr(params.ScannerWorkflowWorkflowID),
RunId: c.StringPtr(params.ScannerWorkflowRunID),
},
})
if err != nil {
return nil, err
}
if descResp.WorkflowExecutionInfo.CloseStatus == nil {
return nil, cadence.NewCustomError(ErrScanWorkflowNotClosed)
}
queryArgs := PaginatedShardQueryRequest{
StartingShardID: params.StartingShardID,
}
queryArgsBytes, err := json.Marshal(queryArgs)
if err != nil {
return nil, cadence.NewCustomError(ErrSerialization)
}
queryResp, err := client.QueryWorkflow(activityCtx, &shared.QueryWorkflowRequest{
Domain: c.StringPtr(c.SystemLocalDomainName),
Execution: &shared.WorkflowExecution{
WorkflowId: c.StringPtr(params.ScannerWorkflowWorkflowID),
RunId: c.StringPtr(params.ScannerWorkflowRunID),
},
Query: &shared.WorkflowQuery{
QueryType: c.StringPtr(ShardCorruptKeysQuery),
QueryArgs: queryArgsBytes,
},
})
if err != nil {
return nil, err
}
queryResult := &ShardCorruptKeysQueryResult{}
if err := json.Unmarshal(queryResp.QueryResult, &queryResult); err != nil {
return nil, cadence.NewCustomError(ErrSerialization)
}
var corrupted []CorruptedKeysEntry
var minShardID *int
var maxShardID *int
for sid, keys := range queryResult.Result {
if minShardID == nil || *minShardID > sid {
minShardID = c.IntPtr(sid)
}
if maxShardID == nil || *maxShardID < sid {
maxShardID = c.IntPtr(sid)
}
corrupted = append(corrupted, CorruptedKeysEntry{
ShardID: sid,
CorruptedKeys: keys,
})
}
return &FixerCorruptedKeysActivityResult{
CorruptedKeys: corrupted,
MinShard: minShardID,
MaxShard: maxShardID,
ShardQueryPaginationToken: queryResult.ShardQueryPaginationToken,
}, nil
}
type (
FixShardConfigParams struct {
// intentionally empty, no args needed currently. just reserving arg space for future needs.
}
FixShardConfigResults struct {
EnabledInvariants CustomScannerConfig
}
)
// fixerConfigActivity returns a list of all enabled invariants for this fixer.
// The type of the workflow determines the type of the fixer (concrete, current, etc).
//
// It essentially mirrors scannerConfigActivity, but does not try to merge into a common structure.
func fixerConfigActivity(activityCtx context.Context, params FixShardConfigParams) (*FixShardConfigResults, error) {
ctx, err := GetFixerContext(activityCtx)
if err != nil {
return nil, err
}
cfg := ctx.Hooks.GetFixerConfig(ctx)
if len(cfg) == 0 {
// sanity check for new code. historically this field did not exist, now it is required to be populated.
return nil, fmt.Errorf(`invalid empty fixer config, you must explicitly specify "true" or "false" for all relevant invariants`)
}
return &FixShardConfigResults{
EnabledInvariants: cfg,
}, nil
}
// fixShardActivity will fix a collection of shards.
func fixShardActivity(
activityCtx context.Context,
params FixShardActivityParams,
) ([]FixReport, error) {
ctx, err := GetFixerContext(activityCtx)
if err != nil {
return nil, err
}
heartbeatDetails := FixShardHeartbeatDetails{
LastShardIndexHandled: -1,
Reports: nil,
}
if activity.HasHeartbeatDetails(activityCtx) {
if err := activity.GetHeartbeatDetails(activityCtx, &heartbeatDetails); err != nil {
ctx.Logger.Error("getting heartbeat details", tag.Error(err))
return nil, err
}
}
for i := heartbeatDetails.LastShardIndexHandled + 1; i < len(params.CorruptedKeysEntries); i++ {
currentShardID := params.CorruptedKeysEntries[i].ShardID
currentKeys := params.CorruptedKeysEntries[i].CorruptedKeys
shardReport, err := fixShard(activityCtx, params, currentShardID, currentKeys, heartbeatDetails)
if err != nil {
ctx.Logger.Error("fixing shard", tag.Error(err))
return nil, err
}
heartbeatDetails = FixShardHeartbeatDetails{
LastShardIndexHandled: i,
Reports: append(heartbeatDetails.Reports, *shardReport),
}
}
return heartbeatDetails.Reports, nil
}
func fixShard(
activityCtx context.Context,
params FixShardActivityParams,
shardID int,
corruptedKeys store.Keys,
heartbeatDetails FixShardHeartbeatDetails,
) (*FixReport, error) {
ctx, err := GetFixerContext(activityCtx)
if err != nil {
return nil, err
}
resource := ctx.Resource
info := activity.GetInfo(activityCtx)
scope := ctx.Scope.Tagged(
metrics.ActivityTypeTag(ActivityFixShard),
metrics.WorkflowTypeTag(info.WorkflowType.Name),
metrics.DomainTag(c.SystemLocalDomainName),
)
sw := scope.StartTimer(metrics.CadenceLatency)
defer sw.Stop()
if ctx.Hooks == nil {
return nil, cadence.NewCustomError(ErrMissingHooks)
}
execManager, err := resource.GetExecutionManager(shardID)
if err != nil {
scope.IncCounter(metrics.CadenceFailures)
return nil, err
}
pr := persistence.NewPersistenceRetryer(execManager, resource.GetHistoryManager(), c.CreatePersistenceRetryPolicy())
fixer := NewFixer(
activityCtx,
shardID,
ctx.Hooks.InvariantManager(activityCtx, pr, params, resource.GetDomainCache()),
ctx.Hooks.Iterator(activityCtx, resource.GetBlobstoreClient(), corruptedKeys, params),
resource.GetBlobstoreClient(),
params.ResolvedFixerWorkflowConfig.BlobstoreFlushThreshold,
func() { activity.RecordHeartbeat(activityCtx, heartbeatDetails) },
resource.GetDomainCache(),
ctx.Config.DynamicParams.AllowDomain,
scope,
)
report := fixer.Fix()
if report.Result.ControlFlowFailure != nil {
scope.IncCounter(metrics.CadenceFailures)
}
return &report, nil
}
// scannerEmitMetricsActivity will emit metrics for a complete run of ShardScanner
func scannerEmitMetricsActivity(
activityCtx context.Context,
params ScannerEmitMetricsActivityParams,
) error {
ctx, err := GetScannerContext(activityCtx)
if err != nil {
return err
}
info := activity.GetInfo(activityCtx)
scope := ctx.Scope.Tagged(
metrics.ActivityTypeTag(ActivityScannerEmitMetrics),
metrics.WorkflowTypeTag(info.WorkflowType.Name),
metrics.DomainTag(c.SystemLocalDomainName),
)
scope.UpdateGauge(metrics.CadenceShardSuccessGauge, float64(params.ShardSuccessCount))
scope.UpdateGauge(metrics.CadenceShardFailureGauge, float64(params.ShardControlFlowFailureCount))
agg := params.AggregateReportResult
scope.UpdateGauge(metrics.ScannerExecutionsGauge, float64(agg.EntitiesCount))
scope.UpdateGauge(metrics.ScannerCorruptedGauge, float64(agg.CorruptedCount))
scope.UpdateGauge(metrics.ScannerCheckFailedGauge, float64(agg.CheckFailedCount))
for k, v := range agg.CorruptionByType {
scope.Tagged(metrics.InvariantTypeTag(string(k))).UpdateGauge(metrics.ScannerCorruptionByTypeGauge, float64(v))
}
shardStats := params.ShardDistributionStats
scope.UpdateGauge(metrics.ScannerShardSizeMaxGauge, float64(shardStats.Max))
scope.UpdateGauge(metrics.ScannerShardSizeMedianGauge, float64(shardStats.Median))
scope.UpdateGauge(metrics.ScannerShardSizeMinGauge, float64(shardStats.Min))
scope.UpdateGauge(metrics.ScannerShardSizeNinetyGauge, float64(shardStats.P90))
scope.UpdateGauge(metrics.ScannerShardSizeSeventyFiveGauge, float64(shardStats.P75))
scope.UpdateGauge(metrics.ScannerShardSizeTwentyFiveGauge, float64(shardStats.P25))
scope.UpdateGauge(metrics.ScannerShardSizeTenGauge, float64(shardStats.P10))
return nil
}