Skip to content

Commit 5e2b000

Browse files
committed
refactor: move symbols logic from segment to head & change needsSymbolizion by hasNativeProfiles
1 parent 0262842 commit 5e2b000

File tree

4 files changed

+38
-34
lines changed

4 files changed

+38
-34
lines changed

pkg/experiment/block/metadata/metadata_labels.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
// TODO(kolesnikovae): LabelBuilder pool.
1717

1818
const (
19-
LabelNameTenantDataset = "__tenant_dataset__"
20-
LabelValueDatasetTSDBIndex = "dataset_tsdb_index"
21-
LabelNameNeedsSymbolization = "__needs_symbolization__"
19+
LabelNameTenantDataset = "__tenant_dataset__"
20+
LabelValueDatasetTSDBIndex = "dataset_tsdb_index"
21+
LabelNameHasNativeProfiles = "__has_native_profiles__"
2222
)
2323

2424
type LabelBuilder struct {

pkg/experiment/ingester/memdb/head.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121
)
2222

2323
type FlushedHead struct {
24-
Index []byte
25-
Profiles []byte
26-
Symbols []byte
27-
Meta struct {
24+
Index []byte
25+
Profiles []byte
26+
Symbols []byte
27+
HasNativeProfiles bool
28+
Meta struct {
2829
ProfileTypeNames []string
2930
MinTimeNanos int64
3031
MaxTimeNanos int64
@@ -153,6 +154,18 @@ func (h *Head) flush(ctx context.Context) (*FlushedHead, error) {
153154
return res, nil
154155
}
155156

157+
var hasNativeProfiles bool
158+
mappings := h.symbols.Symbols().Mappings
159+
for i := range mappings {
160+
hasNativeProfiles = hasNativeProfiles || !mappings[i].HasFunctions
161+
}
162+
locations := h.symbols.Symbols().Locations
163+
for i := range locations {
164+
hasNativeProfiles = hasNativeProfiles || len(locations[i].Line) == 0
165+
}
166+
167+
res.HasNativeProfiles = hasNativeProfiles
168+
156169
symbolsBuffer := bytes.NewBuffer(nil)
157170
if err := symdb.WritePartition(h.symbols, symbolsBuffer); err != nil {
158171
return nil, err

pkg/experiment/ingester/segment.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"slices"
1313
"strings"
1414
"sync"
15-
"sync/atomic"
1615
"time"
1716

1817
"github.com/go-kit/log"
@@ -338,10 +337,8 @@ func concatSegmentHead(f *headFlush, w *writerOffset, s *metadata.StringTable) (
338337
lb.WithLabelSet(model.LabelNameServiceName, f.head.key.service, model.LabelNameProfileType, profileType)
339338
}
340339

341-
if f.head.needsSymbolization {
342-
lb.WithLabelSet(metadata.LabelNameNeedsSymbolization, "true")
343-
if f.head.needsSymbolization.Load() {
344-
lb.WithLabelSet(model.LabelNameServiceName, f.head.key.service, metadata.LabelNameNeedsSymbolization, "true")
340+
if f.flushed.HasNativeProfiles {
341+
lb.WithLabelSet(model.LabelNameServiceName, f.head.key.service, metadata.LabelNameHasNativeProfiles, "true")
345342
}
346343

347344
// Other optional labels:
@@ -442,9 +439,8 @@ func (k datasetKey) compare(x datasetKey) int {
442439
}
443440

444441
type dataset struct {
445-
key datasetKey
446-
head *memdb.Head
447-
needsSymbolization atomic.Bool
442+
key datasetKey
443+
head *memdb.Head
448444
}
449445

450446
type headFlush struct {
@@ -508,10 +504,6 @@ func (s *segment) ingest(tenantID string, p *profilev1.Profile, id uuid.UUID, la
508504
service: model.Labels(labels).Get(model.LabelNameServiceName),
509505
}
510506
ds := s.datasetForIngest(k)
511-
if !ds.needsSymbolization.Load() && !hasSymbols(p) {
512-
ds.needsSymbolization.Store(true)
513-
}
514-
515507
size := p.SizeVT()
516508
rules := s.sw.limits.IngestionRelabelingRules(tenantID)
517509
usage := s.sw.limits.DistributorUsageGroups(tenantID).GetUsageGroups(tenantID, labels)

pkg/frontend/read_path/query_frontend/query_frontend.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func (q *QueryFrontend) Query(
9595
// TODO(kolesnikovae): Should be dynamic.
9696
p := queryplan.Build(blocks, 4, 20)
9797

98-
needsSymbolization := false
98+
hasNativeProfiles := false
9999
if q.symbolizer != nil {
100100
for _, block := range blocks {
101-
if q.symbolizationNeeded(block) {
102-
needsSymbolization = true
101+
if q.hasNativeProfiles(block) {
102+
hasNativeProfiles = true
103103
break
104104
}
105105
}
@@ -111,7 +111,7 @@ func (q *QueryFrontend) Query(
111111
modifiedQueries[i] = proto.Clone(originalQuery).(*queryv1.Query)
112112

113113
// If we need symbolization and this is a TREE query, convert it to PPROF
114-
if needsSymbolization && originalQuery.QueryType == queryv1.QueryType_QUERY_TREE {
114+
if hasNativeProfiles && originalQuery.QueryType == queryv1.QueryType_QUERY_TREE {
115115
modifiedQueries[i].QueryType = queryv1.QueryType_QUERY_PPROF
116116
modifiedQueries[i].Pprof = &queryv1.PprofQuery{
117117
MaxNodes: 0,
@@ -133,17 +133,17 @@ func (q *QueryFrontend) Query(
133133
return nil, err
134134
}
135135

136-
if needsSymbolization && q.symbolizer != nil {
136+
if hasNativeProfiles && q.symbolizer != nil {
137137
for i, r := range resp.Reports {
138138
if r.Pprof != nil && r.Pprof.Pprof != nil {
139139
var prof profilev1.Profile
140140
if err := pprof.Unmarshal(r.Pprof.Pprof, &prof); err != nil {
141-
level.Error(q.logger).Log("msg", "Unmarshal needsSymbolization", "error", err)
141+
level.Error(q.logger).Log("msg", "unmarshal pprof", "err", err)
142142
continue
143143
}
144144

145145
if err := q.symbolizer.SymbolizePprof(ctx, &prof); err != nil {
146-
level.Error(q.logger).Log("msg", "SymbolizePprof needsSymbolization", "error", err)
146+
level.Error(q.logger).Log("msg", "symbolize pprof", "err", err)
147147
}
148148

149149
// Convert back to tree if originally a tree
@@ -191,7 +191,7 @@ func (q *QueryFrontend) QueryMetadata(
191191
TenantId: tenants,
192192
StartTime: req.StartTime,
193193
EndTime: req.EndTime,
194-
Labels: []string{metadata.LabelNameNeedsSymbolization},
194+
Labels: []string{metadata.LabelNameHasNativeProfiles},
195195
}
196196

197197
// Delete all matchers but service_name with strict match. If no matchers
@@ -221,21 +221,20 @@ func (q *QueryFrontend) QueryMetadata(
221221
return md.Blocks, nil
222222
}
223223

224-
// symbolizationNeeded checks if a block needs symbolization
225-
func (q *QueryFrontend) symbolizationNeeded(block *metastorev1.BlockMeta) bool {
226-
matcher, err := labels.NewMatcher(labels.MatchEqual, metadata.LabelNameNeedsSymbolization, "true")
224+
// hasNativeProfiles checks if a block has native profiles
225+
func (q *QueryFrontend) hasNativeProfiles(block *metastorev1.BlockMeta) bool {
226+
matcher, err := labels.NewMatcher(labels.MatchEqual, metadata.LabelNameHasNativeProfiles, "true")
227227
if err != nil {
228228
return false
229229
}
230230

231231
datasetFinder := metadata.FindDatasets(block, matcher)
232232

233-
// Check if any dataset matches
234-
needsSymbolization := false
233+
hasNativeProfiles := false
235234
datasetFinder(func(ds *metastorev1.Dataset) bool {
236-
needsSymbolization = true
235+
hasNativeProfiles = true
237236
return false
238237
})
239238

240-
return needsSymbolization
239+
return hasNativeProfiles
241240
}

0 commit comments

Comments
 (0)