Skip to content

Commit 5bbec92

Browse files
committed
Added hashmod support for store sharding.
Signed-off-by: Bartlomiej Plotka <[email protected]>
1 parent febddd1 commit 5bbec92

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

pkg/block/fetcher.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,25 @@ func NewLabelShardedMetaFilter(relabelConfig []*relabel.Config) *LabelShardedMet
401401
return &LabelShardedMetaFilter{relabelConfig: relabelConfig}
402402
}
403403

404+
const blockIDLabel = "__block_id"
405+
404406
// Filter filters out blocks that filters blocks that have no labels after relabelling.
405407
func (f *LabelShardedMetaFilter) Filter(metas map[ulid.ULID]*metadata.Meta, synced GaugeLabeled, _ bool) {
408+
lbls := make(labels.Labels, 1)
406409
for id, m := range metas {
407-
if processedLabels := relabel.Process(labels.FromMap(m.Thanos.Labels), f.relabelConfig...); processedLabels != nil {
408-
continue
410+
lbls[0] = labels.Label{Name: blockIDLabel, Value: id.String()}
411+
lbls = lbls[:1]
412+
if len(lbls) < len(m.Thanos.Labels) {
413+
lbls = make([]labels.Label, 0, len(m.Thanos.Labels))
414+
}
415+
for k, v := range m.Thanos.Labels {
416+
lbls = append(lbls, labels.Label{Name: k, Value: v})
417+
}
418+
419+
if processedLabels := relabel.Process(lbls, f.relabelConfig...); processedLabels == nil {
420+
synced.WithLabelValues(labelExcludedMeta).Inc()
421+
delete(metas, id)
409422
}
410-
synced.WithLabelValues(labelExcludedMeta).Inc()
411-
delete(metas, id)
412423
}
413424
}
414425

pkg/block/fetcher_test.go

+67-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestMetaFetcher_Fetch(t *testing.T) {
278278
})
279279
}
280280

281-
func TestLabelShardedMetaFilter_Filter(t *testing.T) {
281+
func TestLabelShardedMetaFilter_Filter_Basic(t *testing.T) {
282282
relabelContentYaml := `
283283
- action: drop
284284
regex: "A"
@@ -340,6 +340,72 @@ func TestLabelShardedMetaFilter_Filter(t *testing.T) {
340340

341341
}
342342

343+
func TestLabelShardedMetaFilter_Filter_Hashmod(t *testing.T) {
344+
relabelContentYamlFmt := `
345+
- action: hashmod
346+
source_labels: ["ff%s"]
347+
target_label: shard
348+
modulus: 3
349+
- action: keep
350+
source_labels: ["shard"]
351+
regex: %d
352+
`
353+
for i := 0; i < 3; i++ {
354+
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
355+
var relabelConfig []*relabel.Config
356+
testutil.Ok(t, yaml.Unmarshal([]byte(fmt.Sprintf(relabelContentYamlFmt, blockIDLabel, i)), &relabelConfig))
357+
358+
f := NewLabelShardedMetaFilter(relabelConfig)
359+
360+
input := map[ulid.ULID]*metadata.Meta{
361+
ULID(1): {
362+
Thanos: metadata.Thanos{
363+
Labels: map[string]string{"cluster": "B", "message": "keepme"},
364+
},
365+
},
366+
ULID(2): {
367+
Thanos: metadata.Thanos{
368+
Labels: map[string]string{"something": "A", "message": "keepme"},
369+
},
370+
},
371+
ULID(3): {
372+
Thanos: metadata.Thanos{
373+
Labels: map[string]string{"cluster": "A", "message": "keepme"},
374+
},
375+
},
376+
ULID(4): {
377+
Thanos: metadata.Thanos{
378+
Labels: map[string]string{"cluster": "A", "something": "B", "message": "keepme"},
379+
},
380+
},
381+
ULID(5): {
382+
Thanos: metadata.Thanos{
383+
Labels: map[string]string{"cluster": "B"},
384+
},
385+
},
386+
ULID(6): {
387+
Thanos: metadata.Thanos{
388+
Labels: map[string]string{"cluster": "B", "message": "keepme"},
389+
},
390+
},
391+
ULID(7): {},
392+
ULID(8): {}, ULID(8): {},
393+
ULID(9): {},
394+
}
395+
expected := map[ulid.ULID]*metadata.Meta{
396+
// ?
397+
}
398+
399+
synced := prometheus.NewGaugeVec(prometheus.GaugeOpts{}, []string{"state"})
400+
f.Filter(input, synced, false)
401+
402+
testutil.Equals(t, 3.0, promtest.ToFloat64(synced.WithLabelValues(labelExcludedMeta)))
403+
testutil.Equals(t, expected, input)
404+
})
405+
406+
}
407+
}
408+
343409
func TestTimePartitionMetaFilter_Filter(t *testing.T) {
344410
mint := time.Unix(0, 1*time.Millisecond.Nanoseconds())
345411
maxt := time.Unix(0, 10*time.Millisecond.Nanoseconds())

0 commit comments

Comments
 (0)