Skip to content

fix: Negative collection_offset Causes Multiple Executions#18178

Open
majiayu000 wants to merge 3 commits intoinfluxdata:masterfrom
majiayu000:fix-18175-negative-collectionoffset-caus-0102-1150
Open

fix: Negative collection_offset Causes Multiple Executions#18178
majiayu000 wants to merge 3 commits intoinfluxdata:masterfrom
majiayu000:fix-18175-negative-collectionoffset-caus-0102-1150

Conversation

@majiayu000
Copy link

Fixes #18175

Changes

  • Add check in AlignedTicker.next() to ensure duration is positive after applying negative offset
  • Update sleep() to handle negative durations by returning immediately
  • Add tests for negative offset behavior

When using a negative collection_offset value, the AlignedTicker could
calculate a duration less than or equal to zero, causing the timer to
fire immediately and repeatedly. This resulted in multiple executions
and "Previous collection has not completed" debug messages.

The fix ensures that when the calculated duration (time to next aligned
interval plus offset) is non-positive, one full interval is added to
schedule for the next valid tick time. This check is performed before
adding jitter to maintain proper timing behavior.

Also updated the sleep function to handle negative durations by
returning immediately instead of creating a timer with a negative value.

Fixes influxdata#18175

Signed-off-by: majiayu000 <1835304752@qq.com>
@Hipska
Copy link
Contributor

Hipska commented Jan 5, 2026

Shouldn't the solution be to let Telegraf error out when a negative duration is configured anywhere?

@srebhan
Copy link
Member

srebhan commented Jan 13, 2026

@majiayu000 adding to the comment of @Hipska, what is the reasoning for a negative offset?

I'm asking because the effect is basically the same as doing interval - abs(negative_offset) isn't it? So maybe that's an easier and more elegant solution?

@srebhan srebhan self-assigned this Jan 13, 2026
@majiayu000
Copy link
Author

@Hipska @srebhan Thanks! I updated the logic to normalize negative offsets into [0, interval), which is equivalent to interval - abs(offset) for negative values. This makes the behaviorexplicit and avoids the rapid-fire tick issue without changing scheduling semantics.

@telegraf-tiger
Copy link
Contributor

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @majiayu000 for the update! I think you should error out in cases where

  1. the offset is larger than interval
  2. the offset is negative and its absolute value is larger than interval (i.e. offset + interval < 0)
  3. the interval is negative

Given all that I'm not sure if we should support negative offsets at all... It seems to me that the semantics of a negative offset is somewhat ambiguous and chances are high that the user means something different from what we implement. So how about simply error out and suggest to use interval - offset instead?

Comment on lines +82 to +86
// Ensure the duration is positive before adding jitter. This can still
// happen with small intervals and clock adjustments.
if d <= 0 {
d += t.interval
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never happen, can it?

Comment on lines +149 to +151
if interval <= 0 || offset == 0 {
return offset
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please error out during configuration if the interval is negative! This is clearly a misconfiguration!

if interval <= 0 || offset == 0 {
return offset
}
offset = offset % interval
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this unexpected to the user? I rather prefer to error out during configuration if the offset is larger than the interval...

Comment on lines +153 to +155
if offset < 0 {
offset += interval
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure that the result is positive during configuration and error out otherwise!

@srebhan srebhan added the waiting for response waiting for response from contributor label Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting for response waiting for response from contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Negative collection_offset Causes Multiple Executions

3 participants