-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathebb_and_flow.go
More file actions
332 lines (288 loc) · 11.2 KB
/
ebb_and_flow.go
File metadata and controls
332 lines (288 loc) · 11.2 KB
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
package scenarios
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/temporalio/omes/loadgen"
"github.com/temporalio/omes/loadgen/ebbandflow"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/sdk/temporal"
)
const (
// MinBacklogFlag defines the minimum backlog to target.
// Note that it controls total backlog size, not counting partitions or priority levels.
MinBacklogFlag = "min-backlog"
// MaxBacklogFlag defines the maximum backlog to target.
// Note that it controls total backlog size, not counting partitions or priority levels.
MaxBacklogFlag = "max-backlog"
// PeriodFlag defines the period of oscilation of backlog size.
PeriodFlag = "period"
// SleepDurationFlag defines the duration an activity sleeps for (default 1ms).
// This can be used to slow down activity processing, but
// --worker-worker-activity-per-second may be more intuitive.
SleepDurationFlag = "sleep-duration"
// MaxRateFlag defines the maximum number of workflows to spawn per control interval.
MaxRateFlag = "max-rate"
// ControlIntervalFlag defines how often the backlog is controlled.
ControlIntervalFlag = "control-interval"
// MaxConsecutiveErrorsFlag defines how many consecutive errors are tolerated before stopping the scenario.
MaxConsecutiveErrorsFlag = "max-consecutive-errors"
// BacklogLogIntervalFlag defines how often the current backlog stats are logged.
BacklogLogIntervalFlag = "backlog-log-interval"
)
type ebbAndFlowConfig struct {
MinBacklog int64
MaxBacklog int64
Period time.Duration
SleepDuration time.Duration
MaxRate int64
ControlInterval time.Duration
MaxConsecutiveErrors int
BacklogLogInterval time.Duration
VisibilityVerificationTimeout time.Duration
SleepActivityConfig *loadgen.SleepActivityConfig
}
type ebbAndFlowState struct {
// TotalCompletedWorkflows tracks the total number of completed workflows across
// all restarts. It is used to verify workflow counts after the scenario completes.
TotalCompletedWorkflows int64 `json:"totalCompletedWorkflows"`
}
type ebbAndFlowExecutor struct {
loadgen.ScenarioInfo
config *ebbAndFlowConfig
rng *rand.Rand
id string
isResuming bool
startTime time.Time
scheduledActivities atomic.Int64
completedActivities atomic.Int64
stateLock sync.Mutex
state *ebbAndFlowState
}
var _ loadgen.Configurable = (*ebbAndFlowExecutor)(nil)
var _ loadgen.Resumable = (*ebbAndFlowExecutor)(nil)
func init() {
loadgen.MustRegisterScenario(loadgen.Scenario{
Description: "Oscillates backlog between min and max.\n" +
"Options:\n" +
" min-backlog, max-backlog, period, sleep-duration, max-rate,\n" +
" control-interval, max-consecutive-errors, backlog-log-interval.\n" +
"Duration must be set.",
ExecutorFn: func() loadgen.Executor { return newEbbAndFlowExecutor() },
})
}
func newEbbAndFlowExecutor() *ebbAndFlowExecutor {
return &ebbAndFlowExecutor{state: &ebbAndFlowState{}}
}
func (e *ebbAndFlowExecutor) Configure(info loadgen.ScenarioInfo) error {
config := &ebbAndFlowConfig{
SleepDuration: info.ScenarioOptionDuration(SleepDurationFlag, 1*time.Millisecond),
MaxRate: int64(info.ScenarioOptionInt(MaxRateFlag, 1000)),
ControlInterval: info.ScenarioOptionDuration(ControlIntervalFlag, 100*time.Millisecond),
MaxConsecutiveErrors: info.ScenarioOptionInt(MaxConsecutiveErrorsFlag, 10),
BacklogLogInterval: info.ScenarioOptionDuration(BacklogLogIntervalFlag, 30*time.Second),
VisibilityVerificationTimeout: info.ScenarioOptionDuration(VisibilityVerificationTimeoutFlag, 30*time.Second),
}
config.MinBacklog = int64(info.ScenarioOptionInt(MinBacklogFlag, 0))
if config.MinBacklog < 0 {
return fmt.Errorf("min-backlog must be non-negative, got %d", config.MinBacklog)
}
config.MaxBacklog = int64(info.ScenarioOptionInt(MaxBacklogFlag, 30))
if config.MaxBacklog <= config.MinBacklog {
return fmt.Errorf("max-backlog must be greater than min-backlog, got max=%d min=%d", config.MaxBacklog, config.MinBacklog)
}
// TODO: backwards-compatibility, remove later
pt := info.ScenarioOptionDuration("phase-time", 60*time.Second)
config.Period = info.ScenarioOptionDuration(PeriodFlag, pt)
if config.Period <= 0 {
return fmt.Errorf("period must be greater than 0, got %v", config.Period)
}
if sleepActivitiesStr, ok := info.ScenarioOptions[SleepActivityJsonFlag]; ok {
var err error
// This scenario overrides "count" and "sleepDuration" so do not require them.
config.SleepActivityConfig, err = loadgen.ParseAndValidateSleepActivityConfig(sleepActivitiesStr, false, false)
if err != nil {
return fmt.Errorf("invalid %s: %w", SleepActivityJsonFlag, err)
}
}
if config.SleepActivityConfig == nil {
config.SleepActivityConfig = &loadgen.SleepActivityConfig{}
}
if len(config.SleepActivityConfig.Groups) == 0 {
config.SleepActivityConfig.Groups = map[string]loadgen.SleepActivityGroupConfig{"default": {}}
}
for name, group := range config.SleepActivityConfig.Groups {
fixedDist := loadgen.NewFixedDistribution(config.SleepDuration)
group.SleepDuration = &fixedDist
config.SleepActivityConfig.Groups[name] = group
}
e.config = config
return nil
}
// Run executes the ebb and flow scenario.
func (e *ebbAndFlowExecutor) Run(ctx context.Context, info loadgen.ScenarioInfo) error {
if err := e.Configure(info); err != nil {
return fmt.Errorf("failed to parse scenario configuration: %w", err)
}
e.ScenarioInfo = info
e.id = fmt.Sprintf("ebb_and_flow_%s", e.ExecutionID)
e.rng = rand.New(rand.NewSource(time.Now().UnixNano()))
e.startTime = time.Now()
// Get parsed configuration
config := e.config
if config == nil {
return fmt.Errorf("configuration not parsed - Parse must be called before run")
}
var consecutiveErrCount int
errCh := make(chan error, 10000)
ticker := time.NewTicker(config.ControlInterval)
defer ticker.Stop()
// Setup configurable backlog logging
backlogTicker := time.NewTicker(config.BacklogLogInterval)
defer backlogTicker.Stop()
var startWG sync.WaitGroup
var iter int64 = 1
e.Logger.Infof("Starting ebb and flow scenario: min_backlog=%d, max_backlog=%d, period=%v, duration=%v",
config.MinBacklog, config.MaxBacklog, config.Period, e.Configuration.Duration)
var started, completed, backlog, target, activities int64
for elapsed := time.Duration(0); elapsed < e.Configuration.Duration; elapsed = time.Since(e.startTime) {
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errCh:
if err != nil {
e.Logger.Errorf("Failed to spawn workflow: %v", err)
consecutiveErrCount++
if consecutiveErrCount >= config.MaxConsecutiveErrors {
return fmt.Errorf("got %v consecutive errors, most recent: %w", config.MaxConsecutiveErrors, err)
}
} else {
consecutiveErrCount = 0
}
case <-ticker.C:
started = e.scheduledActivities.Load()
completed = e.completedActivities.Load()
backlog = started - completed
target = calculateBacklogTarget(elapsed, config.Period, config.MinBacklog, config.MaxBacklog)
activities = target - backlog
activities = max(activities, 0)
activities = min(activities, config.MaxRate)
if activities > 0 {
startWG.Add(1)
go func(iter, activities int64) {
defer startWG.Done()
errCh <- e.spawnWorkflowWithActivities(ctx, iter, activities, config.SleepActivityConfig)
}(iter, activities)
iter++
}
case <-backlogTicker.C:
e.Logger.Debugf("Backlog: %d, target: %d, last iter: %d, started: %d, completed: %d",
backlog, target, activities, started, completed)
}
}
e.Logger.Info("Scenario complete; waiting for all workflows to finish...")
startWG.Wait()
e.Logger.Info("Verifying scenario completion...")
e.stateLock.Lock()
totalCompletedWorkflows := int(e.state.TotalCompletedWorkflows)
e.stateLock.Unlock()
// Post-scenario: verify that at least one workflow was completed.
if totalCompletedWorkflows == 0 {
return errors.New("No iterations completed. Either the scenario never ran, or it failed to resume correctly.")
}
// Post-scenario: verify reported workflow completion count from Visibility.
if err := loadgen.MinVisibilityCountEventually(
ctx,
e.ScenarioInfo,
&workflowservice.CountWorkflowExecutionsRequest{
Namespace: e.Namespace,
Query: fmt.Sprintf("%s='%s'",
loadgen.OmesExecutionIDSearchAttribute, e.ExecutionID),
},
totalCompletedWorkflows,
config.VisibilityVerificationTimeout,
); err != nil {
return err
}
// Post-scenario: ensure there are no failed or terminated workflows for this run.
return loadgen.VerifyNoFailedWorkflows(ctx, e.ScenarioInfo, loadgen.OmesExecutionIDSearchAttribute, e.ExecutionID)
}
// Snapshot returns a snapshot of the current state.
func (e *ebbAndFlowExecutor) Snapshot() any {
e.stateLock.Lock()
defer e.stateLock.Unlock()
return *e.state
}
// LoadState loads the state from the provided loader function.
func (e *ebbAndFlowExecutor) LoadState(loader func(any) error) error {
var state ebbAndFlowState
if err := loader(&state); err != nil {
return err
}
e.stateLock.Lock()
defer e.stateLock.Unlock()
e.state = &state
e.isResuming = true
return nil
}
func (e *ebbAndFlowExecutor) spawnWorkflowWithActivities(
ctx context.Context,
iteration, activities int64,
template *loadgen.SleepActivityConfig,
) error {
// Override activity count to fixed rate.
fixedDist := loadgen.NewFixedDistribution(activities)
config := loadgen.SleepActivityConfig{
Count: &fixedDist,
Groups: template.Groups,
}
// Start workflow.
run := e.NewRun(int(iteration))
e.RegisterDefaultSearchAttributes(ctx)
options := run.DefaultStartWorkflowOptions()
options.ID = fmt.Sprintf("%s-track-%d", e.id, iteration)
options.WorkflowExecutionErrorWhenAlreadyStarted = false
options.TypedSearchAttributes = temporal.NewSearchAttributes(
temporal.NewSearchAttributeKeyString(loadgen.OmesExecutionIDSearchAttribute).ValueSet(e.ExecutionID),
)
workflowInput := &ebbandflow.WorkflowParams{
SleepActivities: &config,
}
// Start workflow to track activity timings.
wf, err := e.Client.ExecuteWorkflow(ctx, options, "ebbAndFlowTrack", workflowInput)
if err != nil {
return fmt.Errorf("failed to start ebbAndFlowTrack workflow for iteration %d: %w", iteration, err)
}
e.scheduledActivities.Add(activities)
// Wait for workflow completion
var result ebbandflow.WorkflowOutput
err = wf.Get(ctx, &result)
if err != nil {
e.Logger.Errorf("ebbAndFlowTrack workflow failed for iteration %d: %v", iteration, err)
}
e.completedActivities.Add(activities)
e.incrementTotalCompletedWorkflow()
return nil
}
func (e *ebbAndFlowExecutor) incrementTotalCompletedWorkflow() {
e.stateLock.Lock()
if e.state != nil {
e.state.TotalCompletedWorkflows++
}
e.stateLock.Unlock()
}
func calculateBacklogTarget(
elapsed, period time.Duration,
minBacklog, maxBacklog int64,
) int64 {
periods := elapsed.Seconds() / period.Seconds()
osc := (math.Sin(2*math.Pi*(periods-0.25)) + 1.0) / 2
backlogRange := float64(maxBacklog - minBacklog)
baseTarget := float64(minBacklog) + osc*backlogRange
return int64(math.Round(baseTarget))
}