Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 4 additions & 5 deletions pkg/cortexpb/timeseriesv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func ReuseWriteRequestV2(req *PreallocWriteRequestV2) {
req.data = nil
}
req.Source = 0

for i := range req.Symbols {
req.Symbols[i] = ""
}
req.Symbols = req.Symbols[:0]
if req.Timeseries != nil {
ReuseSliceV2(req.Timeseries)
Expand Down Expand Up @@ -121,11 +125,6 @@ func ReuseTimeseriesV2(ts *TimeSeriesV2) {
ts.Metadata.UnitRef = 0
ts.Metadata.HelpRef = 0

// clear exemplar label refs
for i := range ts.Exemplars {
ts.Exemplars[i].LabelsRefs = ts.Exemplars[i].LabelsRefs[:0]
}

for i := range ts.Histograms {
ts.Histograms[i].Reset()
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/cortexpb/timeseriesv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ func TestTimeseriesV2FromPool(t *testing.T) {
})
}

func TestReuseWriteRequestV2(t *testing.T) {
req := PreallocWriteRequestV2FromPool()

// Populate req with some data.
req.Source = RULE
req.Symbols = append(req.Symbols, "", "__name__", "test")

tsSlice := PreallocTimeseriesV2SliceFromPool()
tsSlice = append(tsSlice, PreallocTimeseriesV2{TimeSeriesV2: TimeseriesV2FromPool()})
req.Timeseries = tsSlice

// Put the request back into the pool
ReuseWriteRequestV2(req)

// Retrieve it from the pool again
reused := PreallocWriteRequestV2FromPool()

// Source is reset to default
assert.Equal(t, API, reused.Source)
// The symbol length is properly reset to 0.
assert.Len(t, reused.Symbols, 0)
// Timeseries slice is nil
assert.Nil(t, reused.Timeseries)

// The underlying array's string pointers are cleared to "" to prevent memory leak.
if cap(reused.Symbols) > 0 {
underlyingArray := reused.Symbols[:cap(reused.Symbols)]
assert.Equal(t, "", underlyingArray[0])
assert.Equal(t, "", underlyingArray[1])
}
}

func BenchmarkMarshallWriteRequestV2(b *testing.B) {
ts := PreallocTimeseriesV2SliceFromPool()

Expand Down
Loading