Skip to content

Commit d660af3

Browse files
authored
fix: better fix integer overrun when calculating parallelism for very long time range queries (#17430)
1 parent 0851b6f commit d660af3

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎pkg/querier/queryrange/limits.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,11 @@ func WeightedParallelism(
679679
}
680680
}
681681

682-
totalDur := int64(tsdbDur + otherDur)
682+
// We don't need nanosecond precision here so convert to seconds.
683+
// This is also important to prevent overflowing int64 when multiplying by parallelism below.
684+
tsdbDur = tsdbDur / 1e9
685+
otherDur = otherDur / 1e9
686+
totalDur := int(tsdbDur + otherDur)
683687
// If totalDur is 0, the query likely does not overlap any of the schema configs so just use parallelism of 1 and
684688
// let the downstream code handle it.
685689
if totalDur == 0 {
@@ -688,8 +692,8 @@ func WeightedParallelism(
688692
return 1
689693
}
690694

691-
tsdbPart := int64(tsdbDur) * int64(tsdbMaxQueryParallelism) / totalDur
692-
regPart := int64(otherDur) * int64(regMaxQueryParallelism) / totalDur
695+
tsdbPart := int(tsdbDur) * tsdbMaxQueryParallelism / totalDur
696+
regPart := int(otherDur) * regMaxQueryParallelism / totalDur
693697

694698
if combined := regPart + tsdbPart; combined > 0 {
695699
return int(combined)

‎pkg/querier/queryrange/limits_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ func Test_GenerateCacheKey_NoDivideZero(t *testing.T) {
667667

668668
func Test_WeightedParallelism(t *testing.T) {
669669
limits := &fakeLimits{
670-
tsdbMaxQueryParallelism: 100,
670+
tsdbMaxQueryParallelism: 2048,
671671
maxQueryParallelism: 10,
672672
}
673673

@@ -727,25 +727,25 @@ func Test_WeightedParallelism(t *testing.T) {
727727
desc: "50% each",
728728
start: borderTime.Add(-time.Hour),
729729
end: borderTime.Add(time.Hour),
730-
exp: 55,
730+
exp: 1029,
731731
},
732732
{
733733
desc: "75/25 split",
734734
start: borderTime.Add(-3 * time.Hour),
735735
end: borderTime.Add(time.Hour),
736-
exp: 32,
736+
exp: 519,
737737
},
738738
{
739739
desc: "start==end",
740740
start: borderTime.Add(time.Hour),
741741
end: borderTime.Add(time.Hour),
742-
exp: 100,
742+
exp: 2048,
743743
},
744744
{
745-
desc: "huge range which previously overflowed int",
746-
start: model.Now().Add(-24 * 60 * time.Hour),
747-
end: model.Now(),
748-
exp: 100,
745+
desc: "huge range to make sure we don't int overflow in the calculations.",
746+
start: borderTime,
747+
end: borderTime.Add(24 * time.Hour * 365 * 20),
748+
exp: 2048,
749749
},
750750
} {
751751
t.Run(cfgs.desc+tc.desc, func(t *testing.T) {

0 commit comments

Comments
 (0)