2015-01-21 11:07:45 -08:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
2014-09-19 09:18:44 -07:00
|
|
|
// 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.
|
|
|
|
|
2014-09-16 06:47:24 -07:00
|
|
|
package local
|
2014-06-06 02:55:53 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
2015-03-08 18:33:10 -07:00
|
|
|
"time"
|
2014-06-06 02:55:53 -07:00
|
|
|
|
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
|
|
|
)
|
|
|
|
|
2015-03-08 18:33:10 -07:00
|
|
|
const (
|
|
|
|
// chunkDescEvictionFactor is a factor used for chunkDesc eviction (as opposed
|
|
|
|
// to evictions of chunks, see method evictOlderThan. A chunk takes about 20x
|
|
|
|
// more memory than a chunkDesc. With a chunkDescEvictionFactor of 10, not more
|
|
|
|
// than a third of the total memory taken by a series will be used for
|
|
|
|
// chunkDescs.
|
|
|
|
chunkDescEvictionFactor = 10
|
|
|
|
|
|
|
|
headChunkTimeout = time.Hour // Close head chunk if not touched for that long.
|
|
|
|
)
|
2014-10-21 08:53:53 -07:00
|
|
|
|
2014-10-07 10:11:24 -07:00
|
|
|
// fingerprintSeriesPair pairs a fingerprint with a memorySeries pointer.
|
|
|
|
type fingerprintSeriesPair struct {
|
|
|
|
fp clientmodel.Fingerprint
|
|
|
|
series *memorySeries
|
|
|
|
}
|
|
|
|
|
|
|
|
// seriesMap maps fingerprints to memory series. All its methods are
|
|
|
|
// goroutine-safe. A SeriesMap is effectively is a goroutine-safe version of
|
|
|
|
// map[clientmodel.Fingerprint]*memorySeries.
|
|
|
|
type seriesMap struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
m map[clientmodel.Fingerprint]*memorySeries
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSeriesMap returns a newly allocated empty seriesMap. To create a seriesMap
|
|
|
|
// based on a prefilled map, use an explicit initializer.
|
2014-10-08 04:49:42 -07:00
|
|
|
func newSeriesMap() *seriesMap {
|
|
|
|
return &seriesMap{m: make(map[clientmodel.Fingerprint]*memorySeries)}
|
2014-10-07 10:11:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// length returns the number of mappings in the seriesMap.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) length() int {
|
2014-10-07 10:11:24 -07:00
|
|
|
sm.mtx.RLock()
|
|
|
|
defer sm.mtx.RUnlock()
|
|
|
|
|
|
|
|
return len(sm.m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get returns a memorySeries for a fingerprint. Return values have the same
|
|
|
|
// semantics as the native Go map.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) get(fp clientmodel.Fingerprint) (s *memorySeries, ok bool) {
|
2014-10-07 10:11:24 -07:00
|
|
|
sm.mtx.RLock()
|
|
|
|
defer sm.mtx.RUnlock()
|
|
|
|
|
|
|
|
s, ok = sm.m[fp]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:16:07 -07:00
|
|
|
// put adds a mapping to the seriesMap. It panics if s == nil.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) put(fp clientmodel.Fingerprint, s *memorySeries) {
|
2014-10-07 10:11:24 -07:00
|
|
|
sm.mtx.Lock()
|
|
|
|
defer sm.mtx.Unlock()
|
|
|
|
|
2014-10-10 10:16:07 -07:00
|
|
|
if s == nil {
|
|
|
|
panic("tried to add nil pointer to seriesMap")
|
|
|
|
}
|
2014-10-07 10:11:24 -07:00
|
|
|
sm.m[fp] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
// del removes a mapping from the series Map.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) del(fp clientmodel.Fingerprint) {
|
2014-10-07 10:11:24 -07:00
|
|
|
sm.mtx.Lock()
|
|
|
|
defer sm.mtx.Unlock()
|
|
|
|
|
|
|
|
delete(sm.m, fp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// iter returns a channel that produces all mappings in the seriesMap. The
|
|
|
|
// channel will be closed once all fingerprints have been received. Not
|
|
|
|
// consuming all fingerprints from the channel will leak a goroutine. The
|
2014-10-07 11:09:56 -07:00
|
|
|
// semantics of concurrent modification of seriesMap is the similar as the one
|
|
|
|
// for iterating over a map with a 'range' clause. However, if the next element
|
|
|
|
// in iteration order is removed after the current element has been received
|
|
|
|
// from the channel, it will still be produced by the channel.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) iter() <-chan fingerprintSeriesPair {
|
2014-10-07 10:11:24 -07:00
|
|
|
ch := make(chan fingerprintSeriesPair)
|
|
|
|
go func() {
|
|
|
|
sm.mtx.RLock()
|
|
|
|
for fp, s := range sm.m {
|
|
|
|
sm.mtx.RUnlock()
|
|
|
|
ch <- fingerprintSeriesPair{fp, s}
|
|
|
|
sm.mtx.RLock()
|
|
|
|
}
|
|
|
|
sm.mtx.RUnlock()
|
2014-10-07 11:09:56 -07:00
|
|
|
close(ch)
|
2014-10-07 10:11:24 -07:00
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
|
|
|
// fpIter returns a channel that produces all fingerprints in the seriesMap. The
|
|
|
|
// channel will be closed once all fingerprints have been received. Not
|
|
|
|
// consuming all fingerprints from the channel will leak a goroutine. The
|
2014-10-07 11:09:56 -07:00
|
|
|
// semantics of concurrent modification of seriesMap is the similar as the one
|
|
|
|
// for iterating over a map with a 'range' clause. However, if the next element
|
|
|
|
// in iteration order is removed after the current element has been received
|
|
|
|
// from the channel, it will still be produced by the channel.
|
2014-10-08 04:49:42 -07:00
|
|
|
func (sm *seriesMap) fpIter() <-chan clientmodel.Fingerprint {
|
2014-10-07 10:11:24 -07:00
|
|
|
ch := make(chan clientmodel.Fingerprint)
|
|
|
|
go func() {
|
|
|
|
sm.mtx.RLock()
|
|
|
|
for fp := range sm.m {
|
|
|
|
sm.mtx.RUnlock()
|
|
|
|
ch <- fp
|
|
|
|
sm.mtx.RLock()
|
|
|
|
}
|
|
|
|
sm.mtx.RUnlock()
|
2014-10-07 11:09:56 -07:00
|
|
|
close(ch)
|
2014-10-07 10:11:24 -07:00
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
type memorySeries struct {
|
|
|
|
metric clientmodel.Metric
|
2014-09-10 09:41:52 -07:00
|
|
|
// Sorted by start time, overlapping chunk ranges are forbidden.
|
2014-10-15 06:53:05 -07:00
|
|
|
chunkDescs []*chunkDesc
|
2015-03-08 18:33:10 -07:00
|
|
|
// The index (within chunkDescs above) of the first chunkDesc that
|
|
|
|
// points to a non-persisted chunk. If all chunks are persisted, then
|
|
|
|
// persistWatermark == len(chunkDescs).
|
|
|
|
persistWatermark int
|
2015-03-19 04:59:26 -07:00
|
|
|
// The modification time of the series file. The zero value of time.Time
|
|
|
|
// is used to mark an unknown modification time.
|
|
|
|
modTime time.Time
|
2014-10-27 12:40:48 -07:00
|
|
|
// The chunkDescs in memory might not have all the chunkDescs for the
|
|
|
|
// chunks that are persisted to disk. The missing chunkDescs are all
|
|
|
|
// contiguous and at the tail end. chunkDescsOffset is the index of the
|
|
|
|
// chunk on disk that corresponds to the first chunkDesc in memory. If
|
|
|
|
// it is 0, the chunkDescs are all loaded. A value of -1 denotes a
|
|
|
|
// special case: There are chunks on disk, but the offset to the
|
2015-03-08 18:33:10 -07:00
|
|
|
// chunkDescs in memory is unknown. Also, in this special case, there is
|
|
|
|
// no overlap between chunks on disk and chunks in memory (implying that
|
|
|
|
// upon first persisting of a chunk in memory, the offset has to be
|
|
|
|
// set).
|
2014-10-27 12:40:48 -07:00
|
|
|
chunkDescsOffset int
|
2014-11-05 11:02:45 -08:00
|
|
|
// The savedFirstTime field is used as a fallback when the
|
|
|
|
// chunkDescsOffset is not 0. It can be used to save the firstTime of the
|
|
|
|
// first chunk before its chunk desc is evicted. In doubt, this field is
|
|
|
|
// just set to the oldest possible timestamp.
|
|
|
|
savedFirstTime clientmodel.Timestamp
|
2015-07-13 12:12:27 -07:00
|
|
|
// The timestamp of the last sample in this series. Needed for fast access to
|
|
|
|
// ensure timestamp monotonicity during ingestion.
|
|
|
|
lastTime clientmodel.Timestamp
|
2015-03-08 18:33:10 -07:00
|
|
|
// Whether the current head chunk has already been finished. If true,
|
|
|
|
// the current head chunk must not be modified anymore.
|
|
|
|
headChunkClosed bool
|
2014-10-27 07:55:44 -07:00
|
|
|
// Whether the current head chunk is used by an iterator. In that case,
|
2015-03-08 18:33:10 -07:00
|
|
|
// a non-closed head chunk has to be cloned before more samples are
|
2014-10-27 07:55:44 -07:00
|
|
|
// appended.
|
|
|
|
headChunkUsedByIterator bool
|
2015-03-08 18:33:10 -07:00
|
|
|
// Whether the series is inconsistent with the last checkpoint in a way
|
2015-03-18 11:09:07 -07:00
|
|
|
// that would require a disk seek during crash recovery.
|
2015-03-08 18:33:10 -07:00
|
|
|
dirty bool
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-07 10:11:24 -07:00
|
|
|
// newMemorySeries returns a pointer to a newly allocated memorySeries for the
|
2015-07-13 12:12:27 -07:00
|
|
|
// given metric. chunkDescs and modTime in the new series are set according to
|
|
|
|
// the provided parameters. chunkDescs can be nil or empty if this is a
|
|
|
|
// genuinely new time series (i.e. not one that is being unarchived). In that
|
|
|
|
// case, headChunkClosed is set to false, and firstTime and lastTime are both
|
|
|
|
// set to clientmodel.Earliest. The zero value for modTime can be used if the
|
|
|
|
// modification time of the series file is unknown (e.g. if this is a genuinely
|
|
|
|
// new series).
|
|
|
|
func newMemorySeries(m clientmodel.Metric, chunkDescs []*chunkDesc, modTime time.Time) *memorySeries {
|
|
|
|
firstTime := clientmodel.Earliest
|
|
|
|
lastTime := clientmodel.Earliest
|
|
|
|
if len(chunkDescs) > 0 {
|
|
|
|
firstTime = chunkDescs[0].firstTime()
|
|
|
|
lastTime = chunkDescs[len(chunkDescs)-1].lastTime()
|
2014-11-05 11:02:45 -08:00
|
|
|
}
|
2015-07-13 12:12:27 -07:00
|
|
|
return &memorySeries{
|
|
|
|
metric: m,
|
|
|
|
chunkDescs: chunkDescs,
|
|
|
|
headChunkClosed: len(chunkDescs) > 0,
|
|
|
|
savedFirstTime: firstTime,
|
|
|
|
lastTime: lastTime,
|
|
|
|
persistWatermark: len(chunkDescs),
|
|
|
|
modTime: modTime,
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 18:33:10 -07:00
|
|
|
// add adds a sample pair to the series. It returns the number of newly
|
|
|
|
// completed chunks (which are now eligible for persistence).
|
|
|
|
//
|
2014-10-07 10:11:24 -07:00
|
|
|
// The caller must have locked the fingerprint of the series.
|
2015-03-08 18:33:10 -07:00
|
|
|
func (s *memorySeries) add(v *metric.SamplePair) int {
|
|
|
|
if len(s.chunkDescs) == 0 || s.headChunkClosed {
|
2015-03-13 07:49:07 -07:00
|
|
|
newHead := newChunkDesc(newChunk())
|
2014-09-16 06:47:24 -07:00
|
|
|
s.chunkDescs = append(s.chunkDescs, newHead)
|
2015-03-08 18:33:10 -07:00
|
|
|
s.headChunkClosed = false
|
2015-05-20 10:13:06 -07:00
|
|
|
} else if s.headChunkUsedByIterator && s.head().refCount() > 1 {
|
2014-10-27 07:55:44 -07:00
|
|
|
// We only need to clone the head chunk if the current head
|
|
|
|
// chunk was used in an iterator at all and if the refCount is
|
|
|
|
// still greater than the 1 we always have because the head
|
|
|
|
// chunk is not yet persisted. The latter is just an
|
|
|
|
// approximation. We will still clone unnecessarily if an older
|
|
|
|
// iterator using a previous version of the head chunk is still
|
|
|
|
// around and keep the head chunk pinned. We needed to track
|
|
|
|
// pins by version of the head chunk, which is probably not
|
|
|
|
// worth the effort.
|
|
|
|
chunkOps.WithLabelValues(clone).Inc()
|
|
|
|
// No locking needed here because a non-persisted head chunk can
|
|
|
|
// not get evicted concurrently.
|
2015-05-20 10:13:06 -07:00
|
|
|
s.head().c = s.head().c.clone()
|
2014-10-27 07:55:44 -07:00
|
|
|
s.headChunkUsedByIterator = false
|
2014-09-16 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
chunks := s.head().add(v)
|
2015-05-20 10:13:06 -07:00
|
|
|
s.head().c = chunks[0]
|
2014-06-06 02:55:53 -07:00
|
|
|
|
2015-03-08 18:33:10 -07:00
|
|
|
for _, c := range chunks[1:] {
|
|
|
|
s.chunkDescs = append(s.chunkDescs, newChunkDesc(c))
|
|
|
|
}
|
2015-07-13 12:12:27 -07:00
|
|
|
|
|
|
|
s.lastTime = v.Timestamp
|
2015-03-08 18:33:10 -07:00
|
|
|
return len(chunks) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybeCloseHeadChunk closes the head chunk if it has not been touched for the
|
2015-03-18 11:09:07 -07:00
|
|
|
// duration of headChunkTimeout. It returns whether the head chunk was closed.
|
2015-03-08 18:33:10 -07:00
|
|
|
// If the head chunk is already closed, the method is a no-op and returns false.
|
|
|
|
//
|
|
|
|
// The caller must have locked the fingerprint of the series.
|
|
|
|
func (s *memorySeries) maybeCloseHeadChunk() bool {
|
|
|
|
if s.headChunkClosed {
|
|
|
|
return false
|
|
|
|
}
|
2015-07-13 12:12:27 -07:00
|
|
|
if time.Now().Sub(s.lastTime.Time()) > headChunkTimeout {
|
2015-03-08 18:33:10 -07:00
|
|
|
s.headChunkClosed = true
|
|
|
|
// Since we cannot modify the head chunk from now on, we
|
|
|
|
// don't need to bother with cloning anymore.
|
|
|
|
s.headChunkUsedByIterator = false
|
|
|
|
return true
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2015-03-08 18:33:10 -07:00
|
|
|
return false
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:50:25 -08:00
|
|
|
// evictChunkDescs evicts chunkDescs if there are chunkDescEvictionFactor times
|
|
|
|
// more than non-evicted chunks. iOldestNotEvicted is the index within the
|
|
|
|
// current chunkDescs of the oldest chunk that is not evicted.
|
|
|
|
func (s *memorySeries) evictChunkDescs(iOldestNotEvicted int) {
|
|
|
|
lenToKeep := chunkDescEvictionFactor * (len(s.chunkDescs) - iOldestNotEvicted)
|
|
|
|
if lenToKeep < len(s.chunkDescs) {
|
|
|
|
s.savedFirstTime = s.firstTime()
|
|
|
|
lenEvicted := len(s.chunkDescs) - lenToKeep
|
|
|
|
s.chunkDescsOffset += lenEvicted
|
2015-03-08 18:33:10 -07:00
|
|
|
s.persistWatermark -= lenEvicted
|
2014-11-13 11:50:25 -08:00
|
|
|
chunkDescOps.WithLabelValues(evict).Add(float64(lenEvicted))
|
2014-11-27 09:25:03 -08:00
|
|
|
numMemChunkDescs.Sub(float64(lenEvicted))
|
2014-11-13 11:50:25 -08:00
|
|
|
s.chunkDescs = append(
|
|
|
|
make([]*chunkDesc, 0, lenToKeep),
|
|
|
|
s.chunkDescs[lenEvicted:]...,
|
|
|
|
)
|
2015-03-08 18:33:10 -07:00
|
|
|
s.dirty = true
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-08 18:33:10 -07:00
|
|
|
// dropChunks removes chunkDescs older than t. The caller must have locked the
|
|
|
|
// fingerprint of the series.
|
|
|
|
func (s *memorySeries) dropChunks(t clientmodel.Timestamp) {
|
2014-06-06 02:55:53 -07:00
|
|
|
keepIdx := len(s.chunkDescs)
|
|
|
|
for i, cd := range s.chunkDescs {
|
|
|
|
if !cd.lastTime().Before(t) {
|
|
|
|
keepIdx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 12:40:48 -07:00
|
|
|
if keepIdx > 0 {
|
2015-03-08 18:33:10 -07:00
|
|
|
s.chunkDescs = append(
|
|
|
|
make([]*chunkDesc, 0, len(s.chunkDescs)-keepIdx),
|
|
|
|
s.chunkDescs[keepIdx:]...,
|
|
|
|
)
|
|
|
|
s.persistWatermark -= keepIdx
|
|
|
|
if s.persistWatermark < 0 {
|
|
|
|
panic("dropped unpersisted chunks from memory")
|
|
|
|
}
|
|
|
|
if s.chunkDescsOffset != -1 {
|
|
|
|
s.chunkDescsOffset += keepIdx
|
|
|
|
}
|
2014-11-27 09:25:03 -08:00
|
|
|
numMemChunkDescs.Sub(float64(keepIdx))
|
2015-03-08 18:33:10 -07:00
|
|
|
s.dirty = true
|
2014-10-27 12:40:48 -07:00
|
|
|
}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-07 10:11:24 -07:00
|
|
|
// preloadChunks is an internal helper method.
|
2015-05-06 07:53:12 -07:00
|
|
|
func (s *memorySeries) preloadChunks(
|
|
|
|
indexes []int, fp clientmodel.Fingerprint, mss *memorySeriesStorage,
|
|
|
|
) ([]*chunkDesc, error) {
|
2014-06-06 02:55:53 -07:00
|
|
|
loadIndexes := []int{}
|
2014-10-15 06:53:05 -07:00
|
|
|
pinnedChunkDescs := make([]*chunkDesc, 0, len(indexes))
|
2014-06-06 02:55:53 -07:00
|
|
|
for _, idx := range indexes {
|
2014-10-22 10:21:23 -07:00
|
|
|
cd := s.chunkDescs[idx]
|
|
|
|
pinnedChunkDescs = append(pinnedChunkDescs, cd)
|
2014-11-13 11:50:25 -08:00
|
|
|
cd.pin(mss.evictRequests) // Have to pin everything first to prevent immediate eviction on chunk loading.
|
2014-10-22 10:21:23 -07:00
|
|
|
if cd.isEvicted() {
|
2014-06-06 02:55:53 -07:00
|
|
|
loadIndexes = append(loadIndexes, idx)
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 10:21:23 -07:00
|
|
|
chunkOps.WithLabelValues(pin).Add(float64(len(pinnedChunkDescs)))
|
2014-06-06 02:55:53 -07:00
|
|
|
|
|
|
|
if len(loadIndexes) > 0 {
|
2014-10-27 12:40:48 -07:00
|
|
|
if s.chunkDescsOffset == -1 {
|
|
|
|
panic("requested loading chunks from persistence in a situation where we must not have persisted data for chunk descriptors in memory")
|
|
|
|
}
|
2014-11-27 11:46:45 -08:00
|
|
|
chunks, err := mss.loadChunks(fp, loadIndexes, s.chunkDescsOffset)
|
2014-06-06 02:55:53 -07:00
|
|
|
if err != nil {
|
2014-10-22 10:21:23 -07:00
|
|
|
// Unpin the chunks since we won't return them as pinned chunks now.
|
2014-08-20 06:05:58 -07:00
|
|
|
for _, cd := range pinnedChunkDescs {
|
2014-11-13 11:50:25 -08:00
|
|
|
cd.unpin(mss.evictRequests)
|
2014-08-20 06:05:58 -07:00
|
|
|
}
|
2014-10-22 10:21:23 -07:00
|
|
|
chunkOps.WithLabelValues(unpin).Add(float64(len(pinnedChunkDescs)))
|
2014-06-06 02:55:53 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i, c := range chunks {
|
2014-10-22 10:21:23 -07:00
|
|
|
s.chunkDescs[loadIndexes[i]].setChunk(c)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pinnedChunkDescs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-10-07 10:11:24 -07:00
|
|
|
func (s *memorySeries) preloadChunksAtTime(t clientmodel.Timestamp, p *persistence) (chunkDescs, error) {
|
2014-06-06 02:55:53 -07:00
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
if len(s.chunkDescs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var pinIndexes []int
|
|
|
|
// Find first chunk where lastTime() is after or equal to t.
|
|
|
|
i := sort.Search(len(s.chunkDescs), func(i int) bool {
|
|
|
|
return !s.chunkDescs[i].lastTime().Before(t)
|
|
|
|
})
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
pinIndexes = []int{0}
|
|
|
|
case len(s.chunkDescs):
|
|
|
|
pinIndexes = []int{i - 1}
|
|
|
|
default:
|
|
|
|
if s.chunkDescs[i].contains(t) {
|
|
|
|
pinIndexes = []int{i}
|
|
|
|
} else {
|
|
|
|
pinIndexes = []int{i - 1, i}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.preloadChunks(pinIndexes, p)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2014-10-07 10:11:24 -07:00
|
|
|
// preloadChunksForRange loads chunks for the given range from the persistence.
|
|
|
|
// The caller must have locked the fingerprint of the series.
|
2014-10-14 09:23:32 -07:00
|
|
|
func (s *memorySeries) preloadChunksForRange(
|
|
|
|
from clientmodel.Timestamp, through clientmodel.Timestamp,
|
2014-11-13 11:50:25 -08:00
|
|
|
fp clientmodel.Fingerprint, mss *memorySeriesStorage,
|
2014-10-15 06:53:05 -07:00
|
|
|
) ([]*chunkDesc, error) {
|
2015-02-12 08:23:42 -08:00
|
|
|
firstChunkDescTime := clientmodel.Latest
|
2014-10-14 09:23:32 -07:00
|
|
|
if len(s.chunkDescs) > 0 {
|
|
|
|
firstChunkDescTime = s.chunkDescs[0].firstTime()
|
|
|
|
}
|
2014-10-27 12:40:48 -07:00
|
|
|
if s.chunkDescsOffset != 0 && from.Before(firstChunkDescTime) {
|
2015-07-06 16:10:14 -07:00
|
|
|
cds, err := mss.loadChunkDescs(fp, s.persistWatermark)
|
2014-10-14 09:23:32 -07:00
|
|
|
if err != nil {
|
2014-06-06 02:55:53 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-14 09:23:32 -07:00
|
|
|
s.chunkDescs = append(cds, s.chunkDescs...)
|
2014-10-27 12:40:48 -07:00
|
|
|
s.chunkDescsOffset = 0
|
2015-03-08 18:33:10 -07:00
|
|
|
s.persistWatermark += len(cds)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.chunkDescs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find first chunk with start time after "from".
|
|
|
|
fromIdx := sort.Search(len(s.chunkDescs), func(i int) bool {
|
|
|
|
return s.chunkDescs[i].firstTime().After(from)
|
|
|
|
})
|
|
|
|
// Find first chunk with start time after "through".
|
|
|
|
throughIdx := sort.Search(len(s.chunkDescs), func(i int) bool {
|
|
|
|
return s.chunkDescs[i].firstTime().After(through)
|
|
|
|
})
|
|
|
|
if fromIdx > 0 {
|
|
|
|
fromIdx--
|
|
|
|
}
|
|
|
|
if throughIdx == len(s.chunkDescs) {
|
|
|
|
throughIdx--
|
|
|
|
}
|
|
|
|
|
|
|
|
pinIndexes := make([]int, 0, throughIdx-fromIdx+1)
|
|
|
|
for i := fromIdx; i <= throughIdx; i++ {
|
|
|
|
pinIndexes = append(pinIndexes, i)
|
|
|
|
}
|
2015-05-06 07:53:12 -07:00
|
|
|
return s.preloadChunks(pinIndexes, fp, mss)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 07:55:44 -07:00
|
|
|
// newIterator returns a new SeriesIterator. The caller must have locked the
|
|
|
|
// fingerprint of the memorySeries.
|
2015-05-04 11:16:01 -07:00
|
|
|
func (s *memorySeries) newIterator() SeriesIterator {
|
2014-10-15 06:53:05 -07:00
|
|
|
chunks := make([]chunk, 0, len(s.chunkDescs))
|
2014-06-06 02:55:53 -07:00
|
|
|
for i, cd := range s.chunkDescs {
|
2015-05-20 10:13:06 -07:00
|
|
|
if chunk := cd.chunk(); chunk != nil {
|
2015-03-08 18:33:10 -07:00
|
|
|
if i == len(s.chunkDescs)-1 && !s.headChunkClosed {
|
2014-10-27 07:55:44 -07:00
|
|
|
s.headChunkUsedByIterator = true
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2014-12-03 09:07:23 -08:00
|
|
|
chunks = append(chunks, chunk)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memorySeriesIterator{
|
2015-05-04 11:16:01 -07:00
|
|
|
chunks: chunks,
|
|
|
|
chunkIts: make([]chunkIterator, len(chunks)),
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 11:01:41 -07:00
|
|
|
// head returns a pointer to the head chunk descriptor. The caller must have
|
Fix a bug handling freshly unarchived series.
Usually, if you unarchive a series, it is to add something to it,
which will create a new head chunk. However, if a series in
unarchived, and before anything is added to it, it is handled by the
maintenance loop, it will be archived again. In that case, we have to
load the chunkDescs to know the lastTime of the series to be
archived. Usually, this case will happen only rarely (as a race, has
never happened so far, possibly because the locking around unarchiving
and the subsequent sample append is smart enough). However, during
crash recovery, we sometimes treat series as "freshly unarchived"
without directly appending a sample. We might add more cases of that
type later, so better deal with archiving properly and load chunkDescs
if required.
2015-01-08 07:10:31 -08:00
|
|
|
// locked the fingerprint of the memorySeries. This method will panic if this
|
|
|
|
// series has no chunk descriptors.
|
2014-06-06 02:55:53 -07:00
|
|
|
func (s *memorySeries) head() *chunkDesc {
|
|
|
|
return s.chunkDescs[len(s.chunkDescs)-1]
|
|
|
|
}
|
|
|
|
|
2014-10-28 11:01:41 -07:00
|
|
|
// firstTime returns the timestamp of the first sample in the series. The caller
|
|
|
|
// must have locked the fingerprint of the memorySeries.
|
2014-09-15 10:24:26 -07:00
|
|
|
func (s *memorySeries) firstTime() clientmodel.Timestamp {
|
2014-11-05 11:02:45 -08:00
|
|
|
if s.chunkDescsOffset == 0 && len(s.chunkDescs) > 0 {
|
|
|
|
return s.chunkDescs[0].firstTime()
|
|
|
|
}
|
|
|
|
return s.savedFirstTime
|
2014-09-15 10:24:26 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// chunksToPersist returns a slice of chunkDescs eligible for persistence. It's
|
|
|
|
// the caller's responsibility to actually persist the returned chunks
|
|
|
|
// afterwards. The method sets the persistWatermark and the dirty flag
|
|
|
|
// accordingly.
|
2015-03-08 18:33:10 -07:00
|
|
|
//
|
|
|
|
// The caller must have locked the fingerprint of the series.
|
2015-05-20 10:13:06 -07:00
|
|
|
func (s *memorySeries) chunksToPersist() []*chunkDesc {
|
2015-03-08 18:33:10 -07:00
|
|
|
newWatermark := len(s.chunkDescs)
|
|
|
|
if !s.headChunkClosed {
|
|
|
|
newWatermark--
|
|
|
|
}
|
|
|
|
if newWatermark == s.persistWatermark {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cds := s.chunkDescs[s.persistWatermark:newWatermark]
|
|
|
|
s.dirty = true
|
|
|
|
s.persistWatermark = newWatermark
|
|
|
|
return cds
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:52:39 -07:00
|
|
|
// memorySeriesIterator implements SeriesIterator.
|
|
|
|
type memorySeriesIterator struct {
|
2015-05-20 10:13:06 -07:00
|
|
|
chunkIt chunkIterator // Last chunkIterator used by ValueAtTime.
|
2015-05-04 11:16:01 -07:00
|
|
|
chunkIts []chunkIterator // Caches chunkIterators.
|
|
|
|
chunks []chunk
|
2014-10-14 04:52:39 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// ValueAtTime implements SeriesIterator.
|
|
|
|
func (it *memorySeriesIterator) ValueAtTime(t clientmodel.Timestamp) metric.Values {
|
2014-06-06 02:55:53 -07:00
|
|
|
// The most common case. We are iterating through a chunk.
|
|
|
|
if it.chunkIt != nil && it.chunkIt.contains(t) {
|
2015-05-20 10:13:06 -07:00
|
|
|
return it.chunkIt.valueAtTime(t)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(it.chunks) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before or exactly on the first sample of the series.
|
2015-05-20 10:13:06 -07:00
|
|
|
it.chunkIt = it.chunkIterator(0)
|
|
|
|
ts := it.chunkIt.timestampAtIndex(0)
|
2015-04-14 04:46:38 -07:00
|
|
|
if !t.After(ts) {
|
2014-06-06 02:55:53 -07:00
|
|
|
// return first value of first chunk
|
2015-04-14 04:46:38 -07:00
|
|
|
return metric.Values{metric.SamplePair{
|
|
|
|
Timestamp: ts,
|
2015-05-20 10:13:06 -07:00
|
|
|
Value: it.chunkIt.sampleValueAtIndex(0),
|
2015-04-14 04:46:38 -07:00
|
|
|
}}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
2015-04-14 04:46:38 -07:00
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
// After or exactly on the last sample of the series.
|
2015-05-20 10:13:06 -07:00
|
|
|
it.chunkIt = it.chunkIterator(len(it.chunks) - 1)
|
|
|
|
ts = it.chunkIt.lastTimestamp()
|
2015-04-14 04:46:38 -07:00
|
|
|
if !t.Before(ts) {
|
2014-06-06 02:55:53 -07:00
|
|
|
// return last value of last chunk
|
2015-04-14 04:46:38 -07:00
|
|
|
return metric.Values{metric.SamplePair{
|
|
|
|
Timestamp: ts,
|
2015-05-20 10:13:06 -07:00
|
|
|
Value: it.chunkIt.sampleValueAtIndex(it.chunkIt.length() - 1),
|
2015-04-14 04:46:38 -07:00
|
|
|
}}
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2015-04-14 04:46:38 -07:00
|
|
|
// Find last chunk where firstTime() is before or equal to t.
|
|
|
|
l := len(it.chunks) - 1
|
2014-06-06 02:55:53 -07:00
|
|
|
i := sort.Search(len(it.chunks), func(i int) bool {
|
2015-04-14 04:46:38 -07:00
|
|
|
return !it.chunks[l-i].firstTime().After(t)
|
2014-06-06 02:55:53 -07:00
|
|
|
})
|
|
|
|
if i == len(it.chunks) {
|
|
|
|
panic("out of bounds")
|
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
it.chunkIt = it.chunkIterator(l - i)
|
|
|
|
ts = it.chunkIt.lastTimestamp()
|
2015-04-14 04:46:38 -07:00
|
|
|
if t.After(ts) {
|
2014-06-06 02:55:53 -07:00
|
|
|
// We ended up between two chunks.
|
2015-04-14 04:46:38 -07:00
|
|
|
sp1 := metric.SamplePair{
|
|
|
|
Timestamp: ts,
|
2015-05-20 10:13:06 -07:00
|
|
|
Value: it.chunkIt.sampleValueAtIndex(it.chunkIt.length() - 1),
|
2015-04-14 04:46:38 -07:00
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
it.chunkIt = it.chunkIterator(l - i + 1)
|
2014-06-06 02:55:53 -07:00
|
|
|
return metric.Values{
|
2015-04-14 04:46:38 -07:00
|
|
|
sp1,
|
|
|
|
metric.SamplePair{
|
2015-05-20 10:13:06 -07:00
|
|
|
Timestamp: it.chunkIt.timestampAtIndex(0),
|
|
|
|
Value: it.chunkIt.sampleValueAtIndex(0),
|
2015-04-14 04:46:38 -07:00
|
|
|
},
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
return it.chunkIt.valueAtTime(t)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// BoundaryValues implements SeriesIterator.
|
|
|
|
func (it *memorySeriesIterator) BoundaryValues(in metric.Interval) metric.Values {
|
2015-04-14 04:46:38 -07:00
|
|
|
// Find the first chunk for which the first sample is within the interval.
|
2014-09-16 06:47:24 -07:00
|
|
|
i := sort.Search(len(it.chunks), func(i int) bool {
|
2015-04-14 04:46:38 -07:00
|
|
|
return !it.chunks[i].firstTime().Before(in.OldestInclusive)
|
2014-09-16 06:47:24 -07:00
|
|
|
})
|
2015-04-14 04:46:38 -07:00
|
|
|
// Only now check the last timestamp of the previous chunk (which is
|
|
|
|
// fairly expensive).
|
2015-05-20 10:13:06 -07:00
|
|
|
if i > 0 && !it.chunkIterator(i-1).lastTimestamp().Before(in.OldestInclusive) {
|
2015-04-14 04:46:38 -07:00
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
2014-10-10 06:34:09 -07:00
|
|
|
values := make(metric.Values, 0, 2)
|
2015-04-14 04:46:38 -07:00
|
|
|
for j, c := range it.chunks[i:] {
|
2014-09-16 06:47:24 -07:00
|
|
|
if c.firstTime().After(in.NewestInclusive) {
|
|
|
|
if len(values) == 1 {
|
2015-04-14 04:46:38 -07:00
|
|
|
// We found the first value before but are now
|
2014-09-16 06:47:24 -07:00
|
|
|
// already past the last value. The value we
|
|
|
|
// want must be the last value of the previous
|
|
|
|
// chunk. So backtrack...
|
2015-05-20 10:13:06 -07:00
|
|
|
chunkIt := it.chunkIterator(i + j - 1)
|
2015-04-14 04:46:38 -07:00
|
|
|
values = append(values, metric.SamplePair{
|
2015-05-20 10:13:06 -07:00
|
|
|
Timestamp: chunkIt.lastTimestamp(),
|
|
|
|
Value: chunkIt.lastSampleValue(),
|
2015-04-14 04:46:38 -07:00
|
|
|
})
|
2014-09-16 06:47:24 -07:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
chunkIt := it.chunkIterator(i + j)
|
2014-09-16 06:47:24 -07:00
|
|
|
if len(values) == 0 {
|
2015-05-20 10:13:06 -07:00
|
|
|
firstValues := chunkIt.valueAtTime(in.OldestInclusive)
|
2014-09-16 06:47:24 -07:00
|
|
|
switch len(firstValues) {
|
|
|
|
case 2:
|
|
|
|
values = append(values, firstValues[1])
|
|
|
|
case 1:
|
|
|
|
values = firstValues
|
|
|
|
default:
|
2015-05-20 10:13:06 -07:00
|
|
|
panic("unexpected return from valueAtTime")
|
2014-09-16 06:47:24 -07:00
|
|
|
}
|
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
if chunkIt.lastTimestamp().After(in.NewestInclusive) {
|
|
|
|
values = append(values, chunkIt.valueAtTime(in.NewestInclusive)[0])
|
2014-09-16 06:47:24 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 06:34:09 -07:00
|
|
|
if len(values) == 1 {
|
|
|
|
// We found exactly one value. In that case, add the most recent we know.
|
2015-05-20 10:13:06 -07:00
|
|
|
chunkIt := it.chunkIterator(len(it.chunks) - 1)
|
2015-04-14 04:46:38 -07:00
|
|
|
values = append(values, metric.SamplePair{
|
2015-05-20 10:13:06 -07:00
|
|
|
Timestamp: chunkIt.lastTimestamp(),
|
|
|
|
Value: chunkIt.lastSampleValue(),
|
2015-04-14 04:46:38 -07:00
|
|
|
})
|
2014-10-10 06:34:09 -07:00
|
|
|
}
|
2014-09-16 06:47:24 -07:00
|
|
|
if len(values) == 2 && values[0].Equal(&values[1]) {
|
|
|
|
return values[:1]
|
|
|
|
}
|
|
|
|
return values
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// RangeValues implements SeriesIterator.
|
|
|
|
func (it *memorySeriesIterator) RangeValues(in metric.Interval) metric.Values {
|
2015-04-14 04:46:38 -07:00
|
|
|
// Find the first chunk for which the first sample is within the interval.
|
2014-06-06 02:55:53 -07:00
|
|
|
i := sort.Search(len(it.chunks), func(i int) bool {
|
2015-04-14 04:46:38 -07:00
|
|
|
return !it.chunks[i].firstTime().Before(in.OldestInclusive)
|
2014-06-06 02:55:53 -07:00
|
|
|
})
|
2015-04-14 04:46:38 -07:00
|
|
|
// Only now check the last timestamp of the previous chunk (which is
|
|
|
|
// fairly expensive).
|
2015-05-20 10:13:06 -07:00
|
|
|
if i > 0 && !it.chunkIterator(i-1).lastTimestamp().Before(in.OldestInclusive) {
|
2015-04-14 04:46:38 -07:00
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
2014-06-06 02:55:53 -07:00
|
|
|
values := metric.Values{}
|
2015-05-04 11:16:01 -07:00
|
|
|
for j, c := range it.chunks[i:] {
|
2014-06-06 02:55:53 -07:00
|
|
|
if c.firstTime().After(in.NewestInclusive) {
|
|
|
|
break
|
|
|
|
}
|
2015-05-20 10:13:06 -07:00
|
|
|
values = append(values, it.chunkIterator(i+j).rangeValues(in)...)
|
2014-06-06 02:55:53 -07:00
|
|
|
}
|
|
|
|
return values
|
|
|
|
}
|
2014-10-14 04:52:39 -07:00
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// chunkIterator returns the chunkIterator for the chunk at position i (and
|
2015-05-04 11:16:01 -07:00
|
|
|
// creates it if needed).
|
2015-05-20 10:13:06 -07:00
|
|
|
func (it *memorySeriesIterator) chunkIterator(i int) chunkIterator {
|
2015-05-04 11:16:01 -07:00
|
|
|
chunkIt := it.chunkIts[i]
|
|
|
|
if chunkIt == nil {
|
|
|
|
chunkIt = it.chunks[i].newIterator()
|
|
|
|
it.chunkIts[i] = chunkIt
|
|
|
|
}
|
|
|
|
return chunkIt
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:52:39 -07:00
|
|
|
// nopSeriesIterator implements Series Iterator. It never returns any values.
|
|
|
|
type nopSeriesIterator struct{}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// ValueAtTime implements SeriesIterator.
|
|
|
|
func (_ nopSeriesIterator) ValueAtTime(t clientmodel.Timestamp) metric.Values {
|
2014-10-14 04:52:39 -07:00
|
|
|
return metric.Values{}
|
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// BoundaryValues implements SeriesIterator.
|
|
|
|
func (_ nopSeriesIterator) BoundaryValues(in metric.Interval) metric.Values {
|
2014-10-14 04:52:39 -07:00
|
|
|
return metric.Values{}
|
|
|
|
}
|
|
|
|
|
2015-05-20 10:13:06 -07:00
|
|
|
// RangeValues implements SeriesIterator.
|
|
|
|
func (_ nopSeriesIterator) RangeValues(in metric.Interval) metric.Values {
|
2014-10-14 04:52:39 -07:00
|
|
|
return metric.Values{}
|
|
|
|
}
|