From 443a04fcc471ba6d7de134cd39341df5fad4f3ba Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 27 Feb 2024 16:21:47 +0000 Subject: [PATCH] Add Head.RebuildSymbolTable Signed-off-by: Bryan Boreham --- tsdb/head.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tsdb/head.go b/tsdb/head.go index b41657ff5..ee5776da2 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -2438,3 +2438,35 @@ func (h *Head) ForEachSecondaryHash(fn func(secondaryHash []uint32)) { } } } + +// WARNING: This code introduces a race - `lset` is accessed without a lock. +// Go through all the series in h, build a SymbolTable with all names and values, +// replace each series' Labels with one using that SymbolTable. +func (h *Head) RebuildSymbolTable() *labels.SymbolTable { + st := labels.NewSymbolTable() + builder := labels.NewScratchBuilderWithSymbolTable(st, 0) + rebuildLabels := func(lbls labels.Labels) labels.Labels { + builder.Reset() + lbls.Range(func(l labels.Label) { + builder.Add(l.Name, l.Value) + }) + return builder.Labels() + } + + for i := 0; i < h.series.size; i++ { + h.series.locks[i].Lock() + + for _, s := range h.series.hashes[i].unique { + s.lset = rebuildLabels(s.lset) + } + + for _, all := range h.series.hashes[i].conflicts { + for _, s := range all { + s.lset = rebuildLabels(s.lset) + } + } + + h.series.locks[i].Unlock() + } + return st +}