Skip to content

Commit e9c7d69

Browse files
committed
Clean Symbol Tables
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent b2c0475 commit e9c7d69

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [BUGFIX] Fix nil when ingester_query_max_attempts > 1. #7369
1111
* [BUGFIX] Querier: Fix queryWithRetry and labelsWithRetry returning (nil, nil) on cancelled context by propagating ctx.Err(). #7370
1212
* [BUGFIX] Metrics Helper: Fix non-deterministic bucket order in merged histograms by sorting buckets after map iteration, matching Prometheus client library behavior. #7380
13+
* [BUGFIX] Fix memory leak in `ReuseWriteRequestV2` by explicitly clearing the `Symbols` backing array string pointers before returning the object to `sync.Pool`. #7373
1314
* [BUGFIX] Distributor: Return HTTP 401 Unauthorized when tenant ID resolution fails in the Prometheus Remote Write 2.0 path. #7389
1415

1516
## 1.21.0 in progress

pkg/cortexpb/timeseriesv2.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ func ReuseWriteRequestV2(req *PreallocWriteRequestV2) {
7777
req.data = nil
7878
}
7979
req.Source = 0
80+
81+
for i := range req.Symbols {
82+
req.Symbols[i] = ""
83+
}
8084
req.Symbols = req.Symbols[:0]
8185
if req.Timeseries != nil {
8286
ReuseSliceV2(req.Timeseries)

pkg/cortexpb/timeseriesv2_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ func TestTimeseriesV2FromPool(t *testing.T) {
5858
})
5959
}
6060

61+
func TestReuseWriteRequestV2(t *testing.T) {
62+
req := PreallocWriteRequestV2FromPool()
63+
64+
// Populate req with some data.
65+
req.Source = RULE
66+
req.Symbols = append(req.Symbols, "", "__name__", "test")
67+
68+
tsSlice := PreallocTimeseriesV2SliceFromPool()
69+
tsSlice = append(tsSlice, PreallocTimeseriesV2{TimeSeriesV2: TimeseriesV2FromPool()})
70+
req.Timeseries = tsSlice
71+
72+
// Capture backing array before reuse
73+
symbolsBackingArray := req.Symbols[:cap(req.Symbols)]
74+
require.Equal(t, "__name__", symbolsBackingArray[1])
75+
require.Equal(t, "test", symbolsBackingArray[2])
76+
77+
// Put the request back into the pool
78+
ReuseWriteRequestV2(req)
79+
80+
// Verify clearing directly on the backing array
81+
for i, s := range symbolsBackingArray[:3] {
82+
assert.Equalf(t, "", s, "symbol at index %d not cleared", i)
83+
}
84+
85+
// Source is reset to default
86+
assert.Equal(t, API, req.Source)
87+
// The symbol length is properly reset to 0.
88+
assert.Len(t, req.Symbols, 0)
89+
// Timeseries slice is nil
90+
assert.Nil(t, req.Timeseries)
91+
}
92+
6193
func BenchmarkMarshallWriteRequestV2(b *testing.B) {
6294
ts := PreallocTimeseriesV2SliceFromPool()
6395

0 commit comments

Comments
 (0)