From 1f119e1356032e6a85785c4d11ce8eb972b5123a Mon Sep 17 00:00:00 2001 From: Travis Patterson Date: Fri, 2 Jun 2023 08:45:28 -0600 Subject: [PATCH] Prevent GetFactorOfTime from returning NaN (#9602) `GetFactorOfTime` is used to calculate overlaps with chunk time ranges -- particularly calls to `stats`. When a chunk has only one entry, it's `start time == end time` and this `GetFactorOfTime` returns `NaN`. The stats calculation is `uint64(total chunk bytes * factor)`. When `factor` is `NaN`: `uint64(total chunk bytes * factor) == uint64(math.Nan()) == 9223372036854775808` `uint64(math.Nan()) == 9223372036854775808` is an implementation detail of golang. --- pkg/util/time.go | 7 +++++++ pkg/util/time_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/util/time.go b/pkg/util/time.go index cd80ceb66a..59fcc4d08f 100644 --- a/pkg/util/time.go +++ b/pkg/util/time.go @@ -133,6 +133,13 @@ func GetFactorOfTime(from, through int64, minTime, maxTime int64) (factor float6 return 0 } + if minTime == maxTime { + // This function is most often used for chunk overlaps + // a chunk maxTime == minTime when it has only 1 entry + // return factor 1 to count that chunk's entry + return 1 + } + totalTime := maxTime - minTime leadingTime := utilsMath.Max64(0, from-minTime) trailingTime := utilsMath.Max64(0, maxTime-through) diff --git a/pkg/util/time_test.go b/pkg/util/time_test.go index 074e53db7a..7dc80d4163 100644 --- a/pkg/util/time_test.go +++ b/pkg/util/time_test.go @@ -270,6 +270,12 @@ func TestGetFactorOfTime(t *testing.T) { extentMin: 20, extentMax: 35, exp: 0, }, + { + desc: "factor would be NaN", + from: 1685655637000000000, through: 1685656237000000000, + extentMin: 1685656107442496000, extentMax: 1685656107442496000, + exp: 1, + }, } { t.Run(tc.desc, func(t *testing.T) { factor := GetFactorOfTime(tc.from, tc.through, tc.extentMin, tc.extentMax)