Skip to content

Commit

Permalink
Reset the flush interval timer when flush is requested or batch is re…
Browse files Browse the repository at this point in the history
…ady. (influxdata#8953)

* Reset the flush interval timer when flush is requested or batch is ready, so that timer doesn't expire while one of those flushes is occurring.

* Update tick.go
  • Loading branch information
ivorybilled authored Mar 9, 2021
1 parent 380911f commit a6d2c4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
12 changes: 4 additions & 8 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ func (a *Agent) runOutputs(
func (a *Agent) flushLoop(
ctx context.Context,
output *models.RunningOutput,
ticker Ticker,
ticker *RollingTicker,
) {
logError := func(err error) {
if err != nil {
Expand Down Expand Up @@ -822,15 +822,11 @@ func (a *Agent) flushLoop(
case <-ticker.Elapsed():
logError(a.flushOnce(output, ticker, output.Write))
case <-flushRequested:
ticker.Reset()
logError(a.flushOnce(output, ticker, output.Write))
case <-output.BatchReady:
// Favor the ticker over batch ready
select {
case <-ticker.Elapsed():
logError(a.flushOnce(output, ticker, output.Write))
default:
logError(a.flushOnce(output, ticker, output.WriteBatch))
}
ticker.Reset()
logError(a.flushOnce(output, ticker, output.WriteBatch))
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions agent/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ type RollingTicker struct {
ch chan time.Time
cancel context.CancelFunc
wg sync.WaitGroup
timer *clock.Timer
}

func NewRollingTicker(interval, jitter time.Duration) *RollingTicker {
Expand All @@ -232,12 +233,12 @@ func newRollingTicker(interval, jitter time.Duration, clock clock.Clock) *Rollin
}

d := t.next()
timer := clock.Timer(d)
t.timer = clock.Timer(d)

t.wg.Add(1)
go func() {
defer t.wg.Done()
t.run(ctx, timer)
t.run(ctx)
}()

return t
Expand All @@ -247,24 +248,28 @@ func (t *RollingTicker) next() time.Duration {
return t.interval + internal.RandomDuration(t.jitter)
}

func (t *RollingTicker) run(ctx context.Context, timer *clock.Timer) {
func (t *RollingTicker) run(ctx context.Context) {
for {
select {
case <-ctx.Done():
timer.Stop()
t.timer.Stop()
return
case now := <-timer.C:
case now := <-t.timer.C:
select {
case t.ch <- now:
default:
}

d := t.next()
timer.Reset(d)
t.Reset()
}
}
}

// Reset the ticker to the next interval + jitter.
func (t *RollingTicker) Reset() {
t.timer.Reset(t.next())
}

func (t *RollingTicker) Elapsed() <-chan time.Time {
return t.ch
}
Expand Down

0 comments on commit a6d2c4f

Please sign in to comment.