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 <csmarchbanks@gmail.com>
This commit is contained in:
Chris Marchbanks 2019-08-07 13:39:07 -06:00
parent 24844cc222
commit 160186da18
No known key found for this signature in database
GPG key ID: B7FD940BC86A8E7A
2 changed files with 20 additions and 13 deletions

View file

@ -526,8 +526,8 @@ func labelsToLabelsProto(labels labels.Labels) []prompb.Label {
result := make([]prompb.Label, 0, len(labels)) result := make([]prompb.Label, 0, len(labels))
for _, l := range labels { for _, l := range labels {
result = append(result, prompb.Label{ result = append(result, prompb.Label{
Name: interner.intern(l.Name), Name: l.Name,
Value: interner.intern(l.Value), Value: l.Value,
}) })
} }
return result return result

View file

@ -166,7 +166,7 @@ type QueueManager struct {
client StorageClient client StorageClient
watcher *WALWatcher watcher *WALWatcher
seriesLabels map[uint64][]prompb.Label seriesLabels map[uint64]labels.Labels
seriesSegmentIndexes map[uint64]int seriesSegmentIndexes map[uint64]int
droppedSeries map[uint64]struct{} droppedSeries map[uint64]struct{}
@ -208,7 +208,7 @@ func NewQueueManager(logger log.Logger, walDir string, samplesIn *ewmaRate, cfg
relabelConfigs: relabelConfigs, relabelConfigs: relabelConfigs,
client: client, client: client,
seriesLabels: make(map[uint64][]prompb.Label), seriesLabels: make(map[uint64]labels.Labels),
seriesSegmentIndexes: make(map[uint64]int), seriesSegmentIndexes: make(map[uint64]int),
droppedSeries: make(map[uint64]struct{}), droppedSeries: make(map[uint64]struct{}),
@ -252,7 +252,7 @@ outer:
} }
ts := prompb.TimeSeries{ ts := prompb.TimeSeries{
Labels: lbls, Labels: labelsToLabelsProto(lbls),
Samples: []prompb.Sample{ Samples: []prompb.Sample{
{ {
Value: float64(sample.V), Value: float64(sample.V),
@ -325,7 +325,7 @@ func (t *QueueManager) Stop() {
// On shutdown, release the strings in the labels from the intern pool. // On shutdown, release the strings in the labels from the intern pool.
for _, labels := range t.seriesLabels { for _, labels := range t.seriesLabels {
release(labels) releaseLabels(labels)
} }
// Delete metrics so we don't have alerts for queues that are gone. // Delete metrics so we don't have alerts for queues that are gone.
name := t.client.Name() name := t.client.Name()
@ -345,21 +345,21 @@ func (t *QueueManager) Stop() {
func (t *QueueManager) StoreSeries(series []tsdb.RefSeries, index int) { func (t *QueueManager) StoreSeries(series []tsdb.RefSeries, index int) {
for _, s := range series { for _, s := range series {
ls := processExternalLabels(s.Labels, t.externalLabels) ls := processExternalLabels(s.Labels, t.externalLabels)
rl := relabel.Process(ls, t.relabelConfigs...) lbls := relabel.Process(ls, t.relabelConfigs...)
if len(rl) == 0 { if len(lbls) == 0 {
t.droppedSeries[s.Ref] = struct{}{} t.droppedSeries[s.Ref] = struct{}{}
continue continue
} }
t.seriesSegmentIndexes[s.Ref] = index t.seriesSegmentIndexes[s.Ref] = index
labels := labelsToLabelsProto(rl) internLabels(lbls)
// We should not ever be replacing a series labels in the map, but just // 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 // in case we do we need to ensure we do not leak the replaced interned
// strings. // strings.
if orig, ok := t.seriesLabels[s.Ref]; ok { 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 { for k, v := range t.seriesSegmentIndexes {
if v < index { if v < index {
delete(t.seriesSegmentIndexes, k) delete(t.seriesSegmentIndexes, k)
release(t.seriesLabels[k]) releaseLabels(t.seriesLabels[k])
delete(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 { for _, l := range ls {
interner.release(l.Name) interner.release(l.Name)
interner.release(l.Value) interner.release(l.Value)