forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_runner.go
460 lines (408 loc) · 14.3 KB
/
pull_runner.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
455
456
457
458
459
460
package studio
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"runtime/pprof"
"time"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/urfave/cli/v2"
ibloblang "github.com/benthosdev/benthos/v4/internal/bloblang"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/bundle/tracing"
"github.com/benthosdev/benthos/v4/internal/cli/common"
"github.com/benthosdev/benthos/v4/internal/cli/studio/metrics"
stracing "github.com/benthosdev/benthos/v4/internal/cli/studio/tracing"
"github.com/benthosdev/benthos/v4/internal/config"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/filepath/ifs"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/manager"
"github.com/benthosdev/benthos/v4/internal/stream"
"github.com/benthosdev/benthos/v4/public/bloblang"
)
type noopStopper struct{}
func (n noopStopper) Stop(_ context.Context) error {
return nil
}
// When a stream component (manager with resources or stream running a config)
// is instructed to shutdown this deadline determines the maximum amount of time
// we're willing to wait for it to be done gracefully when otherwise not
// configured.
const defaultCloseDeadline = time.Second * 30
// PullRunner encapsulates a component that runs a Benthos stream continuously
// by obtaining a deployment allocation from a Studio session, pulling the
// configs from that deployment, and then executing the configs in the
// background.
//
// Each time Sync is called the runner will poll the session for any deployment
// reallocations, or config changes and attempt to reflect those changes in the
// running stream.
type PullRunner struct {
localConf config.Type
confReader *config.Reader
sessionTracker *sessionTracker
// Controls disabled deployment rotations
isDisabled bool
latestMainConf *stream.Config
metricsFlushPeriod time.Duration
metrics *metrics.Tracker
mgr bundle.NewManagement
tracingSummary *tracing.Summary
stoppableMgr *common.StoppableManager
stoppableStream *common.SwappableStopper
logger log.Modular
exitDelay time.Duration
exitTimeout time.Duration
cliContext *cli.Context
setList []string
strictMode bool
version string
dateBuilt string
allowTraces bool
nowFn func() time.Time
}
// OptSetNowFn sets the function used to obtain a new time value representing
// now. By default time.Now is used.
func OptSetNowFn(fn func() time.Time) func(*PullRunner) {
return func(pr *PullRunner) {
pr.nowFn = fn
}
}
// NewPullRunner creates a new PullRunner from a cli context, which is used for
// overriding a range of stream behaviours and settings various studio specific
// details such as the endpoint. The version, date stamps must be provided as
// well as a valid token and secret for the session that will be accessed.
//
// It's odd having to push a *cli.Context through here but I wanted to avoid
// needing to pass tens of parameters through for things like --set,
// --prefix-stream-endpoints, etc. Some of those customisation options are
// pushed deep into things like the manager constructor, and as cli options are
// expanded it'd be a drag to have to update every single constructor signature
// that calls into it.
func NewPullRunner(c *cli.Context, version, dateBuilt, token, secret string, opts ...func(p *PullRunner)) (*PullRunner, error) {
r := &PullRunner{
localConf: config.New(),
metricsFlushPeriod: time.Second * 30,
stoppableStream: common.NewSwappableStopper(&noopStopper{}),
cliContext: c,
setList: c.StringSlice("set"),
strictMode: !c.Bool("chilled"),
version: version,
dateBuilt: dateBuilt,
nowFn: time.Now,
allowTraces: c.Bool("send-traces"),
}
for _, opt := range opts {
opt(r)
}
r.metrics = metrics.NewTracker(metrics.OptSetNowFn(r.nowFn))
nodeName := c.String("name")
if nodeName == "" {
var err error
if nodeName, err = gonanoid.New(); err != nil {
return nil, fmt.Errorf("failed to generate name: %w", err)
}
}
baseURL, err := url.Parse(c.String("endpoint"))
if err != nil {
return nil, fmt.Errorf("failed to parse endpoint: %w", err)
}
baseURL.Path = path.Join(baseURL.Path, fmt.Sprintf("/api/v1/node/session/%v", c.String("session")))
var localLints []string
if localConfPath := c.String("config"); localConfPath != "" {
localReader := config.NewReader(localConfPath, c.StringSlice("resources"),
config.OptAddOverrides(r.setList...),
config.OptTestSuffix("_benthos_test"),
)
if r.localConf, localLints, err = localReader.Read(); err != nil {
return nil, fmt.Errorf("configuration file read error: %w", err)
}
_ = r.withExitContext(c.Context, func(ctx context.Context) error {
return localReader.Close(ctx)
})
}
// Logger is suuuuper primitive so we only instantiate it from the local
// config and cli args.
if r.logger, err = common.CreateLogger(c, r.localConf, false); err != nil {
return nil, fmt.Errorf("failed to create logger: %w", err)
}
r.logLints(localLints)
if r.strictMode && len(localLints) > 0 {
return nil, errors.New("linter errors were found in local configuration files, to ignore these errors run Benthos with --chilled")
}
if r.sessionTracker, err = initSessionTracker(c.Context, r.nowFn, r.logger, nodeName, baseURL.String(), token, secret); err != nil {
return nil, fmt.Errorf("failed to initialise session connection: %w", err)
}
r.metricsFlushPeriod = r.sessionTracker.MetricsGuideFlushPeriod()
err = r.bootstrapConfigReader(c.Context)
if err != nil {
r.logger.Errorf("Failed to run initial sync config: %v", err)
}
r.sessionTracker.SetRunError(err)
return r, nil
}
func (r *PullRunner) logLints(lints []string) {
for _, lint := range lints {
if r.strictMode {
r.logger.With("lint", lint).Errorln("Config lint error")
} else {
r.logger.With("lint", lint).Warnln("Config lint error")
}
}
}
func (r *PullRunner) setStreamDisabled(ctx context.Context, toDisabled bool) error {
if r.isDisabled == toDisabled {
return nil // Already set
}
return r.withExitContext(ctx, func(ctx context.Context) error {
if toDisabled {
if err := r.stoppableStream.Replace(ctx, func() (common.Stoppable, error) {
return &noopStopper{}, nil
}); err != nil {
return err
}
} else if r.latestMainConf != nil && r.mgr != nil {
if err := r.stoppableStream.Replace(ctx, func() (common.Stoppable, error) {
return stream.New(*r.latestMainConf, r.mgr)
}); err != nil {
return err
}
}
r.isDisabled = toDisabled
return nil
})
}
func (r *PullRunner) triggerStreamReset(ctx context.Context, conf *config.Type, mgr bundle.NewManagement) error {
r.latestMainConf = &conf.Config
if r.isDisabled {
return nil
}
return r.withExitContext(ctx, func(ctx context.Context) error {
return r.stoppableStream.Replace(ctx, func() (common.Stoppable, error) {
return stream.New(conf.Config, mgr)
})
})
}
func (r *PullRunner) bootstrapConfigReader(ctx context.Context) (bootstrapErr error) {
var initMainFile string
var initResources []string
initFiles := r.sessionTracker.Files()
if initFiles.MainConfig != nil {
initMainFile = initFiles.MainConfig.Name
}
for _, f := range initFiles.ResourceConfigs {
initResources = append(initResources, f.Name)
}
sessFS := &sessionFS{
tracker: r.sessionTracker,
backup: ifs.OS(),
}
bloblEnv := ibloblang.GlobalEnvironment().WithCustomImporter(func(name string) ([]byte, error) {
return ifs.ReadFile(sessFS, name)
})
lintConf := docs.NewLintConfig()
lintConf.BloblangEnv = bloblang.XWrapEnvironment(bloblEnv).Deactivated()
confReaderTmp := config.NewReader(initMainFile, initResources,
config.OptSetBootstrapConfig(&r.localConf),
config.OptAddOverrides(r.setList...),
config.OptTestSuffix("_benthos_test"),
config.OptUseFS(sessFS),
config.OptSetLintConfig(lintConf),
)
defer func() {
if bootstrapErr != nil {
_ = r.withExitContext(ctx, func(ctx context.Context) error {
return confReaderTmp.Close(ctx)
})
}
}()
conf, lints, err := confReaderTmp.Read()
if err != nil {
return fmt.Errorf("failed bootstrap config read: %w", err)
}
r.logLints(lints)
if r.strictMode && len(lints) > 0 {
return errors.New("found linting errors in config")
}
tmpEnv, tmpTracingSummary := tracing.TracedBundle(bundle.GlobalEnvironment)
tmpTracingSummary.SetEnabled(false)
stopMgrTmp, err := common.CreateManager(
r.cliContext, r.logger, false, r.version, r.dateBuilt, conf,
manager.OptSetEnvironment(tmpEnv),
manager.OptSetBloblangEnvironment(bloblEnv),
manager.OptSetFS(sessFS))
if err != nil {
return fmt.Errorf("failed to create manager from bootstrap config: %w", err)
}
defer func() {
if bootstrapErr != nil {
_ = r.withExitContext(ctx, func(ctx context.Context) error {
return stopMgrTmp.Stop(ctx)
})
}
}()
mgrTmp := stopMgrTmp.Manager().WithAddedMetrics(r.metrics)
if err := r.triggerStreamReset(ctx, &conf, mgrTmp); err != nil {
return fmt.Errorf("failed initial stream reset: %w", err)
}
// Extract shutdown timeout values
var exitDelay time.Duration
if td := conf.SystemCloseDelay; len(td) > 0 {
var err error
if exitDelay, err = time.ParseDuration(td); err != nil {
return fmt.Errorf("failed to parse shutdown delay period string: %w", err)
}
}
var exitTimeout time.Duration
if tout := conf.SystemCloseTimeout; len(tout) > 0 {
var err error
if exitTimeout, err = time.ParseDuration(tout); err != nil {
return fmt.Errorf("failed to parse shutdown timeout period string: %w", err)
}
}
r.stoppableMgr = stopMgrTmp
r.mgr = mgrTmp
r.tracingSummary = tmpTracingSummary
r.confReader = confReaderTmp
r.exitDelay = exitDelay
r.exitTimeout = exitTimeout
if err := confReaderTmp.SubscribeConfigChanges(func(conf *config.Type) error {
return r.triggerStreamReset(context.Background(), conf, mgrTmp)
}); err != nil {
return fmt.Errorf("failed to subscribe to config changes: %w", err)
}
return
}
// Sync with the target session, obtaining new allocations, config changes,
// passing errors and metrics, etc.
func (r *PullRunner) Sync(ctx context.Context) {
var metricsOut *metrics.Observed
if r.nowFn().Sub(r.metrics.LastFlushed()) > r.metricsFlushPeriod {
metricsOut = r.metrics.Flush()
}
// Pause traces (if previously enabled), and flush all events collected
// since the last sync.
var tracingOut *stracing.Observed
if r.tracingSummary != nil {
r.tracingSummary.SetEventLimit(0)
r.tracingSummary.SetEnabled(false)
if r.allowTraces {
tracingOut = stracing.FromInternal(r.tracingSummary)
}
}
isDisabled, diff, requestedTraces, err := r.sessionTracker.Sync(ctx, metricsOut, tracingOut)
if err != nil {
r.logger.Errorf("Failed session sync: %v", err)
return
}
if r.confReader == nil {
// We haven't bootstrapped yet, likely due to a bad config on
// our first attempt. The latest sync may have fixed the issue
// so we can potentially try again.
if isDisabled {
// Except the deployment is disabled now, so don't.
r.logger.Infoln("Deployment is disabled, so skipping bootstrap of initial config")
return
}
err := r.bootstrapConfigReader(ctx)
if err != nil {
r.logger.Errorf("Failed to bootstrap initial config: %v", err)
}
r.sessionTracker.SetRunError(err)
return
}
if err = r.setStreamDisabled(ctx, isDisabled); err != nil {
r.logger.Errorf("Failed to toggle deployment enablement: %v", err)
return
}
var runErr error // TODO: Use new multi error
if diff != nil {
// We've already bootstrapped, and so we need to update our
// config reader of all changes.
for _, resName := range diff.RemoveResources {
if err := r.confReader.TriggerResourceDelete(r.mgr, resName); err != nil {
r.logger.Errorf("Failed to reflect resource file '%v' deletion: %v", r, err)
runErr = err
}
}
for _, res := range diff.AddResources {
if err := r.confReader.TriggerResourceUpdate(r.mgr, r.strictMode, res.Name); err != nil {
r.logger.Errorf("Failed to reflect resource file '%v' update: %v", res.Name, err)
runErr = err
}
}
if diff.MainConfig != nil {
if err := r.confReader.TriggerMainUpdate(r.mgr, r.strictMode, diff.MainConfig.Name); err != nil {
r.logger.Errorf("Failed to reflect main config file '%v' update: %v", diff.MainConfig.Name, err)
runErr = err
}
}
r.sessionTracker.SetRunError(runErr)
}
if runErr != nil {
return
}
// Set a new trace limit and re-enable if appropriate, we want to do this if
// either the files we already have match the deployment, or after we've
// successfully followed the diff.
if r.allowTraces {
r.tracingSummary.SetEventLimit(requestedTraces)
r.tracingSummary.SetEnabled(requestedTraces > 0)
}
}
func (r *PullRunner) withExitContext(ctx context.Context, fn func(context.Context) error) error {
tout := r.exitTimeout
if tout <= 0 {
tout = defaultCloseDeadline
}
ctx, done := context.WithTimeout(ctx, tout)
defer done()
return fn(ctx)
}
// Stop any underlying stream and managers that may exist.
func (r *PullRunner) Stop(ctx context.Context) error {
{
// Use a shorter deadline for leaving as it's optional
leaveCtx := ctx
if dl, exists := ctx.Deadline(); !exists || dl.Sub(r.nowFn()) > time.Second {
var done func()
leaveCtx, done = context.WithTimeout(leaveCtx, time.Second)
defer done()
}
if err := r.sessionTracker.Leave(leaveCtx); err != nil {
r.logger.Warnf("Failed to inform Studio session that we're shutting down: %v", err)
}
}
if r.exitDelay > 0 {
r.logger.Infof("Shutdown delay is in effect for %s", r.exitDelay)
if err := common.DelayShutdown(ctx, r.exitDelay); err != nil {
r.logger.Errorf("Shutdown delay failed: %s", err)
}
}
return r.withExitContext(ctx, func(ctx context.Context) error {
if err := r.stoppableStream.Stop(ctx); err != nil {
r.logger.Warnf(
"Service failed to close the running stream cleanly within allocated time: %v."+
" Exiting forcefully and dumping stack trace to stderr\n", err,
)
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
return err
}
if r.stoppableMgr == nil {
return nil
}
if err := r.stoppableMgr.Stop(ctx); err != nil {
r.logger.Warnf(
"Service failed to close resources cleanly within allocated time: %v."+
" Exiting forcefully and dumping stack trace to stderr\n", err,
)
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
return err
}
return nil
})
}