2013-02-08 09:03:26 -08: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.
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
package tiered
|
2013-02-08 09:03:26 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-11-08 08:43:05 -08:00
|
|
|
"os"
|
2013-03-06 17:16:39 -08:00
|
|
|
"sort"
|
2013-06-25 05:02:27 -07:00
|
|
|
"sync"
|
2013-02-08 09:03:26 -08:00
|
|
|
"time"
|
2013-05-22 10:06:06 -07:00
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
"github.com/golang/glog"
|
2013-12-11 17:21:37 -08:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2013-05-22 10:06:06 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
"github.com/prometheus/prometheus/stats"
|
2014-04-11 00:27:05 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/metric"
|
2013-05-22 10:06:06 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/raw/leveldb"
|
2013-08-07 14:28:11 -07:00
|
|
|
"github.com/prometheus/prometheus/utility"
|
2013-02-08 09:03:26 -08:00
|
|
|
)
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
type chunk metric.Values
|
2013-05-08 11:39:59 -07:00
|
|
|
|
|
|
|
// TruncateBefore returns a subslice of the original such that extraneous
|
|
|
|
// samples in the collection that occur before the provided time are
|
|
|
|
// dropped. The original slice is not mutated. It works with the assumption
|
|
|
|
// that consumers of these values could want preceding values if none would
|
|
|
|
// exist prior to the defined time.
|
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 06:35:02 -07:00
|
|
|
func (c chunk) TruncateBefore(t clientmodel.Timestamp) chunk {
|
2013-05-08 11:39:59 -07:00
|
|
|
index := sort.Search(len(c), func(i int) bool {
|
|
|
|
timestamp := c[i].Timestamp
|
|
|
|
|
|
|
|
return !timestamp.Before(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
switch index {
|
|
|
|
case 0:
|
|
|
|
return c
|
|
|
|
case len(c):
|
|
|
|
return c[len(c)-1:]
|
|
|
|
default:
|
|
|
|
return c[index-1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
type tieredStorageState uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
tieredStorageStarting tieredStorageState = iota
|
|
|
|
tieredStorageServing
|
|
|
|
tieredStorageDraining
|
|
|
|
tieredStorageStopping
|
|
|
|
)
|
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
// Ignore timeseries in queries that are more stale than this limit.
|
|
|
|
const stalenessLimit = time.Minute * 5
|
2013-06-06 09:16:22 -07:00
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// TieredStorage both persists samples and generates materialized views for
|
2013-02-08 09:03:26 -08:00
|
|
|
// queries.
|
2013-05-02 09:27:12 -07:00
|
|
|
type TieredStorage struct {
|
2013-06-05 01:40:39 -07:00
|
|
|
// mu is purely used for state transitions.
|
2013-06-06 01:32:00 -07:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
2013-05-07 01:18:19 -07:00
|
|
|
// BUG(matt): This introduces a Law of Demeter violation. Ugh.
|
2014-04-11 00:27:05 -07:00
|
|
|
DiskStorage *LevelDBPersistence
|
2013-05-07 01:18:19 -07:00
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
appendToDiskQueue chan clientmodel.Samples
|
2013-05-14 02:21:27 -07:00
|
|
|
|
2013-05-16 07:02:07 -07:00
|
|
|
memoryArena *memorySeriesStorage
|
2013-02-08 09:03:26 -08:00
|
|
|
memoryTTL time.Duration
|
2013-05-14 02:21:27 -07:00
|
|
|
flushMemoryInterval time.Duration
|
|
|
|
|
2013-08-26 10:12:43 -07:00
|
|
|
ViewQueue chan viewJob
|
2013-05-14 02:21:27 -07:00
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
draining chan chan<- bool
|
2013-05-14 02:21:27 -07:00
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
state tieredStorageState
|
2013-06-05 01:40:39 -07:00
|
|
|
|
|
|
|
memorySemaphore chan bool
|
2013-06-06 09:16:22 -07:00
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
wmCache *watermarkCache
|
2013-08-07 03:07:35 -07:00
|
|
|
|
|
|
|
Indexer MetricIndexer
|
2013-08-29 00:37:34 -07:00
|
|
|
|
|
|
|
flushSema chan bool
|
2013-08-29 06:15:22 -07:00
|
|
|
|
|
|
|
dtoSampleKeys *dtoSampleKeyList
|
|
|
|
sampleKeys *sampleKeyList
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// viewJob encapsulates a request to extract sample values from the datastore.
|
|
|
|
type viewJob struct {
|
2014-04-11 00:27:05 -07:00
|
|
|
builder metric.ViewRequestBuilder
|
|
|
|
output chan metric.View
|
2013-03-26 09:15:04 -07:00
|
|
|
abort chan bool
|
2013-02-08 09:03:26 -08:00
|
|
|
err chan error
|
2013-06-03 08:07:03 -07:00
|
|
|
stats *stats.TimerGroup
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2013-06-05 01:40:39 -07:00
|
|
|
const (
|
|
|
|
tieredMemorySemaphores = 5
|
|
|
|
)
|
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
const watermarkCacheLimit = 1024 * 1024
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// NewTieredStorage returns a TieredStorage object ready to use.
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
func NewTieredStorage(
|
|
|
|
appendToDiskQueueDepth,
|
|
|
|
viewQueueDepth uint,
|
|
|
|
flushMemoryInterval time.Duration,
|
|
|
|
memoryTTL time.Duration,
|
|
|
|
rootDirectory string,
|
|
|
|
) (*TieredStorage, error) {
|
2013-10-10 11:54:06 -07:00
|
|
|
if isDir, _ := utility.IsDir(rootDirectory); !isDir {
|
2013-11-08 08:43:05 -08:00
|
|
|
if err := os.MkdirAll(rootDirectory, 0755); err != nil {
|
2014-02-14 10:36:27 -08:00
|
|
|
return nil, fmt.Errorf("could not find or create metrics directory %s: %s", rootDirectory, err)
|
2013-11-08 08:43:05 -08:00
|
|
|
}
|
2013-10-10 11:54:06 -07:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
diskStorage, err := NewLevelDBPersistence(rootDirectory)
|
2013-02-08 09:03:26 -08:00
|
|
|
if err != nil {
|
2013-06-05 01:40:39 -07:00
|
|
|
return nil, err
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
wmCache := &watermarkCache{
|
|
|
|
C: utility.NewSynchronizedCache(utility.NewLRUCache(watermarkCacheLimit)),
|
|
|
|
}
|
|
|
|
|
|
|
|
memOptions := MemorySeriesOptions{
|
|
|
|
WatermarkCache: wmCache,
|
|
|
|
}
|
2013-06-06 09:16:22 -07:00
|
|
|
|
2013-06-05 01:40:39 -07:00
|
|
|
s := &TieredStorage{
|
2013-06-25 05:02:27 -07:00
|
|
|
appendToDiskQueue: make(chan clientmodel.Samples, appendToDiskQueueDepth),
|
2013-05-07 01:18:19 -07:00
|
|
|
DiskStorage: diskStorage,
|
2013-06-06 01:32:00 -07:00
|
|
|
draining: make(chan chan<- bool),
|
2013-02-08 09:03:26 -08:00
|
|
|
flushMemoryInterval: flushMemoryInterval,
|
2013-06-06 09:16:22 -07:00
|
|
|
memoryArena: NewMemorySeriesStorage(memOptions),
|
2013-02-08 09:03:26 -08:00
|
|
|
memoryTTL: memoryTTL,
|
2013-08-26 10:12:43 -07:00
|
|
|
ViewQueue: make(chan viewJob, viewQueueDepth),
|
2013-06-05 01:40:39 -07:00
|
|
|
|
|
|
|
memorySemaphore: make(chan bool, tieredMemorySemaphores),
|
2013-06-06 09:16:22 -07:00
|
|
|
|
|
|
|
wmCache: wmCache,
|
2013-08-29 00:37:34 -07:00
|
|
|
|
|
|
|
flushSema: make(chan bool, 1),
|
2013-08-29 06:15:22 -07:00
|
|
|
|
|
|
|
dtoSampleKeys: newDtoSampleKeyList(10),
|
|
|
|
sampleKeys: newSampleKeyList(10),
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-06-05 01:40:39 -07:00
|
|
|
|
|
|
|
for i := 0; i < tieredMemorySemaphores; i++ {
|
|
|
|
s.memorySemaphore <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// AppendSamples enqueues Samples for storage.
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) AppendSamples(samples clientmodel.Samples) (err error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
if t.state != tieredStorageServing {
|
2014-02-14 10:36:27 -08:00
|
|
|
return fmt.Errorf("storage is not serving")
|
2013-03-01 09:51:36 -08:00
|
|
|
}
|
|
|
|
|
2013-05-08 06:30:27 -07:00
|
|
|
t.memoryArena.AppendSamples(samples)
|
2013-12-11 17:21:37 -08:00
|
|
|
storedSamplesCount.IncrementBy(prometheus.NilLabels, float64(len(samples)))
|
2013-03-01 09:51:36 -08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Drain stops the storage subsystem, flushing all pending operations.
|
2013-06-06 01:32:00 -07:00
|
|
|
func (t *TieredStorage) Drain(drained chan<- bool) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
t.drain(drained)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TieredStorage) drain(drained chan<- bool) {
|
|
|
|
if t.state >= tieredStorageDraining {
|
2013-06-06 03:40:06 -07:00
|
|
|
panic("Illegal State: Supplemental drain requested.")
|
2013-06-06 01:32:00 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
t.state = tieredStorageDraining
|
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
glog.Info("Triggering drain...")
|
2013-06-06 03:40:06 -07:00
|
|
|
t.draining <- (drained)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
// NewViewRequestBuilder furnishes a ViewRequestBuilder for remarking what types
|
|
|
|
// of queries to perform.
|
|
|
|
func (t *TieredStorage) NewViewRequestBuilder() metric.ViewRequestBuilder {
|
|
|
|
return &viewRequestBuilder{storage: t}
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeView materializes a View according to a ViewRequestBuilder, subject to a
|
2014-02-14 10:36:27 -08:00
|
|
|
// timeout.
|
2014-04-11 00:27:05 -07:00
|
|
|
func (t *TieredStorage) makeView(builder metric.ViewRequestBuilder, deadline time.Duration, queryStats *stats.TimerGroup) (metric.View, error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
if t.state != tieredStorageServing {
|
2014-02-14 10:36:27 -08:00
|
|
|
return nil, fmt.Errorf("storage is not serving")
|
2013-03-01 09:51:36 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// The result channel needs a one-element buffer in case we have timed
|
2014-04-11 00:27:05 -07:00
|
|
|
// out in makeView, but the view rendering still completes afterwards
|
2014-02-14 10:36:27 -08:00
|
|
|
// and writes to the channel.
|
2014-04-11 00:27:05 -07:00
|
|
|
result := make(chan metric.View, 1)
|
2014-02-14 10:36:27 -08:00
|
|
|
// The abort channel needs a one-element buffer in case the view
|
|
|
|
// rendering has already exited and doesn't consume from the channel
|
|
|
|
// anymore.
|
2013-03-26 09:15:04 -07:00
|
|
|
abortChan := make(chan bool, 1)
|
2013-02-08 09:03:26 -08:00
|
|
|
errChan := make(chan error)
|
2013-06-03 08:07:03 -07:00
|
|
|
queryStats.GetTimer(stats.ViewQueueTime).Start()
|
2013-08-26 10:12:43 -07:00
|
|
|
t.ViewQueue <- viewJob{
|
2013-02-08 09:03:26 -08:00
|
|
|
builder: builder,
|
|
|
|
output: result,
|
2013-03-26 09:15:04 -07:00
|
|
|
abort: abortChan,
|
2013-02-08 09:03:26 -08:00
|
|
|
err: errChan,
|
2013-06-03 08:07:03 -07:00
|
|
|
stats: queryStats,
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2013-05-21 09:12:02 -07:00
|
|
|
case view := <-result:
|
|
|
|
return view, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, err
|
2013-02-08 09:03:26 -08:00
|
|
|
case <-time.After(deadline):
|
2013-03-26 09:15:04 -07:00
|
|
|
abortChan <- true
|
2014-02-14 10:36:27 -08:00
|
|
|
return nil, fmt.Errorf("fetching query data timed out after %s", deadline)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Serve starts serving requests.
|
2013-06-06 01:42:21 -07:00
|
|
|
func (t *TieredStorage) Serve(started chan<- bool) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.Lock()
|
|
|
|
if t.state != tieredStorageStarting {
|
|
|
|
panic("Illegal State: Attempted to restart TieredStorage.")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.state = tieredStorageServing
|
|
|
|
t.mu.Unlock()
|
|
|
|
|
2013-04-29 02:17:56 -07:00
|
|
|
flushMemoryTicker := time.NewTicker(t.flushMemoryInterval)
|
|
|
|
defer flushMemoryTicker.Stop()
|
2013-05-14 02:21:27 -07:00
|
|
|
queueReportTicker := time.NewTicker(time.Second)
|
|
|
|
defer queueReportTicker.Stop()
|
2013-03-01 09:51:36 -08:00
|
|
|
|
2013-04-16 08:13:29 -07:00
|
|
|
go func() {
|
2013-05-14 02:21:27 -07:00
|
|
|
for _ = range queueReportTicker.C {
|
2013-04-25 04:04:45 -07:00
|
|
|
t.reportQueues()
|
2013-04-16 08:13:29 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-06-06 01:42:21 -07:00
|
|
|
started <- true
|
2013-04-16 08:13:29 -07:00
|
|
|
for {
|
2013-02-08 09:03:26 -08:00
|
|
|
select {
|
2013-04-29 02:17:56 -07:00
|
|
|
case <-flushMemoryTicker.C:
|
2013-08-29 00:37:34 -07:00
|
|
|
select {
|
|
|
|
case t.flushSema <- true:
|
|
|
|
go func() {
|
|
|
|
t.flushMemory(t.memoryTTL)
|
|
|
|
<-t.flushSema
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
glog.Warning("Backlogging on flush...")
|
|
|
|
}
|
2013-08-26 10:12:43 -07:00
|
|
|
case viewRequest := <-t.ViewQueue:
|
2013-06-05 01:40:39 -07:00
|
|
|
<-t.memorySemaphore
|
2014-02-13 06:24:43 -08:00
|
|
|
viewRequest.stats.GetTimer(stats.ViewQueueTime).Stop()
|
2013-06-05 01:40:39 -07:00
|
|
|
go t.renderView(viewRequest)
|
2013-03-21 09:53:57 -07:00
|
|
|
case drainingDone := <-t.draining:
|
2013-05-08 06:30:27 -07:00
|
|
|
t.Flush()
|
2013-03-21 09:53:57 -07:00
|
|
|
drainingDone <- true
|
2013-08-29 00:37:34 -07:00
|
|
|
|
2013-04-15 03:45:45 -07:00
|
|
|
return
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 06:12:33 -07:00
|
|
|
func (t *TieredStorage) reportQueues() {
|
2013-03-06 17:16:39 -08:00
|
|
|
queueSizes.Set(map[string]string{"queue": "append_to_disk", "facet": "occupancy"}, float64(len(t.appendToDiskQueue)))
|
|
|
|
queueSizes.Set(map[string]string{"queue": "append_to_disk", "facet": "capacity"}, float64(cap(t.appendToDiskQueue)))
|
|
|
|
|
2013-08-26 10:12:43 -07:00
|
|
|
queueSizes.Set(map[string]string{"queue": "view_generation", "facet": "occupancy"}, float64(len(t.ViewQueue)))
|
|
|
|
queueSizes.Set(map[string]string{"queue": "view_generation", "facet": "capacity"}, float64(cap(t.ViewQueue)))
|
2013-03-06 17:16:39 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Flush flushes all samples to disk.
|
2013-05-07 06:12:33 -07:00
|
|
|
func (t *TieredStorage) Flush() {
|
2013-08-29 00:37:34 -07:00
|
|
|
t.flushSema <- true
|
2013-05-22 08:31:49 -07:00
|
|
|
t.flushMemory(0)
|
2013-08-29 00:37:34 -07:00
|
|
|
<-t.flushSema
|
2013-03-06 17:16:39 -08:00
|
|
|
}
|
|
|
|
|
2013-05-22 08:31:49 -07:00
|
|
|
func (t *TieredStorage) flushMemory(ttl time.Duration) {
|
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 06:35:02 -07:00
|
|
|
flushOlderThan := clientmodel.Now().Add(-1 * ttl)
|
2013-05-22 08:31:49 -07:00
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
glog.Info("Flushing samples to disk...")
|
2013-06-19 02:55:34 -07:00
|
|
|
t.memoryArena.Flush(flushOlderThan, t.appendToDiskQueue)
|
2013-05-22 08:31:49 -07:00
|
|
|
|
|
|
|
queueLength := len(t.appendToDiskQueue)
|
|
|
|
if queueLength > 0 {
|
2013-06-25 05:02:27 -07:00
|
|
|
samples := clientmodel.Samples{}
|
2013-05-22 08:31:49 -07:00
|
|
|
for i := 0; i < queueLength; i++ {
|
|
|
|
chunk := <-t.appendToDiskQueue
|
|
|
|
samples = append(samples, chunk...)
|
|
|
|
}
|
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
glog.Infof("Writing %d samples...", len(samples))
|
2013-05-22 08:31:49 -07:00
|
|
|
t.DiskStorage.AppendSamples(samples)
|
|
|
|
}
|
|
|
|
|
2013-08-12 08:18:02 -07:00
|
|
|
glog.Info("Done flushing.")
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Close stops serving, flushes all pending operations, and frees all resources.
|
2013-05-21 09:12:02 -07:00
|
|
|
func (t *TieredStorage) Close() {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
t.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TieredStorage) close() {
|
2013-06-06 01:32:00 -07:00
|
|
|
if t.state == tieredStorageStopping {
|
|
|
|
panic("Illegal State: Attempted to restop TieredStorage.")
|
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
drained := make(chan bool)
|
|
|
|
t.drain(drained)
|
|
|
|
<-drained
|
|
|
|
|
|
|
|
t.memoryArena.Close()
|
|
|
|
t.DiskStorage.Close()
|
2014-02-14 10:36:27 -08:00
|
|
|
// BUG(matt): There is a probability that pending items may hang here
|
|
|
|
// and not get flushed.
|
2013-05-21 09:12:02 -07:00
|
|
|
close(t.appendToDiskQueue)
|
2013-08-26 10:12:43 -07:00
|
|
|
close(t.ViewQueue)
|
2013-06-06 09:16:22 -07:00
|
|
|
t.wmCache.Clear()
|
2013-06-06 01:32:00 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
t.dtoSampleKeys.Close()
|
|
|
|
t.sampleKeys.Close()
|
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
t.state = tieredStorageStopping
|
2013-02-08 09:03:26 -08: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 06:35:02 -07:00
|
|
|
func (t *TieredStorage) seriesTooOld(f *clientmodel.Fingerprint, i clientmodel.Timestamp) (bool, error) {
|
2013-06-06 09:16:22 -07:00
|
|
|
// BUG(julius): Make this configurable by query layer.
|
|
|
|
i = i.Add(-stalenessLimit)
|
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
wm, cacheHit, _ := t.wmCache.Get(f)
|
2013-06-21 01:16:41 -07:00
|
|
|
if !cacheHit {
|
2013-06-21 09:34:08 -07:00
|
|
|
if t.memoryArena.HasFingerprint(f) {
|
|
|
|
samples := t.memoryArena.CloneSamples(f)
|
|
|
|
if len(samples) > 0 {
|
2013-06-21 12:17:50 -07:00
|
|
|
newest := samples[len(samples)-1].Timestamp
|
2013-08-07 14:28:11 -07:00
|
|
|
t.wmCache.Put(f, &watermarks{High: newest})
|
2013-06-21 09:34:08 -07:00
|
|
|
|
|
|
|
return newest.Before(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 05:18:02 -07:00
|
|
|
highTime, diskHit, err := t.DiskStorage.MetricHighWatermarks.Get(f)
|
2013-06-06 09:16:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2013-06-21 01:16:41 -07:00
|
|
|
|
|
|
|
if diskHit {
|
2013-08-07 14:28:11 -07:00
|
|
|
t.wmCache.Put(f, &watermarks{High: highTime})
|
2013-06-21 01:16:41 -07:00
|
|
|
|
2013-08-06 05:18:02 -07:00
|
|
|
return highTime.Before(i), nil
|
2013-06-21 01:16:41 -07:00
|
|
|
}
|
|
|
|
|
2013-08-07 14:28:11 -07:00
|
|
|
t.wmCache.Put(f, &watermarks{})
|
2013-06-27 08:01:31 -07:00
|
|
|
return true, nil
|
2013-06-06 09:16:22 -07:00
|
|
|
}
|
2013-06-21 01:16:41 -07:00
|
|
|
|
2013-06-06 09:16:22 -07:00
|
|
|
return wm.High.Before(i), nil
|
|
|
|
}
|
|
|
|
|
2013-05-07 06:12:33 -07:00
|
|
|
func (t *TieredStorage) renderView(viewJob viewJob) {
|
2013-03-16 01:30:31 -07:00
|
|
|
// Telemetry.
|
|
|
|
var err error
|
2013-03-01 09:51:36 -08:00
|
|
|
begin := time.Now()
|
|
|
|
defer func() {
|
2013-06-05 01:40:39 -07:00
|
|
|
t.memorySemaphore <- true
|
|
|
|
|
2013-03-11 14:21:25 -07:00
|
|
|
duration := time.Since(begin)
|
2013-02-08 09:03:26 -08:00
|
|
|
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
recordOutcome(
|
|
|
|
duration,
|
|
|
|
err,
|
|
|
|
map[string]string{operation: renderView, result: success},
|
|
|
|
map[string]string{operation: renderView, result: failure},
|
|
|
|
)
|
2013-03-01 09:51:36 -08:00
|
|
|
}()
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-05-08 06:30:27 -07:00
|
|
|
view := newView()
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
var iterator leveldb.Iterator
|
|
|
|
diskPresent := true
|
2013-08-29 06:15:22 -07:00
|
|
|
|
|
|
|
firstBlock, _ := t.sampleKeys.Get()
|
|
|
|
defer t.sampleKeys.Give(firstBlock)
|
|
|
|
|
|
|
|
lastBlock, _ := t.sampleKeys.Get()
|
|
|
|
defer t.sampleKeys.Give(lastBlock)
|
|
|
|
|
|
|
|
sampleKeyDto, _ := t.dtoSampleKeys.Get()
|
|
|
|
defer t.dtoSampleKeys.Give(sampleKeyDto)
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2014-03-10 07:04:42 -07:00
|
|
|
defer func() {
|
|
|
|
// Give back all ops not yet popped.
|
|
|
|
for viewJob.builder.HasOp() {
|
|
|
|
giveBackOp(viewJob.builder.PopOp())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
extractionTimer := viewJob.stats.GetTimer(stats.ViewDataExtractionTime).Start()
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
for viewJob.builder.HasOp() {
|
|
|
|
op := viewJob.builder.PopOp()
|
2014-03-07 05:33:13 -08:00
|
|
|
defer giveBackOp(op)
|
|
|
|
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
fp := op.Fingerprint()
|
|
|
|
old, err := t.seriesTooOld(fp, op.CurrentTime())
|
2013-06-06 09:16:22 -07:00
|
|
|
if err != nil {
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
glog.Errorf("Error getting watermark from cache for %s: %s", fp, err)
|
2013-06-06 09:16:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if old {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
memValues := t.memoryArena.CloneSamples(fp)
|
2013-05-21 09:12:02 -07:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
for !op.Consumed() {
|
2014-04-11 00:27:05 -07:00
|
|
|
// Abort the view rendering if the caller (makeView) has timed out.
|
2014-03-06 15:54:28 -08:00
|
|
|
if len(viewJob.abort) > 0 {
|
|
|
|
return
|
|
|
|
}
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
// Load data value chunk(s) around the current time.
|
|
|
|
targetTime := op.CurrentTime()
|
|
|
|
|
|
|
|
currentChunk := chunk{}
|
|
|
|
// If we aimed before the oldest value in memory, load more data from disk.
|
|
|
|
if (len(memValues) == 0 || memValues.FirstTimeAfter(targetTime)) && diskPresent {
|
|
|
|
if iterator == nil {
|
|
|
|
// Get a single iterator that will be used for all data extraction
|
|
|
|
// below.
|
|
|
|
iterator, _ = t.DiskStorage.MetricSamples.NewIterator(true)
|
|
|
|
defer iterator.Close()
|
|
|
|
if diskPresent = iterator.SeekToLast(); diskPresent {
|
2013-08-29 06:15:22 -07:00
|
|
|
if err := iterator.Key(sampleKeyDto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
lastBlock.Load(sampleKeyDto)
|
|
|
|
|
|
|
|
if !iterator.SeekToFirst() {
|
|
|
|
diskPresent = false
|
|
|
|
} else {
|
|
|
|
if err := iterator.Key(sampleKeyDto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstBlock.Load(sampleKeyDto)
|
|
|
|
}
|
2013-05-22 10:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
if diskPresent {
|
|
|
|
diskTimer := viewJob.stats.GetTimer(stats.ViewDiskExtractionTime).Start()
|
|
|
|
diskValues, expired := t.loadChunkAroundTime(
|
|
|
|
iterator,
|
|
|
|
fp,
|
|
|
|
targetTime,
|
|
|
|
firstBlock,
|
|
|
|
lastBlock,
|
|
|
|
)
|
|
|
|
if expired {
|
|
|
|
diskPresent = false
|
|
|
|
}
|
|
|
|
diskTimer.Stop()
|
2013-04-18 16:00:57 -07:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
// If we aimed past the newest value on disk,
|
|
|
|
// combine it with the next value from memory.
|
|
|
|
if len(diskValues) == 0 {
|
|
|
|
currentChunk = chunk(memValues)
|
2013-05-22 10:06:06 -07:00
|
|
|
} else {
|
2014-03-06 15:54:28 -08:00
|
|
|
if len(memValues) > 0 && diskValues.LastTimeBefore(targetTime) {
|
|
|
|
latestDiskValue := diskValues[len(diskValues)-1:]
|
|
|
|
currentChunk = append(chunk(latestDiskValue), chunk(memValues)...)
|
|
|
|
} else {
|
|
|
|
currentChunk = chunk(diskValues)
|
|
|
|
}
|
2013-05-22 10:06:06 -07:00
|
|
|
}
|
2014-03-06 15:54:28 -08:00
|
|
|
} else {
|
|
|
|
currentChunk = chunk(memValues)
|
2013-04-18 16:00:57 -07:00
|
|
|
}
|
|
|
|
} else {
|
2013-05-08 11:39:59 -07:00
|
|
|
currentChunk = chunk(memValues)
|
2013-04-18 16:00:57 -07:00
|
|
|
}
|
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
// There's no data at all for this fingerprint, so stop processing.
|
|
|
|
if len(currentChunk) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2013-04-24 02:02:51 -07:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
currentChunk = currentChunk.TruncateBefore(targetTime)
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
lastChunkTime := currentChunk[len(currentChunk)-1].Timestamp
|
|
|
|
if lastChunkTime.After(targetTime) {
|
|
|
|
targetTime = lastChunkTime
|
|
|
|
}
|
2013-08-29 06:15:22 -07:00
|
|
|
|
2014-03-06 15:54:28 -08:00
|
|
|
if op.CurrentTime().After(targetTime) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract all needed data from the current chunk and append the
|
|
|
|
// extracted samples to the materialized view.
|
|
|
|
for !op.Consumed() && !op.CurrentTime().After(targetTime) {
|
2014-04-11 00:27:05 -07:00
|
|
|
view.appendSamples(fp, op.ExtractSamples(metric.Values(currentChunk)))
|
2014-03-06 15:54:28 -08:00
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
2013-06-03 08:07:03 -07:00
|
|
|
extractionTimer.Stop()
|
2013-03-16 01:30:31 -07:00
|
|
|
|
|
|
|
viewJob.output <- view
|
|
|
|
return
|
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
|
Remove the multi-op-per-fingerprint capability.
Currently, rendering a view is capable of handling multiple ops for
the same fingerprint efficiently. However, this capability requires a
lot of complexity in the code, which we are not using at all because
the way we assemble a viewRequest will never have more than one
operation per fingerprint.
This commit weeds out the said capability, along with all the code
needed for it. It is still possible to have more than one operation
for the same fingerprint, it will just be handled in a less efficient
way (as proven by the unit tests).
As a result, scanjob.go could be removed entirely.
This commit also contains a few related refactorings and removals of
dead code in operation.go, view,go, and freelist.go. Also, the
docstrings received some love.
Change-Id: I032b976e0880151c3f3fdb3234fb65e484f0e2e5
2014-02-27 11:09:00 -08:00
|
|
|
func (t *TieredStorage) loadChunkAroundTime(
|
|
|
|
iterator leveldb.Iterator,
|
|
|
|
fingerprint *clientmodel.Fingerprint,
|
|
|
|
ts clientmodel.Timestamp,
|
|
|
|
firstBlock,
|
|
|
|
lastBlock *SampleKey,
|
2014-04-11 00:27:05 -07:00
|
|
|
) (chunk metric.Values, expired bool) {
|
2013-08-22 08:40:23 -07:00
|
|
|
if fingerprint.Less(firstBlock.Fingerprint) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if lastBlock.Fingerprint.Less(fingerprint) {
|
|
|
|
return nil, true
|
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
seekingKey, _ := t.sampleKeys.Get()
|
|
|
|
defer t.sampleKeys.Give(seekingKey)
|
|
|
|
|
|
|
|
seekingKey.Fingerprint = fingerprint
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
if fingerprint.Equal(firstBlock.Fingerprint) && ts.Before(firstBlock.FirstTimestamp) {
|
|
|
|
seekingKey.FirstTimestamp = firstBlock.FirstTimestamp
|
|
|
|
} else if fingerprint.Equal(lastBlock.Fingerprint) && ts.After(lastBlock.FirstTimestamp) {
|
|
|
|
seekingKey.FirstTimestamp = lastBlock.FirstTimestamp
|
2013-03-16 01:30:31 -07:00
|
|
|
} else {
|
2013-08-22 08:40:23 -07:00
|
|
|
seekingKey.FirstTimestamp = ts
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-03-01 09:51:36 -08:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
dto, _ := t.dtoSampleKeys.Get()
|
|
|
|
defer t.dtoSampleKeys.Give(dto)
|
2013-03-01 09:51:36 -08:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
seekingKey.Dump(dto)
|
|
|
|
if !iterator.Seek(dto) {
|
2013-08-22 08:40:23 -07:00
|
|
|
return chunk, true
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
var foundValues metric.Values
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if err := iterator.Key(dto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
seekingKey.Load(dto)
|
2013-08-22 08:40:23 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if seekingKey.Fingerprint.Equal(fingerprint) {
|
2013-08-22 08:40:23 -07:00
|
|
|
// Figure out if we need to rewind by one block.
|
|
|
|
// Imagine the following supertime blocks with time ranges:
|
|
|
|
//
|
|
|
|
// Block 1: ft 1000 - lt 1009 <data>
|
|
|
|
// Block 1: ft 1010 - lt 1019 <data>
|
|
|
|
//
|
|
|
|
// If we are aiming to find time 1005, we would first seek to the block with
|
|
|
|
// supertime 1010, then need to rewind by one block by virtue of LevelDB
|
|
|
|
// iterator seek behavior.
|
|
|
|
//
|
|
|
|
// Only do the rewind if there is another chunk before this one.
|
2013-08-29 06:15:22 -07:00
|
|
|
if !seekingKey.MayContain(ts) {
|
2014-04-15 11:49:09 -07:00
|
|
|
postValues := unmarshalValues(iterator.RawValue(), nil)
|
2013-08-29 06:15:22 -07:00
|
|
|
if !seekingKey.Equal(firstBlock) {
|
2013-08-22 08:40:23 -07:00
|
|
|
if !iterator.Previous() {
|
|
|
|
panic("This should never return false.")
|
|
|
|
}
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if err := iterator.Key(dto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
seekingKey.Load(dto)
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if !seekingKey.Fingerprint.Equal(fingerprint) {
|
2013-08-22 08:40:23 -07:00
|
|
|
return postValues, false
|
|
|
|
}
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2014-04-15 11:49:09 -07:00
|
|
|
foundValues = unmarshalValues(iterator.RawValue(), nil)
|
2013-08-22 08:40:23 -07:00
|
|
|
foundValues = append(foundValues, postValues...)
|
|
|
|
return foundValues, false
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 08:40:23 -07:00
|
|
|
|
2014-04-15 11:49:09 -07:00
|
|
|
foundValues = unmarshalValues(iterator.RawValue(), nil)
|
2013-08-22 08:40:23 -07:00
|
|
|
return foundValues, false
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if fingerprint.Less(seekingKey.Fingerprint) {
|
|
|
|
if !seekingKey.Equal(firstBlock) {
|
2013-08-22 08:40:23 -07:00
|
|
|
if !iterator.Previous() {
|
|
|
|
panic("This should never return false.")
|
|
|
|
}
|
2013-03-06 17:16:39 -08:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if err := iterator.Key(dto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
seekingKey.Load(dto)
|
2013-08-22 08:40:23 -07:00
|
|
|
|
2013-08-29 06:15:22 -07:00
|
|
|
if !seekingKey.Fingerprint.Equal(fingerprint) {
|
2013-08-22 08:40:23 -07:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2014-04-15 11:49:09 -07:00
|
|
|
foundValues = unmarshalValues(iterator.RawValue(), nil)
|
2013-08-22 08:40:23 -07:00
|
|
|
return foundValues, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("illegal state: violated sort invariant")
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-03-21 09:59:42 -07:00
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// GetAllValuesForLabel gets all label values that are associated with the
|
|
|
|
// provided label name.
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) GetAllValuesForLabel(labelName clientmodel.LabelName) (clientmodel.LabelValues, error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
|
|
|
|
if t.state != tieredStorageServing {
|
|
|
|
panic("Illegal State: Attempted to query non-running TieredStorage.")
|
|
|
|
}
|
|
|
|
|
2013-05-07 01:18:19 -07:00
|
|
|
diskValues, err := t.DiskStorage.GetAllValuesForLabel(labelName)
|
2013-03-25 05:04:47 -07:00
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2013-03-26 03:45:56 -07:00
|
|
|
memoryValues, err := t.memoryArena.GetAllValuesForLabel(labelName)
|
2013-03-25 05:04:47 -07:00
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
valueSet := map[clientmodel.LabelValue]bool{}
|
|
|
|
values := clientmodel.LabelValues{}
|
2013-03-26 03:45:56 -07:00
|
|
|
for _, value := range append(diskValues, memoryValues...) {
|
2013-03-26 06:46:02 -07:00
|
|
|
if !valueSet[value] {
|
|
|
|
values = append(values, value)
|
|
|
|
valueSet[value] = true
|
|
|
|
}
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
return values, nil
|
2013-03-21 09:59:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
// GetFingerprintsForLabelMatchers gets all of the metric fingerprints that are
|
|
|
|
// associated with the provided label matchers.
|
2014-04-11 00:27:05 -07:00
|
|
|
func (t *TieredStorage) GetFingerprintsForLabelMatchers(matchers metric.LabelMatchers) (clientmodel.Fingerprints, error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
|
|
|
|
if t.state != tieredStorageServing {
|
|
|
|
panic("Illegal State: Attempted to query non-running TieredStorage.")
|
|
|
|
}
|
|
|
|
|
2014-03-28 03:58:47 -07:00
|
|
|
memFingerprints, err := t.memoryArena.GetFingerprintsForLabelMatchers(matchers)
|
2013-03-25 05:04:47 -07:00
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2014-03-28 03:58:47 -07:00
|
|
|
diskFingerprints, err := t.DiskStorage.GetFingerprintsForLabelMatchers(matchers)
|
2013-03-25 05:04:47 -07:00
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
fingerprintSet := map[clientmodel.Fingerprint]bool{}
|
2013-03-25 05:04:47 -07:00
|
|
|
for _, fingerprint := range append(memFingerprints, diskFingerprints...) {
|
2013-05-17 03:58:15 -07:00
|
|
|
fingerprintSet[*fingerprint] = true
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2013-06-25 05:02:27 -07:00
|
|
|
fingerprints := clientmodel.Fingerprints{}
|
2013-03-25 05:04:47 -07:00
|
|
|
for fingerprint := range fingerprintSet {
|
2013-05-17 03:58:15 -07:00
|
|
|
fpCopy := fingerprint
|
|
|
|
fingerprints = append(fingerprints, &fpCopy)
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
|
|
|
|
2013-06-06 01:32:00 -07:00
|
|
|
return fingerprints, nil
|
2013-03-21 09:59:42 -07:00
|
|
|
}
|
|
|
|
|
2014-04-11 00:27:05 -07:00
|
|
|
// Get all of the label values that are associated with a given label name.
|
|
|
|
func (t *TieredStorage) GetLabelValuesForLabelName(clientmodel.LabelName) (clientmodel.LabelValues, error) {
|
|
|
|
// TODO(julius): Implement this or decide what to do with this
|
|
|
|
// Persistence interface method. It's currently unused on the
|
|
|
|
// TieredStorage, but used on the LevelDBPersistence and the
|
|
|
|
// memorySeriesStorage.
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// GetMetricForFingerprint gets the metric associated with the provided
|
|
|
|
// fingerprint.
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) GetMetricForFingerprint(f *clientmodel.Fingerprint) (clientmodel.Metric, error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
|
|
|
|
if t.state != tieredStorageServing {
|
|
|
|
panic("Illegal State: Attempted to query non-running TieredStorage.")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := t.memoryArena.GetMetricForFingerprint(f)
|
2013-03-25 05:04:47 -07:00
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
|
|
|
if m == nil {
|
2013-05-07 01:18:19 -07:00
|
|
|
m, err = t.DiskStorage.GetMetricForFingerprint(f)
|
2013-06-18 05:08:58 -07:00
|
|
|
t.memoryArena.CreateEmptySeries(m)
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2013-06-06 01:32:00 -07:00
|
|
|
return m, err
|
2013-03-21 09:59:42 -07:00
|
|
|
}
|