Skip to content

Commit

Permalink
reduce the buff
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-jeanne committed Dec 9, 2024
1 parent f122ac3 commit 1570629
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 12 deletions.
3 changes: 1 addition & 2 deletions pkg/costattribution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func (m *Manager) running(ctx context.Context) error {
if m == nil {
return nil
}
currentTime := time.Now()
t := time.NewTicker(m.cleanupInterval)
defer t.Stop()

Expand All @@ -69,7 +68,7 @@ func (m *Manager) running(ctx context.Context) error {
for {
select {
case <-t.C:
err := m.purgeInactiveAttributionsUntil(currentTime.Add(-m.inactiveTimeout).Unix())
err := m.purgeInactiveAttributionsUntil(time.Now().Add(-m.inactiveTimeout).Unix())
if err != nil {
return err
}
Expand Down
41 changes: 31 additions & 10 deletions pkg/costattribution/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package costattribution

import (
"bytes"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -31,6 +32,7 @@ const (
type Tracker struct {
userID string
caLabels []string
caLabelMap map[string]int
maxCardinality int
activeSeriesPerUserAttribution *prometheus.GaugeVec
receivedSamplesAttribution *prometheus.CounterVec
Expand All @@ -53,9 +55,14 @@ func newTracker(userID string, trackedLabels []string, limit int, cooldown time.
sort.Slice(trackedLabels, func(i, j int) bool {
return trackedLabels[i] < trackedLabels[j]
})
caLabelMap := make(map[string]int, len(trackedLabels))
for i, label := range trackedLabels {
caLabelMap[label] = i
}
m := &Tracker{
userID: userID,
caLabels: trackedLabels,
caLabelMap: caLabelMap,
maxCardinality: limit,
obseveredMtx: sync.RWMutex{},
observed: map[string]*Observation{},
Expand Down Expand Up @@ -113,6 +120,12 @@ func (t *Tracker) CooldownDuration() int64 {
return t.cooldownDuration
}

var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}

// sep is used to separate the labels in the key, it is not a valid label caracter
const sep = rune(0x80)

Expand Down Expand Up @@ -199,33 +212,42 @@ func (t *Tracker) updateCounters(lbls labels.Labels, ts int64, activeSeriesIncre
if t == nil {
return
}

labelValues := make([]string, len(t.caLabels)+1)
for i, label := range t.caLabels {
labelValues[i] = lbls.Get(label)
lbls.Range(func(l labels.Label) {
if idx, ok := t.caLabelMap[l.Name]; ok {
labelValues[idx] = l.Value
}
})
labelValues[len(labelValues)-1] = t.userID
for i := 0; i < len(labelValues)-1; i++ {
if labelValues[i] == "" {
labelValues[i] = missingValue
}
}
labelValues[len(labelValues)-1] = t.userID

var sb strings.Builder
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf)

for i, value := range labelValues[:len(labelValues)-1] {
if i > 0 {
sb.WriteRune(sep)
buf.WriteRune(sep)
}
sb.WriteString(value)
buf.WriteString(value)
}

t.updateOverflow(sb.String(), ts, activeSeriesIncrement, receviedSampleIncrement, discardedSampleIncrement, reason)
t.obseveredMtx.Lock()
defer t.obseveredMtx.Unlock()

t.updateOverflow(buf.String(), ts, activeSeriesIncrement, receviedSampleIncrement, discardedSampleIncrement, reason)
}

func (t *Tracker) updateOverflow(stream string, ts int64, activeSeriesIncrement, receviedSampleIncrement, discardedSampleIncrement int64, reason *string) {
if t == nil {
return
}

t.obseveredMtx.Lock()
// we store up to 2 * maxCardinality observations, if we have seen the stream before, we update the last update time
if o, known := t.observed[stream]; known && o.lastUpdate != nil {
if o.lastUpdate.Load() < ts {
o.lastUpdate.Store(ts)
Expand Down Expand Up @@ -255,7 +277,6 @@ func (t *Tracker) updateOverflow(stream string, ts int64, activeSeriesIncrement,
t.observed[stream].discardSamplemu.Unlock()
}
}
t.obseveredMtx.Unlock()

// If the maximum cardinality is hit all streams become `__overflow__`, the function would return true.
// the origin labels ovserved time is not updated, but the overflow hash is updated.
Expand Down
90 changes: 90 additions & 0 deletions pkg/distributor/allcase.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
goos: darwin
goarch: amd64
pkg: github.com/grafana/mimir/pkg/distributor
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 637 1853179 ns/op 166418 B/op 79 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 639 1895205 ns/op 165412 B/op 78 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 606 1892974 ns/op 163882 B/op 79 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 639 1907549 ns/op 167614 B/op 82 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 624 1884032 ns/op 164782 B/op 79 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_successfully_pushed-8 607 1887582 ns/op 166831 B/op 78 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1236 929959 ns/op 2508 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1281 982885 ns/op 2470 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1267 933902 ns/op 2442 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1216 936387 ns/op 2480 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1278 933974 ns/op 2499 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=ingestion_rate_limit_reached-8 1214 929894 ns/op 2361 B/op 43 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 196 6103227 ns/op 1201367 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 196 6092985 ns/op 1202556 B/op 5058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 199 5986432 ns/op 1201042 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 198 5930995 ns/op 1200964 B/op 5058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 196 5947211 ns/op 1202236 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=too_many_labels_limit_reached-8 198 6130152 ns/op 1201279 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 298 3970543 ns/op 1137518 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 294 4233587 ns/op 1136289 B/op 5056 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 270 4462189 ns/op 1136907 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 212 8484710 ns/op 1138269 B/op 5058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 138 8016180 ns/op 1137486 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_name_length_limit_reached-8 300 4011353 ns/op 1136971 B/op 5057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 187 6466417 ns/op 1218690 B/op 6057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 219 5879012 ns/op 1218969 B/op 6058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 219 5297183 ns/op 1216897 B/op 6057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 225 5271546 ns/op 1218623 B/op 6058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 223 5292385 ns/op 1218010 B/op 6058 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=max_label_value_length_limit_reached-8 224 5264725 ns/op 1217656 B/op 6057 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 679 1728152 ns/op 324954 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 666 1735820 ns/op 324749 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 642 1762658 ns/op 324898 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 660 1756348 ns/op 325070 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 651 1826077 ns/op 324740 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=timestamp_too_new-8 678 1748920 ns/op 324890 B/op 4054 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 296 3914359 ns/op 1566607 B/op 7090 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 302 3929262 ns/op 1564399 B/op 7091 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 296 3945855 ns/op 1575166 B/op 7097 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 283 3969611 ns/op 1563748 B/op 7088 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 309 3923366 ns/op 1566517 B/op 7092 allocs/op
BenchmarkDistributor_Push/cost_attribution=disabled/scenario=all_samples_go_to_metric_relabel_configs-8 304 3883719 ns/op 1567272 B/op 7091 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 546 2160785 ns/op 206548 B/op 2081 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 522 2198887 ns/op 206315 B/op 2079 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 553 2203611 ns/op 207243 B/op 2081 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 536 2183919 ns/op 206614 B/op 2079 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 529 2197734 ns/op 206679 B/op 2080 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_successfully_pushed-8 526 2390356 ns/op 208231 B/op 2083 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1134 1021807 ns/op 2511 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1042 1100917 ns/op 2390 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1124 2202501 ns/op 2586 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1197 994139 ns/op 2535 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1208 1193533 ns/op 2566 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=ingestion_rate_limit_reached-8 1155 980992 ns/op 2543 B/op 46 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 182 6460385 ns/op 1249816 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 186 6477713 ns/op 1249483 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 184 6418529 ns/op 1249053 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 187 6438986 ns/op 1251058 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 184 6596688 ns/op 1250124 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=too_many_labels_limit_reached-8 184 6355415 ns/op 1249171 B/op 8058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 270 4314450 ns/op 1185126 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 270 4359515 ns/op 1185189 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 266 4713813 ns/op 1185453 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 192 7228030 ns/op 1184618 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 277 4415891 ns/op 1185916 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_name_length_limit_reached-8 273 4381573 ns/op 1185519 B/op 8057 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 205 5639131 ns/op 1266525 B/op 9058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 199 5855152 ns/op 1266610 B/op 9058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 205 5819444 ns/op 1265659 B/op 9058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 208 5725823 ns/op 1266413 B/op 9058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 174 5847499 ns/op 1267865 B/op 9059 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=max_label_value_length_limit_reached-8 208 6097036 ns/op 1266590 B/op 9058 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 541 2116989 ns/op 373316 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 538 2106373 ns/op 373357 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 540 2082513 ns/op 372600 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 538 2081507 ns/op 372469 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 564 2075526 ns/op 372886 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=timestamp_too_new-8 537 2084192 ns/op 372905 B/op 7054 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 283 4218062 ns/op 1605597 B/op 9089 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 279 4221775 ns/op 1605400 B/op 9090 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 285 4162809 ns/op 1608485 B/op 9089 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 282 4246972 ns/op 1604051 B/op 9092 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 286 4189918 ns/op 1605391 B/op 9088 allocs/op
BenchmarkDistributor_Push/cost_attribution=enabled/scenario=all_samples_go_to_metric_relabel_configs-8 290 4179196 ns/op 1602168 B/op 9087 allocs/op
PASS
ok github.com/grafana/mimir/pkg/distributor 389.802s

0 comments on commit 1570629

Please sign in to comment.