From: Martina Ferrari <tina@debian.org>
Date: Wed, 12 Feb 2025 21:59:20 +0000
Subject: Backport fixes for 32-bit archs

Forwarded: not-needed
Last-Update: 2025-02-12

Adapted from pull request #15548 (318d6bc4b)
---
 model/histogram/test_utils.go        |  4 ++--
 promql/engine.go                     | 14 +++++++-------
 storage/merge_test.go                |  4 ++--
 storage/remote/queue_manager_test.go |  2 +-
 tsdb/querier_test.go                 | 12 ++++++------
 tsdb/tsdbutil/histogram.go           | 20 ++++++++++----------
 util/testutil/context.go             |  2 +-
 7 files changed, 29 insertions(+), 29 deletions(-)

diff --git a/model/histogram/test_utils.go b/model/histogram/test_utils.go
index 9e9a711..e6b3386 100644
--- a/model/histogram/test_utils.go
+++ b/model/histogram/test_utils.go
@@ -19,12 +19,12 @@ func GenerateBigTestHistograms(numHistograms, numBuckets int) []*Histogram {
 	bucketsPerSide := numBuckets / 2
 	spanLength := uint32(bucketsPerSide / numSpans)
 	// Given all bucket deltas are 1, sum bucketsPerSide + 1.
-	observationCount := bucketsPerSide * (1 + bucketsPerSide)
+	observationCount := uint64(bucketsPerSide) * (1 + uint64(bucketsPerSide))
 
 	var histograms []*Histogram
 	for i := 0; i < numHistograms; i++ {
 		h := &Histogram{
-			Count:           uint64(i + observationCount),
+			Count:           uint64(i) + observationCount,
 			ZeroCount:       uint64(i),
 			ZeroThreshold:   1e-128,
 			Sum:             18.4 * float64(i+1),
diff --git a/promql/engine.go b/promql/engine.go
index f9d6f16..7f7f6c5 100644
--- a/promql/engine.go
+++ b/promql/engine.go
@@ -1325,16 +1325,16 @@ func (ev *evaluator) rangeEvalAgg(aggExpr *parser.AggregateExpr, sortedGrouping
 	}
 	groups := make([]groupedAggregation, groupCount)
 
-	var k int
+	var k int64
 	var seriess map[uint64]Series
 	switch aggExpr.Op {
 	case parser.TOPK, parser.BOTTOMK:
 		if !convertibleToInt64(param) {
 			ev.errorf("Scalar value %v overflows int64", param)
 		}
-		k = int(param)
-		if k > len(inputMatrix) {
-			k = len(inputMatrix)
+		k = int64(param)
+		if k > int64(len(inputMatrix)) {
+			k = int64(len(inputMatrix))
 		}
 		if k < 1 {
 			return nil, warnings
@@ -2925,7 +2925,7 @@ func (ev *evaluator) aggregation(e *parser.AggregateExpr, q float64, inputMatrix
 // seriesToResult maps inputMatrix indexes to groups indexes.
 // For an instant query, returns a Matrix in descending order for topk or ascending for bottomk.
 // For a range query, aggregates output in the seriess map.
-func (ev *evaluator) aggregationK(e *parser.AggregateExpr, k int, inputMatrix Matrix, seriesToResult []int, groups []groupedAggregation, enh *EvalNodeHelper, seriess map[uint64]Series) (Matrix, annotations.Annotations) {
+func (ev *evaluator) aggregationK(e *parser.AggregateExpr, k int64, inputMatrix Matrix, seriesToResult []int, groups []groupedAggregation, enh *EvalNodeHelper, seriess map[uint64]Series) (Matrix, annotations.Annotations) {
 	op := e.Op
 	var s Sample
 	var annos annotations.Annotations
@@ -2955,7 +2955,7 @@ func (ev *evaluator) aggregationK(e *parser.AggregateExpr, k int, inputMatrix Ma
 		case parser.TOPK:
 			// We build a heap of up to k elements, with the smallest element at heap[0].
 			switch {
-			case len(group.heap) < k:
+			case int64(len(group.heap)) < k:
 				heap.Push(&group.heap, &s)
 			case group.heap[0].F < s.F || (math.IsNaN(group.heap[0].F) && !math.IsNaN(s.F)):
 				// This new element is bigger than the previous smallest element - overwrite that.
@@ -2968,7 +2968,7 @@ func (ev *evaluator) aggregationK(e *parser.AggregateExpr, k int, inputMatrix Ma
 		case parser.BOTTOMK:
 			// We build a heap of up to k elements, with the biggest element at heap[0].
 			switch {
-			case len(group.heap) < k:
+			case int64(len(group.heap)) < k:
 				heap.Push((*vectorByReverseValueHeap)(&group.heap), &s)
 			case group.heap[0].F > s.F || (math.IsNaN(group.heap[0].F) && !math.IsNaN(s.F)):
 				// This new element is smaller than the previous biggest element - overwrite that.
diff --git a/storage/merge_test.go b/storage/merge_test.go
index 0e63aff..781029a 100644
--- a/storage/merge_test.go
+++ b/storage/merge_test.go
@@ -385,13 +385,13 @@ func TestMergeChunkQuerierWithNoVerticalChunkSeriesMerger(t *testing.T) {
 }
 
 func histogramSample(ts int64, hint histogram.CounterResetHint) hSample {
-	h := tsdbutil.GenerateTestHistogram(int(ts + 1))
+	h := tsdbutil.GenerateTestHistogram(ts + 1)
 	h.CounterResetHint = hint
 	return hSample{t: ts, h: h}
 }
 
 func floatHistogramSample(ts int64, hint histogram.CounterResetHint) fhSample {
-	fh := tsdbutil.GenerateTestFloatHistogram(int(ts + 1))
+	fh := tsdbutil.GenerateTestFloatHistogram(ts + 1)
 	fh.CounterResetHint = hint
 	return fhSample{t: ts, fh: fh}
 }
diff --git a/storage/remote/queue_manager_test.go b/storage/remote/queue_manager_test.go
index 6121fb6..80f5776 100644
--- a/storage/remote/queue_manager_test.go
+++ b/storage/remote/queue_manager_test.go
@@ -1460,7 +1460,7 @@ func createTimeseriesWithOldSamples(numSamples, numSeries int, extraLabels ...la
 		for j := 0; j < numSamples/2; j++ {
 			sample := record.RefSample{
 				Ref: chunks.HeadSeriesRef(i),
-				T:   int64(int(time.Now().UnixMilli()) + j),
+				T:   time.Now().UnixMilli() + int64(j),
 				V:   float64(i),
 			}
 			samples = append(samples, sample)
diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go
index c7e60a0..dcb5d1b 100644
--- a/tsdb/querier_test.go
+++ b/tsdb/querier_test.go
@@ -1826,12 +1826,12 @@ func checkCurrVal(t *testing.T, valType chunkenc.ValueType, it *populateWithDelS
 		ts, h := it.AtHistogram(nil)
 		require.Equal(t, int64(expectedTs), ts)
 		h.CounterResetHint = histogram.UnknownCounterReset
-		require.Equal(t, tsdbutil.GenerateTestHistogram(expectedValue), h)
+		require.Equal(t, tsdbutil.GenerateTestHistogram(int64(expectedValue)), h)
 	case chunkenc.ValFloatHistogram:
 		ts, h := it.AtFloatHistogram(nil)
 		require.Equal(t, int64(expectedTs), ts)
 		h.CounterResetHint = histogram.UnknownCounterReset
-		require.Equal(t, tsdbutil.GenerateTestFloatHistogram(expectedValue), h)
+		require.Equal(t, tsdbutil.GenerateTestFloatHistogram(int64(expectedValue)), h)
 	default:
 		panic("unexpected value type")
 	}
@@ -3498,16 +3498,16 @@ func TestQueryWithDeletedHistograms(t *testing.T) {
 	ctx := context.Background()
 	testcases := map[string]func(int) (*histogram.Histogram, *histogram.FloatHistogram){
 		"intCounter": func(i int) (*histogram.Histogram, *histogram.FloatHistogram) {
-			return tsdbutil.GenerateTestHistogram(i), nil
+			return tsdbutil.GenerateTestHistogram(int64(i)), nil
 		},
 		"intgauge": func(i int) (*histogram.Histogram, *histogram.FloatHistogram) {
-			return tsdbutil.GenerateTestGaugeHistogram(rand.Int() % 1000), nil
+			return tsdbutil.GenerateTestGaugeHistogram(rand.Int63() % 1000), nil
 		},
 		"floatCounter": func(i int) (*histogram.Histogram, *histogram.FloatHistogram) {
-			return nil, tsdbutil.GenerateTestFloatHistogram(i)
+			return nil, tsdbutil.GenerateTestFloatHistogram(int64(i))
 		},
 		"floatGauge": func(i int) (*histogram.Histogram, *histogram.FloatHistogram) {
-			return nil, tsdbutil.GenerateTestGaugeFloatHistogram(rand.Int() % 1000)
+			return nil, tsdbutil.GenerateTestGaugeFloatHistogram(rand.Int63() % 1000)
 		},
 	}
 
diff --git a/tsdb/tsdbutil/histogram.go b/tsdb/tsdbutil/histogram.go
index bb8d49b..366674b 100644
--- a/tsdb/tsdbutil/histogram.go
+++ b/tsdb/tsdbutil/histogram.go
@@ -21,7 +21,7 @@ import (
 
 func GenerateTestHistograms(n int) (r []*histogram.Histogram) {
 	for i := 0; i < n; i++ {
-		h := GenerateTestHistogram(i)
+		h := GenerateTestHistogram(int64(i))
 		if i > 0 {
 			h.CounterResetHint = histogram.NotCounterReset
 		}
@@ -39,7 +39,7 @@ func GenerateTestHistogramsWithUnknownResetHint(n int) []*histogram.Histogram {
 }
 
 // GenerateTestHistogram but it is up to the user to set any known counter reset hint.
-func GenerateTestHistogram(i int) *histogram.Histogram {
+func GenerateTestHistogram(i int64) *histogram.Histogram {
 	return &histogram.Histogram{
 		Count:         12 + uint64(i*9),
 		ZeroCount:     2 + uint64(i),
@@ -50,24 +50,24 @@ func GenerateTestHistogram(i int) *histogram.Histogram {
 			{Offset: 0, Length: 2},
 			{Offset: 1, Length: 2},
 		},
-		PositiveBuckets: []int64{int64(i + 1), 1, -1, 0},
+		PositiveBuckets: []int64{i + 1, 1, -1, 0},
 		NegativeSpans: []histogram.Span{
 			{Offset: 0, Length: 2},
 			{Offset: 1, Length: 2},
 		},
-		NegativeBuckets: []int64{int64(i + 1), 1, -1, 0},
+		NegativeBuckets: []int64{i + 1, 1, -1, 0},
 	}
 }
 
 func GenerateTestGaugeHistograms(n int) (r []*histogram.Histogram) {
 	for x := 0; x < n; x++ {
-		i := int(math.Sin(float64(x))*100) + 100
+		i := int64(math.Sin(float64(x))*100) + 100
 		r = append(r, GenerateTestGaugeHistogram(i))
 	}
 	return r
 }
 
-func GenerateTestGaugeHistogram(i int) *histogram.Histogram {
+func GenerateTestGaugeHistogram(i int64) *histogram.Histogram {
 	h := GenerateTestHistogram(i)
 	h.CounterResetHint = histogram.GaugeType
 	return h
@@ -75,7 +75,7 @@ func GenerateTestGaugeHistogram(i int) *histogram.Histogram {
 
 func GenerateTestFloatHistograms(n int) (r []*histogram.FloatHistogram) {
 	for i := 0; i < n; i++ {
-		h := GenerateTestFloatHistogram(i)
+		h := GenerateTestFloatHistogram(int64(i))
 		if i > 0 {
 			h.CounterResetHint = histogram.NotCounterReset
 		}
@@ -85,7 +85,7 @@ func GenerateTestFloatHistograms(n int) (r []*histogram.FloatHistogram) {
 }
 
 // GenerateTestFloatHistogram but it is up to the user to set any known counter reset hint.
-func GenerateTestFloatHistogram(i int) *histogram.FloatHistogram {
+func GenerateTestFloatHistogram(i int64) *histogram.FloatHistogram {
 	return &histogram.FloatHistogram{
 		Count:         12 + float64(i*9),
 		ZeroCount:     2 + float64(i),
@@ -107,13 +107,13 @@ func GenerateTestFloatHistogram(i int) *histogram.FloatHistogram {
 
 func GenerateTestGaugeFloatHistograms(n int) (r []*histogram.FloatHistogram) {
 	for x := 0; x < n; x++ {
-		i := int(math.Sin(float64(x))*100) + 100
+		i := int64(math.Sin(float64(x))*100) + 100
 		r = append(r, GenerateTestGaugeFloatHistogram(i))
 	}
 	return r
 }
 
-func GenerateTestGaugeFloatHistogram(i int) *histogram.FloatHistogram {
+func GenerateTestGaugeFloatHistogram(i int64) *histogram.FloatHistogram {
 	h := GenerateTestFloatHistogram(i)
 	h.CounterResetHint = histogram.GaugeType
 	return h
diff --git a/util/testutil/context.go b/util/testutil/context.go
index 0c9e0f6..ea4b0e3 100644
--- a/util/testutil/context.go
+++ b/util/testutil/context.go
@@ -49,8 +49,8 @@ func (c *MockContext) Value(interface{}) interface{} {
 // MockContextErrAfter is a MockContext that will return an error after a certain
 // number of calls to Err().
 type MockContextErrAfter struct {
+	count atomic.Uint64
 	MockContext
-	count     atomic.Uint64
 	FailAfter uint64
 }
 
