2020-01-30 23:09:44 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2020-02-10 11:03:52 +00:00
|
|
|
"time"
|
2020-01-30 23:09:44 +00:00
|
|
|
)
|
|
|
|
|
2020-02-10 11:03:52 +00:00
|
|
|
// SetMinScrapeIntervalForDeduplication sets the minimum interval for data points during de-duplication.
|
|
|
|
//
|
|
|
|
// De-duplication is disabled if interval is 0.
|
|
|
|
//
|
|
|
|
// This function must be called before initializing the storage.
|
|
|
|
func SetMinScrapeIntervalForDeduplication(interval time.Duration) {
|
2020-04-26 10:04:58 +00:00
|
|
|
minScrapeInterval = interval.Milliseconds()
|
2020-02-10 11:03:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 10:04:58 +00:00
|
|
|
var minScrapeInterval = int64(0)
|
2020-01-30 23:09:44 +00:00
|
|
|
|
|
|
|
// DeduplicateSamples removes samples from src* if they are closer to each other than minScrapeInterval.
|
|
|
|
func DeduplicateSamples(srcTimestamps []int64, srcValues []float64) ([]int64, []float64) {
|
2020-02-10 11:03:52 +00:00
|
|
|
if minScrapeInterval <= 0 {
|
2020-01-30 23:09:44 +00:00
|
|
|
return srcTimestamps, srcValues
|
|
|
|
}
|
2020-04-26 10:04:58 +00:00
|
|
|
if !needsDedup(srcTimestamps, minScrapeInterval) {
|
2020-01-30 23:09:44 +00:00
|
|
|
// Fast path - nothing to deduplicate
|
|
|
|
return srcTimestamps, srcValues
|
|
|
|
}
|
2021-07-12 07:42:54 +00:00
|
|
|
return deduplicateInternal(minScrapeInterval, srcTimestamps, srcValues)
|
|
|
|
}
|
2020-01-30 23:09:44 +00:00
|
|
|
|
2021-07-12 07:42:54 +00:00
|
|
|
func deduplicateInternal(interval int64, srcTimestamps []int64, srcValues []float64) ([]int64, []float64) {
|
|
|
|
tsNext := (srcTimestamps[0] - srcTimestamps[0]%interval) + interval
|
2020-01-30 23:09:44 +00:00
|
|
|
dstTimestamps := srcTimestamps[:1]
|
|
|
|
dstValues := srcValues[:1]
|
|
|
|
for i := 1; i < len(srcTimestamps); i++ {
|
|
|
|
ts := srcTimestamps[i]
|
2020-04-26 10:04:58 +00:00
|
|
|
if ts < tsNext {
|
2020-01-30 23:09:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstTimestamps = append(dstTimestamps, ts)
|
|
|
|
dstValues = append(dstValues, srcValues[i])
|
2020-04-26 10:04:58 +00:00
|
|
|
|
|
|
|
// Update tsNext
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext += interval
|
2020-04-26 10:04:58 +00:00
|
|
|
if ts >= tsNext {
|
|
|
|
// Slow path for updating ts.
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext = (ts - ts%interval) + interval
|
2020-04-26 10:04:58 +00:00
|
|
|
}
|
2020-01-30 23:09:44 +00:00
|
|
|
}
|
|
|
|
return dstTimestamps, dstValues
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:36:00 +00:00
|
|
|
func deduplicateSamplesDuringMerge(srcTimestamps, srcValues []int64) ([]int64, []int64) {
|
2020-02-10 11:03:52 +00:00
|
|
|
if minScrapeInterval <= 0 {
|
2020-01-30 23:09:44 +00:00
|
|
|
return srcTimestamps, srcValues
|
|
|
|
}
|
2020-04-26 10:04:58 +00:00
|
|
|
if !needsDedup(srcTimestamps, minScrapeInterval) {
|
2020-01-30 23:09:44 +00:00
|
|
|
// Fast path - nothing to deduplicate
|
|
|
|
return srcTimestamps, srcValues
|
|
|
|
}
|
2021-07-12 07:42:54 +00:00
|
|
|
return deduplicateDuringMergeInternal(minScrapeInterval, srcTimestamps, srcValues)
|
|
|
|
}
|
2020-01-30 23:09:44 +00:00
|
|
|
|
2021-07-12 07:42:54 +00:00
|
|
|
func deduplicateDuringMergeInternal(interval int64, srcTimestamps, srcValues []int64) ([]int64, []int64) {
|
|
|
|
tsNext := (srcTimestamps[0] - srcTimestamps[0]%interval) + interval
|
2020-01-30 23:09:44 +00:00
|
|
|
dstTimestamps := srcTimestamps[:1]
|
|
|
|
dstValues := srcValues[:1]
|
|
|
|
for i := 1; i < len(srcTimestamps); i++ {
|
|
|
|
ts := srcTimestamps[i]
|
2020-04-26 10:04:58 +00:00
|
|
|
if ts < tsNext {
|
2020-01-30 23:09:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
dstTimestamps = append(dstTimestamps, ts)
|
|
|
|
dstValues = append(dstValues, srcValues[i])
|
2020-04-26 10:04:58 +00:00
|
|
|
|
|
|
|
// Update tsNext
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext += interval
|
2020-04-26 10:04:58 +00:00
|
|
|
if ts >= tsNext {
|
|
|
|
// Slow path for updating ts.
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext = (ts - ts%interval) + interval
|
2020-04-26 10:04:58 +00:00
|
|
|
}
|
2020-01-30 23:09:44 +00:00
|
|
|
}
|
|
|
|
return dstTimestamps, dstValues
|
|
|
|
}
|
|
|
|
|
2021-07-12 07:42:54 +00:00
|
|
|
func needsDedup(timestamps []int64, interval int64) bool {
|
|
|
|
if len(timestamps) == 0 || interval <= 0 {
|
2020-01-30 23:09:44 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext := (timestamps[0] - timestamps[0]%interval) + interval
|
2020-01-30 23:09:44 +00:00
|
|
|
for _, ts := range timestamps[1:] {
|
2021-07-12 07:42:54 +00:00
|
|
|
if ts < tsNext {
|
2020-01-30 23:09:44 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-07-12 07:42:54 +00:00
|
|
|
tsNext += interval
|
|
|
|
if ts >= tsNext {
|
|
|
|
tsNext = (ts - ts%interval) + interval
|
|
|
|
}
|
2020-01-30 23:09:44 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|