2023-01-04 07:19:18 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2023-03-29 18:05:58 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2023-04-01 06:27:45 +02:00
|
|
|
"sync/atomic"
|
2023-01-04 07:19:18 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2023-03-29 18:05:58 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
2023-01-04 07:19:18 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/streamaggr"
|
2023-03-29 18:05:58 +02:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
2023-01-04 07:19:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
streamAggrConfig = flag.String("streamAggr.config", "", "Optional path to file with stream aggregation config. "+
|
2023-01-04 07:40:00 +01:00
|
|
|
"See https://docs.victoriametrics.com/stream-aggregation.html . "+
|
2023-07-25 01:44:09 +02:00
|
|
|
"See also -streamAggr.keepInput, -streamAggr.dropInput and -streamAggr.dedupInterval")
|
|
|
|
streamAggrKeepInput = flag.Bool("streamAggr.keepInput", false, "Whether to keep all the input samples after the aggregation with -streamAggr.config. "+
|
|
|
|
"By default, only aggregated samples are dropped, while the remaining samples are stored in the database. "+
|
|
|
|
"See also -streamAggr.dropInput and https://docs.victoriametrics.com/stream-aggregation.html")
|
|
|
|
streamAggrDropInput = flag.Bool("streamAggr.dropInput", false, "Whether to drop all the input samples after the aggregation with -streamAggr.config. "+
|
|
|
|
"By default, only aggregated samples are dropped, while the remaining samples are stored in the database. "+
|
|
|
|
"See also -streamAggr.keepInput and https://docs.victoriametrics.com/stream-aggregation.html")
|
2023-01-25 18:14:49 +01:00
|
|
|
streamAggrDedupInterval = flag.Duration("streamAggr.dedupInterval", 0, "Input samples are de-duplicated with this interval before being aggregated. "+
|
|
|
|
"Only the last sample per each time series per each interval is aggregated if the interval is greater than zero")
|
2023-01-04 07:19:18 +01:00
|
|
|
)
|
|
|
|
|
2023-03-29 18:05:58 +02:00
|
|
|
var (
|
2023-07-20 15:07:10 +02:00
|
|
|
saCfgReloaderStopCh chan struct{}
|
2023-04-01 06:27:45 +02:00
|
|
|
saCfgReloaderWG sync.WaitGroup
|
2023-03-29 18:05:58 +02:00
|
|
|
|
|
|
|
saCfgReloads = metrics.NewCounter(`vminsert_streamagg_config_reloads_total`)
|
|
|
|
saCfgReloadErr = metrics.NewCounter(`vminsert_streamagg_config_reloads_errors_total`)
|
|
|
|
saCfgSuccess = metrics.NewCounter(`vminsert_streamagg_config_last_reload_successful`)
|
|
|
|
saCfgTimestamp = metrics.NewCounter(`vminsert_streamagg_config_last_reload_success_timestamp_seconds`)
|
|
|
|
|
2023-04-01 06:27:45 +02:00
|
|
|
sasGlobal atomic.Pointer[streamaggr.Aggregators]
|
2023-03-29 18:05:58 +02:00
|
|
|
)
|
|
|
|
|
2023-04-01 06:27:45 +02:00
|
|
|
// CheckStreamAggrConfig checks config pointed by -stramaggr.config
|
|
|
|
func CheckStreamAggrConfig() error {
|
|
|
|
if *streamAggrConfig == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pushNoop := func(tss []prompbmarshal.TimeSeries) {}
|
|
|
|
sas, err := streamaggr.LoadFromFile(*streamAggrConfig, pushNoop, *streamAggrDedupInterval)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error when loading -streamAggr.config=%q: %w", *streamAggrConfig, err)
|
|
|
|
}
|
|
|
|
sas.MustStop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:19:18 +01:00
|
|
|
// InitStreamAggr must be called after flag.Parse and before using the common package.
|
|
|
|
//
|
|
|
|
// MustStopStreamAggr must be called when stream aggr is no longer needed.
|
|
|
|
func InitStreamAggr() {
|
2023-07-20 15:07:10 +02:00
|
|
|
saCfgReloaderStopCh = make(chan struct{})
|
2023-01-04 07:19:18 +01:00
|
|
|
if *streamAggrConfig == "" {
|
|
|
|
return
|
|
|
|
}
|
2023-03-29 18:05:58 +02:00
|
|
|
|
|
|
|
sighupCh := procutil.NewSighupChan()
|
|
|
|
|
2023-04-01 06:27:45 +02:00
|
|
|
sas, err := streamaggr.LoadFromFile(*streamAggrConfig, pushAggregateSeries, *streamAggrDedupInterval)
|
2023-01-04 07:19:18 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("cannot load -streamAggr.config=%q: %s", *streamAggrConfig, err)
|
|
|
|
}
|
2023-04-01 06:27:45 +02:00
|
|
|
sasGlobal.Store(sas)
|
2023-03-29 18:05:58 +02:00
|
|
|
saCfgSuccess.Set(1)
|
|
|
|
saCfgTimestamp.Set(fasttime.UnixTimestamp())
|
|
|
|
|
|
|
|
// Start config reloader.
|
2023-04-01 06:27:45 +02:00
|
|
|
saCfgReloaderWG.Add(1)
|
2023-03-29 18:05:58 +02:00
|
|
|
go func() {
|
2023-04-01 06:27:45 +02:00
|
|
|
defer saCfgReloaderWG.Done()
|
2023-03-29 18:05:58 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sighupCh:
|
2023-04-01 06:27:45 +02:00
|
|
|
case <-saCfgReloaderStopCh:
|
2023-03-29 18:05:58 +02:00
|
|
|
return
|
|
|
|
}
|
2023-04-01 06:27:45 +02:00
|
|
|
reloadStreamAggrConfig()
|
2023-03-29 18:05:58 +02:00
|
|
|
}
|
|
|
|
}()
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
|
|
|
|
2023-04-01 06:27:45 +02:00
|
|
|
func reloadStreamAggrConfig() {
|
|
|
|
logger.Infof("reloading -streamAggr.config=%q", *streamAggrConfig)
|
|
|
|
saCfgReloads.Inc()
|
|
|
|
|
|
|
|
sasNew, err := streamaggr.LoadFromFile(*streamAggrConfig, pushAggregateSeries, *streamAggrDedupInterval)
|
|
|
|
if err != nil {
|
|
|
|
saCfgSuccess.Set(0)
|
|
|
|
saCfgReloadErr.Inc()
|
|
|
|
logger.Errorf("cannot reload -streamAggr.config=%q: use the previously loaded config; error: %s", *streamAggrConfig, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sas := sasGlobal.Load()
|
|
|
|
if !sasNew.Equal(sas) {
|
|
|
|
sasOld := sasGlobal.Swap(sasNew)
|
|
|
|
sasOld.MustStop()
|
|
|
|
logger.Infof("successfully reloaded stream aggregation config at -streamAggr.config=%q", *streamAggrConfig)
|
|
|
|
} else {
|
|
|
|
logger.Infof("nothing changed in -streamAggr.config=%q", *streamAggrConfig)
|
|
|
|
sasNew.MustStop()
|
|
|
|
}
|
|
|
|
saCfgSuccess.Set(1)
|
|
|
|
saCfgTimestamp.Set(fasttime.UnixTimestamp())
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:19:18 +01:00
|
|
|
// MustStopStreamAggr stops stream aggregators.
|
|
|
|
func MustStopStreamAggr() {
|
2023-04-01 06:27:45 +02:00
|
|
|
close(saCfgReloaderStopCh)
|
|
|
|
saCfgReloaderWG.Wait()
|
2023-03-29 18:05:58 +02:00
|
|
|
|
2023-04-01 06:27:45 +02:00
|
|
|
sas := sasGlobal.Swap(nil)
|
|
|
|
sas.MustStop()
|
2023-03-29 18:05:58 +02:00
|
|
|
}
|
2023-01-04 07:19:18 +01:00
|
|
|
|
|
|
|
type streamAggrCtx struct {
|
|
|
|
mn storage.MetricName
|
|
|
|
tss [1]prompbmarshal.TimeSeries
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *streamAggrCtx) Reset() {
|
|
|
|
ctx.mn.Reset()
|
|
|
|
ts := &ctx.tss[0]
|
|
|
|
promrelabel.CleanLabels(ts.Labels)
|
|
|
|
}
|
|
|
|
|
2023-07-25 01:44:09 +02:00
|
|
|
func (ctx *streamAggrCtx) push(mrs []storage.MetricRow, matchIdxs []byte) []byte {
|
|
|
|
matchIdxs = bytesutil.ResizeNoCopyMayOverallocate(matchIdxs, len(mrs))
|
|
|
|
for i := 0; i < len(matchIdxs); i++ {
|
|
|
|
matchIdxs[i] = 0
|
|
|
|
}
|
|
|
|
|
2023-01-04 07:19:18 +01:00
|
|
|
mn := &ctx.mn
|
|
|
|
tss := ctx.tss[:]
|
|
|
|
ts := &tss[0]
|
|
|
|
labels := ts.Labels
|
|
|
|
samples := ts.Samples
|
2023-04-01 06:27:45 +02:00
|
|
|
sas := sasGlobal.Load()
|
2023-07-25 01:44:09 +02:00
|
|
|
var matchIdxsLocal []byte
|
|
|
|
for idx, mr := range mrs {
|
2023-01-04 07:19:18 +01:00
|
|
|
if err := mn.UnmarshalRaw(mr.MetricNameRaw); err != nil {
|
|
|
|
logger.Panicf("BUG: cannot unmarshal recently marshaled MetricName: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels = append(labels[:0], prompbmarshal.Label{
|
|
|
|
Name: "__name__",
|
|
|
|
Value: bytesutil.ToUnsafeString(mn.MetricGroup),
|
|
|
|
})
|
|
|
|
for _, tag := range mn.Tags {
|
|
|
|
labels = append(labels, prompbmarshal.Label{
|
|
|
|
Name: bytesutil.ToUnsafeString(tag.Key),
|
|
|
|
Value: bytesutil.ToUnsafeString(tag.Value),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
samples = append(samples[:0], prompbmarshal.Sample{
|
|
|
|
Timestamp: mr.Timestamp,
|
|
|
|
Value: mr.Value,
|
|
|
|
})
|
|
|
|
|
|
|
|
ts.Labels = labels
|
|
|
|
ts.Samples = samples
|
|
|
|
|
2023-07-25 01:44:09 +02:00
|
|
|
matchIdxsLocal = sas.Push(tss, matchIdxsLocal)
|
|
|
|
if matchIdxsLocal[0] != 0 {
|
|
|
|
matchIdxs[idx] = 1
|
|
|
|
}
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
2023-07-25 01:44:09 +02:00
|
|
|
|
|
|
|
return matchIdxs
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pushAggregateSeries(tss []prompbmarshal.TimeSeries) {
|
|
|
|
currentTimestamp := int64(fasttime.UnixTimestamp()) * 1000
|
|
|
|
var ctx InsertCtx
|
|
|
|
ctx.Reset(len(tss))
|
|
|
|
ctx.skipStreamAggr = true
|
|
|
|
for _, ts := range tss {
|
|
|
|
labels := ts.Labels
|
2023-05-09 17:33:58 +02:00
|
|
|
ctx.Labels = ctx.Labels[:0]
|
2023-01-04 07:19:18 +01:00
|
|
|
for _, label := range labels {
|
|
|
|
name := label.Name
|
|
|
|
if name == "__name__" {
|
|
|
|
name = ""
|
|
|
|
}
|
|
|
|
ctx.AddLabel(name, label.Value)
|
|
|
|
}
|
|
|
|
value := ts.Samples[0].Value
|
|
|
|
if err := ctx.WriteDataPoint(nil, ctx.Labels, currentTimestamp, value); err != nil {
|
|
|
|
logger.Errorf("cannot store aggregate series: %s", err)
|
|
|
|
// Do not continue pushing the remaining samples, since it is likely they will return the same error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-01-07 07:40:07 +01:00
|
|
|
// There is no need in limiting the number of concurrent calls to vmstorage.AddRows() here,
|
|
|
|
// since the number of concurrent pushAggregateSeries() calls should be already limited by lib/streamaggr.
|
2023-01-04 07:19:18 +01:00
|
|
|
if err := vmstorage.AddRows(ctx.mrs); err != nil {
|
|
|
|
logger.Errorf("cannot flush aggregate series: %s", err)
|
|
|
|
}
|
|
|
|
}
|