From 160186da18d8daa39b2318f9bb7263fc106847b9 Mon Sep 17 00:00:00 2001 From: Chris Marchbanks Date: Wed, 7 Aug 2019 13:39:07 -0600 Subject: [PATCH] Store labels.Labels instead of []prompb.Label This will use half the steady state memory as required by prompb.Label. Signed-off-by: Chris Marchbanks --- storage/remote/codec.go | 4 ++-- storage/remote/queue_manager.go | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/storage/remote/codec.go b/storage/remote/codec.go index 15397f8a8..aa67935cb 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -526,8 +526,8 @@ func labelsToLabelsProto(labels labels.Labels) []prompb.Label { result := make([]prompb.Label, 0, len(labels)) for _, l := range labels { result = append(result, prompb.Label{ - Name: interner.intern(l.Name), - Value: interner.intern(l.Value), + Name: l.Name, + Value: l.Value, }) } return result diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index cda8eda3b..581356898 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -166,7 +166,7 @@ type QueueManager struct { client StorageClient watcher *WALWatcher - seriesLabels map[uint64][]prompb.Label + seriesLabels map[uint64]labels.Labels seriesSegmentIndexes map[uint64]int droppedSeries map[uint64]struct{} @@ -208,7 +208,7 @@ func NewQueueManager(logger log.Logger, walDir string, samplesIn *ewmaRate, cfg relabelConfigs: relabelConfigs, client: client, - seriesLabels: make(map[uint64][]prompb.Label), + seriesLabels: make(map[uint64]labels.Labels), seriesSegmentIndexes: make(map[uint64]int), droppedSeries: make(map[uint64]struct{}), @@ -252,7 +252,7 @@ outer: } ts := prompb.TimeSeries{ - Labels: lbls, + Labels: labelsToLabelsProto(lbls), Samples: []prompb.Sample{ { Value: float64(sample.V), @@ -325,7 +325,7 @@ func (t *QueueManager) Stop() { // On shutdown, release the strings in the labels from the intern pool. for _, labels := range t.seriesLabels { - release(labels) + releaseLabels(labels) } // Delete metrics so we don't have alerts for queues that are gone. name := t.client.Name() @@ -345,21 +345,21 @@ func (t *QueueManager) Stop() { func (t *QueueManager) StoreSeries(series []tsdb.RefSeries, index int) { for _, s := range series { ls := processExternalLabels(s.Labels, t.externalLabels) - rl := relabel.Process(ls, t.relabelConfigs...) - if len(rl) == 0 { + lbls := relabel.Process(ls, t.relabelConfigs...) + if len(lbls) == 0 { t.droppedSeries[s.Ref] = struct{}{} continue } t.seriesSegmentIndexes[s.Ref] = index - labels := labelsToLabelsProto(rl) + internLabels(lbls) // We should not ever be replacing a series labels in the map, but just // in case we do we need to ensure we do not leak the replaced interned // strings. if orig, ok := t.seriesLabels[s.Ref]; ok { - release(orig) + releaseLabels(orig) } - t.seriesLabels[s.Ref] = labels + t.seriesLabels[s.Ref] = lbls } } @@ -372,13 +372,20 @@ func (t *QueueManager) SeriesReset(index int) { for k, v := range t.seriesSegmentIndexes { if v < index { delete(t.seriesSegmentIndexes, k) - release(t.seriesLabels[k]) + releaseLabels(t.seriesLabels[k]) delete(t.seriesLabels, k) } } } -func release(ls []prompb.Label) { +func internLabels(lbls labels.Labels) { + for i, l := range lbls { + lbls[i].Name = interner.intern(l.Name) + lbls[i].Value = interner.intern(l.Value) + } +} + +func releaseLabels(ls labels.Labels) { for _, l := range ls { interner.release(l.Name) interner.release(l.Value)