2024-03-04 22:45:22 +00:00
|
|
|
package streamaggr
|
|
|
|
|
|
|
|
import (
|
2024-07-01 12:56:17 +00:00
|
|
|
"fmt"
|
2024-03-05 00:13:21 +00:00
|
|
|
"slices"
|
2024-03-04 22:45:22 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2024-03-05 00:13:21 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2024-03-04 22:45:22 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deduplicator deduplicates samples per each time series.
|
|
|
|
type Deduplicator struct {
|
|
|
|
da *dedupAggr
|
|
|
|
|
2024-03-05 00:13:21 +00:00
|
|
|
dropLabels []string
|
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
wg sync.WaitGroup
|
|
|
|
stopCh chan struct{}
|
|
|
|
|
|
|
|
ms *metrics.Set
|
2024-03-05 00:13:21 +00:00
|
|
|
|
|
|
|
dedupFlushDuration *metrics.Histogram
|
|
|
|
dedupFlushTimeouts *metrics.Counter
|
2024-03-04 22:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeduplicator returns new deduplicator, which deduplicates samples per each time series.
|
|
|
|
//
|
|
|
|
// The de-duplicated samples are passed to pushFunc once per dedupInterval.
|
|
|
|
//
|
2024-03-05 00:13:21 +00:00
|
|
|
// An optional dropLabels list may contain label names, which must be dropped before de-duplicating samples.
|
|
|
|
// Common case is to drop `replica`-like labels from samples received from HA datasources.
|
|
|
|
//
|
2024-03-04 22:45:22 +00:00
|
|
|
// MustStop must be called on the returned deduplicator in order to free up occupied resources.
|
2024-07-01 12:56:17 +00:00
|
|
|
func NewDeduplicator(pushFunc PushFunc, dedupInterval time.Duration, dropLabels []string, alias string) *Deduplicator {
|
2024-03-04 22:45:22 +00:00
|
|
|
d := &Deduplicator{
|
2024-05-08 11:10:53 +00:00
|
|
|
da: newDedupAggr(),
|
2024-03-05 00:13:21 +00:00
|
|
|
dropLabels: dropLabels,
|
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
ms: metrics.NewSet(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ms := d.ms
|
2024-07-01 12:56:17 +00:00
|
|
|
|
|
|
|
metricLabels := fmt.Sprintf(`url=%q`, alias)
|
|
|
|
_ = ms.NewGauge(fmt.Sprintf(`vm_streamaggr_dedup_state_size_bytes{%s}`, metricLabels), func() float64 {
|
2024-03-04 22:45:22 +00:00
|
|
|
return float64(d.da.sizeBytes())
|
|
|
|
})
|
2024-07-01 12:56:17 +00:00
|
|
|
_ = ms.NewGauge(fmt.Sprintf(`vm_streamaggr_dedup_state_items_count{%s}`, metricLabels), func() float64 {
|
2024-03-04 22:45:22 +00:00
|
|
|
return float64(d.da.itemsCount())
|
|
|
|
})
|
|
|
|
|
2024-07-01 12:56:17 +00:00
|
|
|
d.dedupFlushDuration = ms.GetOrCreateHistogram(fmt.Sprintf(`vm_streamaggr_dedup_flush_duration_seconds{%s}`, metricLabels))
|
|
|
|
d.dedupFlushTimeouts = ms.GetOrCreateCounter(fmt.Sprintf(`vm_streamaggr_dedup_flush_timeouts_total{%s}`, metricLabels))
|
2024-03-05 00:13:21 +00:00
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
metrics.RegisterSet(ms)
|
|
|
|
|
|
|
|
d.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer d.wg.Done()
|
|
|
|
d.runFlusher(pushFunc, dedupInterval)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustStop stops d.
|
|
|
|
func (d *Deduplicator) MustStop() {
|
|
|
|
metrics.UnregisterSet(d.ms)
|
|
|
|
d.ms = nil
|
|
|
|
|
|
|
|
close(d.stopCh)
|
|
|
|
d.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes tss to d.
|
|
|
|
func (d *Deduplicator) Push(tss []prompbmarshal.TimeSeries) {
|
|
|
|
ctx := getDeduplicatorPushCtx()
|
|
|
|
pss := ctx.pss
|
2024-03-05 00:13:21 +00:00
|
|
|
labels := &ctx.labels
|
2024-03-04 22:45:22 +00:00
|
|
|
buf := ctx.buf
|
|
|
|
|
2024-03-05 00:13:21 +00:00
|
|
|
dropLabels := d.dropLabels
|
2024-03-04 22:45:22 +00:00
|
|
|
for _, ts := range tss {
|
2024-03-05 00:13:21 +00:00
|
|
|
if len(dropLabels) > 0 {
|
|
|
|
labels.Labels = dropSeriesLabels(labels.Labels[:0], ts.Labels, dropLabels)
|
|
|
|
} else {
|
|
|
|
labels.Labels = append(labels.Labels[:0], ts.Labels...)
|
|
|
|
}
|
|
|
|
if len(labels.Labels) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
labels.Sort()
|
|
|
|
|
2024-06-07 14:24:09 +00:00
|
|
|
bufLen := len(buf)
|
|
|
|
buf = lc.Compress(buf, labels.Labels)
|
|
|
|
key := bytesutil.ToUnsafeString(buf[bufLen:])
|
2024-03-04 22:45:22 +00:00
|
|
|
for _, s := range ts.Samples {
|
|
|
|
pss = append(pss, pushSample{
|
2024-03-12 21:47:29 +00:00
|
|
|
key: key,
|
|
|
|
value: s.Value,
|
|
|
|
timestamp: s.Timestamp,
|
2024-03-04 22:45:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.da.pushSamples(pss)
|
|
|
|
|
|
|
|
ctx.pss = pss
|
|
|
|
ctx.buf = buf
|
|
|
|
putDeduplicatorPushCtx(ctx)
|
|
|
|
}
|
|
|
|
|
2024-03-05 00:13:21 +00:00
|
|
|
func dropSeriesLabels(dst, src []prompbmarshal.Label, labelNames []string) []prompbmarshal.Label {
|
|
|
|
for _, label := range src {
|
|
|
|
if !slices.Contains(labelNames, label.Name) {
|
|
|
|
dst = append(dst, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
func (d *Deduplicator) runFlusher(pushFunc PushFunc, dedupInterval time.Duration) {
|
|
|
|
t := time.NewTicker(dedupInterval)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-d.stopCh:
|
|
|
|
return
|
|
|
|
case <-t.C:
|
2024-03-05 00:13:21 +00:00
|
|
|
d.flush(pushFunc, dedupInterval)
|
2024-03-04 22:45:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 00:13:21 +00:00
|
|
|
func (d *Deduplicator) flush(pushFunc PushFunc, dedupInterval time.Duration) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
timestamp := startTime.UnixMilli()
|
2024-03-04 22:45:22 +00:00
|
|
|
d.da.flush(func(pss []pushSample) {
|
|
|
|
ctx := getDeduplicatorFlushCtx()
|
|
|
|
|
|
|
|
tss := ctx.tss
|
|
|
|
labels := ctx.labels
|
|
|
|
samples := ctx.samples
|
|
|
|
for _, ps := range pss {
|
|
|
|
labelsLen := len(labels)
|
2024-05-08 11:10:53 +00:00
|
|
|
labels = decompressLabels(labels, ps.key)
|
2024-03-04 22:45:22 +00:00
|
|
|
|
|
|
|
samplesLen := len(samples)
|
|
|
|
samples = append(samples, prompbmarshal.Sample{
|
|
|
|
Value: ps.value,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
})
|
|
|
|
|
|
|
|
tss = append(tss, prompbmarshal.TimeSeries{
|
|
|
|
Labels: labels[labelsLen:],
|
|
|
|
Samples: samples[samplesLen:],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
pushFunc(tss)
|
|
|
|
|
|
|
|
ctx.tss = tss
|
|
|
|
ctx.labels = labels
|
|
|
|
ctx.samples = samples
|
|
|
|
putDeduplicatorFlushCtx(ctx)
|
2024-06-10 12:29:45 +00:00
|
|
|
})
|
2024-03-05 00:13:21 +00:00
|
|
|
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
d.dedupFlushDuration.Update(duration.Seconds())
|
|
|
|
if duration > dedupInterval {
|
|
|
|
d.dedupFlushTimeouts.Inc()
|
|
|
|
logger.Warnf("deduplication couldn't be finished in the configured dedupInterval=%s; it took %.03fs; "+
|
|
|
|
"possible solutions: increase dedupInterval; reduce samples' ingestion rate", dedupInterval, duration.Seconds())
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type deduplicatorPushCtx struct {
|
2024-03-05 00:13:21 +00:00
|
|
|
pss []pushSample
|
|
|
|
labels promutils.Labels
|
|
|
|
buf []byte
|
2024-03-04 22:45:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *deduplicatorPushCtx) reset() {
|
|
|
|
clear(ctx.pss)
|
|
|
|
ctx.pss = ctx.pss[:0]
|
|
|
|
|
2024-03-05 00:13:21 +00:00
|
|
|
ctx.labels.Reset()
|
|
|
|
|
2024-03-04 22:45:22 +00:00
|
|
|
ctx.buf = ctx.buf[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeduplicatorPushCtx() *deduplicatorPushCtx {
|
|
|
|
v := deduplicatorPushCtxPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &deduplicatorPushCtx{}
|
|
|
|
}
|
|
|
|
return v.(*deduplicatorPushCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putDeduplicatorPushCtx(ctx *deduplicatorPushCtx) {
|
|
|
|
ctx.reset()
|
|
|
|
deduplicatorPushCtxPool.Put(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
var deduplicatorPushCtxPool sync.Pool
|
|
|
|
|
|
|
|
type deduplicatorFlushCtx struct {
|
|
|
|
tss []prompbmarshal.TimeSeries
|
|
|
|
labels []prompbmarshal.Label
|
|
|
|
samples []prompbmarshal.Sample
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *deduplicatorFlushCtx) reset() {
|
|
|
|
clear(ctx.tss)
|
|
|
|
ctx.tss = ctx.tss[:0]
|
|
|
|
|
|
|
|
clear(ctx.labels)
|
|
|
|
ctx.labels = ctx.labels[:0]
|
|
|
|
|
|
|
|
clear(ctx.samples)
|
|
|
|
ctx.samples = ctx.samples[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeduplicatorFlushCtx() *deduplicatorFlushCtx {
|
|
|
|
v := deduplicatorFlushCtxPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &deduplicatorFlushCtx{}
|
|
|
|
}
|
|
|
|
return v.(*deduplicatorFlushCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putDeduplicatorFlushCtx(ctx *deduplicatorFlushCtx) {
|
|
|
|
ctx.reset()
|
|
|
|
deduplicatorFlushCtxPool.Put(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
var deduplicatorFlushCtxPool sync.Pool
|