Skip to content

Commit 42138cd

Browse files
committed
fallback to dataset index query
1 parent b3183ae commit 42138cd

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

pkg/experiment/block/compaction.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,11 @@ func (b *CompactionPlan) writeDatasetIndex(w *Writer) error {
227227
metadata.LabelValueDatasetTSDBIndex,
228228
)
229229
b.meta.Datasets = append(b.meta.Datasets, &metastorev1.Dataset{
230-
Tenant: b.meta.Tenant,
231-
Name: 0, // Anonymous.
232-
MinTime: b.meta.MinTime,
233-
MaxTime: b.meta.MaxTime,
234-
// FIXME: We mimic the default layout: empty profiles, index, and empty symbols.
235-
// Instead, it should be handled at the query time: substitute the dataset layout.
236-
TableOfContents: []uint64{off, off, w.Offset()},
230+
Tenant: b.meta.Tenant,
231+
Name: 0, // Anonymous.
232+
MinTime: b.meta.MinTime,
233+
MaxTime: b.meta.MaxTime,
234+
TableOfContents: []uint64{off},
237235
Size: uint64(n),
238236
Labels: labels,
239237
})

pkg/experiment/query_backend/query.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,10 @@ func newBlockContext(
171171
func (q *blockContext) needsDatasetLookup() bool {
172172
md := q.obj.Metadata()
173173
if len(md.Datasets) > 1 {
174-
// The metadata explicitly lists datasets to be queried.
174+
// The blocks metadata explicitly lists datasets to be queried.
175175
return false
176176
}
177+
177178
ds := md.Datasets[0]
178179
t := metadata.OpenStringTable(md)
179180
m := metadata.NewLabelMatcher(t, []*labels.Matcher{{
@@ -185,14 +186,21 @@ func (q *blockContext) needsDatasetLookup() bool {
185186
if !matches {
186187
return false
187188
}
189+
190+
// The block contains a dataset TSDB index.
191+
//
192+
// If a query only requires TSDB data, we can serve it using
193+
// the dataset index without accessing the full dataset.
188194
qc := queryContext{req: q.req}
189195
if s := qc.sections(); len(s) == 1 && s[0] == block.SectionTSDB {
190-
// The block has a dataset tsdb index and the queries
191-
// only need TSDB data. In this case, we can serve the
192-
// query directly using the dataset index, without need
193-
// to access datasets.
196+
// Create a TOC with a TSDB entry pointing to the dataset index.
197+
// This allows the dataset to be used like a regular one.
198+
// Version-specific behavior.
199+
offset := ds.TableOfContents[0]
200+
ds.TableOfContents = []uint64{offset, offset, ds.Size}
194201
return false
195202
}
203+
196204
q.idx = ds
197205
return true
198206
}

pkg/frontend/read_path/query_frontend/query_frontend.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ func (q *QueryFrontend) Query(
7171
if len(blocks) == 0 {
7272
return new(queryv1.QueryResponse), nil
7373
}
74-
7574
// Randomize the order of blocks to avoid hotspots.
7675
xrand.Shuffle(len(blocks), func(i, j int) {
7776
blocks[i], blocks[j] = blocks[j], blocks[i]
7877
})
78+
// TODO(kolesnikovae): Should be dynamic.
7979
p := queryplan.Build(blocks, 4, 20)
8080

8181
resp, err := q.querybackendClient.Invoke(ctx, &queryv1.InvokeRequest{
@@ -110,25 +110,34 @@ func (q *QueryFrontend) QueryMetadata(
110110
if err != nil {
111111
return nil, status.Error(codes.InvalidArgument, err.Error())
112112
}
113+
query := &metastorev1.QueryMetadataRequest{
114+
TenantId: tenants,
115+
StartTime: req.StartTime,
116+
EndTime: req.EndTime,
117+
}
118+
119+
// Delete all labels but service_name. If no labels left, request the
120+
// dataset index for query backend to lookup datasets to be accessed.
113121
matchers = slices.DeleteFunc(matchers, func(m *labels.Matcher) bool {
114122
return m.Name != phlaremodel.LabelNameServiceName
115123
})
116124
if len(matchers) == 0 {
125+
// We preserve the __tenant_dataset__= label: this is needed for the
126+
// query backend to identify that the dataset is the tenant-wide index,
127+
// and a dataset lookup is needed.
128+
query.Labels = []string{metadata.LabelNameTenantDataset}
117129
matchers = []*labels.Matcher{{
118130
Name: metadata.LabelNameTenantDataset,
119131
Value: metadata.LabelValueDatasetTSDBIndex,
120132
Type: labels.MatchEqual,
121133
}}
122134
}
123-
query := matchersToLabelSelector(matchers)
124-
md, err := q.metadataQueryClient.QueryMetadata(ctx, &metastorev1.QueryMetadataRequest{
125-
TenantId: tenants,
126-
StartTime: req.StartTime,
127-
EndTime: req.EndTime,
128-
Query: query,
129-
})
135+
136+
query.Query = matchersToLabelSelector(matchers)
137+
md, err := q.metadataQueryClient.QueryMetadata(ctx, query)
130138
if err != nil {
131139
return nil, err
132140
}
141+
133142
return md.Blocks, nil
134143
}

0 commit comments

Comments
 (0)