Skip to content

Commit 8b78f79

Browse files
fix: avoid recalculating the segmentation key hash twice (#19961)
1 parent bd31176 commit 8b78f79

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎pkg/distributor/dataobj_tee.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ func NewDataObjTee(
9898
// A SegmentedStream is a KeyedStream with a segmentation key.
9999
type SegmentedStream struct {
100100
KeyedStream
101-
SegmentationKey SegmentationKey
101+
SegmentationKey SegmentationKey
102+
SegmentationKeyHash uint64
102103
}
103104

104105
// Duplicate implements the [Tee] interface.
@@ -112,8 +113,9 @@ func (t *DataObjTee) Duplicate(ctx context.Context, tenant string, streams []Key
112113
return
113114
}
114115
segmentationKeyStreams = append(segmentationKeyStreams, SegmentedStream{
115-
KeyedStream: stream,
116-
SegmentationKey: segmentationKey,
116+
KeyedStream: stream,
117+
SegmentationKey: segmentationKey,
118+
SegmentationKeyHash: segmentationKey.Sum64(),
117119
})
118120
}
119121
rates, err := t.limitsClient.UpdateRates(ctx, tenant, segmentationKeyStreams)
@@ -127,7 +129,7 @@ func (t *DataObjTee) Duplicate(ctx context.Context, tenant string, streams []Key
127129
fastRates[rate.StreamHash] = rate.Rate
128130
}
129131
for _, s := range segmentationKeyStreams {
130-
go t.duplicate(ctx, tenant, s, fastRates[s.SegmentationKey.Sum64()])
132+
go t.duplicate(ctx, tenant, s, fastRates[s.SegmentationKeyHash])
131133
}
132134
}
133135

‎pkg/distributor/ingest_limits.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func newUpdateRatesRequest(tenant string, streams []SegmentedStream) (*proto.Upd
218218
for _, stream := range streams {
219219
entriesSize, structuredMetadataSize := calculateStreamSizes(stream.Stream)
220220
streamMetadata = append(streamMetadata, &proto.StreamMetadata{
221-
StreamHash: stream.SegmentationKey.Sum64(),
221+
StreamHash: stream.SegmentationKeyHash,
222222
TotalSize: entriesSize + structuredMetadataSize,
223223
IngestionPolicy: stream.Policy,
224224
})

‎pkg/distributor/ingest_limits_test.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,22 +320,23 @@ func TestIngestLimits_UpdateRates(t *testing.T) {
320320
name: "updates rates",
321321
tenant: "test",
322322
streams: []SegmentedStream{{
323-
SegmentationKey: "test",
323+
SegmentationKey: "test",
324+
SegmentationKeyHash: 13113208752873574959,
324325
}},
325326
expectedRequest: &proto.UpdateRatesRequest{
326327
Tenant: "test",
327328
Streams: []*proto.StreamMetadata{{
328-
StreamHash: 0xb5fb79e24c92922f,
329+
StreamHash: 13113208752873574959,
329330
}},
330331
},
331332
response: &proto.UpdateRatesResponse{
332333
Results: []*proto.UpdateRatesResult{{
333-
StreamHash: 0xb5fb79e24c92922f,
334+
StreamHash: 13113208752873574959,
334335
Rate: 1024,
335336
}},
336337
},
337338
expectedResult: []*proto.UpdateRatesResult{{
338-
StreamHash: 0xb5fb79e24c92922f,
339+
StreamHash: 13113208752873574959,
339340
Rate: 1024,
340341
}},
341342
}}

‎pkg/distributor/segment_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func TestGetSegmentationKey(t *testing.T) {
5454

5555
func TestSegmentationKey_Sum64(t *testing.T) {
5656
k1 := SegmentationKey("")
57-
require.Equal(t, uint64(0x552129d0d55dcd1b), k1.Sum64())
57+
require.Equal(t, uint64(6134230144364956955), k1.Sum64())
5858
k2 := SegmentationKey("abc")
59-
require.Equal(t, uint64(0x4a3a160be83aefc5), k2.Sum64())
59+
require.Equal(t, uint64(5348611747852513221), k2.Sum64())
6060
// The same key always produces the same 64 bit sum.
6161
k3 := SegmentationKey("abc")
6262
require.Equal(t, k2.Sum64(), k3.Sum64())

0 commit comments

Comments
 (0)