2013-02-08 17:03:26 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
2013-05-21 16:12:02 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2013-06-21 08:16:41 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-06-21 08:16:41 +00:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-02-08 17:03:26 +00:00
|
|
|
)
|
|
|
|
|
2013-06-06 10:08:20 +00:00
|
|
|
// Assuming sample rate of 1 / 15Hz, this allows for one hour's worth of
|
|
|
|
// storage per metric without any major reallocations.
|
|
|
|
const initialSeriesArenaSize = 4 * 60
|
2013-05-21 16:12:02 +00:00
|
|
|
|
2013-02-08 17:03:26 +00:00
|
|
|
// Models a given sample entry stored in the in-memory arena.
|
|
|
|
type value interface {
|
|
|
|
// Gets the given value.
|
2013-06-25 12:02:27 +00:00
|
|
|
get() clientmodel.SampleValue
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Models a single sample value. It presumes that there is either no subsequent
|
|
|
|
// value seen or that any subsequent values are of a different value.
|
2013-06-25 12:02:27 +00:00
|
|
|
type singletonValue clientmodel.SampleValue
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (v singletonValue) get() clientmodel.SampleValue {
|
|
|
|
return clientmodel.SampleValue(v)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
type stream interface {
|
|
|
|
add(...*SamplePair)
|
|
|
|
|
|
|
|
clone() Values
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
expunge(age clientmodel.Timestamp) Values
|
2013-08-08 23:20:10 +00:00
|
|
|
|
|
|
|
size() int
|
|
|
|
clear()
|
|
|
|
|
|
|
|
metric() clientmodel.Metric
|
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
getValueAtTime(t clientmodel.Timestamp) Values
|
2013-08-08 23:20:10 +00:00
|
|
|
getBoundaryValues(in Interval) Values
|
|
|
|
getRangeValues(in Interval) Values
|
|
|
|
}
|
|
|
|
|
|
|
|
type arrayStream struct {
|
2013-05-21 16:12:02 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
m clientmodel.Metric
|
2013-06-25 12:02:27 +00:00
|
|
|
values Values
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) metric() clientmodel.Metric {
|
|
|
|
return s.m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *arrayStream) add(v ...*SamplePair) {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
s.values = append(s.values, v...)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) clone() Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
clone := make(Values, len(s.values))
|
2013-05-21 16:12:02 +00:00
|
|
|
copy(clone, s.values)
|
2013-03-07 01:16:39 +00:00
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
return clone
|
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
func (s *arrayStream) expunge(t clientmodel.Timestamp) Values {
|
2013-08-08 23:20:10 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
finder := func(i int) bool {
|
|
|
|
return s.values[i].Timestamp.After(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := sort.Search(len(s.values), finder)
|
|
|
|
expunged := s.values[:i]
|
|
|
|
s.values = s.values[i:]
|
|
|
|
|
|
|
|
return expunged
|
|
|
|
}
|
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
func (s *arrayStream) getValueAtTime(t clientmodel.Timestamp) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-22 12:13:05 +00:00
|
|
|
// BUG(all): May be avenues for simplification.
|
2013-05-21 16:12:02 +00:00
|
|
|
l := len(s.values)
|
|
|
|
switch l {
|
|
|
|
case 0:
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{}
|
2013-05-21 16:12:02 +00:00
|
|
|
case 1:
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{s.values[0]}
|
2013-05-21 16:12:02 +00:00
|
|
|
default:
|
|
|
|
index := sort.Search(l, func(i int) bool {
|
|
|
|
return !s.values[i].Timestamp.Before(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
if index == 0 {
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{s.values[0]}
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
2013-05-21 16:12:02 +00:00
|
|
|
if index == l {
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{s.values[l-1]}
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
if s.values[index].Timestamp.Equal(t) {
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{s.values[index]}
|
2013-03-07 01:16:39 +00:00
|
|
|
}
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{s.values[index-1], s.values[index]}
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
2013-05-21 16:12:02 +00:00
|
|
|
}
|
2013-05-20 17:10:26 +00:00
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) getBoundaryValues(in Interval) Values {
|
2013-05-12 00:03:16 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
oldest := sort.Search(len(s.values), func(i int) bool {
|
|
|
|
return !s.values[i].Timestamp.Before(in.OldestInclusive)
|
|
|
|
})
|
|
|
|
|
|
|
|
newest := sort.Search(len(s.values), func(i int) bool {
|
|
|
|
return s.values[i].Timestamp.After(in.NewestInclusive)
|
|
|
|
})
|
|
|
|
|
|
|
|
resultRange := s.values[oldest:newest]
|
|
|
|
switch len(resultRange) {
|
|
|
|
case 0:
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{}
|
2013-05-12 00:03:16 +00:00
|
|
|
case 1:
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{resultRange[0]}
|
2013-05-12 00:03:16 +00:00
|
|
|
default:
|
2013-06-25 12:02:27 +00:00
|
|
|
return Values{resultRange[0], resultRange[len(resultRange)-1]}
|
2013-05-12 00:03:16 +00:00
|
|
|
}
|
2013-05-21 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) getRangeValues(in Interval) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
oldest := sort.Search(len(s.values), func(i int) bool {
|
|
|
|
return !s.values[i].Timestamp.Before(in.OldestInclusive)
|
|
|
|
})
|
|
|
|
|
|
|
|
newest := sort.Search(len(s.values), func(i int) bool {
|
|
|
|
return s.values[i].Timestamp.After(in.NewestInclusive)
|
|
|
|
})
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
result := make(Values, newest-oldest)
|
2013-05-21 16:12:02 +00:00
|
|
|
copy(result, s.values[oldest:newest])
|
|
|
|
|
|
|
|
return result
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) size() int {
|
|
|
|
return len(s.values)
|
2013-06-19 12:19:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *arrayStream) clear() {
|
|
|
|
s.values = Values{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newArrayStream(metric clientmodel.Metric) *arrayStream {
|
|
|
|
return &arrayStream{
|
|
|
|
m: metric,
|
2013-06-25 12:02:27 +00:00
|
|
|
values: make(Values, 0, initialSeriesArenaSize),
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type memorySeriesStorage struct {
|
2013-05-21 16:12:02 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2013-08-07 21:28:11 +00:00
|
|
|
wmCache *watermarkCache
|
2013-08-08 23:20:10 +00:00
|
|
|
fingerprintToSeries map[clientmodel.Fingerprint]stream
|
2013-06-25 12:02:27 +00:00
|
|
|
labelPairToFingerprints map[LabelPair]clientmodel.Fingerprints
|
|
|
|
labelNameToFingerprints map[clientmodel.LabelName]clientmodel.Fingerprints
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:16:22 +00:00
|
|
|
type MemorySeriesOptions struct {
|
|
|
|
// If provided, this WatermarkCache will be updated for any samples that are
|
|
|
|
// appended to the memorySeriesStorage.
|
2013-08-07 21:28:11 +00:00
|
|
|
WatermarkCache *watermarkCache
|
2013-06-06 16:16:22 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) AppendSamples(samples clientmodel.Samples) error {
|
2013-02-08 17:03:26 +00:00
|
|
|
for _, sample := range samples {
|
|
|
|
s.AppendSample(sample)
|
|
|
|
}
|
|
|
|
|
2013-05-16 14:02:07 +00:00
|
|
|
return nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) AppendSample(sample *clientmodel.Sample) error {
|
2013-06-06 10:08:20 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
fingerprint := &clientmodel.Fingerprint{}
|
|
|
|
fingerprint.LoadFromMetric(sample.Metric)
|
2013-06-18 12:08:58 +00:00
|
|
|
series := s.getOrCreateSeries(sample.Metric, fingerprint)
|
2013-08-08 23:20:10 +00:00
|
|
|
series.add(&SamplePair{
|
|
|
|
Value: sample.Value,
|
|
|
|
Timestamp: sample.Timestamp,
|
|
|
|
})
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-06-06 16:16:22 +00:00
|
|
|
if s.wmCache != nil {
|
2013-08-07 21:28:11 +00:00
|
|
|
s.wmCache.Put(fingerprint, &watermarks{High: sample.Timestamp})
|
2013-06-06 16:16:22 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:08:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) CreateEmptySeries(metric clientmodel.Metric) {
|
2013-06-18 12:08:58 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
fingerprint := &clientmodel.Fingerprint{}
|
|
|
|
fingerprint.LoadFromMetric(metric)
|
2013-06-18 12:08:58 +00:00
|
|
|
s.getOrCreateSeries(metric, fingerprint)
|
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
func (s *memorySeriesStorage) getOrCreateSeries(metric clientmodel.Metric, fingerprint *clientmodel.Fingerprint) stream {
|
2013-06-18 12:08:58 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*fingerprint]
|
|
|
|
|
2013-02-08 17:03:26 +00:00
|
|
|
if !ok {
|
2013-08-08 23:20:10 +00:00
|
|
|
series = newArrayStream(metric)
|
2013-05-17 10:58:15 +00:00
|
|
|
s.fingerprintToSeries[*fingerprint] = series
|
2013-02-08 17:03:26 +00:00
|
|
|
|
|
|
|
for k, v := range metric {
|
2013-06-25 12:02:27 +00:00
|
|
|
labelPair := LabelPair{
|
2013-05-20 17:10:26 +00:00
|
|
|
Name: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
labelPairValues := s.labelPairToFingerprints[labelPair]
|
|
|
|
labelPairValues = append(labelPairValues, fingerprint)
|
|
|
|
s.labelPairToFingerprints[labelPair] = labelPairValues
|
|
|
|
|
|
|
|
labelNameValues := s.labelNameToFingerprints[k]
|
|
|
|
labelNameValues = append(labelNameValues, fingerprint)
|
|
|
|
s.labelNameToFingerprints[k] = labelNameValues
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 12:08:58 +00:00
|
|
|
return series
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
func (s *memorySeriesStorage) Flush(flushOlderThan clientmodel.Timestamp, queue chan<- clientmodel.Samples) {
|
2013-06-26 16:00:47 +00:00
|
|
|
emptySeries := []clientmodel.Fingerprint{}
|
2013-06-19 09:55:34 +00:00
|
|
|
|
|
|
|
s.RLock()
|
|
|
|
for fingerprint, stream := range s.fingerprintToSeries {
|
2013-08-08 23:20:10 +00:00
|
|
|
toArchive := stream.expunge(flushOlderThan)
|
2013-06-25 12:02:27 +00:00
|
|
|
queued := make(clientmodel.Samples, 0, len(toArchive))
|
2013-08-08 23:20:10 +00:00
|
|
|
// NOTE: This duplication will go away soon.
|
2013-06-19 09:55:34 +00:00
|
|
|
for _, value := range toArchive {
|
2013-06-25 12:02:27 +00:00
|
|
|
queued = append(queued, &clientmodel.Sample{
|
2013-08-08 23:20:10 +00:00
|
|
|
Metric: stream.metric(),
|
2013-06-19 09:55:34 +00:00
|
|
|
Timestamp: value.Timestamp,
|
|
|
|
Value: value.Value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BUG(all): this can deadlock if the queue is full, as we only ever clear
|
|
|
|
// the queue after calling this method:
|
|
|
|
// https://github.com/prometheus/prometheus/issues/275
|
|
|
|
queue <- queued
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
if stream.size() == 0 {
|
2013-06-26 16:00:47 +00:00
|
|
|
emptySeries = append(emptySeries, fingerprint)
|
2013-06-19 09:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s.RUnlock()
|
|
|
|
|
|
|
|
for _, fingerprint := range emptySeries {
|
2013-08-29 07:37:34 +00:00
|
|
|
if series, ok := s.fingerprintToSeries[fingerprint]; ok && series.size() == 0 {
|
|
|
|
s.Lock()
|
2013-06-26 16:00:47 +00:00
|
|
|
s.dropSeries(&fingerprint)
|
2013-08-29 07:37:34 +00:00
|
|
|
s.Unlock()
|
|
|
|
|
2013-06-19 09:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop all references to a series, including any samples.
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) dropSeries(fingerprint *clientmodel.Fingerprint) {
|
2013-06-19 09:55:34 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*fingerprint]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2013-08-08 23:20:10 +00:00
|
|
|
for k, v := range series.metric() {
|
2013-06-25 12:02:27 +00:00
|
|
|
labelPair := LabelPair{
|
2013-06-19 09:55:34 +00:00
|
|
|
Name: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
delete(s.labelPairToFingerprints, labelPair)
|
|
|
|
delete(s.labelNameToFingerprints, k)
|
|
|
|
}
|
|
|
|
delete(s.fingerprintToSeries, *fingerprint)
|
|
|
|
}
|
|
|
|
|
2013-05-28 12:36:03 +00:00
|
|
|
// Append raw samples, bypassing indexing. Only used to add data to views,
|
|
|
|
// which don't need to lookup by metric.
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) appendSamplesWithoutIndexing(fingerprint *clientmodel.Fingerprint, samples Values) {
|
2013-06-06 10:08:20 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-05-28 12:36:03 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*fingerprint]
|
2013-04-18 14:10:52 +00:00
|
|
|
|
|
|
|
if !ok {
|
2013-08-08 23:20:10 +00:00
|
|
|
series = newArrayStream(clientmodel.Metric{})
|
2013-05-28 12:36:03 +00:00
|
|
|
s.fingerprintToSeries[*fingerprint] = series
|
2013-04-18 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
series.add(samples...)
|
2013-04-18 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetFingerprintsForLabelSet(l clientmodel.LabelSet) (fingerprints clientmodel.Fingerprints, err error) {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
sets := []utility.Set{}
|
2013-02-08 17:03:26 +00:00
|
|
|
for k, v := range l {
|
2013-06-25 12:02:27 +00:00
|
|
|
values := s.labelPairToFingerprints[LabelPair{
|
2013-05-20 17:10:26 +00:00
|
|
|
Name: k,
|
|
|
|
Value: v,
|
|
|
|
}]
|
2013-02-08 17:03:26 +00:00
|
|
|
set := utility.Set{}
|
|
|
|
for _, fingerprint := range values {
|
2013-05-17 10:58:15 +00:00
|
|
|
set.Add(*fingerprint)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
sets = append(sets, set)
|
|
|
|
}
|
|
|
|
|
|
|
|
setCount := len(sets)
|
|
|
|
if setCount == 0 {
|
2013-05-20 17:10:26 +00:00
|
|
|
return fingerprints, nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base := sets[0]
|
|
|
|
for i := 1; i < setCount; i++ {
|
|
|
|
base = base.Intersection(sets[i])
|
|
|
|
}
|
|
|
|
for _, e := range base.Elements() {
|
2013-06-25 12:02:27 +00:00
|
|
|
fingerprint := e.(clientmodel.Fingerprint)
|
2013-05-17 10:58:15 +00:00
|
|
|
fingerprints = append(fingerprints, &fingerprint)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 17:10:26 +00:00
|
|
|
return fingerprints, nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetFingerprintsForLabelName(l clientmodel.LabelName) (clientmodel.Fingerprints, error) {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-05-22 12:23:35 +00:00
|
|
|
defer s.RUnlock()
|
2013-06-06 10:08:20 +00:00
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
values, ok := s.labelNameToFingerprints[l]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
fingerprints := make(clientmodel.Fingerprints, len(values))
|
2013-05-21 16:12:02 +00:00
|
|
|
copy(fingerprints, values)
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-05-20 17:10:26 +00:00
|
|
|
return fingerprints, nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetMetricForFingerprint(f *clientmodel.Fingerprint) (clientmodel.Metric, error) {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-17 10:58:15 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*f]
|
2013-02-08 17:03:26 +00:00
|
|
|
if !ok {
|
2013-05-20 17:10:26 +00:00
|
|
|
return nil, nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
metric := clientmodel.Metric{}
|
2013-08-08 23:20:10 +00:00
|
|
|
for label, value := range series.metric() {
|
2013-05-14 14:25:06 +00:00
|
|
|
metric[label] = value
|
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-05-20 17:10:26 +00:00
|
|
|
return metric, nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) HasFingerprint(f *clientmodel.Fingerprint) bool {
|
2013-06-21 08:16:41 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
_, has := s.fingerprintToSeries[*f]
|
|
|
|
|
|
|
|
return has
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) CloneSamples(f *clientmodel.Fingerprint) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-17 10:58:15 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*f]
|
2013-02-08 17:03:26 +00:00
|
|
|
if !ok {
|
2013-05-21 16:12:02 +00:00
|
|
|
return nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
return series.clone()
|
|
|
|
}
|
2013-04-18 14:10:52 +00:00
|
|
|
|
Use custom timestamp type for sample timestamps and related code.
So far we've been using Go's native time.Time for anything related to sample
timestamps. Since the range of time.Time is much bigger than what we need, this
has created two problems:
- there could be time.Time values which were out of the range/precision of the
time type that we persist to disk, therefore causing incorrectly ordered keys.
One bug caused by this was:
https://github.com/prometheus/prometheus/issues/367
It would be good to use a timestamp type that's more closely aligned with
what the underlying storage supports.
- sizeof(time.Time) is 192, while Prometheus should be ok with a single 64-bit
Unix timestamp (possibly even a 32-bit one). Since we store samples in large
numbers, this seriously affects memory usage. Furthermore, copying/working
with the data will be faster if it's smaller.
*MEMORY USAGE RESULTS*
Initial memory usage comparisons for a running Prometheus with 1 timeseries and
100,000 samples show roughly a 13% decrease in total (VIRT) memory usage. In my
tests, this advantage for some reason decreased a bit the more samples the
timeseries had (to 5-7% for millions of samples). This I can't fully explain,
but perhaps garbage collection issues were involved.
*WHEN TO USE THE NEW TIMESTAMP TYPE*
The new clientmodel.Timestamp type should be used whenever time
calculations are either directly or indirectly related to sample
timestamps.
For example:
- the timestamp of a sample itself
- all kinds of watermarks
- anything that may become or is compared to a sample timestamp (like the timestamp
passed into Target.Scrape()).
When to still use time.Time:
- for measuring durations/times not related to sample timestamps, like duration
telemetry exporting, timers that indicate how frequently to execute some
action, etc.
*NOTE ON OPERATOR OPTIMIZATION TESTS*
We don't use operator optimization code anymore, but it still lives in
the code as dead code. It still has tests, but I couldn't get all of them to
pass with the new timestamp format. I commented out the failing cases for now,
but we should probably remove the dead code soon. I just didn't want to do that
in the same change as this.
Change-Id: I821787414b0debe85c9fffaeb57abd453727af0f
2013-10-28 13:35:02 +00:00
|
|
|
func (s *memorySeriesStorage) GetValueAtTime(f *clientmodel.Fingerprint, t clientmodel.Timestamp) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*f]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
return series.getValueAtTime(t)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetBoundaryValues(f *clientmodel.Fingerprint, i Interval) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-17 10:58:15 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*f]
|
2013-02-08 17:03:26 +00:00
|
|
|
if !ok {
|
2013-05-12 00:03:16 +00:00
|
|
|
return nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
return series.getBoundaryValues(i)
|
|
|
|
}
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetRangeValues(f *clientmodel.Fingerprint, i Interval) Values {
|
2013-05-21 16:12:02 +00:00
|
|
|
s.RLock()
|
2013-06-06 10:08:20 +00:00
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
series, ok := s.fingerprintToSeries[*f]
|
2013-02-08 17:03:26 +00:00
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 16:12:02 +00:00
|
|
|
return series.getRangeValues(i)
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 18:31:58 +00:00
|
|
|
func (s *memorySeriesStorage) Close() {
|
2013-06-06 10:08:20 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-08-08 23:20:10 +00:00
|
|
|
s.fingerprintToSeries = map[clientmodel.Fingerprint]stream{}
|
2013-06-25 12:02:27 +00:00
|
|
|
s.labelPairToFingerprints = map[LabelPair]clientmodel.Fingerprints{}
|
|
|
|
s.labelNameToFingerprints = map[clientmodel.LabelName]clientmodel.Fingerprints{}
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
func (s *memorySeriesStorage) GetAllValuesForLabel(labelName clientmodel.LabelName) (values clientmodel.LabelValues, err error) {
|
2013-06-06 10:08:20 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-06-25 12:02:27 +00:00
|
|
|
valueSet := map[clientmodel.LabelValue]bool{}
|
2013-03-25 12:01:29 +00:00
|
|
|
for _, series := range s.fingerprintToSeries {
|
2013-08-08 23:20:10 +00:00
|
|
|
if value, ok := series.metric()[labelName]; ok {
|
2013-03-26 13:46:02 +00:00
|
|
|
if !valueSet[value] {
|
|
|
|
values = append(values, value)
|
|
|
|
valueSet[value] = true
|
|
|
|
}
|
2013-03-25 12:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-06 10:08:20 +00:00
|
|
|
|
2013-03-25 12:01:29 +00:00
|
|
|
return
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:16:22 +00:00
|
|
|
func NewMemorySeriesStorage(o MemorySeriesOptions) *memorySeriesStorage {
|
2013-05-20 18:31:58 +00:00
|
|
|
return &memorySeriesStorage{
|
2013-08-08 23:20:10 +00:00
|
|
|
fingerprintToSeries: make(map[clientmodel.Fingerprint]stream),
|
2013-06-25 12:02:27 +00:00
|
|
|
labelPairToFingerprints: make(map[LabelPair]clientmodel.Fingerprints),
|
|
|
|
labelNameToFingerprints: make(map[clientmodel.LabelName]clientmodel.Fingerprints),
|
2013-06-06 16:16:22 +00:00
|
|
|
wmCache: o.WatermarkCache,
|
2013-02-08 17:03:26 +00:00
|
|
|
}
|
|
|
|
}
|