2023-01-04 06:19:18 +00:00
|
|
|
package streamaggr
|
|
|
|
|
|
|
|
import (
|
2023-07-21 04:44:21 +00:00
|
|
|
"math"
|
2023-01-04 06:19:18 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-06-07 14:24:09 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2023-01-04 06:19:18 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
// histogramBucketAggrState calculates output=histogram_bucket, e.g. VictoriaMetrics histogram over input samples.
|
2023-01-04 06:19:18 +00:00
|
|
|
type histogramBucketAggrState struct {
|
|
|
|
m sync.Map
|
|
|
|
|
2023-07-21 04:44:21 +00:00
|
|
|
stalenessSecs uint64
|
2023-01-04 06:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type histogramBucketStateValue struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
h metrics.Histogram
|
|
|
|
deleteDeadline uint64
|
|
|
|
deleted bool
|
|
|
|
}
|
|
|
|
|
2023-07-20 14:07:33 +00:00
|
|
|
func newHistogramBucketAggrState(stalenessInterval time.Duration) *histogramBucketAggrState {
|
2023-07-21 04:44:21 +00:00
|
|
|
stalenessSecs := roundDurationToSecs(stalenessInterval)
|
2023-01-04 06:19:18 +00:00
|
|
|
return &histogramBucketAggrState{
|
2023-07-21 04:44:21 +00:00
|
|
|
stalenessSecs: stalenessSecs,
|
2023-01-04 06:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
func (as *histogramBucketAggrState) pushSamples(samples []pushSample) {
|
2023-01-04 06:19:18 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2023-07-21 04:44:21 +00:00
|
|
|
deleteDeadline := currentTime + as.stalenessSecs
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
for i := range samples {
|
|
|
|
s := &samples[i]
|
|
|
|
outputKey := getOutputKey(s.key)
|
2023-01-04 06:19:18 +00:00
|
|
|
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
again:
|
|
|
|
v, ok := as.m.Load(outputKey)
|
|
|
|
if !ok {
|
|
|
|
// The entry is missing in the map. Try creating it.
|
|
|
|
v = &histogramBucketStateValue{}
|
2024-06-07 14:24:09 +00:00
|
|
|
outputKey = bytesutil.InternString(outputKey)
|
|
|
|
vNew, loaded := as.m.LoadOrStore(outputKey, v)
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
if loaded {
|
|
|
|
// Use the entry created by a concurrent goroutine.
|
|
|
|
v = vNew
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sv := v.(*histogramBucketStateValue)
|
|
|
|
sv.mu.Lock()
|
|
|
|
deleted := sv.deleted
|
|
|
|
if !deleted {
|
|
|
|
sv.h.Update(s.value)
|
|
|
|
sv.deleteDeadline = deleteDeadline
|
|
|
|
}
|
|
|
|
sv.mu.Unlock()
|
|
|
|
if deleted {
|
2024-03-03 23:21:39 +00:00
|
|
|
// The entry has been deleted by the concurrent call to flushState
|
lib/streamaggr: huge pile of changes
- Reduce memory usage by up to 5x when de-duplicating samples across big number of time series.
- Reduce memory usage by up to 5x when aggregating across big number of output time series.
- Add lib/promutils.LabelsCompressor, which is going to be used by other VictoriaMetrics components
for reducing memory usage for marshaled []prompbmarshal.Label.
- Add `dedup_interval` option at aggregation config, which allows setting individual
deduplication intervals per each aggregation.
- Add `keep_metric_names` option at aggregation config, which allows keeping the original
metric names in the output samples.
- Add `unique_samples` output, which counts the number of unique sample values.
- Add `increase_prometheus` and `total_prometheus` outputs, which ignore the first sample
per each newly encountered time series.
- Use 64-bit hashes instead of marshaled labels as map keys when calculating `count_series` output.
This makes obsolete https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5579
- Expose various metrics, which may help debugging stream aggregation:
- vm_streamaggr_dedup_state_size_bytes - the size of data structures responsible for deduplication
- vm_streamaggr_dedup_state_items_count - the number of items in the deduplication data structures
- vm_streamaggr_labels_compressor_size_bytes - the size of labels compressor data structures
- vm_streamaggr_labels_compressor_items_count - the number of entries in the labels compressor
- vm_streamaggr_flush_duration_seconds - a histogram, which shows the duration of stream aggregation flushes
- vm_streamaggr_dedup_flush_duration_seconds - a histogram, which shows the duration of deduplication flushes
- vm_streamaggr_flush_timeouts_total - counter for timed out stream aggregation flushes,
which took longer than the configured interval
- vm_streamaggr_dedup_flush_timeouts_total - counter for timed out deduplication flushes,
which took longer than the configured dedup_interval
- Actualize docs/stream-aggregation.md
The memory usage reduction increases CPU usage during stream aggregation by up to 30%.
This commit is based on https://github.com/VictoriaMetrics/VictoriaMetrics/pull/5850
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5898
2024-03-02 00:42:26 +00:00
|
|
|
// Try obtaining and updating the entry again.
|
|
|
|
goto again
|
2023-01-04 06:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-01 12:56:17 +00:00
|
|
|
func (as *histogramBucketAggrState) removeOldEntries(ctx *flushCtx, currentTime uint64) {
|
2023-01-04 06:19:18 +00:00
|
|
|
m := &as.m
|
2024-07-01 12:56:17 +00:00
|
|
|
var staleOutputSamples int
|
2023-01-04 06:19:18 +00:00
|
|
|
m.Range(func(k, v interface{}) bool {
|
|
|
|
sv := v.(*histogramBucketStateValue)
|
|
|
|
|
|
|
|
sv.mu.Lock()
|
|
|
|
deleted := currentTime > sv.deleteDeadline
|
|
|
|
if deleted {
|
|
|
|
// Mark the current entry as deleted
|
|
|
|
sv.deleted = deleted
|
2024-07-01 12:56:17 +00:00
|
|
|
staleOutputSamples++
|
2023-01-04 06:19:18 +00:00
|
|
|
}
|
|
|
|
sv.mu.Unlock()
|
|
|
|
|
|
|
|
if deleted {
|
|
|
|
m.Delete(k)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2024-07-01 12:56:17 +00:00
|
|
|
ctx.a.staleOutputSamples["histogram_bucket"].Add(staleOutputSamples)
|
2023-01-04 06:19:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 12:56:17 +00:00
|
|
|
func (as *histogramBucketAggrState) flushState(ctx *flushCtx, _ bool) {
|
2023-01-04 06:19:18 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
|
|
|
currentTimeMsec := int64(currentTime) * 1000
|
|
|
|
|
2024-07-01 12:56:17 +00:00
|
|
|
as.removeOldEntries(ctx, currentTime)
|
2023-01-04 06:19:18 +00:00
|
|
|
|
|
|
|
m := &as.m
|
|
|
|
m.Range(func(k, v interface{}) bool {
|
|
|
|
sv := v.(*histogramBucketStateValue)
|
|
|
|
sv.mu.Lock()
|
|
|
|
if !sv.deleted {
|
|
|
|
key := k.(string)
|
|
|
|
sv.h.VisitNonZeroBuckets(func(vmrange string, count uint64) {
|
|
|
|
ctx.appendSeriesWithExtraLabel(key, "histogram_bucket", currentTimeMsec, float64(count), "vmrange", vmrange)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sv.mu.Unlock()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2023-07-21 04:44:21 +00:00
|
|
|
|
|
|
|
func roundDurationToSecs(d time.Duration) uint64 {
|
|
|
|
if d < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
secs := d.Seconds()
|
|
|
|
return uint64(math.Ceil(secs))
|
|
|
|
}
|