2017-04-10 11:59:45 -07:00
|
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
|
// 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.
|
|
|
|
|
|
2016-12-04 04:16:11 -08:00
|
|
|
|
package tsdb
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-04 05:06:40 -08:00
|
|
|
|
"math"
|
2016-12-14 09:38:46 -08:00
|
|
|
|
"sort"
|
2016-12-04 04:16:11 -08:00
|
|
|
|
"sync"
|
2017-02-04 02:53:52 -08:00
|
|
|
|
"sync/atomic"
|
2017-08-30 09:34:54 -07:00
|
|
|
|
"time"
|
2017-05-17 07:43:01 -07:00
|
|
|
|
|
2017-01-06 06:18:06 -08:00
|
|
|
|
"github.com/go-kit/kit/log"
|
2017-01-19 02:22:47 -08:00
|
|
|
|
"github.com/pkg/errors"
|
2017-08-30 09:34:54 -07:00
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-04-04 02:27:26 -07:00
|
|
|
|
"github.com/prometheus/tsdb/chunks"
|
|
|
|
|
"github.com/prometheus/tsdb/labels"
|
2016-12-04 04:16:11 -08:00
|
|
|
|
)
|
|
|
|
|
|
2017-01-17 07:33:58 -08:00
|
|
|
|
var (
|
|
|
|
|
// ErrNotFound is returned if a looked up resource was not found.
|
2017-03-21 02:11:23 -07:00
|
|
|
|
ErrNotFound = errors.Errorf("not found")
|
2017-01-17 07:33:58 -08:00
|
|
|
|
|
|
|
|
|
// ErrOutOfOrderSample is returned if an appended sample has a
|
|
|
|
|
// timestamp larger than the most recent sample.
|
|
|
|
|
ErrOutOfOrderSample = errors.New("out of order sample")
|
|
|
|
|
|
|
|
|
|
// ErrAmendSample is returned if an appended sample has the same timestamp
|
|
|
|
|
// as the most recent sample but a different value.
|
|
|
|
|
ErrAmendSample = errors.New("amending sample")
|
|
|
|
|
|
|
|
|
|
// ErrOutOfBounds is returned if an appended sample is out of the
|
|
|
|
|
// writable time range.
|
|
|
|
|
ErrOutOfBounds = errors.New("out of bounds")
|
|
|
|
|
)
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Head handles reads and writes of time series data within a time window.
|
|
|
|
|
type Head struct {
|
|
|
|
|
chunkRange int64
|
2017-08-30 09:34:54 -07:00
|
|
|
|
metrics *headMetrics
|
|
|
|
|
wal WAL
|
|
|
|
|
logger log.Logger
|
|
|
|
|
appendPool sync.Pool
|
2017-01-19 02:22:47 -08:00
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
minTime, maxTime int64
|
2017-09-04 07:08:38 -07:00
|
|
|
|
lastSeriesID uint64
|
2017-02-04 02:53:52 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// All series addressable by their ID or hash.
|
|
|
|
|
series *stripeSeries
|
2016-12-21 16:12:28 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
symMtx sync.RWMutex
|
|
|
|
|
symbols map[string]struct{}
|
|
|
|
|
values map[string]stringset // label names to possible values
|
|
|
|
|
|
|
|
|
|
postings *memPostings // postings lists for terms
|
2016-12-07 08:10:49 -08:00
|
|
|
|
|
2017-05-23 22:54:24 -07:00
|
|
|
|
tombstones tombstoneReader
|
2016-12-04 04:16:11 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
type headMetrics struct {
|
|
|
|
|
activeAppenders prometheus.Gauge
|
|
|
|
|
series prometheus.Gauge
|
|
|
|
|
seriesCreated prometheus.Counter
|
|
|
|
|
seriesRemoved prometheus.Counter
|
|
|
|
|
chunks prometheus.Gauge
|
|
|
|
|
chunksCreated prometheus.Gauge
|
|
|
|
|
chunksRemoved prometheus.Gauge
|
|
|
|
|
gcDuration prometheus.Summary
|
|
|
|
|
minTime prometheus.GaugeFunc
|
|
|
|
|
maxTime prometheus.GaugeFunc
|
|
|
|
|
samplesAppended prometheus.Counter
|
|
|
|
|
walTruncateDuration prometheus.Summary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newHeadMetrics(h *Head, r prometheus.Registerer) *headMetrics {
|
|
|
|
|
m := &headMetrics{}
|
|
|
|
|
|
|
|
|
|
m.activeAppenders = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_active_appenders",
|
|
|
|
|
Help: "Number of currently active appender transactions",
|
|
|
|
|
})
|
|
|
|
|
m.series = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_series",
|
|
|
|
|
Help: "Total number of series in the head block.",
|
|
|
|
|
})
|
|
|
|
|
m.seriesCreated = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_series_created_total",
|
|
|
|
|
Help: "Total number of series created in the head",
|
|
|
|
|
})
|
|
|
|
|
m.seriesRemoved = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_series_removed_total",
|
|
|
|
|
Help: "Total number of series removed in the head",
|
|
|
|
|
})
|
|
|
|
|
m.chunks = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_chunks",
|
|
|
|
|
Help: "Total number of chunks in the head block.",
|
|
|
|
|
})
|
|
|
|
|
m.chunksCreated = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_chunks_created_total",
|
|
|
|
|
Help: "Total number of chunks created in the head",
|
|
|
|
|
})
|
|
|
|
|
m.chunksRemoved = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_chunks_removed_total",
|
|
|
|
|
Help: "Total number of chunks removed in the head",
|
|
|
|
|
})
|
|
|
|
|
m.gcDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
|
Name: "tsdb_head_gc_duration_seconds",
|
|
|
|
|
Help: "Runtime of garbage collection in the head block.",
|
|
|
|
|
})
|
|
|
|
|
m.minTime = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_max_time",
|
|
|
|
|
Help: "Maximum timestamp of the head block.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.MaxTime())
|
|
|
|
|
})
|
|
|
|
|
m.maxTime = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "tsdb_head_min_time",
|
|
|
|
|
Help: "Minimum time bound of the head block.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.MinTime())
|
|
|
|
|
})
|
|
|
|
|
m.walTruncateDuration = prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
|
Name: "tsdb_wal_truncate_duration_seconds",
|
|
|
|
|
Help: "Duration of WAL truncation.",
|
|
|
|
|
})
|
|
|
|
|
m.samplesAppended = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "tsdb_head_samples_appended_total",
|
|
|
|
|
Help: "Total number of appended sampledb.",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
|
r.MustRegister(
|
|
|
|
|
m.activeAppenders,
|
|
|
|
|
m.chunks,
|
|
|
|
|
m.chunksCreated,
|
|
|
|
|
m.chunksRemoved,
|
|
|
|
|
m.series,
|
|
|
|
|
m.seriesCreated,
|
|
|
|
|
m.seriesRemoved,
|
|
|
|
|
m.minTime,
|
|
|
|
|
m.maxTime,
|
|
|
|
|
m.gcDuration,
|
|
|
|
|
m.walTruncateDuration,
|
|
|
|
|
m.samplesAppended,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// NewHead opens the head block in dir.
|
2017-08-30 09:34:54 -07:00
|
|
|
|
func NewHead(r prometheus.Registerer, l log.Logger, wal WAL, chunkRange int64) (*Head, error) {
|
|
|
|
|
if l == nil {
|
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
|
}
|
|
|
|
|
if wal == nil {
|
2017-09-06 07:20:37 -07:00
|
|
|
|
wal = NopWAL()
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
if chunkRange < 1 {
|
|
|
|
|
return nil, errors.Errorf("invalid chunk range %d", chunkRange)
|
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
h := &Head{
|
2017-08-30 09:34:54 -07:00
|
|
|
|
wal: wal,
|
|
|
|
|
logger: l,
|
2017-08-28 15:39:17 -07:00
|
|
|
|
chunkRange: chunkRange,
|
2017-09-06 07:20:37 -07:00
|
|
|
|
minTime: math.MinInt64,
|
2017-08-28 15:39:17 -07:00
|
|
|
|
maxTime: math.MinInt64,
|
2017-09-05 02:45:18 -07:00
|
|
|
|
series: newStripeSeries(),
|
2017-05-15 10:58:14 -07:00
|
|
|
|
values: map[string]stringset{},
|
2017-08-05 04:31:48 -07:00
|
|
|
|
symbols: map[string]struct{}{},
|
2017-09-05 02:45:18 -07:00
|
|
|
|
postings: newMemPostings(),
|
2017-05-19 10:24:29 -07:00
|
|
|
|
tombstones: newEmptyTombstoneReader(),
|
2017-01-07 07:20:32 -08:00
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
h.metrics = newHeadMetrics(h, r)
|
|
|
|
|
|
2017-09-06 07:20:37 -07:00
|
|
|
|
return h, nil
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
// ReadWAL initializes the head by consuming the write ahead log.
|
2017-09-06 07:20:37 -07:00
|
|
|
|
func (h *Head) ReadWAL() error {
|
2017-09-06 05:59:25 -07:00
|
|
|
|
r := h.wal.Reader()
|
2017-09-06 07:20:37 -07:00
|
|
|
|
mint := h.MinTime()
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2017-09-21 02:02:30 -07:00
|
|
|
|
// Track number of samples that referenced a series we don't know about
|
|
|
|
|
// for error reporting.
|
|
|
|
|
var unknownRefs int
|
|
|
|
|
|
2017-08-31 02:39:22 -07:00
|
|
|
|
seriesFunc := func(series []RefSeries) error {
|
|
|
|
|
for _, s := range series {
|
2017-09-19 01:20:19 -07:00
|
|
|
|
h.getOrCreateWithID(s.Ref, s.Labels.Hash(), s.Labels)
|
|
|
|
|
|
|
|
|
|
if h.lastSeriesID < s.Ref {
|
|
|
|
|
h.lastSeriesID = s.Ref
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
samplesFunc := func(samples []RefSample) error {
|
|
|
|
|
for _, s := range samples {
|
2017-09-06 07:20:37 -07:00
|
|
|
|
if s.T < mint {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
ms := h.series.getByID(s.Ref)
|
|
|
|
|
if ms == nil {
|
2017-09-21 02:02:30 -07:00
|
|
|
|
unknownRefs++
|
2017-09-19 01:20:19 -07:00
|
|
|
|
continue
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
_, chunkCreated := ms.append(s.T, s.V)
|
|
|
|
|
if chunkCreated {
|
|
|
|
|
h.metrics.chunksCreated.Inc()
|
|
|
|
|
h.metrics.chunks.Inc()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
deletesFunc := func(stones []Stone) error {
|
|
|
|
|
for _, s := range stones {
|
|
|
|
|
for _, itv := range s.intervals {
|
2017-09-06 07:20:37 -07:00
|
|
|
|
if itv.Maxt < mint {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
h.tombstones.add(s.ref, itv)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 05:57:51 -07:00
|
|
|
|
if unknownRefs > 0 {
|
|
|
|
|
h.logger.Log("msg", "unknown series references in WAL samples", "count", unknownRefs)
|
|
|
|
|
}
|
2017-09-21 02:02:30 -07:00
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if err := r.Read(seriesFunc, samplesFunc, deletesFunc); err != nil {
|
|
|
|
|
return errors.Wrap(err, "consume WAL")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2017-05-13 09:14:18 -07:00
|
|
|
|
}
|
2017-01-06 03:37:28 -08:00
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
// Truncate removes all data before mint from the head block and truncates its WAL.
|
2017-09-01 05:38:49 -07:00
|
|
|
|
func (h *Head) Truncate(mint int64) error {
|
2017-09-06 07:20:37 -07:00
|
|
|
|
initialize := h.MinTime() == math.MinInt64
|
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if mint%h.chunkRange != 0 {
|
|
|
|
|
return errors.Errorf("truncating at %d not aligned", mint)
|
|
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
|
if h.MinTime() >= mint {
|
2017-09-01 05:38:49 -07:00
|
|
|
|
return nil
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
atomic.StoreInt64(&h.minTime, mint)
|
|
|
|
|
|
2017-09-07 04:04:02 -07:00
|
|
|
|
// Ensure that max time is at least as high as min time.
|
|
|
|
|
for h.MaxTime() < mint {
|
|
|
|
|
atomic.CompareAndSwapInt64(&h.maxTime, h.MaxTime(), mint)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 07:20:37 -07:00
|
|
|
|
// This was an initial call to Truncate after loading blocks on startup.
|
|
|
|
|
// We haven't read back the WAL yet, so do not attempt to truncate it.
|
|
|
|
|
if initialize {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
h.gc()
|
|
|
|
|
h.logger.Log("msg", "head GC completed", "duration", time.Since(start))
|
|
|
|
|
h.metrics.gcDuration.Observe(time.Since(start).Seconds())
|
|
|
|
|
|
|
|
|
|
start = time.Now()
|
|
|
|
|
|
2017-09-21 02:02:30 -07:00
|
|
|
|
keep := func(id uint64) bool {
|
|
|
|
|
return h.series.getByID(id) != nil
|
2017-08-31 02:39:22 -07:00
|
|
|
|
}
|
2017-09-21 02:02:30 -07:00
|
|
|
|
if err := h.wal.Truncate(mint, keep); err == nil {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
h.logger.Log("msg", "WAL truncation completed", "duration", time.Since(start))
|
|
|
|
|
} else {
|
|
|
|
|
h.logger.Log("msg", "WAL truncation failed", "err", err, "duration", time.Since(start))
|
|
|
|
|
}
|
|
|
|
|
h.metrics.walTruncateDuration.Observe(time.Since(start).Seconds())
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
|
|
|
|
return nil
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initTime initializes a head with the first timestamp. This only needs to be called
|
|
|
|
|
// for a compltely fresh head with an empty WAL.
|
|
|
|
|
// Returns true if the initialization took an effect.
|
|
|
|
|
func (h *Head) initTime(t int64) (initialized bool) {
|
|
|
|
|
// In the init state, the head has a high timestamp of math.MinInt64.
|
|
|
|
|
mint, _ := rangeForTimestamp(t, h.chunkRange)
|
|
|
|
|
|
2017-09-07 04:04:02 -07:00
|
|
|
|
if !atomic.CompareAndSwapInt64(&h.minTime, math.MinInt64, mint) {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return false
|
|
|
|
|
}
|
2017-09-07 04:04:02 -07:00
|
|
|
|
// Ensure that max time is initialized to at least the min time we just set.
|
|
|
|
|
// Concurrent appenders may already have set it to a higher value.
|
|
|
|
|
atomic.CompareAndSwapInt64(&h.maxTime, math.MinInt64, t)
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initAppender is a helper to initialize the time bounds of a the head
|
|
|
|
|
// upon the first sample it receives.
|
|
|
|
|
type initAppender struct {
|
|
|
|
|
app Appender
|
|
|
|
|
head *Head
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
func (a *initAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if a.app != nil {
|
|
|
|
|
return a.app.Add(lset, t, v)
|
|
|
|
|
}
|
2017-09-01 03:09:29 -07:00
|
|
|
|
a.head.initTime(t)
|
|
|
|
|
a.app = a.head.appender()
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return a.app.Add(lset, t, v)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
func (a *initAppender) AddFast(ref uint64, t int64, v float64) error {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if a.app == nil {
|
|
|
|
|
return ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
return a.app.AddFast(ref, t, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *initAppender) Commit() error {
|
|
|
|
|
if a.app == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return a.app.Commit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *initAppender) Rollback() error {
|
|
|
|
|
if a.app == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return a.app.Rollback()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Appender returns a new Appender on the database.
|
|
|
|
|
func (h *Head) Appender() Appender {
|
|
|
|
|
h.metrics.activeAppenders.Inc()
|
|
|
|
|
|
|
|
|
|
// The head cache might not have a starting point yet. The init appender
|
|
|
|
|
// picks up the first appended timestamp as the base.
|
2017-09-07 04:04:02 -07:00
|
|
|
|
if h.MinTime() == math.MinInt64 {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return &initAppender{head: h}
|
|
|
|
|
}
|
|
|
|
|
return h.appender()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) appender() *headAppender {
|
|
|
|
|
return &headAppender{
|
|
|
|
|
head: h,
|
|
|
|
|
mint: h.MaxTime() - h.chunkRange/2,
|
|
|
|
|
samples: h.getAppendBuffer(),
|
|
|
|
|
highTimestamp: math.MinInt64,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) getAppendBuffer() []RefSample {
|
|
|
|
|
b := h.appendPool.Get()
|
|
|
|
|
if b == nil {
|
|
|
|
|
return make([]RefSample, 0, 512)
|
|
|
|
|
}
|
|
|
|
|
return b.([]RefSample)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) putAppendBuffer(b []RefSample) {
|
|
|
|
|
h.appendPool.Put(b[:0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type headAppender struct {
|
|
|
|
|
head *Head
|
|
|
|
|
mint int64
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
series []RefSeries
|
2017-08-30 09:34:54 -07:00
|
|
|
|
samples []RefSample
|
|
|
|
|
highTimestamp int64
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
func (a *headAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if t < a.mint {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
return 0, ErrOutOfBounds
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
s, created := a.head.getOrCreate(lset.Hash(), lset)
|
|
|
|
|
if created {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
a.series = append(a.series, RefSeries{
|
|
|
|
|
Ref: s.ref,
|
|
|
|
|
Labels: lset,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return s.ref, a.AddFast(s.ref, t, v)
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
func (a *headAppender) AddFast(ref uint64, t int64, v float64) error {
|
|
|
|
|
s := a.head.series.getByID(ref)
|
|
|
|
|
|
|
|
|
|
if s == nil {
|
|
|
|
|
return errors.Wrap(ErrNotFound, "unknown series")
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.Lock()
|
2017-09-08 01:12:28 -07:00
|
|
|
|
err := s.appendable(t, v)
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.Unlock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
2017-09-08 01:12:28 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if t < a.mint {
|
|
|
|
|
return ErrOutOfBounds
|
|
|
|
|
}
|
|
|
|
|
if t > a.highTimestamp {
|
|
|
|
|
a.highTimestamp = t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.samples = append(a.samples, RefSample{
|
2017-09-05 02:45:18 -07:00
|
|
|
|
Ref: ref,
|
|
|
|
|
T: t,
|
|
|
|
|
V: v,
|
|
|
|
|
series: s,
|
2017-08-30 09:34:54 -07:00
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *headAppender) Commit() error {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
defer a.Rollback()
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
if err := a.head.wal.LogSeries(a.series); err != nil {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := a.head.wal.LogSamples(a.samples); err != nil {
|
|
|
|
|
return errors.Wrap(err, "WAL log samples")
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
total := len(a.samples)
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
|
|
|
|
for _, s := range a.samples {
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.series.Lock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
ok, chunkCreated := s.series.append(s.T, s.V)
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.series.Unlock()
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if !ok {
|
|
|
|
|
total--
|
|
|
|
|
}
|
|
|
|
|
if chunkCreated {
|
|
|
|
|
a.head.metrics.chunks.Inc()
|
|
|
|
|
a.head.metrics.chunksCreated.Inc()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a.head.metrics.samplesAppended.Add(float64(total))
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
ht := a.head.MaxTime()
|
|
|
|
|
if a.highTimestamp <= ht {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if atomic.CompareAndSwapInt64(&a.head.maxTime, ht, a.highTimestamp) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *headAppender) Rollback() error {
|
|
|
|
|
a.head.metrics.activeAppenders.Dec()
|
|
|
|
|
a.head.putAppendBuffer(a.samples)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all samples in the range of [mint, maxt] for series that satisfy the given
|
|
|
|
|
// label matchers.
|
|
|
|
|
func (h *Head) Delete(mint, maxt int64, ms ...labels.Matcher) error {
|
|
|
|
|
// Do not delete anything beyond the currently valid range.
|
|
|
|
|
mint, maxt = clampInterval(mint, maxt, h.MinTime(), h.MaxTime())
|
|
|
|
|
|
|
|
|
|
ir := h.indexRange(mint, maxt)
|
|
|
|
|
|
|
|
|
|
pr := newPostingsReader(ir)
|
|
|
|
|
p, absent := pr.Select(ms...)
|
|
|
|
|
|
|
|
|
|
var stones []Stone
|
|
|
|
|
|
|
|
|
|
Outer:
|
|
|
|
|
for p.Next() {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
series := h.series.getByID(p.At())
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
|
|
|
|
for _, abs := range absent {
|
|
|
|
|
if series.lset.Get(abs) != "" {
|
|
|
|
|
continue Outer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete only until the current values and not beyond.
|
|
|
|
|
t0, t1 := clampInterval(mint, maxt, series.minTime(), series.maxTime())
|
|
|
|
|
stones = append(stones, Stone{p.At(), Intervals{{t0, t1}}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if p.Err() != nil {
|
|
|
|
|
return p.Err()
|
|
|
|
|
}
|
|
|
|
|
if err := h.wal.LogDeletes(stones); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, s := range stones {
|
|
|
|
|
h.tombstones.add(s.ref, s.intervals[0])
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// gc removes data before the minimum timestmap from the head.
|
2017-08-30 09:34:54 -07:00
|
|
|
|
func (h *Head) gc() {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Only data strictly lower than this timestamp must be deleted.
|
|
|
|
|
mint := h.MinTime()
|
2017-01-19 05:01:38 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// Drop old chunks and remember series IDs and hashes if they can be
|
|
|
|
|
// deleted entirely.
|
|
|
|
|
deleted, chunksRemoved := h.series.gc(mint)
|
|
|
|
|
seriesRemoved := len(deleted)
|
2017-03-20 02:41:43 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.metrics.seriesRemoved.Add(float64(seriesRemoved))
|
|
|
|
|
h.metrics.series.Sub(float64(seriesRemoved))
|
|
|
|
|
h.metrics.chunksRemoved.Add(float64(chunksRemoved))
|
|
|
|
|
h.metrics.chunks.Sub(float64(chunksRemoved))
|
2017-03-04 07:50:48 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// Remove deleted series IDs from the postings lists. First do a collection
|
|
|
|
|
// run where we rebuild all postings that have something to delete
|
|
|
|
|
h.postings.mtx.RLock()
|
2017-03-17 04:12:50 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
type replEntry struct {
|
|
|
|
|
idx int
|
|
|
|
|
l []uint64
|
2017-03-23 10:27:20 -07:00
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
collected := map[labels.Label]replEntry{}
|
2017-01-19 02:22:47 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
for t, p := range h.postings.m {
|
|
|
|
|
repl := replEntry{idx: len(p)}
|
|
|
|
|
|
|
|
|
|
for i, id := range p {
|
|
|
|
|
if _, ok := deleted[id]; ok {
|
|
|
|
|
// First ID that got deleted, initialize replacement with
|
|
|
|
|
// all remaining IDs so far.
|
|
|
|
|
if repl.l == nil {
|
|
|
|
|
repl.l = make([]uint64, 0, len(p))
|
|
|
|
|
repl.l = append(repl.l, p[:i]...)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// Only add to the replacement once we know we have to do it.
|
|
|
|
|
if repl.l != nil {
|
|
|
|
|
repl.l = append(repl.l, id)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
if repl.l != nil {
|
|
|
|
|
collected[t] = repl
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
|
}
|
2017-05-15 10:58:14 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.postings.mtx.RUnlock()
|
2017-05-23 03:45:16 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// Replace all postings that have changed. Append all IDs that may have
|
|
|
|
|
// been added while we switched locks.
|
|
|
|
|
h.postings.mtx.Lock()
|
2017-05-14 02:06:26 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
for t, repl := range collected {
|
|
|
|
|
l := append(repl.l, h.postings.m[t][repl.idx:]...)
|
|
|
|
|
|
|
|
|
|
if len(l) > 0 {
|
|
|
|
|
h.postings.m[t] = l
|
2017-08-28 15:39:17 -07:00
|
|
|
|
} else {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
delete(h.postings.m, t)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
2017-08-09 02:10:29 -07:00
|
|
|
|
}
|
2017-06-05 01:18:31 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.postings.mtx.Unlock()
|
|
|
|
|
|
|
|
|
|
// Rebuild symbols and label value indices from what is left in the postings terms.
|
|
|
|
|
h.postings.mtx.RLock()
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
symbols := make(map[string]struct{}, len(h.symbols))
|
|
|
|
|
values := make(map[string]stringset, len(h.values))
|
2017-06-05 01:18:31 -07:00
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
for t := range h.postings.m {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
symbols[t.Name] = struct{}{}
|
|
|
|
|
symbols[t.Value] = struct{}{}
|
2017-08-05 04:31:48 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
ss, ok := values[t.Name]
|
2017-08-28 15:39:17 -07:00
|
|
|
|
if !ok {
|
|
|
|
|
ss = stringset{}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
values[t.Name] = ss
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
ss.set(t.Value)
|
2017-03-20 02:21:21 -07:00
|
|
|
|
}
|
2017-03-21 04:12:33 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.postings.mtx.RUnlock()
|
|
|
|
|
|
|
|
|
|
h.symMtx.Lock()
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
h.symbols = symbols
|
|
|
|
|
h.values = values
|
2017-08-30 08:38:25 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.symMtx.Unlock()
|
2017-03-20 02:21:21 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (h *Head) Tombstones() TombstoneReader {
|
|
|
|
|
return h.tombstones
|
2017-06-25 10:02:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Index returns an IndexReader against the block.
|
|
|
|
|
func (h *Head) Index() IndexReader {
|
|
|
|
|
return h.indexRange(math.MinInt64, math.MaxInt64)
|
2017-03-20 00:41:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (h *Head) indexRange(mint, maxt int64) *headIndexReader {
|
|
|
|
|
if hmin := h.MinTime(); hmin > mint {
|
|
|
|
|
mint = hmin
|
2017-01-12 11:00:36 -08:00
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
return &headIndexReader{head: h, mint: mint, maxt: maxt}
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Chunks returns a ChunkReader against the block.
|
|
|
|
|
func (h *Head) Chunks() ChunkReader {
|
|
|
|
|
return h.chunksRange(math.MinInt64, math.MaxInt64)
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (h *Head) chunksRange(mint, maxt int64) *headChunkReader {
|
|
|
|
|
if hmin := h.MinTime(); hmin > mint {
|
|
|
|
|
mint = hmin
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
return &headChunkReader{head: h, mint: mint, maxt: maxt}
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// MinTime returns the lowest time bound on visible data in the head.
|
|
|
|
|
func (h *Head) MinTime() int64 {
|
|
|
|
|
return atomic.LoadInt64(&h.minTime)
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// MaxTime returns the highest timestamp seen in data of the head.
|
|
|
|
|
func (h *Head) MaxTime() int64 {
|
|
|
|
|
return atomic.LoadInt64(&h.maxTime)
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
type headChunkReader struct {
|
|
|
|
|
head *Head
|
|
|
|
|
mint, maxt int64
|
2017-01-12 10:18:51 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (h *headChunkReader) Close() error {
|
2017-01-12 10:18:51 -08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
|
// packChunkID packs a seriesID and a chunkID within it into a global 8 byte ID.
|
|
|
|
|
// It panicks if the seriesID exceeds 5 bytes or the chunk ID 3 bytes.
|
|
|
|
|
func packChunkID(seriesID, chunkID uint64) uint64 {
|
|
|
|
|
if seriesID > (1<<40)-1 {
|
|
|
|
|
panic("series ID exceeds 5 bytes")
|
|
|
|
|
}
|
|
|
|
|
if chunkID > (1<<24)-1 {
|
|
|
|
|
panic("chunk ID exceeds 3 bytes")
|
|
|
|
|
}
|
|
|
|
|
return (seriesID << 24) | chunkID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unpackChunkID(id uint64) (seriesID, chunkID uint64) {
|
|
|
|
|
return id >> 24, (id << 40) >> 40
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 07:14:33 -08:00
|
|
|
|
// Chunk returns the chunk for the reference number.
|
2017-02-23 01:50:22 -08:00
|
|
|
|
func (h *headChunkReader) Chunk(ref uint64) (chunks.Chunk, error) {
|
2017-09-04 07:08:38 -07:00
|
|
|
|
sid, cid := unpackChunkID(ref)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
s := h.head.series.getByID(sid)
|
2017-01-06 08:23:12 -08:00
|
|
|
|
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.Lock()
|
2017-09-04 07:08:38 -07:00
|
|
|
|
c := s.chunk(int(cid))
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.Unlock()
|
2017-02-18 08:33:20 -08:00
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Do not expose chunks that are outside of the specified range.
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if c == nil || !intervalOverlap(c.minTime, c.maxTime, h.mint, h.maxt) {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
return nil, ErrNotFound
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
return &safeChunk{
|
|
|
|
|
Chunk: c.chunk,
|
|
|
|
|
s: s,
|
2017-09-04 07:08:38 -07:00
|
|
|
|
cid: int(cid),
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 07:51:39 -08:00
|
|
|
|
type safeChunk struct {
|
2017-01-11 04:02:38 -08:00
|
|
|
|
chunks.Chunk
|
2017-08-28 15:39:17 -07:00
|
|
|
|
s *memSeries
|
|
|
|
|
cid int
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *safeChunk) Iterator() chunks.Iterator {
|
2017-09-07 23:48:19 -07:00
|
|
|
|
c.s.Lock()
|
|
|
|
|
it := c.s.iterator(c.cid)
|
|
|
|
|
c.s.Unlock()
|
|
|
|
|
return it
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
// func (c *safeChunk) Appender() (chunks.Appender, error) { panic("illegal") }
|
|
|
|
|
// func (c *safeChunk) Bytes() []byte { panic("illegal") }
|
|
|
|
|
// func (c *safeChunk) Encoding() chunks.Encoding { panic("illegal") }
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
type rangeHead struct {
|
|
|
|
|
head *Head
|
|
|
|
|
mint, maxt int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *rangeHead) Index() IndexReader {
|
|
|
|
|
return h.head.indexRange(h.mint, h.maxt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *rangeHead) Chunks() ChunkReader {
|
|
|
|
|
return h.head.chunksRange(h.mint, h.maxt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *rangeHead) Tombstones() TombstoneReader {
|
|
|
|
|
return newEmptyTombstoneReader()
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 06:28:22 -08:00
|
|
|
|
type headIndexReader struct {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
head *Head
|
|
|
|
|
mint, maxt int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *headIndexReader) Close() error {
|
|
|
|
|
return nil
|
2017-08-05 04:31:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *headIndexReader) Symbols() (map[string]struct{}, error) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.head.symMtx.RLock()
|
|
|
|
|
defer h.head.symMtx.RUnlock()
|
|
|
|
|
|
|
|
|
|
res := make(map[string]struct{}, len(h.head.symbols))
|
|
|
|
|
|
|
|
|
|
for s := range h.head.symbols {
|
|
|
|
|
res[s] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LabelValues returns the possible label values
|
2017-01-10 06:28:22 -08:00
|
|
|
|
func (h *headIndexReader) LabelValues(names ...string) (StringTuples, error) {
|
2016-12-14 09:38:46 -08:00
|
|
|
|
if len(names) != 1 {
|
|
|
|
|
return nil, errInvalidSize
|
|
|
|
|
}
|
|
|
|
|
var sl []string
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.head.symMtx.RLock()
|
|
|
|
|
defer h.head.symMtx.RUnlock()
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
for s := range h.head.values[names[0]] {
|
2016-12-14 09:38:46 -08:00
|
|
|
|
sl = append(sl, s)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(sl)
|
|
|
|
|
|
2017-01-05 06:13:01 -08:00
|
|
|
|
return &stringTuples{l: len(names), s: sl}, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Postings returns the postings list iterator for the label pair.
|
2017-01-10 06:28:22 -08:00
|
|
|
|
func (h *headIndexReader) Postings(name, value string) (Postings, error) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
return h.head.postings.get(name, value), nil
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-05 04:31:48 -07:00
|
|
|
|
func (h *headIndexReader) SortedPostings(p Postings) Postings {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
ep := make([]uint64, 0, 128)
|
2017-08-05 04:31:48 -07:00
|
|
|
|
|
|
|
|
|
for p.Next() {
|
|
|
|
|
ep = append(ep, p.At())
|
|
|
|
|
}
|
|
|
|
|
if err := p.Err(); err != nil {
|
|
|
|
|
return errPostings{err: errors.Wrap(err, "expand postings")}
|
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
var err error
|
2017-08-05 04:31:48 -07:00
|
|
|
|
|
|
|
|
|
sort.Slice(ep, func(i, j int) bool {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
a := h.head.series.getByID(ep[i])
|
|
|
|
|
b := h.head.series.getByID(ep[j])
|
2017-08-28 15:39:17 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
if a == nil || b == nil {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
err = errors.Errorf("series not found")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return labels.Compare(a.lset, b.lset) < 0
|
2017-08-05 04:31:48 -07:00
|
|
|
|
})
|
2017-08-28 15:39:17 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errPostings{err: err}
|
|
|
|
|
}
|
2017-08-05 04:31:48 -07:00
|
|
|
|
return newListPostings(ep)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 09:38:46 -08:00
|
|
|
|
// Series returns the series for the given reference.
|
2017-09-04 07:08:38 -07:00
|
|
|
|
func (h *headIndexReader) Series(ref uint64, lbls *labels.Labels, chks *[]ChunkMeta) error {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
s := h.head.series.getByID(ref)
|
2017-01-06 08:23:12 -08:00
|
|
|
|
|
2017-05-17 07:43:01 -07:00
|
|
|
|
if s == nil {
|
2017-08-05 04:31:48 -07:00
|
|
|
|
return ErrNotFound
|
2017-05-17 07:43:01 -07:00
|
|
|
|
}
|
2017-08-05 04:31:48 -07:00
|
|
|
|
*lbls = append((*lbls)[:0], s.lset...)
|
2017-01-11 04:02:38 -08:00
|
|
|
|
|
2017-09-07 23:48:19 -07:00
|
|
|
|
s.Lock()
|
|
|
|
|
defer s.Unlock()
|
2017-01-11 04:02:38 -08:00
|
|
|
|
|
2017-08-05 04:31:48 -07:00
|
|
|
|
*chks = (*chks)[:0]
|
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
for i, c := range s.chunks {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Do not expose chunks that are outside of the specified range.
|
|
|
|
|
if !intervalOverlap(c.minTime, c.maxTime, h.mint, h.maxt) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-08-06 11:41:24 -07:00
|
|
|
|
*chks = append(*chks, ChunkMeta{
|
2017-01-11 04:02:38 -08:00
|
|
|
|
MinTime: c.minTime,
|
|
|
|
|
MaxTime: c.maxTime,
|
2017-09-04 07:08:38 -07:00
|
|
|
|
Ref: packChunkID(s.ref, uint64(s.chunkID(i))),
|
2017-01-11 04:02:38 -08:00
|
|
|
|
})
|
2017-01-03 06:43:26 -08:00
|
|
|
|
}
|
2017-01-11 04:02:38 -08:00
|
|
|
|
|
2017-08-05 04:31:48 -07:00
|
|
|
|
return nil
|
2016-12-31 06:35:08 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 06:28:22 -08:00
|
|
|
|
func (h *headIndexReader) LabelIndices() ([][]string, error) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.head.symMtx.RLock()
|
|
|
|
|
defer h.head.symMtx.RUnlock()
|
2017-01-06 08:23:12 -08:00
|
|
|
|
|
2016-12-31 06:35:08 -08:00
|
|
|
|
res := [][]string{}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
for s := range h.head.values {
|
2016-12-31 06:35:08 -08:00
|
|
|
|
res = append(res, []string{s})
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
2016-12-31 06:35:08 -08:00
|
|
|
|
return res, nil
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
func (h *Head) getOrCreate(hash uint64, lset labels.Labels) (*memSeries, bool) {
|
|
|
|
|
// Just using `getOrSet` below would be semantically sufficient, but we'd create
|
|
|
|
|
// a new series on every sample inserted via Add(), which causes allocations
|
|
|
|
|
// and makes our series IDs rather random and harder to compress in postings.
|
|
|
|
|
s := h.series.getByHash(hash, lset)
|
|
|
|
|
if s != nil {
|
|
|
|
|
return s, false
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// Optimistically assume that we are the first one to create the series.
|
2017-09-04 07:08:38 -07:00
|
|
|
|
id := atomic.AddUint64(&h.lastSeriesID, 1)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
|
|
|
|
|
return h.getOrCreateWithID(id, hash, lset)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) getOrCreateWithID(id, hash uint64, lset labels.Labels) (*memSeries, bool) {
|
|
|
|
|
s := newMemSeries(lset, id, h.chunkRange)
|
2016-12-21 16:12:28 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
s, created := h.series.getOrSet(hash, s)
|
|
|
|
|
if !created {
|
2017-09-18 03:28:56 -07:00
|
|
|
|
return s, false
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
h.metrics.series.Inc()
|
|
|
|
|
h.metrics.seriesCreated.Inc()
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
h.postings.add(id, lset)
|
|
|
|
|
|
|
|
|
|
h.symMtx.Lock()
|
|
|
|
|
defer h.symMtx.Unlock()
|
2016-12-21 16:12:28 -08:00
|
|
|
|
|
|
|
|
|
for _, l := range lset {
|
|
|
|
|
valset, ok := h.values[l.Name]
|
|
|
|
|
if !ok {
|
|
|
|
|
valset = stringset{}
|
|
|
|
|
h.values[l.Name] = valset
|
|
|
|
|
}
|
|
|
|
|
valset.set(l.Value)
|
2017-01-03 06:43:26 -08:00
|
|
|
|
|
2017-08-05 04:31:48 -07:00
|
|
|
|
h.symbols[l.Name] = struct{}{}
|
|
|
|
|
h.symbols[l.Value] = struct{}{}
|
2016-12-21 16:12:28 -08:00
|
|
|
|
}
|
2017-01-03 06:43:26 -08:00
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
return s, true
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// seriesHashmap is a simple hashmap for memSeries by their label set. It is built
|
|
|
|
|
// on top of a regular hashmap and holds a slice of series to resolve hash collisions.
|
|
|
|
|
// Its methods require the hash to be submitted with it to avoid re-computations throughout
|
|
|
|
|
// the code.
|
|
|
|
|
type seriesHashmap map[uint64][]*memSeries
|
2016-12-04 04:16:11 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
func (m seriesHashmap) get(hash uint64, lset labels.Labels) *memSeries {
|
|
|
|
|
for _, s := range m[hash] {
|
|
|
|
|
if s.lset.Equals(lset) {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m seriesHashmap) set(hash uint64, s *memSeries) {
|
|
|
|
|
l := m[hash]
|
|
|
|
|
for i, prev := range l {
|
|
|
|
|
if prev.lset.Equals(s.lset) {
|
|
|
|
|
l[i] = s
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m[hash] = append(l, s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m seriesHashmap) del(hash uint64, lset labels.Labels) {
|
|
|
|
|
var rem []*memSeries
|
|
|
|
|
for _, s := range m[hash] {
|
|
|
|
|
if !s.lset.Equals(lset) {
|
|
|
|
|
rem = append(rem, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(rem) == 0 {
|
|
|
|
|
delete(m, hash)
|
|
|
|
|
} else {
|
|
|
|
|
m[hash] = rem
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stripeSeries locks modulo ranges of IDs and hashes to reduce lock contention.
|
|
|
|
|
// The locks are padded to not be on the same cache line. Filling the badded space
|
|
|
|
|
// with the maps was profiled to be slower – likely due to the additional pointer
|
|
|
|
|
// dereferences.
|
|
|
|
|
type stripeSeries struct {
|
|
|
|
|
series [stripeSize]map[uint64]*memSeries
|
|
|
|
|
hashes [stripeSize]seriesHashmap
|
|
|
|
|
locks [stripeSize]stripeLock
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
stripeSize = 1 << 14
|
|
|
|
|
stripeMask = stripeSize - 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type stripeLock struct {
|
|
|
|
|
sync.RWMutex
|
|
|
|
|
// Padding to avoid multiple locks being on the same cache line.
|
|
|
|
|
_ [40]byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newStripeSeries() *stripeSeries {
|
|
|
|
|
s := &stripeSeries{}
|
|
|
|
|
|
|
|
|
|
for i := range s.series {
|
|
|
|
|
s.series[i] = map[uint64]*memSeries{}
|
|
|
|
|
}
|
|
|
|
|
for i := range s.hashes {
|
|
|
|
|
s.hashes[i] = seriesHashmap{}
|
|
|
|
|
}
|
2017-01-11 04:02:38 -08:00
|
|
|
|
return s
|
2016-12-04 04:16:11 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// gc garbage collects old chunks that are strictly before mint and removes
|
|
|
|
|
// series entirely that have no chunks left.
|
|
|
|
|
func (s *stripeSeries) gc(mint int64) (map[uint64]struct{}, int) {
|
|
|
|
|
var (
|
|
|
|
|
deleted = map[uint64]struct{}{}
|
|
|
|
|
rmChunks = 0
|
|
|
|
|
)
|
|
|
|
|
// Run through all series and truncate old chunks. Mark those with no
|
2017-09-06 07:20:37 -07:00
|
|
|
|
// chunks left as deleted and store their ID.
|
2017-09-05 02:45:18 -07:00
|
|
|
|
for i := 0; i < stripeSize; i++ {
|
|
|
|
|
s.locks[i].Lock()
|
|
|
|
|
|
|
|
|
|
for hash, all := range s.hashes[i] {
|
|
|
|
|
for _, series := range all {
|
2017-09-07 23:48:19 -07:00
|
|
|
|
series.Lock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
rmChunks += series.truncateChunksBefore(mint)
|
|
|
|
|
|
|
|
|
|
if len(series.chunks) > 0 {
|
2017-09-07 23:48:19 -07:00
|
|
|
|
series.Unlock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The series is gone entirely. We need to keep the series lock
|
|
|
|
|
// and make sure we have acquired the stripe locks for hash and ID of the
|
|
|
|
|
// series alike.
|
|
|
|
|
// If we don't hold them all, there's a very small chance that a series receives
|
|
|
|
|
// samples again while we are half-way into deleting it.
|
|
|
|
|
j := int(series.ref & stripeMask)
|
|
|
|
|
|
|
|
|
|
if i != j {
|
|
|
|
|
s.locks[j].Lock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleted[series.ref] = struct{}{}
|
|
|
|
|
s.hashes[i].del(hash, series.lset)
|
|
|
|
|
delete(s.series[j], series.ref)
|
|
|
|
|
|
|
|
|
|
if i != j {
|
|
|
|
|
s.locks[j].Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 23:48:19 -07:00
|
|
|
|
series.Unlock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.locks[i].Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return deleted, rmChunks
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stripeSeries) getByID(id uint64) *memSeries {
|
|
|
|
|
i := id & stripeMask
|
|
|
|
|
|
|
|
|
|
s.locks[i].RLock()
|
|
|
|
|
series := s.series[i][id]
|
|
|
|
|
s.locks[i].RUnlock()
|
|
|
|
|
|
|
|
|
|
return series
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stripeSeries) getByHash(hash uint64, lset labels.Labels) *memSeries {
|
|
|
|
|
i := hash & stripeMask
|
|
|
|
|
|
|
|
|
|
s.locks[i].RLock()
|
|
|
|
|
series := s.hashes[i].get(hash, lset)
|
|
|
|
|
s.locks[i].RUnlock()
|
|
|
|
|
|
|
|
|
|
return series
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *stripeSeries) getOrSet(hash uint64, series *memSeries) (*memSeries, bool) {
|
|
|
|
|
i := hash & stripeMask
|
|
|
|
|
|
|
|
|
|
s.locks[i].Lock()
|
|
|
|
|
|
|
|
|
|
if prev := s.hashes[i].get(hash, series.lset); prev != nil {
|
2017-09-18 02:23:22 -07:00
|
|
|
|
s.locks[i].Unlock()
|
2017-09-05 02:45:18 -07:00
|
|
|
|
return prev, false
|
|
|
|
|
}
|
|
|
|
|
s.hashes[i].set(hash, series)
|
|
|
|
|
s.locks[i].Unlock()
|
|
|
|
|
|
|
|
|
|
i = series.ref & stripeMask
|
|
|
|
|
|
|
|
|
|
s.locks[i].Lock()
|
|
|
|
|
s.series[i][series.ref] = series
|
|
|
|
|
s.locks[i].Unlock()
|
|
|
|
|
|
|
|
|
|
return series, true
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-24 02:20:39 -07:00
|
|
|
|
type sample struct {
|
|
|
|
|
t int64
|
|
|
|
|
v float64
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 23:48:19 -07:00
|
|
|
|
// memSeries is the in-memory representation of a series. None of its methods
|
|
|
|
|
// are goroutine safe and its the callers responsibility to lock it.
|
2017-01-11 04:02:38 -08:00
|
|
|
|
type memSeries struct {
|
2017-09-07 23:48:19 -07:00
|
|
|
|
sync.Mutex
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
|
ref uint64
|
2017-08-28 15:39:17 -07:00
|
|
|
|
lset labels.Labels
|
|
|
|
|
chunks []*memChunk
|
|
|
|
|
chunkRange int64
|
|
|
|
|
firstChunkID int
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2017-06-07 04:42:53 -07:00
|
|
|
|
nextAt int64 // timestamp at which to cut the next chunk.
|
2017-01-11 04:02:38 -08:00
|
|
|
|
lastValue float64
|
2017-01-09 07:51:39 -08:00
|
|
|
|
sampleBuf [4]sample
|
|
|
|
|
|
2017-03-06 08:34:49 -08:00
|
|
|
|
app chunks.Appender // Current appender for the chunk.
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (s *memSeries) minTime() int64 {
|
|
|
|
|
return s.chunks[0].minTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *memSeries) maxTime() int64 {
|
|
|
|
|
return s.head().maxTime
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 04:42:53 -07:00
|
|
|
|
func (s *memSeries) cut(mint int64) *memChunk {
|
2017-01-11 04:02:38 -08:00
|
|
|
|
c := &memChunk{
|
|
|
|
|
chunk: chunks.NewXORChunk(),
|
2017-06-07 04:42:53 -07:00
|
|
|
|
minTime: mint,
|
2017-01-11 04:02:38 -08:00
|
|
|
|
maxTime: math.MinInt64,
|
|
|
|
|
}
|
|
|
|
|
s.chunks = append(s.chunks, c)
|
|
|
|
|
|
|
|
|
|
app, err := c.chunk.Appender()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
s.app = app
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
|
func newMemSeries(lset labels.Labels, id uint64, chunkRange int64) *memSeries {
|
2017-06-07 04:42:53 -07:00
|
|
|
|
s := &memSeries{
|
2017-08-28 15:39:17 -07:00
|
|
|
|
lset: lset,
|
|
|
|
|
ref: id,
|
|
|
|
|
chunkRange: chunkRange,
|
|
|
|
|
nextAt: math.MinInt64,
|
2017-06-07 04:42:53 -07:00
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// appendable checks whether the given sample is valid for appending to the series.
|
|
|
|
|
func (s *memSeries) appendable(t int64, v float64) error {
|
2017-09-01 05:38:49 -07:00
|
|
|
|
c := s.head()
|
|
|
|
|
if c == nil {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if t > c.maxTime {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if t < c.maxTime {
|
|
|
|
|
return ErrOutOfOrderSample
|
|
|
|
|
}
|
|
|
|
|
// We are allowing exact duplicates as we can encounter them in valid cases
|
|
|
|
|
// like federation and erroring out at that time would be extremely noisy.
|
|
|
|
|
if math.Float64bits(s.lastValue) != math.Float64bits(v) {
|
|
|
|
|
return ErrAmendSample
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *memSeries) chunk(id int) *memChunk {
|
2017-09-01 05:38:49 -07:00
|
|
|
|
ix := id - s.firstChunkID
|
|
|
|
|
if ix < 0 || ix >= len(s.chunks) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return s.chunks[ix]
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *memSeries) chunkID(pos int) int {
|
|
|
|
|
return pos + s.firstChunkID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// truncateChunksBefore removes all chunks from the series that have not timestamp
|
|
|
|
|
// at or after mint. Chunk IDs remain unchanged.
|
2017-08-30 08:38:25 -07:00
|
|
|
|
func (s *memSeries) truncateChunksBefore(mint int64) (removed int) {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
var k int
|
|
|
|
|
for i, c := range s.chunks {
|
|
|
|
|
if c.maxTime >= mint {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
k = i + 1
|
|
|
|
|
}
|
|
|
|
|
s.chunks = append(s.chunks[:0], s.chunks[k:]...)
|
|
|
|
|
s.firstChunkID += k
|
2017-08-30 08:38:25 -07:00
|
|
|
|
|
|
|
|
|
return k
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// append adds the sample (t, v) to the series.
|
2017-08-30 08:38:25 -07:00
|
|
|
|
func (s *memSeries) append(t int64, v float64) (success, chunkCreated bool) {
|
2017-06-07 04:42:53 -07:00
|
|
|
|
const samplesPerChunk = 120
|
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
|
c := s.head()
|
2017-01-11 04:02:38 -08:00
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if c == nil {
|
2017-06-07 04:42:53 -07:00
|
|
|
|
c = s.cut(t)
|
2017-08-30 08:38:25 -07:00
|
|
|
|
chunkCreated = true
|
2017-06-07 04:42:53 -07:00
|
|
|
|
}
|
|
|
|
|
if c.maxTime >= t {
|
2017-08-30 08:38:25 -07:00
|
|
|
|
return false, chunkCreated
|
2017-06-07 04:42:53 -07:00
|
|
|
|
}
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if c.chunk.NumSamples() > samplesPerChunk/4 && t >= s.nextAt {
|
2017-06-07 04:42:53 -07:00
|
|
|
|
c = s.cut(t)
|
2017-08-30 09:34:54 -07:00
|
|
|
|
chunkCreated = true
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
2017-01-11 04:02:38 -08:00
|
|
|
|
s.app.Append(t, v)
|
|
|
|
|
|
|
|
|
|
c.maxTime = t
|
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if c.chunk.NumSamples() == samplesPerChunk/4 {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
_, maxt := rangeForTimestamp(c.minTime, s.chunkRange)
|
|
|
|
|
s.nextAt = computeChunkEndTime(c.minTime, c.maxTime, maxt)
|
2017-06-07 04:42:53 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
s.lastValue = v
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
s.sampleBuf[0] = s.sampleBuf[1]
|
|
|
|
|
s.sampleBuf[1] = s.sampleBuf[2]
|
|
|
|
|
s.sampleBuf[2] = s.sampleBuf[3]
|
|
|
|
|
s.sampleBuf[3] = sample{t: t, v: v}
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2017-08-30 08:38:25 -07:00
|
|
|
|
return true, chunkCreated
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 04:42:53 -07:00
|
|
|
|
// computeChunkEndTime estimates the end timestamp based the beginning of a chunk,
|
|
|
|
|
// its current timestamp and the upper bound up to which we insert data.
|
|
|
|
|
// It assumes that the time range is 1/4 full.
|
|
|
|
|
func computeChunkEndTime(start, cur, max int64) int64 {
|
|
|
|
|
a := (max - start) / ((cur - start + 1) * 4)
|
|
|
|
|
if a == 0 {
|
|
|
|
|
return max
|
|
|
|
|
}
|
|
|
|
|
return start + (max-start)/a
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
func (s *memSeries) iterator(id int) chunks.Iterator {
|
|
|
|
|
c := s.chunk(id)
|
2017-01-11 04:02:38 -08:00
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if id-s.firstChunkID < len(s.chunks)-1 {
|
2017-01-11 04:02:38 -08:00
|
|
|
|
return c.chunk.Iterator()
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
// Serve the last 4 samples for the last chunk from the series buffer
|
|
|
|
|
// as their compressed bytes may be mutated by added samples.
|
2017-01-09 07:51:39 -08:00
|
|
|
|
it := &memSafeIterator{
|
2017-01-11 04:02:38 -08:00
|
|
|
|
Iterator: c.chunk.Iterator(),
|
2017-01-09 07:51:39 -08:00
|
|
|
|
i: -1,
|
2017-09-01 05:38:49 -07:00
|
|
|
|
total: c.chunk.NumSamples(),
|
2017-01-11 04:02:38 -08:00
|
|
|
|
buf: s.sampleBuf,
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
return it
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
func (s *memSeries) head() *memChunk {
|
2017-09-01 05:38:49 -07:00
|
|
|
|
if len(s.chunks) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-01-11 04:02:38 -08:00
|
|
|
|
return s.chunks[len(s.chunks)-1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type memChunk struct {
|
|
|
|
|
chunk chunks.Chunk
|
|
|
|
|
minTime, maxTime int64
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 07:51:39 -08:00
|
|
|
|
type memSafeIterator struct {
|
|
|
|
|
chunks.Iterator
|
|
|
|
|
|
|
|
|
|
i int
|
|
|
|
|
total int
|
|
|
|
|
buf [4]sample
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (it *memSafeIterator) Next() bool {
|
|
|
|
|
if it.i+1 >= it.total {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
it.i++
|
|
|
|
|
if it.total-it.i > 4 {
|
|
|
|
|
return it.Iterator.Next()
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (it *memSafeIterator) At() (int64, float64) {
|
|
|
|
|
if it.total-it.i > 4 {
|
|
|
|
|
return it.Iterator.At()
|
|
|
|
|
}
|
|
|
|
|
s := it.buf[4-(it.total-it.i)]
|
|
|
|
|
return s.t, s.v
|
|
|
|
|
}
|