Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panics in DurationWithJitter utils when computed variance is zero. #10507

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* [BUGFIX] Query-frontend: Add flag `-query-frontend.prom2-range-compat` and corresponding YAML to rewrite queries with ranges that worked in Prometheus 2 but are invalid in Prometheus 3. #10445 #10461 #10502
* [BUGFIX] Distributor: Fix edge case at the HA-tracker with memberlist as KVStore, where when a replica in the KVStore is marked as deleted but not yet removed, it fails to update the KVStore. #10443
* [BUGFIX] Ingester: Fixed a race condition in the `PostingsForMatchers` cache that may have infrequently returned expired cached postings. #10500
* [BUGFIX] Fix panics in `DurationWithJitter` util functions when computed variance is zero. #10507
seizethedave marked this conversation as resolved.
Show resolved Hide resolved

### Mixin

Expand Down
12 changes: 12 additions & 0 deletions pkg/util/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func DurationWithJitter(input time.Duration, variancePerc float64) time.Duration
}

variance := int64(float64(input) * variancePerc)
if variance == 0 {
return 0
}

jitter := rand.Int63n(variance*2) - variance

return input + time.Duration(jitter)
Expand All @@ -85,6 +89,10 @@ func DurationWithPositiveJitter(input time.Duration, variancePerc float64) time.
}

variance := int64(float64(input) * variancePerc)
if variance == 0 {
return 0
}

jitter := rand.Int63n(variance)

return input + time.Duration(jitter)
Expand All @@ -98,6 +106,10 @@ func DurationWithNegativeJitter(input time.Duration, variancePerc float64) time.
}

variance := int64(float64(input) * variancePerc)
if variance == 0 {
return 0
}

jitter := rand.Int63n(variance)

return input - time.Duration(jitter)
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func TestDurationWithJitter_ZeroInputDuration(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(0), 0.5))
}

func TestDurationWithJitter_SmallInput(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(7), 0.1))
}

func TestDurationWithPositiveJitter(t *testing.T) {
const numRuns = 1000

Expand All @@ -86,6 +90,10 @@ func TestDurationWithPositiveJitter_ZeroInputDuration(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithPositiveJitter(time.Duration(0), 0.5))
}

func TestDurationWithPositiveJitter_SmallInputDuration(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithPositiveJitter(time.Duration(7), 0.1))
}

func TestDurationWithNegativeJitter(t *testing.T) {
const numRuns = 1000

Expand All @@ -100,6 +108,10 @@ func TestDurationWithNegativeJitter_ZeroInputDuration(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithNegativeJitter(time.Duration(0), 0.5))
}

func TestDurationWithNegativeJitter_SmallInputDuration(t *testing.T) {
assert.Equal(t, time.Duration(0), DurationWithNegativeJitter(time.Duration(7), 0.1))
}

func TestParseTime(t *testing.T) {
var tests = []struct {
input string
Expand Down
Loading