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-04-29 02:17:56 -07:00
|
|
|
"log"
|
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
|
|
|
|
|
|
|
dto "github.com/prometheus/prometheus/model/generated"
|
|
|
|
|
2013-06-25 05:02:27 -07:00
|
|
|
clientmodel "github.com/prometheus/client_golang/model"
|
|
|
|
|
2013-05-22 10:06:06 -07:00
|
|
|
"github.com/prometheus/prometheus/coding"
|
|
|
|
"github.com/prometheus/prometheus/coding/indexable"
|
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-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-06-06 09:16:22 -07:00
|
|
|
const (
|
|
|
|
// Ignore timeseries in queries that are more stale than this limit.
|
|
|
|
stalenessLimit = time.Minute * 5
|
|
|
|
// Size of the watermarks cache (used in determining timeseries freshness).
|
|
|
|
wmCacheSizeBytes = 5 * 1024 * 1024
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
viewQueue chan viewJob
|
|
|
|
|
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
|
|
|
|
diskSemaphore chan bool
|
2013-06-06 09:16:22 -07:00
|
|
|
|
|
|
|
wmCache *WatermarkCache
|
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 (
|
|
|
|
tieredDiskSemaphores = 1
|
|
|
|
tieredMemorySemaphores = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewTieredStorage(appendToDiskQueueDepth, viewQueueDepth uint, flushMemoryInterval, memoryTTL time.Duration, root string) (*TieredStorage, error) {
|
2013-03-06 17:16:39 -08:00
|
|
|
diskStorage, err := NewLevelDBMetricPersistence(root)
|
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-06-06 09:16:22 -07:00
|
|
|
wmCache := NewWatermarkCache(wmCacheSizeBytes)
|
|
|
|
memOptions := MemorySeriesOptions{WatermarkCache: wmCache}
|
|
|
|
|
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,
|
|
|
|
viewQueue: make(chan viewJob, viewQueueDepth),
|
2013-06-05 01:40:39 -07:00
|
|
|
|
|
|
|
diskSemaphore: make(chan bool, tieredDiskSemaphores),
|
|
|
|
memorySemaphore: make(chan bool, tieredMemorySemaphores),
|
2013-06-06 09:16:22 -07:00
|
|
|
|
|
|
|
wmCache: wmCache,
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-06-05 01:40:39 -07:00
|
|
|
|
|
|
|
for i := 0; i < tieredDiskSemaphores; i++ {
|
|
|
|
s.diskSemaphore <- true
|
|
|
|
}
|
|
|
|
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-06-06 03:40:06 -07:00
|
|
|
log.Println("Triggering drain...")
|
|
|
|
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-02-08 09:03:26 -08:00
|
|
|
t.viewQueue <- viewJob{
|
|
|
|
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-05-22 08:31:49 -07:00
|
|
|
t.flushMemory(t.memoryTTL)
|
2013-02-08 09:03:26 -08: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-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)))
|
|
|
|
|
|
|
|
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-05-07 06:12:33 -07:00
|
|
|
func (t *TieredStorage) Flush() {
|
2013-05-22 08:31:49 -07:00
|
|
|
t.flushMemory(0)
|
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
|
|
|
|
|
|
|
log.Println("Flushing...")
|
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-06-21 03:23:27 -07:00
|
|
|
log.Printf("Writing %d samples...", len(samples))
|
2013-05-22 08:31:49 -07:00
|
|
|
t.DiskStorage.AppendSamples(samples)
|
|
|
|
}
|
|
|
|
|
2013-06-21 03:23:27 -07:00
|
|
|
log.Println("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)
|
|
|
|
close(t.viewQueue)
|
2013-06-06 09:16:22 -07:00
|
|
|
t.wmCache.Clear()
|
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-06-21 01:16:41 -07:00
|
|
|
wm, cacheHit := t.wmCache.Get(f)
|
|
|
|
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-06-25 05:02:27 -07:00
|
|
|
t.wmCache.Set(f, &watermarks{High: newest})
|
2013-06-21 09:34:08 -07:00
|
|
|
|
|
|
|
return newest.Before(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-08 01:27:44 -07:00
|
|
|
value := &dto.MetricHighWatermark{}
|
2013-06-25 05:02:27 -07:00
|
|
|
k := &dto.Fingerprint{}
|
|
|
|
dumpFingerprint(k, f)
|
2013-08-05 00:25:47 -07:00
|
|
|
_, 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-06-06 09:16:22 -07:00
|
|
|
wmTime := time.Unix(*value.Timestamp, 0).UTC()
|
2013-06-25 05:02:27 -07:00
|
|
|
t.wmCache.Set(f, &watermarks{High: wmTime})
|
2013-06-21 01:16:41 -07:00
|
|
|
|
2013-06-21 09:34:08 -07:00
|
|
|
return wmTime.Before(i), nil
|
2013-06-21 01:16:41 -07:00
|
|
|
}
|
|
|
|
|
2013-06-27 08:01:31 -07:00
|
|
|
t.wmCache.Set(f, &watermarks{})
|
|
|
|
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-05-22 10:06:06 -07:00
|
|
|
var iterator leveldb.Iterator = nil
|
|
|
|
var diskFrontier *diskFrontier = nil
|
|
|
|
var diskPresent = true
|
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 {
|
|
|
|
log.Printf("Error getting watermark from cache for %s: %s", scanJob.fingerprint, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if old {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-04-18 16:00:57 -07:00
|
|
|
var seriesFrontier *seriesFrontier = nil
|
2013-06-04 04:16:59 -07:00
|
|
|
var seriesPresent = true
|
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-05-22 10:06:06 -07:00
|
|
|
if (len(memValues) == 0 || memValues.FirstTimeAfter(targetTime)) && diskPresent && seriesPresent {
|
2013-06-03 08:07:03 -07:00
|
|
|
diskPrepareTimer := viewJob.stats.GetTimer(stats.ViewDiskPreparationTime).Start()
|
2013-05-22 10:06:06 -07:00
|
|
|
// Conditionalize disk access.
|
|
|
|
if diskFrontier == nil && diskPresent {
|
|
|
|
if iterator == nil {
|
2013-06-05 01:40:39 -07:00
|
|
|
<-t.diskSemaphore
|
|
|
|
defer func() {
|
|
|
|
t.diskSemaphore <- true
|
|
|
|
}()
|
|
|
|
|
2013-05-22 10:06:06 -07:00
|
|
|
// Get a single iterator that will be used for all data extraction
|
|
|
|
// below.
|
|
|
|
iterator = t.DiskStorage.MetricSamples.NewIterator(true)
|
|
|
|
defer iterator.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
diskFrontier, diskPresent, err = newDiskFrontier(iterator)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !diskPresent {
|
|
|
|
seriesPresent = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if seriesFrontier == nil && diskPresent {
|
|
|
|
seriesFrontier, seriesPresent, err = newSeriesFrontier(scanJob.fingerprint, diskFrontier, iterator)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2013-06-03 08:07:03 -07:00
|
|
|
diskPrepareTimer.Stop()
|
2013-05-22 10:06:06 -07:00
|
|
|
|
|
|
|
if diskPresent && seriesPresent {
|
2013-06-03 08:07:03 -07:00
|
|
|
diskTimer := viewJob.stats.GetTimer(stats.ViewDiskExtractionTime).Start()
|
2013-05-22 10:06:06 -07:00
|
|
|
diskValues := t.loadChunkAroundTime(iterator, seriesFrontier, scanJob.fingerprint, targetTime)
|
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.
|
|
|
|
if len(memValues) > 0 && diskValues.LastTimeBefore(targetTime) {
|
|
|
|
latestDiskValue := diskValues[len(diskValues)-1:]
|
|
|
|
currentChunk = append(chunk(latestDiskValue), chunk(memValues)...)
|
|
|
|
} else {
|
|
|
|
currentChunk = chunk(diskValues)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-06-25 05:02:27 -07:00
|
|
|
func (t *TieredStorage) loadChunkAroundTime(iterator leveldb.Iterator, frontier *seriesFrontier, fingerprint *clientmodel.Fingerprint, ts time.Time) (chunk Values) {
|
|
|
|
|
|
|
|
fd := &dto.Fingerprint{}
|
|
|
|
dumpFingerprint(fd, fingerprint)
|
|
|
|
targetKey := &dto.SampleKey{
|
|
|
|
Fingerprint: fd,
|
|
|
|
}
|
|
|
|
var foundKey *SampleKey
|
|
|
|
var foundValues Values
|
2013-03-16 01:30:31 -07:00
|
|
|
|
|
|
|
// Limit the target key to be within the series' keyspace.
|
|
|
|
if ts.After(frontier.lastSupertime) {
|
|
|
|
targetKey.Timestamp = indexable.EncodeTime(frontier.lastSupertime)
|
|
|
|
} else {
|
|
|
|
targetKey.Timestamp = indexable.EncodeTime(ts)
|
2013-02-08 09:03:26 -08:00
|
|
|
}
|
2013-03-01 09:51:36 -08:00
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
// Try seeking to target key.
|
2013-05-15 22:38:31 -07:00
|
|
|
rawKey := coding.NewPBEncoder(targetKey).MustEncode()
|
2013-03-16 01:30:31 -07:00
|
|
|
iterator.Seek(rawKey)
|
2013-03-01 09:51:36 -08:00
|
|
|
|
2013-03-16 01:30:31 -07:00
|
|
|
foundKey, err := extractSampleKey(iterator)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
rewound := false
|
2013-04-21 10:16:15 -07:00
|
|
|
firstTime := foundKey.FirstTimestamp
|
2013-03-16 01:30:31 -07:00
|
|
|
if ts.Before(firstTime) && !frontier.firstSupertime.After(ts) {
|
2013-03-25 02:24:59 -07:00
|
|
|
iterator.Previous()
|
2013-03-16 01:30:31 -07:00
|
|
|
rewound = true
|
|
|
|
}
|
|
|
|
|
2013-04-22 04:30:16 -07:00
|
|
|
foundValues, err = extractSampleValues(iterator)
|
2013-03-16 01:30:31 -07:00
|
|
|
if err != nil {
|
2013-04-22 04:30:16 -07:00
|
|
|
return
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we rewound, but the target time is still past the current block, return
|
|
|
|
// the last value of the current (rewound) block and the entire next block.
|
|
|
|
if rewound {
|
|
|
|
foundKey, err = extractSampleKey(iterator)
|
|
|
|
if err != nil {
|
2013-04-22 04:30:16 -07:00
|
|
|
return
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
2013-04-21 10:16:15 -07:00
|
|
|
currentChunkLastTime := foundKey.LastTimestamp
|
2013-03-16 01:30:31 -07:00
|
|
|
|
|
|
|
if ts.After(currentChunkLastTime) {
|
2013-04-22 04:30:16 -07:00
|
|
|
sampleCount := len(foundValues)
|
|
|
|
chunk = append(chunk, foundValues[sampleCount-1])
|
2013-03-16 01:30:31 -07:00
|
|
|
// We know there's a next block since we have rewound from it.
|
|
|
|
iterator.Next()
|
|
|
|
|
2013-04-22 04:30:16 -07:00
|
|
|
foundValues, err = extractSampleValues(iterator)
|
2013-03-16 01:30:31 -07:00
|
|
|
if err != nil {
|
2013-04-22 04:30:16 -07:00
|
|
|
return
|
2013-03-16 01:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now append all the samples of the currently seeked block to the output.
|
2013-04-22 04:30:16 -07:00
|
|
|
chunk = append(chunk, foundValues...)
|
2013-03-06 17:16:39 -08:00
|
|
|
|
2013-03-01 09:51:36 -08:00
|
|
|
return
|
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
|
|
|
}
|