2021-08-31 10:52:22 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Default buffer capacity.
|
|
|
|
// `buffer.data` will only ever grow up to it's capacity and a new link
|
|
|
|
// in the buffer chain will be created if needed so that no copying
|
2021-09-07 09:21:08 +02:00
|
|
|
// of data or reallocation needs to happen on writes.
|
2021-08-31 10:52:22 +02:00
|
|
|
const (
|
2021-12-02 12:53:49 +01:00
|
|
|
BUFFER_CAP int = 512
|
2021-08-31 10:52:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// So that we can reuse allocations
|
|
|
|
var bufferPool sync.Pool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2021-10-11 10:56:38 +02:00
|
|
|
return &buffer{
|
|
|
|
data: make([]Float, 0, BUFFER_CAP),
|
|
|
|
}
|
2021-08-31 10:52:22 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:21:08 +02:00
|
|
|
var (
|
|
|
|
ErrNoData error = errors.New("no data for this metric/level")
|
|
|
|
ErrDataDoesNotAlign error = errors.New("data from lower granularities does not align")
|
|
|
|
)
|
|
|
|
|
2021-08-31 10:52:22 +02:00
|
|
|
// Each metric on each level has it's own buffer.
|
|
|
|
// This is where the actual values go.
|
2021-08-31 15:17:36 +02:00
|
|
|
// If `cap(data)` is reached, a new buffer is created and
|
|
|
|
// becomes the new head of a buffer list.
|
2021-08-31 10:52:22 +02:00
|
|
|
type buffer struct {
|
|
|
|
frequency int64 // Time between two "slots"
|
|
|
|
start int64 // Timestamp of when `data[0]` was written.
|
|
|
|
data []Float // The slice should never reallocacte as `cap(data)` is respected.
|
|
|
|
prev, next *buffer // `prev` contains older data, `next` newer data.
|
2021-11-22 17:04:09 +01:00
|
|
|
archived bool // If true, this buffer is already archived
|
2021-12-15 09:59:33 +01:00
|
|
|
|
2022-01-21 10:47:40 +01:00
|
|
|
closed bool
|
|
|
|
/*
|
|
|
|
statisticts struct {
|
|
|
|
samples int
|
|
|
|
min Float
|
|
|
|
max Float
|
|
|
|
avg Float
|
|
|
|
}
|
|
|
|
*/
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBuffer(ts, freq int64) *buffer {
|
2021-10-11 10:56:38 +02:00
|
|
|
b := bufferPool.Get().(*buffer)
|
|
|
|
b.frequency = freq
|
|
|
|
b.start = ts
|
|
|
|
b.prev = nil
|
|
|
|
b.next = nil
|
2021-11-22 17:50:15 +01:00
|
|
|
b.archived = false
|
2021-12-15 09:59:33 +01:00
|
|
|
b.closed = false
|
2021-12-02 12:53:49 +01:00
|
|
|
b.data = b.data[:0]
|
2021-10-11 10:56:38 +02:00
|
|
|
return b
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a new buffer was created, the new head is returnd.
|
|
|
|
// Otherwise, the existing buffer is returnd.
|
2021-08-31 15:17:36 +02:00
|
|
|
// Normaly, only "newer" data should be written, but if the value would
|
|
|
|
// end up in the same buffer anyways it is allowed.
|
2021-08-31 10:52:22 +02:00
|
|
|
func (b *buffer) write(ts int64, value Float) (*buffer, error) {
|
|
|
|
if ts < b.start {
|
|
|
|
return nil, errors.New("cannot write value to buffer from past")
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:57:35 +01:00
|
|
|
// When a new buffer is created, it starts at ts. If we would
|
|
|
|
// use the same index calculation as for a read here, even a very
|
|
|
|
// slight drift in the timestamps of values will cause cases where
|
|
|
|
// a cell is re-written. Adding any value smaller than half the frequency
|
|
|
|
// here creates a time buffer around the cutoff from one cell to the next
|
|
|
|
// with the same semantics as before.
|
|
|
|
idx := int((ts - b.start + (b.frequency / 3)) / b.frequency)
|
2021-08-31 10:52:22 +02:00
|
|
|
if idx >= cap(b.data) {
|
|
|
|
newbuf := newBuffer(ts, b.frequency)
|
|
|
|
newbuf.prev = b
|
|
|
|
b.next = newbuf
|
2021-12-15 09:59:33 +01:00
|
|
|
b.close()
|
2021-08-31 10:52:22 +02:00
|
|
|
b = newbuf
|
|
|
|
idx = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwriting value or writing value from past
|
|
|
|
if idx < len(b.data) {
|
|
|
|
b.data[idx] = value
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill up unwritten slots with NaN
|
|
|
|
for i := len(b.data); i < idx; i++ {
|
|
|
|
b.data = append(b.data, NaN)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.data = append(b.data, value)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 13:09:21 +01:00
|
|
|
func (b *buffer) end() int64 {
|
|
|
|
return b.start + int64(len(b.data))*b.frequency
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:47:40 +01:00
|
|
|
func (b *buffer) close() {}
|
|
|
|
|
|
|
|
/*
|
2021-12-15 09:59:33 +01:00
|
|
|
func (b *buffer) close() {
|
|
|
|
if b.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.closed = true
|
|
|
|
n, sum, min, max := 0, 0., math.MaxFloat64, -math.MaxFloat64
|
|
|
|
for _, x := range b.data {
|
|
|
|
if x.IsNaN() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
n += 1
|
|
|
|
f := float64(x)
|
|
|
|
sum += f
|
|
|
|
min = math.Min(min, f)
|
|
|
|
max = math.Max(max, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.statisticts.samples = n
|
|
|
|
if n > 0 {
|
|
|
|
b.statisticts.avg = Float(sum / float64(n))
|
|
|
|
b.statisticts.min = Float(min)
|
|
|
|
b.statisticts.max = Float(max)
|
|
|
|
} else {
|
|
|
|
b.statisticts.avg = NaN
|
|
|
|
b.statisticts.min = NaN
|
|
|
|
b.statisticts.max = NaN
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 10:47:40 +01:00
|
|
|
*/
|
2021-12-15 09:59:33 +01:00
|
|
|
|
2021-11-30 13:09:21 +01:00
|
|
|
// func interpolate(idx int, data []Float) Float {
|
|
|
|
// if idx == 0 || idx+1 == len(data) {
|
|
|
|
// return NaN
|
|
|
|
// }
|
|
|
|
// return (data[idx-1] + data[idx+1]) / 2.0
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Return all known values from `from` to `to`. Gaps of information are represented as NaN.
|
|
|
|
// Simple linear interpolation is done between the two neighboring cells if possible.
|
|
|
|
// If values at the start or end are missing, instead of NaN values, the second and thrid
|
|
|
|
// return values contain the actual `from`/`to`.
|
|
|
|
// This function goes back the buffer chain if `from` is older than the currents buffer start.
|
2021-09-07 09:21:08 +02:00
|
|
|
// The loaded values are added to `data` and `data` is returned, possibly with a shorter length.
|
|
|
|
// If `data` is not long enough to hold all values, this function will panic!
|
|
|
|
func (b *buffer) read(from, to int64, data []Float) ([]Float, int64, int64, error) {
|
2021-08-31 10:52:22 +02:00
|
|
|
if from < b.start {
|
|
|
|
if b.prev != nil {
|
2021-09-07 09:21:08 +02:00
|
|
|
return b.prev.read(from, to, data)
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
from = b.start
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:21:08 +02:00
|
|
|
var i int = 0
|
|
|
|
var t int64 = from
|
|
|
|
for ; t < to; t += b.frequency {
|
2021-08-31 10:52:22 +02:00
|
|
|
idx := int((t - b.start) / b.frequency)
|
|
|
|
if idx >= cap(b.data) {
|
2021-11-30 13:09:21 +01:00
|
|
|
if b.next == nil {
|
|
|
|
break
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
2021-11-30 13:09:21 +01:00
|
|
|
b = b.next
|
2021-08-31 10:52:22 +02:00
|
|
|
idx = 0
|
|
|
|
}
|
|
|
|
|
2021-09-20 10:29:55 +02:00
|
|
|
if idx >= len(b.data) {
|
|
|
|
if b.next == nil || to <= b.next.start {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
data[i] += NaN
|
|
|
|
} else if t < b.start {
|
2021-09-07 09:21:08 +02:00
|
|
|
data[i] += NaN
|
2021-11-30 13:09:21 +01:00
|
|
|
// } else if b.data[idx].IsNaN() {
|
|
|
|
// data[i] += interpolate(idx, b.data)
|
2021-08-31 10:52:22 +02:00
|
|
|
} else {
|
2021-09-07 09:21:08 +02:00
|
|
|
data[i] += b.data[idx]
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
2021-09-07 09:21:08 +02:00
|
|
|
i++
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:21:08 +02:00
|
|
|
return data[:i], from, t, nil
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
// Returns true if this buffer needs to be freed.
|
|
|
|
func (b *buffer) free(t int64) (delme bool, n int) {
|
|
|
|
if b.prev != nil {
|
|
|
|
delme, m := b.prev.free(t)
|
|
|
|
n += m
|
|
|
|
if delme {
|
|
|
|
b.prev.next = nil
|
|
|
|
if cap(b.prev.data) == BUFFER_CAP {
|
|
|
|
bufferPool.Put(b.prev)
|
2021-12-02 12:53:49 +01:00
|
|
|
}
|
2022-03-08 09:27:44 +01:00
|
|
|
b.prev = nil
|
2021-09-07 09:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
end := b.end()
|
|
|
|
if end < t {
|
|
|
|
return true, n + 1
|
2021-09-07 09:21:08 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
return false, n
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:04:09 +01:00
|
|
|
// Call `callback` on every buffer that contains data in the range from `from` to `to`.
|
|
|
|
func (b *buffer) iterFromTo(from, to int64, callback func(b *buffer) error) error {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.prev.iterFromTo(from, to, callback); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-30 13:09:21 +01:00
|
|
|
if from <= b.end() && b.start <= to {
|
2021-11-22 17:04:09 +01:00
|
|
|
return callback(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:52:22 +02:00
|
|
|
// Could also be called "node" as this forms a node in a tree structure.
|
|
|
|
// Called level because "node" might be confusing here.
|
2021-08-31 15:17:36 +02:00
|
|
|
// Can be both a leaf or a inner node. In this tree structue, inner nodes can
|
2021-08-31 10:52:22 +02:00
|
|
|
// also hold data (in `metrics`).
|
|
|
|
type level struct {
|
2021-09-08 10:29:36 +02:00
|
|
|
lock sync.RWMutex
|
|
|
|
metrics []*buffer // Every level can store metrics.
|
|
|
|
children map[string]*level // Lower levels.
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the correct level for the given selector, creating it if
|
|
|
|
// it does not exist. Example selector in the context of the
|
2022-01-20 10:42:44 +01:00
|
|
|
// ClusterCockpit could be: []string{ "emmy", "host123", "cpu0" }.
|
2021-08-31 10:52:22 +02:00
|
|
|
// This function would probably benefit a lot from `level.children` beeing a `sync.Map`?
|
2021-09-08 10:29:36 +02:00
|
|
|
func (l *level) findLevelOrCreate(selector []string, nMetrics int) *level {
|
2021-08-31 10:52:22 +02:00
|
|
|
if len(selector) == 0 {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2021-09-01 08:47:57 +02:00
|
|
|
// Allow concurrent reads:
|
|
|
|
l.lock.RLock()
|
2021-09-08 10:29:36 +02:00
|
|
|
var child *level
|
|
|
|
var ok bool
|
|
|
|
if l.children == nil {
|
|
|
|
// Children map needs to be created...
|
|
|
|
l.lock.RUnlock()
|
|
|
|
} else {
|
|
|
|
child, ok := l.children[selector[0]]
|
|
|
|
l.lock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return child.findLevelOrCreate(selector[1:], nMetrics)
|
|
|
|
}
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:47:57 +02:00
|
|
|
// The level does not exist, take write lock for unqiue access:
|
|
|
|
l.lock.Lock()
|
|
|
|
// While this thread waited for the write lock, another thread
|
|
|
|
// could have created the child node.
|
2021-09-08 10:29:36 +02:00
|
|
|
if l.children != nil {
|
|
|
|
child, ok = l.children[selector[0]]
|
|
|
|
if ok {
|
|
|
|
l.lock.Unlock()
|
|
|
|
return child.findLevelOrCreate(selector[1:], nMetrics)
|
|
|
|
}
|
2021-09-01 08:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
child = &level{
|
2021-09-08 10:29:36 +02:00
|
|
|
metrics: make([]*buffer, nMetrics),
|
|
|
|
children: nil,
|
2021-09-01 08:47:57 +02:00
|
|
|
}
|
2021-09-08 10:29:36 +02:00
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
if l.children != nil {
|
|
|
|
l.children[selector[0]] = child
|
|
|
|
} else {
|
|
|
|
l.children = map[string]*level{selector[0]: child}
|
|
|
|
}
|
2021-08-31 10:52:22 +02:00
|
|
|
l.lock.Unlock()
|
2021-09-08 10:29:36 +02:00
|
|
|
return child.findLevelOrCreate(selector[1:], nMetrics)
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
func (l *level) free(t int64) (int, error) {
|
|
|
|
l.lock.Lock()
|
|
|
|
defer l.lock.Unlock()
|
2021-09-08 10:29:36 +02:00
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
n := 0
|
|
|
|
for i, b := range l.metrics {
|
|
|
|
if b != nil {
|
|
|
|
delme, m := b.free(t)
|
|
|
|
n += m
|
|
|
|
if delme {
|
|
|
|
if cap(b.data) == BUFFER_CAP {
|
|
|
|
bufferPool.Put(b)
|
|
|
|
}
|
|
|
|
l.metrics[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 10:29:36 +02:00
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
for _, l := range l.children {
|
|
|
|
m, err := l.free(t)
|
|
|
|
n += m
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
2022-01-24 09:50:12 +01:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:52:22 +02:00
|
|
|
type MemoryStore struct {
|
|
|
|
root level // root of the tree structure
|
2022-03-08 09:27:44 +01:00
|
|
|
metrics map[string]MetricConfig
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
// Return a new, initialized instance of a MemoryStore.
|
|
|
|
// Will panic if values in the metric configurations are invalid.
|
2021-08-31 10:52:22 +02:00
|
|
|
func NewMemoryStore(metrics map[string]MetricConfig) *MemoryStore {
|
2021-09-08 10:29:36 +02:00
|
|
|
offset := 0
|
|
|
|
for key, config := range metrics {
|
2022-01-24 09:50:12 +01:00
|
|
|
if config.Frequency == 0 {
|
|
|
|
panic("invalid frequency")
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
metrics[key] = MetricConfig{
|
|
|
|
Frequency: config.Frequency,
|
|
|
|
Aggregation: config.Aggregation,
|
2021-09-08 10:29:36 +02:00
|
|
|
offset: offset,
|
|
|
|
}
|
|
|
|
offset += 1
|
|
|
|
}
|
|
|
|
|
2021-08-31 10:52:22 +02:00
|
|
|
return &MemoryStore{
|
|
|
|
root: level{
|
2021-09-08 10:29:36 +02:00
|
|
|
metrics: make([]*buffer, len(metrics)),
|
2021-08-31 10:52:22 +02:00
|
|
|
children: make(map[string]*level),
|
|
|
|
},
|
2022-03-08 09:27:44 +01:00
|
|
|
metrics: metrics,
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all values in `metrics` to the level specified by `selector` for time `ts`.
|
|
|
|
// Look at `findLevelOrCreate` for how selectors work.
|
|
|
|
func (m *MemoryStore) Write(selector []string, ts int64, metrics []Metric) error {
|
2022-01-24 09:50:12 +01:00
|
|
|
var ok bool
|
|
|
|
for i, metric := range metrics {
|
2022-03-08 09:27:44 +01:00
|
|
|
if metric.mc.Frequency == 0 {
|
|
|
|
metric.mc, ok = m.metrics[metric.Name]
|
2022-01-24 09:50:12 +01:00
|
|
|
if !ok {
|
2022-03-08 09:27:44 +01:00
|
|
|
metric.mc.Frequency = 0
|
2022-01-24 09:50:12 +01:00
|
|
|
}
|
|
|
|
metrics[i] = metric
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:47:40 +01:00
|
|
|
return m.WriteToLevel(&m.root, selector, ts, metrics)
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:50:12 +01:00
|
|
|
func (m *MemoryStore) GetLevel(selector []string) *level {
|
|
|
|
return m.root.findLevelOrCreate(selector, len(m.metrics))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes that `minfo` in `metrics` is filled in!
|
2022-01-21 10:47:40 +01:00
|
|
|
func (m *MemoryStore) WriteToLevel(l *level, selector []string, ts int64, metrics []Metric) error {
|
|
|
|
l = l.findLevelOrCreate(selector, len(m.metrics))
|
2021-09-01 08:47:57 +02:00
|
|
|
l.lock.Lock()
|
2021-08-31 10:52:22 +02:00
|
|
|
defer l.lock.Unlock()
|
|
|
|
|
|
|
|
for _, metric := range metrics {
|
2022-03-08 09:27:44 +01:00
|
|
|
if metric.mc.Frequency == 0 {
|
2021-10-07 14:59:07 +02:00
|
|
|
continue
|
2021-09-08 10:29:36 +02:00
|
|
|
}
|
2021-08-31 10:52:22 +02:00
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
b := l.metrics[metric.mc.offset]
|
2021-09-08 10:29:36 +02:00
|
|
|
if b == nil {
|
2021-08-31 10:52:22 +02:00
|
|
|
// First write to this metric and level
|
2022-03-08 09:27:44 +01:00
|
|
|
b = newBuffer(ts, metric.mc.Frequency)
|
|
|
|
l.metrics[metric.mc.offset] = b
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nb, err := b.write(ts, metric.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last write created a new buffer...
|
|
|
|
if b != nb {
|
2022-03-08 09:27:44 +01:00
|
|
|
l.metrics[metric.mc.offset] = nb
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
// Returns all values for metric `metric` from `from` to `to` for the selected level(s).
|
2021-09-07 09:21:08 +02:00
|
|
|
// If the level does not hold the metric itself, the data will be aggregated recursively from the children.
|
2021-09-13 12:28:33 +02:00
|
|
|
// The second and third return value are the actual from/to for the data. Those can be different from
|
|
|
|
// the range asked for if no data was available.
|
2021-09-08 12:17:10 +02:00
|
|
|
func (m *MemoryStore) Read(selector Selector, metric string, from, to int64) ([]Float, int64, int64, error) {
|
2021-08-31 10:52:22 +02:00
|
|
|
if from > to {
|
|
|
|
return nil, 0, 0, errors.New("invalid time range")
|
|
|
|
}
|
|
|
|
|
|
|
|
minfo, ok := m.metrics[metric]
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, 0, errors.New("unkown metric: " + metric)
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:27:44 +01:00
|
|
|
n, data := 0, make([]Float, (to-from)/minfo.Frequency+1)
|
2021-09-08 12:17:10 +02:00
|
|
|
err := m.root.findBuffers(selector, minfo.offset, func(b *buffer) error {
|
|
|
|
cdata, cfrom, cto, err := b.read(from, to, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
from, to = cfrom, cto
|
|
|
|
} else if from != cfrom || to != cto || len(data) != len(cdata) {
|
2022-03-08 09:27:44 +01:00
|
|
|
missingfront, missingback := int((from-cfrom)/minfo.Frequency), int((to-cto)/minfo.Frequency)
|
|
|
|
if missingfront != 0 {
|
|
|
|
return ErrDataDoesNotAlign
|
|
|
|
}
|
|
|
|
|
|
|
|
cdata = cdata[0 : len(cdata)-missingback]
|
|
|
|
if len(cdata) != len(data) {
|
|
|
|
return ErrDataDoesNotAlign
|
|
|
|
}
|
|
|
|
|
|
|
|
from, to = cfrom, cto
|
2021-09-08 12:17:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data = cdata
|
|
|
|
n += 1
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2021-09-07 09:21:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, 0, err
|
2021-09-08 12:17:10 +02:00
|
|
|
} else if n == 0 {
|
2021-12-01 13:22:07 +01:00
|
|
|
return nil, 0, 0, errors.New("metric or host not found")
|
2021-09-08 12:17:10 +02:00
|
|
|
} else if n > 1 {
|
2022-03-08 09:27:44 +01:00
|
|
|
if minfo.Aggregation == AvgAggregation {
|
2021-09-07 09:21:08 +02:00
|
|
|
normalize := 1. / Float(n)
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
data[i] *= normalize
|
|
|
|
}
|
2022-03-08 09:27:44 +01:00
|
|
|
} else if minfo.Aggregation != SumAggregation {
|
2021-09-08 10:29:36 +02:00
|
|
|
return nil, 0, 0, errors.New("invalid aggregation")
|
2021-09-07 09:21:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 12:17:10 +02:00
|
|
|
return data, from, to, nil
|
2021-09-07 09:21:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release all buffers for the selected level and all its children that contain only
|
|
|
|
// values older than `t`.
|
2022-03-08 09:27:44 +01:00
|
|
|
func (m *MemoryStore) Free(selector []string, t int64) (int, error) {
|
|
|
|
return m.GetLevel(selector).free(t)
|
2021-08-31 10:52:22 +02:00
|
|
|
}
|
2021-11-22 17:04:09 +01:00
|
|
|
|
|
|
|
func (m *MemoryStore) FreeAll() error {
|
|
|
|
for k := range m.root.children {
|
|
|
|
delete(m.root.children, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-20 10:42:44 +01:00
|
|
|
|
|
|
|
// Given a selector, return a list of all children of the level selected.
|
|
|
|
func (m *MemoryStore) ListChildren(selector []string) []string {
|
2022-03-04 15:33:47 +01:00
|
|
|
lvl := &m.root
|
|
|
|
for lvl != nil && len(selector) != 0 {
|
|
|
|
lvl.lock.RLock()
|
|
|
|
next := lvl.children[selector[0]]
|
|
|
|
lvl.lock.RUnlock()
|
|
|
|
lvl = next
|
|
|
|
selector = selector[1:]
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:42:44 +01:00
|
|
|
if lvl == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lvl.lock.RLock()
|
|
|
|
defer lvl.lock.RUnlock()
|
|
|
|
|
|
|
|
children := make([]string, 0, len(lvl.children))
|
|
|
|
for child := range lvl.children {
|
|
|
|
children = append(children, child)
|
|
|
|
}
|
|
|
|
|
|
|
|
return children
|
|
|
|
}
|