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.
|
|
|
|
|
|
|
|
package metric
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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-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"
|
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
|
|
|
)
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
type chunk 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.
|
|
|
|
func (c chunk) TruncateBefore(t time.Time) chunk {
|
|
|
|
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.
|
|
|
|
DiskStorage *LevelDBMetricPersistence
|
|
|
|
|
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 {
|
|
|
|
builder ViewRequestBuilder
|
|
|
|
output chan 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
|
|
|
|
|
2013-10-10 11:54:06 -07:00
|
|
|
func NewTieredStorage(appendToDiskQueueDepth, viewQueueDepth uint, flushMemoryInterval, memoryTTL time.Duration, rootDirectory string) (*TieredStorage, error) {
|
|
|
|
if isDir, _ := utility.IsDir(rootDirectory); !isDir {
|
|
|
|
return nil, fmt.Errorf("Could not find metrics directory %s", rootDirectory)
|
|
|
|
}
|
|
|
|
|
|
|
|
diskStorage, err := NewLevelDBMetricPersistence(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
|
|
|
}
|
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// 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 {
|
|
|
|
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-03-01 09:51:36 -08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-05-07 06:12:33 -07:00
|
|
|
// Enqueues a ViewRequestBuilder for materialization, subject to a timeout.
|
2013-06-03 08:07:03 -07:00
|
|
|
func (t *TieredStorage) MakeView(builder ViewRequestBuilder, deadline time.Duration, queryStats *stats.TimerGroup) (View, error) {
|
2013-06-06 01:32:00 -07:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
|
|
|
if t.state != tieredStorageServing {
|
|
|
|
return nil, fmt.Errorf("Storage is not serving")
|
2013-03-01 09:51:36 -08:00
|
|
|
}
|
|
|
|
|
2013-03-26 09:15:04 -07:00
|
|
|
// The result channel needs a one-element buffer in case we have timed out in
|
|
|
|
// MakeView, but the view rendering still completes afterwards and writes to
|
|
|
|
// the channel.
|
|
|
|
result := make(chan View, 1)
|
|
|
|
// The abort channel needs a one-element buffer in case the view rendering
|
|
|
|
// has already exited and doesn't consume from the channel anymore.
|
|
|
|
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
|
2013-05-21 09:12:02 -07:00
|
|
|
return nil, fmt.Errorf("MakeView timed out after %s.", deadline)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// 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-03 08:07:03 -07:00
|
|
|
viewRequest.stats.GetTimer(stats.ViewQueueTime).Stop()
|
2013-06-05 01:40:39 -07:00
|
|
|
<-t.memorySemaphore
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2013-06-19 02:55:34 -07:00
|
|
|
flushOlderThan := time.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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) seriesTooOld(f *clientmodel.Fingerprint, i time.Time) (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
|
|
|
|
2013-03-16 01:30:31 -07: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-06-03 08:07:03 -07:00
|
|
|
scanJobsTimer := viewJob.stats.GetTimer(stats.ViewScanJobsTime).Start()
|
2013-05-08 06:30:27 -07:00
|
|
|
scans := viewJob.builder.ScanJobs()
|
2013-06-03 08:07:03 -07:00
|
|
|
scanJobsTimer.Stop()
|
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
|
|
|
|
2013-06-03 08:07:03 -07:00
|
|
|
extractionTimer := viewJob.stats.GetTimer(stats.ViewDataExtractionTime).Start()
|
2013-03-01 09:51:36 -08:00
|
|
|
for _, scanJob := range scans {
|
2013-06-06 09:16:22 -07:00
|
|
|
old, err := t.seriesTooOld(scanJob.fingerprint, *scanJob.operations[0].CurrentTime())
|
|
|
|
if err != nil {
|
2013-08-12 08:18:02 -07:00
|
|
|
glog.Errorf("Error getting watermark from cache for %s: %s", scanJob.fingerprint, err)
|
2013-06-06 09:16:22 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if old {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
standingOps := scanJob.operations
|
2013-05-21 09:12:02 -07:00
|
|
|
memValues := t.memoryArena.CloneSamples(scanJob.fingerprint)
|
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
for len(standingOps) > 0 {
|
2013-03-26 09:15:04 -07:00
|
|
|
// Abort the view rendering if the caller (MakeView) has timed out.
|
|
|
|
if len(viewJob.abort) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
// Load data value chunk(s) around the first standing op's current time.
|
2013-04-18 16:00:57 -07:00
|
|
|
targetTime := *standingOps[0].CurrentTime()
|
|
|
|
|
2013-05-08 11:39:59 -07:00
|
|
|
currentChunk := chunk{}
|
2013-04-18 16:00:57 -07:00
|
|
|
// If we aimed before the oldest value in memory, load more data from disk.
|
2013-08-22 08:40:23 -07:00
|
|
|
if (len(memValues) == 0 || memValues.FirstTimeAfter(targetTime)) && diskPresent {
|
|
|
|
if iterator == nil {
|
|
|
|
// Get a single iterator that will be used for all data extraction
|
|
|
|
// below.
|
2013-08-29 06:15:22 -07:00
|
|
|
iterator, _ = t.DiskStorage.MetricSamples.NewIterator(true)
|
2013-08-22 08:40:23 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBlock.Load(sampleKeyDto)
|
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
if !iterator.SeekToFirst() {
|
|
|
|
diskPresent = false
|
|
|
|
} else {
|
2013-08-29 06:15:22 -07:00
|
|
|
if err := iterator.Key(sampleKeyDto); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstBlock.Load(sampleKeyDto)
|
2013-08-22 08:40:23 -07:00
|
|
|
}
|
2013-05-22 10:06:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
if diskPresent {
|
2013-06-03 08:07:03 -07:00
|
|
|
diskTimer := viewJob.stats.GetTimer(stats.ViewDiskExtractionTime).Start()
|
2013-08-22 08:40:23 -07:00
|
|
|
diskValues, expired := t.loadChunkAroundTime(iterator, scanJob.fingerprint, targetTime, firstBlock, lastBlock)
|
|
|
|
if expired {
|
|
|
|
diskPresent = false
|
|
|
|
}
|
2013-06-03 08:07:03 -07:00
|
|
|
diskTimer.Stop()
|
2013-04-18 16:00:57 -07:00
|
|
|
|
2013-05-22 10:06:06 -07:00
|
|
|
// If we aimed past the newest value on disk, combine it with the next value from memory.
|
2013-08-22 08:40:23 -07:00
|
|
|
if len(diskValues) == 0 {
|
|
|
|
currentChunk = chunk(memValues)
|
2013-05-22 10:06:06 -07:00
|
|
|
} else {
|
2013-08-22 08:40:23 -07: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
|
|
|
}
|
2013-04-18 16:00:57 -07:00
|
|
|
} else {
|
2013-05-22 10:06:06 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// There's no data at all for this fingerprint, so stop processing ops for it.
|
2013-05-08 11:39:59 -07:00
|
|
|
if len(currentChunk) == 0 {
|
2013-04-18 16:00:57 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-05-08 11:39:59 -07:00
|
|
|
currentChunk = currentChunk.TruncateBefore(targetTime)
|
2013-04-24 03:42:58 -07:00
|
|
|
|
2013-05-08 11:39:59 -07:00
|
|
|
lastChunkTime := currentChunk[len(currentChunk)-1].Timestamp
|
2013-04-18 16:00:57 -07:00
|
|
|
if lastChunkTime.After(targetTime) {
|
|
|
|
targetTime = lastChunkTime
|
2013-03-01 09:51:36 -08:00
|
|
|
}
|
2013-02-08 09:03:26 -08:00
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
// For each op, extract all needed data from the current chunk.
|
2013-06-25 05:02:27 -07:00
|
|
|
out := Values{}
|
2013-03-16 01:30:31 -07:00
|
|
|
for _, op := range standingOps {
|
2013-04-18 16:00:57 -07:00
|
|
|
if op.CurrentTime().After(targetTime) {
|
2013-03-16 01:30:31 -07:00
|
|
|
break
|
|
|
|
}
|
2013-04-24 02:02:51 -07:00
|
|
|
|
2013-05-08 11:39:59 -07:00
|
|
|
currentChunk = currentChunk.TruncateBefore(*(op.CurrentTime()))
|
2013-04-24 02:02:51 -07:00
|
|
|
|
2013-05-07 05:25:01 -07:00
|
|
|
for !op.Consumed() && !op.CurrentTime().After(targetTime) {
|
2013-06-25 05:02:27 -07:00
|
|
|
out = op.ExtractSamples(Values(currentChunk))
|
2013-03-16 01:30:31 -07:00
|
|
|
|
2013-05-28 05:36:03 -07:00
|
|
|
// Append the extracted samples to the materialized view.
|
|
|
|
view.appendSamples(scanJob.fingerprint, out)
|
|
|
|
}
|
2013-05-23 04:36:22 -07:00
|
|
|
}
|
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
// Throw away standing ops which are finished.
|
|
|
|
filteredOps := ops{}
|
|
|
|
for _, op := range standingOps {
|
2013-05-07 05:25:01 -07:00
|
|
|
if !op.Consumed() {
|
2013-03-16 01:30:31 -07:00
|
|
|
filteredOps = append(filteredOps, op)
|
2013-08-29 06:15:22 -07:00
|
|
|
continue
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
2013-08-29 06:15:22 -07:00
|
|
|
|
|
|
|
giveBackOp(op)
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
standingOps = filteredOps
|
|
|
|
|
|
|
|
// Sort ops by start time again, since they might be slightly off now.
|
|
|
|
// For example, consider a current chunk of values and two interval ops
|
2013-03-19 06:25:38 -07:00
|
|
|
// with different interval lengths. Their states after the cycle above
|
2013-03-16 01:30:31 -07:00
|
|
|
// could be:
|
|
|
|
//
|
|
|
|
// (C = current op time)
|
|
|
|
//
|
|
|
|
// Chunk: [ X X X X X ]
|
|
|
|
// Op 1: [ X X C . . . ]
|
|
|
|
// Op 2: [ X X C . . .]
|
|
|
|
//
|
|
|
|
// Op 2 now has an earlier current time than Op 1.
|
2013-03-16 01:41:43 -07:00
|
|
|
sort.Sort(startsAtSort{standingOps})
|
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
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
func (t *TieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, fingerprint *clientmodel.Fingerprint, ts time.Time, firstBlock, lastBlock *SampleKey) (chunk Values, expired bool) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
var foundValues 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) {
|
2013-08-22 08:40:23 -07:00
|
|
|
postValues, _ := extractSampleValues(iterator)
|
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
|
|
|
|
2013-08-22 08:40:23 -07:00
|
|
|
foundValues, _ = extractSampleValues(iterator)
|
|
|
|
foundValues = append(foundValues, postValues...)
|
|
|
|
return foundValues, false
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 08:40:23 -07:00
|
|
|
|
|
|
|
foundValues, _ = extractSampleValues(iterator)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
foundValues, _ = extractSampleValues(iterator)
|
|
|
|
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
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// Get 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
|
|
|
}
|
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// Get all of the metric fingerprints that are associated with the provided
|
|
|
|
// label set.
|
2013-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) GetFingerprintsForLabelSet(labelSet clientmodel.LabelSet) (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.")
|
|
|
|
}
|
|
|
|
|
2013-03-25 05:04:47 -07:00
|
|
|
memFingerprints, err := t.memoryArena.GetFingerprintsForLabelSet(labelSet)
|
|
|
|
if err != nil {
|
2013-06-06 01:32:00 -07:00
|
|
|
return nil, err
|
2013-03-25 05:04:47 -07:00
|
|
|
}
|
2013-05-07 01:18:19 -07:00
|
|
|
diskFingerprints, err := t.DiskStorage.GetFingerprintsForLabelSet(labelSet)
|
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
|
|
|
}
|
|
|
|
|
2013-05-02 09:27:12 -07:00
|
|
|
// Get 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
|
|
|
}
|