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 (
|
2019-05-24 11:33:28 -07:00
|
|
|
|
"fmt"
|
2021-08-17 10:08:16 -07:00
|
|
|
|
"io"
|
2017-01-04 05:06:40 -08:00
|
|
|
|
"math"
|
2020-05-06 08:30:00 -07:00
|
|
|
|
"path/filepath"
|
2023-03-07 08:41:33 -08:00
|
|
|
|
"runtime"
|
2016-12-04 04:16:11 -08:00
|
|
|
|
"sync"
|
2017-08-30 09:34:54 -07:00
|
|
|
|
"time"
|
2017-05-17 07:43:01 -07:00
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
|
"github.com/go-kit/log/level"
|
2019-07-23 01:04:48 -07:00
|
|
|
|
"github.com/oklog/ulid"
|
2017-01-19 02:22:47 -08:00
|
|
|
|
"github.com/pkg/errors"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
|
2021-07-19 21:52:57 -07:00
|
|
|
|
"github.com/prometheus/prometheus/config"
|
2021-11-08 06:23:17 -08:00
|
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 06:57:07 -07:00
|
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-11-08 06:23:17 -08:00
|
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2022-07-19 01:58:52 -07:00
|
|
|
|
"github.com/prometheus/prometheus/model/metadata"
|
2020-02-06 07:58:38 -08:00
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
2019-08-13 01:34:14 -07:00
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
2020-05-06 08:30:00 -07:00
|
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
2019-08-13 01:34:14 -07:00
|
|
|
|
"github.com/prometheus/prometheus/tsdb/index"
|
2019-09-19 02:15:41 -07:00
|
|
|
|
"github.com/prometheus/prometheus/tsdb/record"
|
|
|
|
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
2020-07-31 08:03:02 -07:00
|
|
|
|
"github.com/prometheus/prometheus/tsdb/tsdbutil"
|
2022-10-10 08:08:46 -07:00
|
|
|
|
"github.com/prometheus/prometheus/tsdb/wlog"
|
2023-03-29 12:34:34 -07:00
|
|
|
|
"github.com/prometheus/prometheus/util/zeropool"
|
2016-12-04 04:16:11 -08:00
|
|
|
|
)
|
|
|
|
|
|
2017-01-17 07:33:58 -08:00
|
|
|
|
var (
|
2020-01-20 03:05:27 -08:00
|
|
|
|
// ErrInvalidSample is returned if an appended sample is not valid and can't
|
|
|
|
|
// be ingested.
|
|
|
|
|
ErrInvalidSample = errors.New("invalid sample")
|
2021-03-16 02:47:45 -07:00
|
|
|
|
// ErrInvalidExemplar is returned if an appended exemplar is not valid and can't
|
|
|
|
|
// be ingested.
|
|
|
|
|
ErrInvalidExemplar = errors.New("invalid exemplar")
|
2020-07-22 02:57:38 -07:00
|
|
|
|
// ErrAppenderClosed is returned if an appender has already be successfully
|
2020-08-07 00:57:25 -07:00
|
|
|
|
// rolled back or committed.
|
2020-07-22 02:57:38 -07:00
|
|
|
|
ErrAppenderClosed = errors.New("appender closed")
|
2021-11-19 02:11:32 -08:00
|
|
|
|
|
|
|
|
|
// defaultIsolationDisabled is true if isolation is disabled by default.
|
|
|
|
|
defaultIsolationDisabled = false
|
2023-03-07 08:41:33 -08:00
|
|
|
|
|
|
|
|
|
defaultWALReplayConcurrency = runtime.GOMAXPROCS(0)
|
2017-01-17 07:33:58 -08:00
|
|
|
|
)
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Head handles reads and writes of time series data within a time window.
|
|
|
|
|
type Head struct {
|
2021-07-20 01:47:20 -07:00
|
|
|
|
chunkRange atomic.Int64
|
|
|
|
|
numSeries atomic.Uint64
|
2022-09-20 10:05:50 -07:00
|
|
|
|
minOOOTime, maxOOOTime atomic.Int64 // TODO(jesusvazquez) These should be updated after garbage collection.
|
|
|
|
|
minTime, maxTime atomic.Int64 // Current min and max of the samples included in the head. TODO(jesusvazquez) Ensure these are properly tracked.
|
2021-07-20 01:47:20 -07:00
|
|
|
|
minValidTime atomic.Int64 // Mint allowed to be added to the head. It shouldn't be lower than the maxt of the last persisted block.
|
|
|
|
|
lastWALTruncationTime atomic.Int64
|
|
|
|
|
lastMemoryTruncationTime atomic.Int64
|
|
|
|
|
lastSeriesID atomic.Uint64
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// All the ooo m-map chunks should be after this. This is used to truncate old ooo m-map chunks.
|
|
|
|
|
// This should be typecasted to chunks.ChunkDiskMapperRef after loading.
|
|
|
|
|
minOOOMmapRef atomic.Uint64
|
2019-10-09 08:41:46 -07:00
|
|
|
|
|
2022-12-28 00:55:07 -08:00
|
|
|
|
metrics *headMetrics
|
|
|
|
|
opts *HeadOptions
|
|
|
|
|
wal, wbl *wlog.WL
|
|
|
|
|
exemplarMetrics *ExemplarMetrics
|
|
|
|
|
exemplars ExemplarStorage
|
|
|
|
|
logger log.Logger
|
2023-03-29 12:34:34 -07:00
|
|
|
|
appendPool zeropool.Pool[[]record.RefSample]
|
|
|
|
|
exemplarsPool zeropool.Pool[[]exemplarWithSeriesRef]
|
|
|
|
|
histogramsPool zeropool.Pool[[]record.RefHistogramSample]
|
|
|
|
|
floatHistogramsPool zeropool.Pool[[]record.RefFloatHistogramSample]
|
|
|
|
|
metadataPool zeropool.Pool[[]record.RefMetadata]
|
|
|
|
|
seriesPool zeropool.Pool[[]*memSeries]
|
|
|
|
|
bytesPool zeropool.Pool[[]byte]
|
2022-12-28 00:55:07 -08:00
|
|
|
|
memChunkPool sync.Pool
|
2017-02-04 02:53:52 -08:00
|
|
|
|
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// All series addressable by their ID or hash.
|
2021-02-09 06:12:48 -08:00
|
|
|
|
series *stripeSeries
|
2016-12-21 16:12:28 -08:00
|
|
|
|
|
2019-04-09 06:16:24 -07:00
|
|
|
|
deletedMtx sync.Mutex
|
2021-11-06 03:10:04 -07:00
|
|
|
|
deleted map[chunks.HeadSeriesRef]int // Deleted series, and what WAL segment they must be kept until.
|
2019-04-09 06:16:24 -07:00
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// TODO(codesome): Extend MemPostings to return only OOOPostings, Set OOOStatus, ... Like an additional map of ooo postings.
|
2020-02-17 10:37:09 -08:00
|
|
|
|
postings *index.MemPostings // Postings lists for terms.
|
2019-11-04 18:06:13 -08:00
|
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
|
tombstones *tombstones.MemTombstones
|
|
|
|
|
|
2020-02-12 11:22:27 -08:00
|
|
|
|
iso *isolation
|
|
|
|
|
|
2019-11-04 18:06:13 -08:00
|
|
|
|
cardinalityMutex sync.Mutex
|
2020-02-17 10:37:09 -08:00
|
|
|
|
cardinalityCache *index.PostingsStats // Posting stats cache which will expire after 30sec.
|
|
|
|
|
lastPostingsStatsCall time.Duration // Last posting stats call (PostingsCardinalityStats()) time for caching.
|
2020-05-06 08:30:00 -07:00
|
|
|
|
|
|
|
|
|
// chunkDiskMapper is used to write and read Head chunks to/from disk.
|
|
|
|
|
chunkDiskMapper *chunks.ChunkDiskMapper
|
2020-05-22 02:03:23 -07:00
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
chunkSnapshotMtx sync.Mutex
|
|
|
|
|
|
2020-05-22 02:03:23 -07:00
|
|
|
|
closedMtx sync.Mutex
|
|
|
|
|
closed bool
|
2021-06-05 07:29:32 -07:00
|
|
|
|
|
|
|
|
|
stats *HeadStats
|
2021-07-19 21:52:57 -07:00
|
|
|
|
reg prometheus.Registerer
|
2021-07-20 01:47:20 -07:00
|
|
|
|
|
2023-05-15 12:31:49 -07:00
|
|
|
|
writeNotified wlog.WriteNotified
|
|
|
|
|
|
2021-07-20 01:47:20 -07:00
|
|
|
|
memTruncationInProcess atomic.Bool
|
2016-12-04 04:16:11 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
type ExemplarStorage interface {
|
|
|
|
|
storage.ExemplarQueryable
|
|
|
|
|
AddExemplar(labels.Labels, exemplar.Exemplar) error
|
|
|
|
|
ValidateExemplar(labels.Labels, exemplar.Exemplar) error
|
2021-08-30 07:04:38 -07:00
|
|
|
|
IterateExemplars(f func(seriesLabels labels.Labels, e exemplar.Exemplar) error) error
|
2016-12-04 04:16:11 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 06:12:48 -08:00
|
|
|
|
// HeadOptions are parameters for the Head block.
|
|
|
|
|
type HeadOptions struct {
|
2021-08-12 09:38:06 -07:00
|
|
|
|
// Runtime reloadable option. At the top of the struct for 32 bit OS:
|
|
|
|
|
// https://pkg.go.dev/sync/atomic#pkg-note-BUG
|
|
|
|
|
MaxExemplars atomic.Int64
|
|
|
|
|
|
2022-11-09 02:18:49 -08:00
|
|
|
|
OutOfOrderTimeWindow atomic.Int64
|
|
|
|
|
OutOfOrderCapMax atomic.Int64
|
|
|
|
|
|
2022-09-14 05:08:34 -07:00
|
|
|
|
// EnableNativeHistograms enables the ingestion of native histograms.
|
|
|
|
|
EnableNativeHistograms atomic.Bool
|
|
|
|
|
|
2021-02-09 06:12:48 -08:00
|
|
|
|
ChunkRange int64
|
|
|
|
|
// ChunkDirRoot is the parent directory of the chunks directory.
|
|
|
|
|
ChunkDirRoot string
|
|
|
|
|
ChunkPool chunkenc.Pool
|
|
|
|
|
ChunkWriteBufferSize int
|
2022-01-10 05:36:45 -08:00
|
|
|
|
ChunkWriteQueueSize int
|
|
|
|
|
|
2023-04-12 09:48:35 -07:00
|
|
|
|
SamplesPerChunk int
|
|
|
|
|
|
2021-02-09 06:12:48 -08:00
|
|
|
|
// StripeSize sets the number of entries in the hash map, it must be a power of 2.
|
|
|
|
|
// A larger StripeSize will allocate more memory up-front, but will increase performance when handling a large number of series.
|
|
|
|
|
// A smaller StripeSize reduces the memory allocated, but can decrease performance with large number of series.
|
2021-08-11 03:13:17 -07:00
|
|
|
|
StripeSize int
|
|
|
|
|
SeriesCallback SeriesLifecycleCallback
|
|
|
|
|
EnableExemplarStorage bool
|
|
|
|
|
EnableMemorySnapshotOnShutdown bool
|
2021-11-19 02:11:32 -08:00
|
|
|
|
|
|
|
|
|
IsolationDisabled bool
|
2023-03-07 08:41:33 -08:00
|
|
|
|
|
|
|
|
|
// Maximum number of CPUs that can simultaneously processes WAL replay.
|
|
|
|
|
// The default value is GOMAXPROCS.
|
|
|
|
|
// If it is set to a negative value or zero, the default value is used.
|
|
|
|
|
WALReplayConcurrency int
|
2021-02-09 06:12:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
const (
|
|
|
|
|
// DefaultOutOfOrderCapMax is the default maximum size of an in-memory out-of-order chunk.
|
|
|
|
|
DefaultOutOfOrderCapMax int64 = 32
|
2023-04-12 09:48:35 -07:00
|
|
|
|
// DefaultSamplesPerChunk provides a default target number of samples per chunk.
|
|
|
|
|
DefaultSamplesPerChunk = 120
|
2022-09-20 10:05:50 -07:00
|
|
|
|
)
|
|
|
|
|
|
2021-02-09 06:12:48 -08:00
|
|
|
|
func DefaultHeadOptions() *HeadOptions {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
ho := &HeadOptions{
|
2021-02-09 06:12:48 -08:00
|
|
|
|
ChunkRange: DefaultBlockDuration,
|
|
|
|
|
ChunkDirRoot: "",
|
|
|
|
|
ChunkPool: chunkenc.NewPool(),
|
|
|
|
|
ChunkWriteBufferSize: chunks.DefaultWriteBufferSize,
|
2022-01-10 05:36:45 -08:00
|
|
|
|
ChunkWriteQueueSize: chunks.DefaultWriteQueueSize,
|
2023-04-12 09:48:35 -07:00
|
|
|
|
SamplesPerChunk: DefaultSamplesPerChunk,
|
2021-02-09 06:12:48 -08:00
|
|
|
|
StripeSize: DefaultStripeSize,
|
|
|
|
|
SeriesCallback: &noopSeriesLifecycleCallback{},
|
2021-11-19 02:11:32 -08:00
|
|
|
|
IsolationDisabled: defaultIsolationDisabled,
|
2023-03-07 08:41:33 -08:00
|
|
|
|
WALReplayConcurrency: defaultWALReplayConcurrency,
|
2021-02-09 06:12:48 -08:00
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
ho.OutOfOrderCapMax.Store(DefaultOutOfOrderCapMax)
|
|
|
|
|
return ho
|
2021-02-09 06:12:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
// SeriesLifecycleCallback specifies a list of callbacks that will be called during a lifecycle of a series.
|
|
|
|
|
// It is always a no-op in Prometheus and mainly meant for external users who import TSDB.
|
|
|
|
|
// All the callbacks should be safe to be called concurrently.
|
|
|
|
|
// It is up to the user to implement soft or hard consistency by making the callbacks
|
|
|
|
|
// atomic or non-atomic. Atomic callbacks can cause degradation performance.
|
|
|
|
|
type SeriesLifecycleCallback interface {
|
|
|
|
|
// PreCreation is called before creating a series to indicate if the series can be created.
|
|
|
|
|
// A non nil error means the series should not be created.
|
|
|
|
|
PreCreation(labels.Labels) error
|
|
|
|
|
// PostCreation is called after creating a series to indicate a creation of series.
|
|
|
|
|
PostCreation(labels.Labels)
|
|
|
|
|
// PostDeletion is called after deletion of series.
|
|
|
|
|
PostDeletion(...labels.Labels)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHead opens the head block in dir.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
func NewHead(r prometheus.Registerer, l log.Logger, wal, wbl *wlog.WL, opts *HeadOptions, stats *HeadStats) (*Head, error) {
|
2021-08-03 05:14:26 -07:00
|
|
|
|
var err error
|
|
|
|
|
if l == nil {
|
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
|
|
|
|
|
if opts.OutOfOrderTimeWindow.Load() < 0 {
|
|
|
|
|
opts.OutOfOrderTimeWindow.Store(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Time window can be set on runtime. So the capMin and capMax should be valid
|
|
|
|
|
// even if ooo is not enabled yet.
|
|
|
|
|
capMax := opts.OutOfOrderCapMax.Load()
|
|
|
|
|
if capMax <= 0 || capMax > 255 {
|
|
|
|
|
return nil, errors.Errorf("OOOCapMax of %d is invalid. must be > 0 and <= 255", capMax)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
if opts.ChunkRange < 1 {
|
|
|
|
|
return nil, errors.Errorf("invalid chunk range %d", opts.ChunkRange)
|
|
|
|
|
}
|
|
|
|
|
if opts.SeriesCallback == nil {
|
|
|
|
|
opts.SeriesCallback = &noopSeriesLifecycleCallback{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if stats == nil {
|
|
|
|
|
stats = NewHeadStats()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 22:21:25 -07:00
|
|
|
|
if !opts.EnableExemplarStorage {
|
|
|
|
|
opts.MaxExemplars.Store(0)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
h := &Head{
|
2021-08-17 10:08:16 -07:00
|
|
|
|
wal: wal,
|
2022-09-20 10:05:50 -07:00
|
|
|
|
wbl: wbl,
|
2021-08-17 10:08:16 -07:00
|
|
|
|
logger: l,
|
|
|
|
|
opts: opts,
|
2021-08-03 05:14:26 -07:00
|
|
|
|
memChunkPool: sync.Pool{
|
|
|
|
|
New: func() interface{} {
|
|
|
|
|
return &memChunk{}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stats: stats,
|
|
|
|
|
reg: r,
|
|
|
|
|
}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
if err := h.resetInMemoryState(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-08-03 05:14:26 -07:00
|
|
|
|
h.metrics = newHeadMetrics(h, r)
|
|
|
|
|
|
|
|
|
|
if opts.ChunkPool == nil {
|
|
|
|
|
opts.ChunkPool = chunkenc.NewPool()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 08:41:33 -08:00
|
|
|
|
if opts.WALReplayConcurrency <= 0 {
|
|
|
|
|
opts.WALReplayConcurrency = defaultWALReplayConcurrency
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
h.chunkDiskMapper, err = chunks.NewChunkDiskMapper(
|
2022-01-10 05:36:45 -08:00
|
|
|
|
r,
|
2021-08-03 05:14:26 -07:00
|
|
|
|
mmappedChunksDir(opts.ChunkDirRoot),
|
|
|
|
|
opts.ChunkPool,
|
|
|
|
|
opts.ChunkWriteBufferSize,
|
2022-01-10 05:36:45 -08:00
|
|
|
|
opts.ChunkWriteQueueSize,
|
2021-08-03 05:14:26 -07:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 10:08:16 -07:00
|
|
|
|
func (h *Head) resetInMemoryState() error {
|
|
|
|
|
var err error
|
2021-10-04 22:21:25 -07:00
|
|
|
|
var em *ExemplarMetrics
|
|
|
|
|
if h.exemplars != nil {
|
|
|
|
|
ce, ok := h.exemplars.(*CircularExemplarStorage)
|
|
|
|
|
if ok {
|
|
|
|
|
em = ce.metrics
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if em == nil {
|
|
|
|
|
em = NewExemplarMetrics(h.reg)
|
|
|
|
|
}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
es, err := NewCircularExemplarStorage(h.opts.MaxExemplars.Load(), em)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-19 02:11:32 -08:00
|
|
|
|
h.iso = newIsolation(h.opts.IsolationDisabled)
|
|
|
|
|
|
2021-08-17 10:08:16 -07:00
|
|
|
|
h.exemplarMetrics = em
|
|
|
|
|
h.exemplars = es
|
|
|
|
|
h.series = newStripeSeries(h.opts.StripeSize, h.opts.SeriesCallback)
|
|
|
|
|
h.postings = index.NewUnorderedMemPostings()
|
|
|
|
|
h.tombstones = tombstones.NewMemTombstones()
|
2021-11-06 03:10:04 -07:00
|
|
|
|
h.deleted = map[chunks.HeadSeriesRef]int{}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
h.chunkRange.Store(h.opts.ChunkRange)
|
|
|
|
|
h.minTime.Store(math.MaxInt64)
|
|
|
|
|
h.maxTime.Store(math.MinInt64)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
h.minOOOTime.Store(math.MaxInt64)
|
|
|
|
|
h.maxOOOTime.Store(math.MinInt64)
|
2021-08-17 10:08:16 -07:00
|
|
|
|
h.lastWALTruncationTime.Store(math.MinInt64)
|
|
|
|
|
h.lastMemoryTruncationTime.Store(math.MinInt64)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
type headMetrics struct {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
activeAppenders prometheus.Gauge
|
|
|
|
|
series prometheus.GaugeFunc
|
|
|
|
|
seriesCreated prometheus.Counter
|
|
|
|
|
seriesRemoved prometheus.Counter
|
|
|
|
|
seriesNotFound prometheus.Counter
|
|
|
|
|
chunks prometheus.Gauge
|
|
|
|
|
chunksCreated prometheus.Counter
|
|
|
|
|
chunksRemoved prometheus.Counter
|
|
|
|
|
gcDuration prometheus.Summary
|
2022-10-11 09:35:35 -07:00
|
|
|
|
samplesAppended *prometheus.CounterVec
|
2022-09-20 10:05:50 -07:00
|
|
|
|
outOfOrderSamplesAppended prometheus.Counter
|
2022-10-11 09:35:35 -07:00
|
|
|
|
outOfBoundSamples *prometheus.CounterVec
|
|
|
|
|
outOfOrderSamples *prometheus.CounterVec
|
|
|
|
|
tooOldSamples *prometheus.CounterVec
|
2022-09-20 10:05:50 -07:00
|
|
|
|
walTruncateDuration prometheus.Summary
|
|
|
|
|
walCorruptionsTotal prometheus.Counter
|
|
|
|
|
dataTotalReplayDuration prometheus.Gauge
|
|
|
|
|
headTruncateFail prometheus.Counter
|
|
|
|
|
headTruncateTotal prometheus.Counter
|
|
|
|
|
checkpointDeleteFail prometheus.Counter
|
|
|
|
|
checkpointDeleteTotal prometheus.Counter
|
|
|
|
|
checkpointCreationFail prometheus.Counter
|
|
|
|
|
checkpointCreationTotal prometheus.Counter
|
|
|
|
|
mmapChunkCorruptionTotal prometheus.Counter
|
|
|
|
|
snapshotReplayErrorTotal prometheus.Counter // Will be either 0 or 1.
|
|
|
|
|
oooHistogram prometheus.Histogram
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 00:53:28 -07:00
|
|
|
|
const (
|
|
|
|
|
sampleMetricTypeFloat = "float"
|
|
|
|
|
sampleMetricTypeHistogram = "histogram"
|
|
|
|
|
)
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
func newHeadMetrics(h *Head, r prometheus.Registerer) *headMetrics {
|
2020-02-12 11:22:27 -08:00
|
|
|
|
m := &headMetrics{
|
|
|
|
|
activeAppenders: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_active_appenders",
|
|
|
|
|
Help: "Number of currently active appender transactions",
|
|
|
|
|
}),
|
|
|
|
|
series: prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_series",
|
|
|
|
|
Help: "Total number of series in the head block.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.NumSeries())
|
|
|
|
|
}),
|
|
|
|
|
seriesCreated: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_series_created_total",
|
|
|
|
|
Help: "Total number of series created in the head",
|
|
|
|
|
}),
|
|
|
|
|
seriesRemoved: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_series_removed_total",
|
|
|
|
|
Help: "Total number of series removed in the head",
|
|
|
|
|
}),
|
|
|
|
|
seriesNotFound: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_series_not_found_total",
|
|
|
|
|
Help: "Total number of requests for series that were not found.",
|
|
|
|
|
}),
|
|
|
|
|
chunks: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_chunks",
|
|
|
|
|
Help: "Total number of chunks in the head block.",
|
|
|
|
|
}),
|
|
|
|
|
chunksCreated: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_chunks_created_total",
|
|
|
|
|
Help: "Total number of chunks created in the head",
|
|
|
|
|
}),
|
|
|
|
|
chunksRemoved: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_chunks_removed_total",
|
|
|
|
|
Help: "Total number of chunks removed in the head",
|
|
|
|
|
}),
|
|
|
|
|
gcDuration: prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_gc_duration_seconds",
|
|
|
|
|
Help: "Runtime of garbage collection in the head block.",
|
|
|
|
|
}),
|
|
|
|
|
walTruncateDuration: prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
|
Name: "prometheus_tsdb_wal_truncate_duration_seconds",
|
|
|
|
|
Help: "Duration of WAL truncation.",
|
|
|
|
|
}),
|
|
|
|
|
walCorruptionsTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_wal_corruptions_total",
|
|
|
|
|
Help: "Total number of WAL corruptions.",
|
|
|
|
|
}),
|
2022-09-20 10:05:50 -07:00
|
|
|
|
dataTotalReplayDuration: prometheus.NewGauge(prometheus.GaugeOpts{
|
2020-09-21 09:25:05 -07:00
|
|
|
|
Name: "prometheus_tsdb_data_replay_duration_seconds",
|
|
|
|
|
Help: "Time taken to replay the data on disk.",
|
|
|
|
|
}),
|
2022-10-03 00:53:28 -07:00
|
|
|
|
samplesAppended: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2020-02-12 11:22:27 -08:00
|
|
|
|
Name: "prometheus_tsdb_head_samples_appended_total",
|
|
|
|
|
Help: "Total number of appended samples.",
|
2022-10-03 00:53:28 -07:00
|
|
|
|
}, []string{"type"}),
|
2022-09-20 10:05:50 -07:00
|
|
|
|
outOfOrderSamplesAppended: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_out_of_order_samples_appended_total",
|
|
|
|
|
Help: "Total number of appended out of order samples.",
|
|
|
|
|
}),
|
2022-10-11 09:35:35 -07:00
|
|
|
|
outOfBoundSamples: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2020-05-06 08:30:00 -07:00
|
|
|
|
Name: "prometheus_tsdb_out_of_bound_samples_total",
|
2022-09-20 10:05:50 -07:00
|
|
|
|
Help: "Total number of out of bound samples ingestion failed attempts with out of order support disabled.",
|
2022-10-11 09:35:35 -07:00
|
|
|
|
}, []string{"type"}),
|
|
|
|
|
outOfOrderSamples: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2020-05-06 08:30:00 -07:00
|
|
|
|
Name: "prometheus_tsdb_out_of_order_samples_total",
|
2022-09-20 10:05:50 -07:00
|
|
|
|
Help: "Total number of out of order samples ingestion failed attempts due to out of order being disabled.",
|
2022-10-11 09:35:35 -07:00
|
|
|
|
}, []string{"type"}),
|
|
|
|
|
tooOldSamples: prometheus.NewCounterVec(prometheus.CounterOpts{
|
2022-09-20 10:05:50 -07:00
|
|
|
|
Name: "prometheus_tsdb_too_old_samples_total",
|
|
|
|
|
Help: "Total number of out of order samples ingestion failed attempts with out of support enabled, but sample outside of time window.",
|
2022-10-11 09:35:35 -07:00
|
|
|
|
}, []string{"type"}),
|
2020-02-12 11:22:27 -08:00
|
|
|
|
headTruncateFail: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_truncations_failed_total",
|
|
|
|
|
Help: "Total number of head truncations that failed.",
|
|
|
|
|
}),
|
|
|
|
|
headTruncateTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_truncations_total",
|
|
|
|
|
Help: "Total number of head truncations attempted.",
|
|
|
|
|
}),
|
|
|
|
|
checkpointDeleteFail: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_checkpoint_deletions_failed_total",
|
|
|
|
|
Help: "Total number of checkpoint deletions that failed.",
|
|
|
|
|
}),
|
|
|
|
|
checkpointDeleteTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_checkpoint_deletions_total",
|
|
|
|
|
Help: "Total number of checkpoint deletions attempted.",
|
|
|
|
|
}),
|
|
|
|
|
checkpointCreationFail: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_checkpoint_creations_failed_total",
|
|
|
|
|
Help: "Total number of checkpoint creations that failed.",
|
|
|
|
|
}),
|
|
|
|
|
checkpointCreationTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_checkpoint_creations_total",
|
|
|
|
|
Help: "Total number of checkpoint creations attempted.",
|
|
|
|
|
}),
|
2020-05-06 08:30:00 -07:00
|
|
|
|
mmapChunkCorruptionTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_mmap_chunk_corruptions_total",
|
|
|
|
|
Help: "Total number of memory-mapped chunk corruptions.",
|
|
|
|
|
}),
|
2021-08-17 10:08:16 -07:00
|
|
|
|
snapshotReplayErrorTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
|
Name: "prometheus_tsdb_snapshot_replay_error_total",
|
|
|
|
|
Help: "Total number snapshot replays that failed.",
|
|
|
|
|
}),
|
2022-09-20 10:05:50 -07:00
|
|
|
|
oooHistogram: prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
|
|
|
Name: "prometheus_tsdb_sample_ooo_delta",
|
|
|
|
|
Help: "Delta in seconds by which a sample is considered out of order (reported regardless of OOO time window and whether sample is accepted or not).",
|
|
|
|
|
Buckets: []float64{
|
|
|
|
|
60 * 10, // 10 min
|
|
|
|
|
60 * 30, // 30 min
|
|
|
|
|
60 * 60, // 60 min
|
|
|
|
|
60 * 60 * 2, // 2h
|
|
|
|
|
60 * 60 * 3, // 3h
|
|
|
|
|
60 * 60 * 6, // 6h
|
|
|
|
|
60 * 60 * 12, // 12h
|
|
|
|
|
},
|
|
|
|
|
}),
|
2020-02-12 11:22:27 -08:00
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
|
|
|
|
if r != nil {
|
|
|
|
|
r.MustRegister(
|
|
|
|
|
m.activeAppenders,
|
2020-02-12 11:22:27 -08:00
|
|
|
|
m.series,
|
2017-08-30 09:34:54 -07:00
|
|
|
|
m.chunks,
|
|
|
|
|
m.chunksCreated,
|
|
|
|
|
m.chunksRemoved,
|
|
|
|
|
m.seriesCreated,
|
|
|
|
|
m.seriesRemoved,
|
2017-10-12 06:25:12 -07:00
|
|
|
|
m.seriesNotFound,
|
2017-08-30 09:34:54 -07:00
|
|
|
|
m.gcDuration,
|
|
|
|
|
m.walTruncateDuration,
|
2018-12-18 02:24:56 -08:00
|
|
|
|
m.walCorruptionsTotal,
|
2022-09-20 10:05:50 -07:00
|
|
|
|
m.dataTotalReplayDuration,
|
2017-08-30 09:34:54 -07:00
|
|
|
|
m.samplesAppended,
|
2022-09-20 10:05:50 -07:00
|
|
|
|
m.outOfOrderSamplesAppended,
|
2020-05-06 08:30:00 -07:00
|
|
|
|
m.outOfBoundSamples,
|
|
|
|
|
m.outOfOrderSamples,
|
2022-09-20 10:05:50 -07:00
|
|
|
|
m.tooOldSamples,
|
2018-09-25 06:48:33 -07:00
|
|
|
|
m.headTruncateFail,
|
|
|
|
|
m.headTruncateTotal,
|
2018-09-25 04:49:09 -07:00
|
|
|
|
m.checkpointDeleteFail,
|
2018-09-25 06:48:33 -07:00
|
|
|
|
m.checkpointDeleteTotal,
|
|
|
|
|
m.checkpointCreationFail,
|
|
|
|
|
m.checkpointCreationTotal,
|
2020-05-06 08:30:00 -07:00
|
|
|
|
m.mmapChunkCorruptionTotal,
|
2021-08-17 10:08:16 -07:00
|
|
|
|
m.snapshotReplayErrorTotal,
|
2020-02-12 11:22:27 -08:00
|
|
|
|
// Metrics bound to functions and not needed in tests
|
|
|
|
|
// can be created and registered on the spot.
|
|
|
|
|
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_max_time",
|
|
|
|
|
Help: "Maximum timestamp of the head block. The unit is decided by the library consumer.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.MaxTime())
|
|
|
|
|
}),
|
|
|
|
|
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_min_time",
|
|
|
|
|
Help: "Minimum time bound of the head block. The unit is decided by the library consumer.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.MinTime())
|
|
|
|
|
}),
|
|
|
|
|
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_isolation_low_watermark",
|
|
|
|
|
Help: "The lowest TSDB append ID that is still referenced.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
return float64(h.iso.lowWatermark())
|
|
|
|
|
}),
|
|
|
|
|
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_isolation_high_watermark",
|
|
|
|
|
Help: "The highest TSDB append ID that has been given out.",
|
|
|
|
|
}, func() float64 {
|
2020-06-03 11:09:05 -07:00
|
|
|
|
return float64(h.iso.lastAppendID())
|
2020-02-12 11:22:27 -08:00
|
|
|
|
}),
|
2023-03-02 01:55:56 -08:00
|
|
|
|
prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
|
|
|
Name: "prometheus_tsdb_head_chunks_storage_size_bytes",
|
|
|
|
|
Help: "Size of the chunks_head directory.",
|
|
|
|
|
}, func() float64 {
|
|
|
|
|
val, err := h.chunkDiskMapper.Size()
|
|
|
|
|
if err != nil {
|
|
|
|
|
level.Error(h.logger).Log("msg", "Failed to calculate size of \"chunks_head\" dir",
|
|
|
|
|
"err", err.Error())
|
|
|
|
|
}
|
|
|
|
|
return float64(val)
|
|
|
|
|
}),
|
2017-08-30 09:34:54 -07:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
func mmappedChunksDir(dir string) string { return filepath.Join(dir, "chunks_head") }
|
|
|
|
|
|
2021-06-05 07:29:32 -07:00
|
|
|
|
// HeadStats are the statistics for the head component of the DB.
|
|
|
|
|
type HeadStats struct {
|
|
|
|
|
WALReplayStatus *WALReplayStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHeadStats returns a new HeadStats object.
|
|
|
|
|
func NewHeadStats() *HeadStats {
|
|
|
|
|
return &HeadStats{
|
|
|
|
|
WALReplayStatus: &WALReplayStatus{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WALReplayStatus contains status information about the WAL replay.
|
|
|
|
|
type WALReplayStatus struct {
|
|
|
|
|
sync.RWMutex
|
|
|
|
|
Min int
|
|
|
|
|
Max int
|
|
|
|
|
Current int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetWALReplayStatus returns the WAL replay status information.
|
|
|
|
|
func (s *WALReplayStatus) GetWALReplayStatus() WALReplayStatus {
|
|
|
|
|
s.RLock()
|
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
|
|
return WALReplayStatus{
|
|
|
|
|
Min: s.Min,
|
|
|
|
|
Max: s.Max,
|
|
|
|
|
Current: s.Current,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 18:06:13 -08:00
|
|
|
|
const cardinalityCacheExpirationTime = time.Duration(30) * time.Second
|
|
|
|
|
|
2018-05-17 06:04:32 -07:00
|
|
|
|
// Init loads data from the write ahead log and prepares the head for writes.
|
2020-02-12 11:22:27 -08:00
|
|
|
|
// It should be called before using an appender so that it
|
2018-12-04 02:30:49 -08:00
|
|
|
|
// limits the ingested samples to the head min valid time.
|
|
|
|
|
func (h *Head) Init(minValidTime int64) error {
|
2020-07-27 21:42:42 -07:00
|
|
|
|
h.minValidTime.Store(minValidTime)
|
2021-12-08 07:32:14 -08:00
|
|
|
|
defer func() {
|
2023-04-18 03:13:05 -07:00
|
|
|
|
h.postings.EnsureOrder(h.opts.WALReplayConcurrency)
|
2021-12-08 07:32:14 -08:00
|
|
|
|
}()
|
2018-12-04 02:30:49 -08:00
|
|
|
|
defer h.gc() // After loading the wal remove the obsolete data from the head.
|
2021-08-11 03:13:17 -07:00
|
|
|
|
defer func() {
|
|
|
|
|
// Loading of m-mapped chunks and snapshot can make the mint of the Head
|
|
|
|
|
// to go below minValidTime.
|
|
|
|
|
if h.MinTime() < h.minValidTime.Load() {
|
|
|
|
|
h.minTime.Store(h.minValidTime.Load())
|
|
|
|
|
}
|
|
|
|
|
}()
|
2019-11-04 18:06:13 -08:00
|
|
|
|
|
2020-07-16 06:04:08 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "Replaying on-disk memory mappable chunks if any")
|
2020-03-03 06:11:14 -08:00
|
|
|
|
start := time.Now()
|
2021-03-16 02:47:45 -07:00
|
|
|
|
|
2021-08-17 10:08:16 -07:00
|
|
|
|
snapIdx, snapOffset := -1, 0
|
2021-11-06 03:10:04 -07:00
|
|
|
|
refSeries := make(map[chunks.HeadSeriesRef]*memSeries)
|
2021-08-17 10:08:16 -07:00
|
|
|
|
|
2023-03-12 22:45:45 -07:00
|
|
|
|
snapshotLoaded := false
|
2021-08-17 10:08:16 -07:00
|
|
|
|
if h.opts.EnableMemorySnapshotOnShutdown {
|
|
|
|
|
level.Info(h.logger).Log("msg", "Chunk snapshot is enabled, replaying from the snapshot")
|
2023-03-01 04:21:02 -08:00
|
|
|
|
// If there are any WAL files, there should be at least one WAL file with an index that is current or newer
|
|
|
|
|
// than the snapshot index. If the WAL index is behind the snapshot index somehow, the snapshot is assumed
|
|
|
|
|
// to be outdated.
|
|
|
|
|
loadSnapshot := true
|
|
|
|
|
if h.wal != nil {
|
|
|
|
|
_, endAt, err := wlog.Segments(h.wal.Dir())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "finding WAL segments")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, idx, _, err := LastChunkSnapshot(h.opts.ChunkDirRoot)
|
|
|
|
|
if err != nil && err != record.ErrNotFound {
|
|
|
|
|
level.Error(h.logger).Log("msg", "Could not find last snapshot", "err", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err == nil && endAt < idx {
|
|
|
|
|
loadSnapshot = false
|
|
|
|
|
level.Warn(h.logger).Log("msg", "Last WAL file is behind snapshot, removing snapshots")
|
|
|
|
|
if err := DeleteChunkSnapshots(h.opts.ChunkDirRoot, math.MaxInt, math.MaxInt); err != nil {
|
|
|
|
|
level.Error(h.logger).Log("msg", "Error while deleting snapshot directories", "err", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if loadSnapshot {
|
|
|
|
|
var err error
|
|
|
|
|
snapIdx, snapOffset, refSeries, err = h.loadChunkSnapshot()
|
|
|
|
|
if err == nil {
|
2023-03-12 22:45:45 -07:00
|
|
|
|
snapshotLoaded = true
|
2023-03-01 04:21:02 -08:00
|
|
|
|
level.Info(h.logger).Log("msg", "Chunk snapshot loading time", "duration", time.Since(start).String())
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
snapIdx, snapOffset = -1, 0
|
|
|
|
|
refSeries = make(map[chunks.HeadSeriesRef]*memSeries)
|
|
|
|
|
|
|
|
|
|
h.metrics.snapshotReplayErrorTotal.Inc()
|
|
|
|
|
level.Error(h.logger).Log("msg", "Failed to load chunk snapshot", "err", err)
|
|
|
|
|
// We clear the partially loaded data to replay fresh from the WAL.
|
|
|
|
|
if err := h.resetInMemoryState(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-11 03:13:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mmapChunkReplayStart := time.Now()
|
2023-03-12 22:45:45 -07:00
|
|
|
|
var (
|
|
|
|
|
mmappedChunks map[chunks.HeadSeriesRef][]*mmappedChunk
|
|
|
|
|
oooMmappedChunks map[chunks.HeadSeriesRef][]*mmappedChunk
|
|
|
|
|
lastMmapRef chunks.ChunkDiskMapperRef
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
if snapshotLoaded || h.wal != nil {
|
|
|
|
|
// If snapshot was not loaded and if there is no WAL, then m-map chunks will be discarded
|
|
|
|
|
// anyway. So we only load m-map chunks when it won't be discarded.
|
|
|
|
|
mmappedChunks, oooMmappedChunks, lastMmapRef, err = h.loadMmappedChunks(refSeries)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// TODO(codesome): clear out all m-map chunks here for refSeries.
|
|
|
|
|
level.Error(h.logger).Log("msg", "Loading on-disk chunks failed", "err", err)
|
|
|
|
|
if _, ok := errors.Cause(err).(*chunks.CorruptionErr); ok {
|
|
|
|
|
h.metrics.mmapChunkCorruptionTotal.Inc()
|
|
|
|
|
}
|
2022-02-25 07:53:40 -08:00
|
|
|
|
|
2023-03-12 22:45:45 -07:00
|
|
|
|
// Discard snapshot data since we need to replay the WAL for the missed m-map chunks data.
|
|
|
|
|
snapIdx, snapOffset = -1, 0
|
2022-02-25 07:53:40 -08:00
|
|
|
|
|
2023-03-12 22:45:45 -07:00
|
|
|
|
// If this fails, data will be recovered from WAL.
|
|
|
|
|
// Hence we wont lose any data (given WAL is not corrupt).
|
|
|
|
|
mmappedChunks, oooMmappedChunks, lastMmapRef, err = h.removeCorruptedMmappedChunks(err)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-02-25 07:53:40 -08:00
|
|
|
|
}
|
2023-03-12 22:45:45 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "On-disk memory mappable chunks replay completed", "duration", time.Since(mmapChunkReplayStart).String())
|
2021-03-16 02:47:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 06:04:08 -07:00
|
|
|
|
if h.wal == nil {
|
|
|
|
|
level.Info(h.logger).Log("msg", "WAL not found")
|
|
|
|
|
return nil
|
2021-06-05 07:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 06:04:08 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "Replaying WAL, this may take a while")
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2020-07-16 06:04:08 -07:00
|
|
|
|
checkpointReplayStart := time.Now()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
// Backfill the checkpoint first if it exists.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
dir, startFrom, err := wlog.LastCheckpoint(h.wal.Dir())
|
2019-09-19 02:15:41 -07:00
|
|
|
|
if err != nil && err != record.ErrNotFound {
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return errors.Wrap(err, "find last checkpoint")
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 03:08:07 -07:00
|
|
|
|
// Find the last segment.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
_, endAt, e := wlog.Segments(h.wal.Dir())
|
2021-07-13 03:08:07 -07:00
|
|
|
|
if e != nil {
|
|
|
|
|
return errors.Wrap(e, "finding WAL segments")
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 03:08:07 -07:00
|
|
|
|
h.startWALReplayStatus(startFrom, endAt)
|
2018-10-31 05:51:21 -07:00
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
multiRef := map[chunks.HeadSeriesRef]chunks.HeadSeriesRef{}
|
2021-08-25 09:18:55 -07:00
|
|
|
|
if err == nil && startFrom >= snapIdx {
|
2022-10-10 08:08:46 -07:00
|
|
|
|
sr, err := wlog.NewSegmentsReader(dir)
|
2018-05-17 06:04:32 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "open checkpoint")
|
|
|
|
|
}
|
2019-06-14 08:39:22 -07:00
|
|
|
|
defer func() {
|
|
|
|
|
if err := sr.Close(); err != nil {
|
2020-04-11 01:22:18 -07:00
|
|
|
|
level.Warn(h.logger).Log("msg", "Error while closing the wal segments reader", "err", err)
|
2018-05-25 14:19:32 -07:00
|
|
|
|
}
|
2019-06-14 08:39:22 -07:00
|
|
|
|
}()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
|
|
|
|
|
// A corrupted checkpoint is a hard error for now and requires user
|
|
|
|
|
// intervention. There's likely little data that can be recovered anyway.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
if err := h.loadWAL(wlog.NewReader(sr), multiRef, mmappedChunks, oooMmappedChunks); err != nil {
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return errors.Wrap(err, "backfill checkpoint")
|
2017-10-07 06:55:11 -07:00
|
|
|
|
}
|
2021-07-13 03:08:07 -07:00
|
|
|
|
h.updateWALReplayStatusRead(startFrom)
|
2018-10-11 08:23:52 -07:00
|
|
|
|
startFrom++
|
2019-07-13 10:10:44 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "WAL checkpoint loaded")
|
2017-10-07 06:55:11 -07:00
|
|
|
|
}
|
2020-07-16 06:04:08 -07:00
|
|
|
|
checkpointReplayDuration := time.Since(checkpointReplayStart)
|
2018-05-25 14:19:32 -07:00
|
|
|
|
|
2020-07-16 06:04:08 -07:00
|
|
|
|
walReplayStart := time.Now()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
if snapIdx > startFrom {
|
|
|
|
|
startFrom = snapIdx
|
|
|
|
|
}
|
2019-05-24 11:33:28 -07:00
|
|
|
|
// Backfill segments from the most recent checkpoint onwards.
|
2021-07-13 03:08:07 -07:00
|
|
|
|
for i := startFrom; i <= endAt; i++ {
|
2022-10-10 08:08:46 -07:00
|
|
|
|
s, err := wlog.OpenReadSegment(wlog.SegmentName(h.wal.Dir(), i))
|
2019-05-24 11:33:28 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, fmt.Sprintf("open WAL segment: %d", i))
|
2018-05-25 14:19:32 -07:00
|
|
|
|
}
|
2019-05-24 11:33:28 -07:00
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
offset := 0
|
|
|
|
|
if i == snapIdx {
|
|
|
|
|
offset = snapOffset
|
|
|
|
|
}
|
2022-10-10 08:08:46 -07:00
|
|
|
|
sr, err := wlog.NewSegmentBufReaderWithOffset(offset, s)
|
2022-12-29 07:23:07 -08:00
|
|
|
|
if errors.Is(err, io.EOF) {
|
2021-08-17 10:08:16 -07:00
|
|
|
|
// File does not exist.
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-08-11 03:13:17 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrapf(err, "segment reader (offset=%d)", offset)
|
|
|
|
|
}
|
2022-10-10 08:08:46 -07:00
|
|
|
|
err = h.loadWAL(wlog.NewReader(sr), multiRef, mmappedChunks, oooMmappedChunks)
|
2019-06-14 08:39:22 -07:00
|
|
|
|
if err := sr.Close(); err != nil {
|
2020-04-11 01:22:18 -07:00
|
|
|
|
level.Warn(h.logger).Log("msg", "Error while closing the wal segments reader", "err", err)
|
2018-05-17 06:04:32 -07:00
|
|
|
|
}
|
2019-06-14 08:39:22 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-05-17 06:04:32 -07:00
|
|
|
|
}
|
2021-07-13 03:08:07 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "WAL segment loaded", "segment", i, "maxSegment", endAt)
|
2021-06-05 07:29:32 -07:00
|
|
|
|
h.updateWALReplayStatusRead(i)
|
2018-05-17 06:04:32 -07:00
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
walReplayDuration := time.Since(walReplayStart)
|
2017-10-07 06:55:11 -07:00
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
wblReplayStart := time.Now()
|
|
|
|
|
if h.wbl != nil {
|
|
|
|
|
// Replay OOO WAL.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
startFrom, endAt, e = wlog.Segments(h.wbl.Dir())
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if e != nil {
|
|
|
|
|
return errors.Wrap(e, "finding OOO WAL segments")
|
|
|
|
|
}
|
|
|
|
|
h.startWALReplayStatus(startFrom, endAt)
|
|
|
|
|
|
|
|
|
|
for i := startFrom; i <= endAt; i++ {
|
2022-10-10 08:08:46 -07:00
|
|
|
|
s, err := wlog.OpenReadSegment(wlog.SegmentName(h.wbl.Dir(), i))
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, fmt.Sprintf("open WBL segment: %d", i))
|
|
|
|
|
}
|
2019-05-24 11:33:28 -07:00
|
|
|
|
|
2022-10-10 08:08:46 -07:00
|
|
|
|
sr := wlog.NewSegmentBufReader(s)
|
|
|
|
|
err = h.loadWBL(wlog.NewReader(sr), multiRef, lastMmapRef)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if err := sr.Close(); err != nil {
|
|
|
|
|
level.Warn(h.logger).Log("msg", "Error while closing the wbl segments reader", "err", err)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
level.Info(h.logger).Log("msg", "WBL segment loaded", "segment", i, "maxSegment", endAt)
|
|
|
|
|
h.updateWALReplayStatusRead(i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wblReplayDuration := time.Since(wblReplayStart)
|
|
|
|
|
|
|
|
|
|
totalReplayDuration := time.Since(start)
|
|
|
|
|
h.metrics.dataTotalReplayDuration.Set(totalReplayDuration.Seconds())
|
2020-07-16 06:04:08 -07:00
|
|
|
|
level.Info(h.logger).Log(
|
|
|
|
|
"msg", "WAL replay completed",
|
|
|
|
|
"checkpoint_replay_duration", checkpointReplayDuration.String(),
|
2022-09-20 10:05:50 -07:00
|
|
|
|
"wal_replay_duration", walReplayDuration.String(),
|
|
|
|
|
"wbl_replay_duration", wblReplayDuration.String(),
|
|
|
|
|
"total_replay_duration", totalReplayDuration.String(),
|
2017-10-07 06:55:11 -07:00
|
|
|
|
)
|
2017-10-11 01:12:29 -07:00
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return nil
|
2017-05-13 09:14:18 -07:00
|
|
|
|
}
|
2021-02-17 21:22:35 -08:00
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
func (h *Head) loadMmappedChunks(refSeries map[chunks.HeadSeriesRef]*memSeries) (map[chunks.HeadSeriesRef][]*mmappedChunk, map[chunks.HeadSeriesRef][]*mmappedChunk, chunks.ChunkDiskMapperRef, error) {
|
2021-11-06 03:10:04 -07:00
|
|
|
|
mmappedChunks := map[chunks.HeadSeriesRef][]*mmappedChunk{}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
oooMmappedChunks := map[chunks.HeadSeriesRef][]*mmappedChunk{}
|
|
|
|
|
var lastRef, secondLastRef chunks.ChunkDiskMapperRef
|
2023-03-16 03:23:47 -07:00
|
|
|
|
if err := h.chunkDiskMapper.IterateAllChunks(func(seriesRef chunks.HeadSeriesRef, chunkRef chunks.ChunkDiskMapperRef, mint, maxt int64, numSamples uint16, encoding chunkenc.Encoding, isOOO bool) error {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
secondLastRef = lastRef
|
|
|
|
|
lastRef = chunkRef
|
|
|
|
|
if !isOOO && maxt < h.minValidTime.Load() {
|
2020-05-06 08:30:00 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
|
|
|
|
|
// We ignore any chunk that doesn't have a valid encoding
|
|
|
|
|
if !chunkenc.IsValidEncoding(encoding) {
|
2020-05-06 08:30:00 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
ms, ok := refSeries[seriesRef]
|
2022-09-20 10:05:50 -07:00
|
|
|
|
|
|
|
|
|
if isOOO {
|
|
|
|
|
if !ok {
|
|
|
|
|
oooMmappedChunks[seriesRef] = append(oooMmappedChunks[seriesRef], &mmappedChunk{
|
|
|
|
|
ref: chunkRef,
|
|
|
|
|
minTime: mint,
|
|
|
|
|
maxTime: maxt,
|
|
|
|
|
numSamples: numSamples,
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.metrics.chunks.Inc()
|
|
|
|
|
h.metrics.chunksCreated.Inc()
|
|
|
|
|
|
2022-12-28 02:19:41 -08:00
|
|
|
|
if ms.ooo == nil {
|
|
|
|
|
ms.ooo = &memSeriesOOOFields{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms.ooo.oooMmappedChunks = append(ms.ooo.oooMmappedChunks, &mmappedChunk{
|
2022-09-20 10:05:50 -07:00
|
|
|
|
ref: chunkRef,
|
|
|
|
|
minTime: mint,
|
|
|
|
|
maxTime: maxt,
|
|
|
|
|
numSamples: numSamples,
|
|
|
|
|
})
|
|
|
|
|
|
2023-03-12 23:57:46 -07:00
|
|
|
|
h.updateMinOOOMaxOOOTime(mint, maxt)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
if !ok {
|
|
|
|
|
slice := mmappedChunks[seriesRef]
|
|
|
|
|
if len(slice) > 0 && slice[len(slice)-1].maxTime >= mint {
|
2022-06-22 01:33:12 -07:00
|
|
|
|
h.metrics.mmapChunkCorruptionTotal.Inc()
|
2022-02-22 07:05:15 -08:00
|
|
|
|
return errors.Errorf("out of sequence m-mapped chunk for series ref %d, last chunk: [%d, %d], new: [%d, %d]",
|
|
|
|
|
seriesRef, slice[len(slice)-1].minTime, slice[len(slice)-1].maxTime, mint, maxt)
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
2021-08-11 03:13:17 -07:00
|
|
|
|
slice = append(slice, &mmappedChunk{
|
|
|
|
|
ref: chunkRef,
|
|
|
|
|
minTime: mint,
|
|
|
|
|
maxTime: maxt,
|
|
|
|
|
numSamples: numSamples,
|
|
|
|
|
})
|
|
|
|
|
mmappedChunks[seriesRef] = slice
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(ms.mmappedChunks) > 0 && ms.mmappedChunks[len(ms.mmappedChunks)-1].maxTime >= mint {
|
2022-06-22 01:33:12 -07:00
|
|
|
|
h.metrics.mmapChunkCorruptionTotal.Inc()
|
2022-02-22 07:05:15 -08:00
|
|
|
|
return errors.Errorf("out of sequence m-mapped chunk for series ref %d, last chunk: [%d, %d], new: [%d, %d]",
|
|
|
|
|
seriesRef, ms.mmappedChunks[len(ms.mmappedChunks)-1].minTime, ms.mmappedChunks[len(ms.mmappedChunks)-1].maxTime,
|
|
|
|
|
mint, maxt)
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 03:13:17 -07:00
|
|
|
|
h.metrics.chunks.Inc()
|
|
|
|
|
h.metrics.chunksCreated.Inc()
|
|
|
|
|
ms.mmappedChunks = append(ms.mmappedChunks, &mmappedChunk{
|
2020-05-06 08:30:00 -07:00
|
|
|
|
ref: chunkRef,
|
|
|
|
|
minTime: mint,
|
|
|
|
|
maxTime: maxt,
|
|
|
|
|
numSamples: numSamples,
|
|
|
|
|
})
|
2021-08-11 03:13:17 -07:00
|
|
|
|
h.updateMinMaxTime(mint, maxt)
|
|
|
|
|
if ms.headChunk != nil && maxt >= ms.headChunk.minTime {
|
|
|
|
|
// The head chunk was completed and was m-mapped after taking the snapshot.
|
|
|
|
|
// Hence remove this chunk.
|
|
|
|
|
ms.nextAt = 0
|
|
|
|
|
ms.headChunk = nil
|
|
|
|
|
ms.app = nil
|
|
|
|
|
}
|
2020-05-06 08:30:00 -07:00
|
|
|
|
return nil
|
|
|
|
|
}); err != nil {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// secondLastRef because the lastRef caused an error.
|
|
|
|
|
return nil, nil, secondLastRef, errors.Wrap(err, "iterate on on-disk chunks")
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return mmappedChunks, oooMmappedChunks, lastRef, nil
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// removeCorruptedMmappedChunks attempts to delete the corrupted mmapped chunks and if it fails, it clears all the previously
|
|
|
|
|
// loaded mmapped chunks.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
func (h *Head) removeCorruptedMmappedChunks(err error) (map[chunks.HeadSeriesRef][]*mmappedChunk, map[chunks.HeadSeriesRef][]*mmappedChunk, chunks.ChunkDiskMapperRef, error) {
|
|
|
|
|
level.Info(h.logger).Log("msg", "Deleting mmapped chunk files")
|
2022-02-25 07:53:40 -08:00
|
|
|
|
// We never want to preserve the in-memory series from snapshots if we are repairing m-map chunks.
|
|
|
|
|
if err := h.resetInMemoryState(); err != nil {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return map[chunks.HeadSeriesRef][]*mmappedChunk{}, map[chunks.HeadSeriesRef][]*mmappedChunk{}, 0, err
|
2022-02-25 07:53:40 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 08:30:00 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "Deleting mmapped chunk files")
|
|
|
|
|
|
|
|
|
|
if err := h.chunkDiskMapper.DeleteCorrupted(err); err != nil {
|
2022-02-22 07:05:15 -08:00
|
|
|
|
level.Info(h.logger).Log("msg", "Deletion of corrupted mmap chunk files failed, discarding chunk files completely", "err", err)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if err := h.chunkDiskMapper.Truncate(math.MaxUint32); err != nil {
|
2022-02-22 07:05:15 -08:00
|
|
|
|
level.Error(h.logger).Log("msg", "Deletion of all mmap chunk files failed", "err", err)
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return map[chunks.HeadSeriesRef][]*mmappedChunk{}, map[chunks.HeadSeriesRef][]*mmappedChunk{}, 0, nil
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
level.Info(h.logger).Log("msg", "Deletion of mmap chunk files successful, reattempting m-mapping the on-disk chunks")
|
2022-09-20 10:05:50 -07:00
|
|
|
|
mmappedChunks, oooMmappedChunks, lastRef, err := h.loadMmappedChunks(make(map[chunks.HeadSeriesRef]*memSeries))
|
2020-05-06 08:30:00 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
level.Error(h.logger).Log("msg", "Loading on-disk chunks failed, discarding chunk files completely", "err", err)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if err := h.chunkDiskMapper.Truncate(math.MaxUint32); err != nil {
|
2022-02-22 07:05:15 -08:00
|
|
|
|
level.Error(h.logger).Log("msg", "Deletion of all mmap chunk files failed after failed loading", "err", err)
|
|
|
|
|
}
|
2021-11-06 03:10:04 -07:00
|
|
|
|
mmappedChunks = map[chunks.HeadSeriesRef][]*mmappedChunk{}
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return mmappedChunks, oooMmappedChunks, lastRef, nil
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 08:08:46 -07:00
|
|
|
|
func (h *Head) ApplyConfig(cfg *config.Config, wbl *wlog.WL) {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
oooTimeWindow := int64(0)
|
|
|
|
|
if cfg.StorageConfig.TSDBConfig != nil {
|
|
|
|
|
oooTimeWindow = cfg.StorageConfig.TSDBConfig.OutOfOrderTimeWindow
|
|
|
|
|
}
|
|
|
|
|
if oooTimeWindow < 0 {
|
|
|
|
|
oooTimeWindow = 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.SetOutOfOrderTimeWindow(oooTimeWindow, wbl)
|
|
|
|
|
|
2021-08-03 05:14:26 -07:00
|
|
|
|
if !h.opts.EnableExemplarStorage {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return
|
2021-08-03 05:14:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Head uses opts.MaxExemplars in combination with opts.EnableExemplarStorage
|
2021-12-06 13:25:22 -08:00
|
|
|
|
// to decide if it should pass exemplars along to its exemplar storage, so we
|
2021-08-03 05:14:26 -07:00
|
|
|
|
// need to update opts.MaxExemplars here.
|
|
|
|
|
prevSize := h.opts.MaxExemplars.Load()
|
|
|
|
|
h.opts.MaxExemplars.Store(cfg.StorageConfig.ExemplarsConfig.MaxExemplars)
|
2021-12-06 13:25:22 -08:00
|
|
|
|
newSize := h.opts.MaxExemplars.Load()
|
2021-08-03 05:14:26 -07:00
|
|
|
|
|
2021-12-06 13:25:22 -08:00
|
|
|
|
if prevSize == newSize {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return
|
2021-08-03 05:14:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 13:25:22 -08:00
|
|
|
|
migrated := h.exemplars.(*CircularExemplarStorage).Resize(newSize)
|
|
|
|
|
level.Info(h.logger).Log("msg", "Exemplar storage resized", "from", prevSize, "to", newSize, "migrated", migrated)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetOutOfOrderTimeWindow updates the out of order related parameters.
|
|
|
|
|
// If the Head already has a WBL set, then the wbl will be ignored.
|
2022-10-10 08:08:46 -07:00
|
|
|
|
func (h *Head) SetOutOfOrderTimeWindow(oooTimeWindow int64, wbl *wlog.WL) {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if oooTimeWindow > 0 && h.wbl == nil {
|
|
|
|
|
h.wbl = wbl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.opts.OutOfOrderTimeWindow.Store(oooTimeWindow)
|
2021-08-03 05:14:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-14 05:08:34 -07:00
|
|
|
|
// EnableNativeHistograms enables the native histogram feature.
|
|
|
|
|
func (h *Head) EnableNativeHistograms() {
|
|
|
|
|
h.opts.EnableNativeHistograms.Store(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DisableNativeHistograms disables the native histogram feature.
|
|
|
|
|
func (h *Head) DisableNativeHistograms() {
|
|
|
|
|
h.opts.EnableNativeHistograms.Store(false)
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 15:50:16 -07:00
|
|
|
|
// PostingsCardinalityStats returns highest cardinality stats by label and value names.
|
2023-05-22 05:37:07 -07:00
|
|
|
|
func (h *Head) PostingsCardinalityStats(statsByLabelName string, limit int) *index.PostingsStats {
|
2021-08-03 05:14:26 -07:00
|
|
|
|
h.cardinalityMutex.Lock()
|
|
|
|
|
defer h.cardinalityMutex.Unlock()
|
|
|
|
|
currentTime := time.Duration(time.Now().Unix()) * time.Second
|
|
|
|
|
seconds := currentTime - h.lastPostingsStatsCall
|
|
|
|
|
if seconds > cardinalityCacheExpirationTime {
|
|
|
|
|
h.cardinalityCache = nil
|
|
|
|
|
}
|
|
|
|
|
if h.cardinalityCache != nil {
|
|
|
|
|
return h.cardinalityCache
|
|
|
|
|
}
|
2023-05-22 05:37:07 -07:00
|
|
|
|
h.cardinalityCache = h.postings.Stats(statsByLabelName, limit)
|
2021-08-03 05:14:26 -07:00
|
|
|
|
h.lastPostingsStatsCall = time.Duration(time.Now().Unix()) * time.Second
|
|
|
|
|
|
|
|
|
|
return h.cardinalityCache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) updateMinMaxTime(mint, maxt int64) {
|
|
|
|
|
for {
|
|
|
|
|
lt := h.MinTime()
|
|
|
|
|
if mint >= lt {
|
|
|
|
|
break
|
|
|
|
|
}
|
2022-09-09 04:28:55 -07:00
|
|
|
|
if h.minTime.CompareAndSwap(lt, mint) {
|
2021-08-03 05:14:26 -07:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
ht := h.MaxTime()
|
|
|
|
|
if maxt <= ht {
|
|
|
|
|
break
|
|
|
|
|
}
|
2022-09-09 04:28:55 -07:00
|
|
|
|
if h.maxTime.CompareAndSwap(ht, maxt) {
|
2021-08-03 05:14:26 -07:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
func (h *Head) updateMinOOOMaxOOOTime(mint, maxt int64) {
|
|
|
|
|
for {
|
|
|
|
|
lt := h.MinOOOTime()
|
|
|
|
|
if mint >= lt {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if h.minOOOTime.CompareAndSwap(lt, mint) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
ht := h.MaxOOOTime()
|
|
|
|
|
if maxt <= ht {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if h.maxOOOTime.CompareAndSwap(ht, maxt) {
|
2021-08-03 05:14:26 -07:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetMinValidTime sets the minimum timestamp the head can ingest.
|
|
|
|
|
func (h *Head) SetMinValidTime(minValidTime int64) {
|
|
|
|
|
h.minValidTime.Store(minValidTime)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
// Truncate removes old data before mint from the head and WAL.
|
2018-09-25 06:48:33 -07:00
|
|
|
|
func (h *Head) Truncate(mint int64) (err error) {
|
2020-10-19 08:27:08 -07:00
|
|
|
|
initialize := h.MinTime() == math.MaxInt64
|
|
|
|
|
if err := h.truncateMemory(mint); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if initialize {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return h.truncateWAL(mint)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 23:50:03 -07:00
|
|
|
|
// OverlapsClosedInterval returns true if the head overlaps [mint, maxt].
|
|
|
|
|
func (h *Head) OverlapsClosedInterval(mint, maxt int64) bool {
|
|
|
|
|
return h.MinTime() <= maxt && mint <= h.MaxTime()
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
// truncateMemory removes old data before mint from the head.
|
|
|
|
|
func (h *Head) truncateMemory(mint int64) (err error) {
|
2021-08-11 03:13:17 -07:00
|
|
|
|
h.chunkSnapshotMtx.Lock()
|
|
|
|
|
defer h.chunkSnapshotMtx.Unlock()
|
|
|
|
|
|
2018-09-25 06:48:33 -07:00
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.metrics.headTruncateFail.Inc()
|
|
|
|
|
}
|
|
|
|
|
}()
|
2021-07-20 01:47:20 -07:00
|
|
|
|
|
2018-05-25 14:19:32 -07:00
|
|
|
|
initialize := h.MinTime() == math.MaxInt64
|
2017-09-06 07:20:37 -07:00
|
|
|
|
|
2018-05-25 14:19:32 -07:00
|
|
|
|
if h.MinTime() >= mint && !initialize {
|
2017-09-01 05:38:49 -07:00
|
|
|
|
return nil
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
2021-07-20 01:47:20 -07:00
|
|
|
|
|
|
|
|
|
// The order of these two Store() should not be changed,
|
|
|
|
|
// i.e. truncation time is set before in-process boolean.
|
|
|
|
|
h.lastMemoryTruncationTime.Store(mint)
|
|
|
|
|
h.memTruncationInProcess.Store(true)
|
|
|
|
|
defer h.memTruncationInProcess.Store(false)
|
|
|
|
|
|
|
|
|
|
// We wait for pending queries to end that overlap with this truncation.
|
|
|
|
|
if !initialize {
|
|
|
|
|
h.WaitForPendingReadersInTimeRange(h.MinTime(), mint)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 21:42:42 -07:00
|
|
|
|
h.minTime.Store(mint)
|
|
|
|
|
h.minValidTime.Store(mint)
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2017-09-07 04:04:02 -07:00
|
|
|
|
// Ensure that max time is at least as high as min time.
|
|
|
|
|
for h.MaxTime() < mint {
|
2022-09-09 04:28:55 -07:00
|
|
|
|
h.maxTime.CompareAndSwap(h.MaxTime(), mint)
|
2017-09-07 04:04:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 06:48:33 -07:00
|
|
|
|
h.metrics.headTruncateTotal.Inc()
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return h.truncateSeriesAndChunkDiskMapper("truncateMemory")
|
2020-10-19 08:27:08 -07:00
|
|
|
|
}
|
2020-05-06 08:30:00 -07:00
|
|
|
|
|
2021-07-20 01:47:20 -07:00
|
|
|
|
// WaitForPendingReadersInTimeRange waits for queries overlapping with given range to finish querying.
|
|
|
|
|
// The query timeout limits the max wait time of this function implicitly.
|
|
|
|
|
// The mint is inclusive and maxt is the truncation time hence exclusive.
|
|
|
|
|
func (h *Head) WaitForPendingReadersInTimeRange(mint, maxt int64) {
|
|
|
|
|
maxt-- // Making it inclusive before checking overlaps.
|
|
|
|
|
overlaps := func() bool {
|
|
|
|
|
o := false
|
|
|
|
|
h.iso.TraverseOpenReads(func(s *isolationState) bool {
|
|
|
|
|
if s.mint <= maxt && mint <= s.maxt {
|
|
|
|
|
// Overlaps with the truncation range.
|
|
|
|
|
o = true
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return o
|
|
|
|
|
}
|
|
|
|
|
for overlaps() {
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 07:01:23 -07:00
|
|
|
|
// WaitForAppendersOverlapping waits for appends overlapping maxt to finish.
|
|
|
|
|
func (h *Head) WaitForAppendersOverlapping(maxt int64) {
|
|
|
|
|
for maxt >= h.iso.lowestAppendTime() {
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 01:47:20 -07:00
|
|
|
|
// IsQuerierCollidingWithTruncation returns if the current querier needs to be closed and if a new querier
|
|
|
|
|
// has to be created. In the latter case, the method also returns the new mint to be used for creating the
|
|
|
|
|
// new range head and the new querier. This methods helps preventing races with the truncation of in-memory data.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: The querier should already be taken before calling this.
|
2021-10-22 01:06:44 -07:00
|
|
|
|
func (h *Head) IsQuerierCollidingWithTruncation(querierMint, querierMaxt int64) (shouldClose, getNew bool, newMint int64) {
|
2021-07-20 01:47:20 -07:00
|
|
|
|
if !h.memTruncationInProcess.Load() {
|
|
|
|
|
return false, false, 0
|
|
|
|
|
}
|
|
|
|
|
// Head truncation is in process. It also means that the block that was
|
|
|
|
|
// created for this truncation range is also available.
|
|
|
|
|
// Check if we took a querier that overlaps with this truncation.
|
|
|
|
|
memTruncTime := h.lastMemoryTruncationTime.Load()
|
|
|
|
|
if querierMaxt < memTruncTime {
|
|
|
|
|
// Head compaction has happened and this time range is being truncated.
|
|
|
|
|
// This query doesn't overlap with the Head any longer.
|
|
|
|
|
// We should close this querier to avoid races and the data would be
|
|
|
|
|
// available with the blocks below.
|
|
|
|
|
// Cases:
|
|
|
|
|
// 1. |------truncation------|
|
|
|
|
|
// |---query---|
|
|
|
|
|
// 2. |------truncation------|
|
|
|
|
|
// |---query---|
|
|
|
|
|
return true, false, 0
|
|
|
|
|
}
|
|
|
|
|
if querierMint < memTruncTime {
|
|
|
|
|
// The truncation time is not same as head mint that we saw above but the
|
|
|
|
|
// query still overlaps with the Head.
|
|
|
|
|
// The truncation started after we got the querier. So it is not safe
|
|
|
|
|
// to use this querier and/or might block truncation. We should get
|
|
|
|
|
// a new querier for the new Head range while remaining will be available
|
|
|
|
|
// in the blocks below.
|
|
|
|
|
// Case:
|
|
|
|
|
// |------truncation------|
|
|
|
|
|
// |----query----|
|
|
|
|
|
// Turns into
|
|
|
|
|
// |------truncation------|
|
|
|
|
|
// |---qu---|
|
|
|
|
|
return true, true, memTruncTime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Other case is this, which is a no-op
|
|
|
|
|
// |------truncation------|
|
|
|
|
|
// |---query---|
|
|
|
|
|
return false, false, 0
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
// truncateWAL removes old data before mint from the WAL.
|
|
|
|
|
func (h *Head) truncateWAL(mint int64) error {
|
2021-08-11 03:13:17 -07:00
|
|
|
|
h.chunkSnapshotMtx.Lock()
|
|
|
|
|
defer h.chunkSnapshotMtx.Unlock()
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
if h.wal == nil || mint <= h.lastWALTruncationTime.Load() {
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-10-19 08:27:08 -07:00
|
|
|
|
start := time.Now()
|
|
|
|
|
h.lastWALTruncationTime.Store(mint)
|
2017-08-30 09:34:54 -07:00
|
|
|
|
|
2022-10-10 08:08:46 -07:00
|
|
|
|
first, last, err := wlog.Segments(h.wal.Dir())
|
2018-05-17 06:04:32 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "get segment range")
|
|
|
|
|
}
|
2019-06-07 03:35:02 -07:00
|
|
|
|
// Start a new segment, so low ingestion volume TSDB don't have more WAL than
|
|
|
|
|
// needed.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if _, err := h.wal.NextSegment(); err != nil {
|
2019-06-07 03:35:02 -07:00
|
|
|
|
return errors.Wrap(err, "next segment")
|
|
|
|
|
}
|
2018-10-11 08:23:52 -07:00
|
|
|
|
last-- // Never consider last segment for checkpoint.
|
|
|
|
|
if last < 0 {
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return nil // no segments yet.
|
|
|
|
|
}
|
2020-04-07 03:25:57 -07:00
|
|
|
|
// The lower two thirds of segments should contain mostly obsolete samples.
|
|
|
|
|
// If we have less than two segments, it's not worth checkpointing yet.
|
|
|
|
|
// With the default 2h blocks, this will keeping up to around 3h worth
|
|
|
|
|
// of WAL segments.
|
|
|
|
|
last = first + (last-first)*2/3
|
2018-10-11 08:23:52 -07:00
|
|
|
|
if last <= first {
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
keep := func(id chunks.HeadSeriesRef) bool {
|
2019-04-09 06:16:24 -07:00
|
|
|
|
if h.series.getByID(id) != nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
h.deletedMtx.Lock()
|
2023-05-01 08:43:15 -07:00
|
|
|
|
keepUntil, ok := h.deleted[id]
|
2019-04-09 06:16:24 -07:00
|
|
|
|
h.deletedMtx.Unlock()
|
2023-05-01 08:43:15 -07:00
|
|
|
|
return ok && keepUntil > last
|
2017-08-31 02:39:22 -07:00
|
|
|
|
}
|
2018-09-25 06:48:33 -07:00
|
|
|
|
h.metrics.checkpointCreationTotal.Inc()
|
2022-10-10 08:08:46 -07:00
|
|
|
|
if _, err = wlog.Checkpoint(h.logger, h.wal, first, last, keep, mint); err != nil {
|
2018-09-25 06:48:33 -07:00
|
|
|
|
h.metrics.checkpointCreationFail.Inc()
|
2022-10-10 08:08:46 -07:00
|
|
|
|
if _, ok := errors.Cause(err).(*wlog.CorruptionErr); ok {
|
2020-07-04 22:55:42 -07:00
|
|
|
|
h.metrics.walCorruptionsTotal.Inc()
|
|
|
|
|
}
|
2018-05-17 06:04:32 -07:00
|
|
|
|
return errors.Wrap(err, "create checkpoint")
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
2018-10-11 08:23:52 -07:00
|
|
|
|
if err := h.wal.Truncate(last + 1); err != nil {
|
2018-09-25 06:48:33 -07:00
|
|
|
|
// If truncating fails, we'll just try again at the next checkpoint.
|
|
|
|
|
// Leftover segments will just be ignored in the future if there's a checkpoint
|
|
|
|
|
// that supersedes them.
|
|
|
|
|
level.Error(h.logger).Log("msg", "truncating segments failed", "err", err)
|
|
|
|
|
}
|
2019-04-09 06:16:24 -07:00
|
|
|
|
|
|
|
|
|
// The checkpoint is written and segments before it is truncated, so we no
|
|
|
|
|
// longer need to track deleted series that are before it.
|
|
|
|
|
h.deletedMtx.Lock()
|
|
|
|
|
for ref, segment := range h.deleted {
|
2023-05-01 08:43:15 -07:00
|
|
|
|
if segment <= last {
|
2019-04-09 06:16:24 -07:00
|
|
|
|
delete(h.deleted, ref)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
h.deletedMtx.Unlock()
|
|
|
|
|
|
2018-09-25 06:48:33 -07:00
|
|
|
|
h.metrics.checkpointDeleteTotal.Inc()
|
2022-10-10 08:08:46 -07:00
|
|
|
|
if err := wlog.DeleteCheckpoints(h.wal.Dir(), last); err != nil {
|
2018-09-25 06:48:33 -07:00
|
|
|
|
// Leftover old checkpoints do not cause problems down the line beyond
|
|
|
|
|
// occupying disk space.
|
|
|
|
|
// They will just be ignored since a higher checkpoint exists.
|
|
|
|
|
level.Error(h.logger).Log("msg", "delete old checkpoints", "err", err)
|
|
|
|
|
h.metrics.checkpointDeleteFail.Inc()
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
h.metrics.walTruncateDuration.Observe(time.Since(start).Seconds())
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
2018-05-17 06:04:32 -07:00
|
|
|
|
level.Info(h.logger).Log("msg", "WAL checkpoint complete",
|
2018-10-11 08:23:52 -07:00
|
|
|
|
"first", first, "last", last, "duration", time.Since(start))
|
2018-05-17 06:04:32 -07:00
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
|
return nil
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// truncateOOO
|
|
|
|
|
// - truncates the OOO WBL files whose index is strictly less than lastWBLFile.
|
|
|
|
|
// - garbage collects all the m-map chunks from the memory that are less than or equal to minOOOMmapRef
|
|
|
|
|
// and then deletes the series that do not have any data anymore.
|
|
|
|
|
func (h *Head) truncateOOO(lastWBLFile int, minOOOMmapRef chunks.ChunkDiskMapperRef) error {
|
|
|
|
|
curMinOOOMmapRef := chunks.ChunkDiskMapperRef(h.minOOOMmapRef.Load())
|
|
|
|
|
if minOOOMmapRef.GreaterThan(curMinOOOMmapRef) {
|
|
|
|
|
h.minOOOMmapRef.Store(uint64(minOOOMmapRef))
|
|
|
|
|
if err := h.truncateSeriesAndChunkDiskMapper("truncateOOO"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 06:18:15 -08:00
|
|
|
|
if h.wbl == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return h.wbl.Truncate(lastWBLFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// truncateSeriesAndChunkDiskMapper is a helper function for truncateMemory and truncateOOO.
|
|
|
|
|
// It runs GC on the Head and truncates the ChunkDiskMapper accordingly.
|
|
|
|
|
func (h *Head) truncateSeriesAndChunkDiskMapper(caller string) error {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
headMaxt := h.MaxTime()
|
|
|
|
|
actualMint, minOOOTime, minMmapFile := h.gc()
|
|
|
|
|
level.Info(h.logger).Log("msg", "Head GC completed", "caller", caller, "duration", time.Since(start))
|
|
|
|
|
h.metrics.gcDuration.Observe(time.Since(start).Seconds())
|
|
|
|
|
|
|
|
|
|
if actualMint > h.minTime.Load() {
|
|
|
|
|
// The actual mint of the head is higher than the one asked to truncate.
|
|
|
|
|
appendableMinValidTime := h.appendableMinValidTime()
|
|
|
|
|
if actualMint < appendableMinValidTime {
|
|
|
|
|
h.minTime.Store(actualMint)
|
|
|
|
|
h.minValidTime.Store(actualMint)
|
|
|
|
|
} else {
|
|
|
|
|
// The actual min time is in the appendable window.
|
|
|
|
|
// So we set the mint to the appendableMinValidTime.
|
|
|
|
|
h.minTime.Store(appendableMinValidTime)
|
|
|
|
|
h.minValidTime.Store(appendableMinValidTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if headMaxt-h.opts.OutOfOrderTimeWindow.Load() < minOOOTime {
|
|
|
|
|
// The allowed OOO window is lower than the min OOO time seen during GC.
|
|
|
|
|
// So it is possible that some OOO sample was inserted that was less that minOOOTime.
|
|
|
|
|
// So we play safe and set it to the min that was possible.
|
|
|
|
|
minOOOTime = headMaxt - h.opts.OutOfOrderTimeWindow.Load()
|
|
|
|
|
}
|
|
|
|
|
h.minOOOTime.Store(minOOOTime)
|
|
|
|
|
|
|
|
|
|
// Truncate the chunk m-mapper.
|
|
|
|
|
if err := h.chunkDiskMapper.Truncate(uint32(minMmapFile)); err != nil {
|
|
|
|
|
return errors.Wrap(err, "truncate chunks.HeadReadWriter by file number")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 09:16:14 -07:00
|
|
|
|
type Stats struct {
|
|
|
|
|
NumSeries uint64
|
|
|
|
|
MinTime, MaxTime int64
|
|
|
|
|
IndexPostingStats *index.PostingsStats
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stats returns important current HEAD statistics. Note that it is expensive to
|
|
|
|
|
// calculate these.
|
2023-05-22 05:37:07 -07:00
|
|
|
|
func (h *Head) Stats(statsByLabelName string, limit int) *Stats {
|
2020-04-29 09:16:14 -07:00
|
|
|
|
return &Stats{
|
|
|
|
|
NumSeries: h.NumSeries(),
|
|
|
|
|
MaxTime: h.MaxTime(),
|
|
|
|
|
MinTime: h.MinTime(),
|
2023-05-22 05:37:07 -07:00
|
|
|
|
IndexPostingStats: h.PostingsCardinalityStats(statsByLabelName, limit),
|
2020-04-29 09:16:14 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 02:21:27 -08:00
|
|
|
|
// RangeHead allows querying Head via an IndexReader, ChunkReader and tombstones.Reader
|
|
|
|
|
// but only within a restricted range. Used for queries and compactions.
|
2020-02-14 01:50:24 -08:00
|
|
|
|
type RangeHead struct {
|
2017-10-09 06:21:46 -07:00
|
|
|
|
head *Head
|
|
|
|
|
mint, maxt int64
|
2022-09-27 07:01:23 -07:00
|
|
|
|
|
|
|
|
|
isolationOff bool
|
2017-10-09 06:21:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 16:17:56 -07:00
|
|
|
|
// NewRangeHead returns a *RangeHead.
|
2021-11-17 02:21:27 -08:00
|
|
|
|
// There are no restrictions on mint/maxt.
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func NewRangeHead(head *Head, mint, maxt int64) *RangeHead {
|
|
|
|
|
return &RangeHead{
|
|
|
|
|
head: head,
|
|
|
|
|
mint: mint,
|
|
|
|
|
maxt: maxt,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-27 07:01:23 -07:00
|
|
|
|
// NewRangeHeadWithIsolationDisabled returns a *RangeHead that does not create an isolationState.
|
|
|
|
|
func NewRangeHeadWithIsolationDisabled(head *Head, mint, maxt int64) *RangeHead {
|
|
|
|
|
rh := NewRangeHead(head, mint, maxt)
|
|
|
|
|
rh.isolationOff = true
|
|
|
|
|
return rh
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 12:13:47 -07:00
|
|
|
|
func (h *RangeHead) Index() (IndexReader, error) {
|
|
|
|
|
return h.head.indexRange(h.mint, h.maxt), nil
|
2017-10-09 06:21:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) Chunks() (ChunkReader, error) {
|
2022-09-27 07:01:23 -07:00
|
|
|
|
var isoState *isolationState
|
|
|
|
|
if !h.isolationOff {
|
|
|
|
|
isoState = h.head.iso.State(h.mint, h.maxt)
|
|
|
|
|
}
|
|
|
|
|
return h.head.chunksRange(h.mint, h.maxt, isoState)
|
2017-10-09 06:21:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) Tombstones() (tombstones.Reader, error) {
|
2020-01-20 07:38:00 -08:00
|
|
|
|
return h.head.tombstones, nil
|
2017-10-09 06:21:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) MinTime() int64 {
|
2019-02-14 05:29:41 -08:00
|
|
|
|
return h.mint
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
// MaxTime returns the max time of actual data fetch-able from the head.
|
|
|
|
|
// This controls the chunks time range which is closed [b.MinTime, b.MaxTime].
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) MaxTime() int64 {
|
2019-02-14 05:29:41 -08:00
|
|
|
|
return h.maxt
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 08:27:08 -07:00
|
|
|
|
// BlockMaxTime returns the max time of the potential block created from this head.
|
|
|
|
|
// It's different to MaxTime as we need to add +1 millisecond to block maxt because block
|
|
|
|
|
// intervals are half-open: [b.MinTime, b.MaxTime). Block intervals are always +1 than the total samples it includes.
|
|
|
|
|
func (h *RangeHead) BlockMaxTime() int64 {
|
|
|
|
|
return h.MaxTime() + 1
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) NumSeries() uint64 {
|
2019-07-23 01:04:48 -07:00
|
|
|
|
return h.head.NumSeries()
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-14 01:50:24 -08:00
|
|
|
|
func (h *RangeHead) Meta() BlockMeta {
|
2019-07-23 01:04:48 -07:00
|
|
|
|
return BlockMeta{
|
|
|
|
|
MinTime: h.MinTime(),
|
|
|
|
|
MaxTime: h.MaxTime(),
|
|
|
|
|
ULID: h.head.Meta().ULID,
|
|
|
|
|
Stats: BlockStats{
|
|
|
|
|
NumSeries: h.NumSeries(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 02:37:29 -08:00
|
|
|
|
// String returns an human readable representation of the range head. It's important to
|
|
|
|
|
// keep this function in order to avoid the struct dump when the head is stringified in
|
|
|
|
|
// errors or logs.
|
|
|
|
|
func (h *RangeHead) String() string {
|
|
|
|
|
return fmt.Sprintf("range head (mint: %d, maxt: %d)", h.MinTime(), h.MaxTime())
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
// Delete all samples in the range of [mint, maxt] for series that satisfy the given
|
|
|
|
|
// label matchers.
|
2019-11-18 11:53:33 -08:00
|
|
|
|
func (h *Head) Delete(mint, maxt int64, ms ...*labels.Matcher) error {
|
2017-08-30 09:34:54 -07:00
|
|
|
|
// Do not delete anything beyond the currently valid range.
|
|
|
|
|
mint, maxt = clampInterval(mint, maxt, h.MinTime(), h.MaxTime())
|
|
|
|
|
|
|
|
|
|
ir := h.indexRange(mint, maxt)
|
2021-03-16 02:47:45 -07:00
|
|
|
|
|
2017-12-17 10:08:21 -08:00
|
|
|
|
p, err := PostingsForMatchers(ir, ms...)
|
2017-11-13 03:16:58 -08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "select series")
|
2021-03-16 02:47:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
|
var stones []tombstones.Stone
|
2017-08-30 09:34:54 -07:00
|
|
|
|
for p.Next() {
|
2021-11-06 03:10:04 -07:00
|
|
|
|
series := h.series.getByID(chunks.HeadSeriesRef(p.At()))
|
2022-06-23 02:56:31 -07:00
|
|
|
|
if series == nil {
|
|
|
|
|
level.Debug(h.logger).Log("msg", "Series not found in Head.Delete")
|
|
|
|
|
continue
|
|
|
|
|
}
|
2021-03-16 02:47:45 -07:00
|
|
|
|
|
2020-03-16 05:59:22 -07:00
|
|
|
|
series.RLock()
|
2018-02-07 05:43:21 -08:00
|
|
|
|
t0, t1 := series.minTime(), series.maxTime()
|
2020-03-16 05:59:22 -07:00
|
|
|
|
series.RUnlock()
|
2018-02-07 05:43:21 -08:00
|
|
|
|
if t0 == math.MinInt64 || t1 == math.MinInt64 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
// Delete only until the current values and not beyond.
|
2018-02-07 05:43:21 -08:00
|
|
|
|
t0, t1 = clampInterval(mint, maxt, t0, t1)
|
2020-01-20 07:38:00 -08:00
|
|
|
|
stones = append(stones, tombstones.Stone{Ref: p.At(), Intervals: tombstones.Intervals{{Mint: t0, Maxt: t1}}})
|
2021-06-28 08:00:55 -07:00
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
if p.Err() != nil {
|
|
|
|
|
return p.Err()
|
|
|
|
|
}
|
2018-05-17 06:04:32 -07:00
|
|
|
|
if h.wal != nil {
|
2020-01-20 07:38:00 -08:00
|
|
|
|
var enc record.Encoder
|
2018-05-17 06:04:32 -07:00
|
|
|
|
if err := h.wal.Log(enc.Tombstones(stones, nil)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
2020-01-20 07:38:00 -08:00
|
|
|
|
for _, s := range stones {
|
2023-04-09 00:08:40 -07:00
|
|
|
|
h.tombstones.AddInterval(s.Ref, s.Intervals[0])
|
2017-08-30 09:34:54 -07:00
|
|
|
|
}
|
2019-01-08 09:08:41 -08:00
|
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 09:51:50 -08:00
|
|
|
|
// gc removes data before the minimum timestamp from the head.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// It returns
|
|
|
|
|
// * The actual min times of the chunks present in the Head.
|
|
|
|
|
// * The min OOO time seen during the GC.
|
|
|
|
|
// * Min mmap file number seen in the series (in-order and out-of-order) after gc'ing the series.
|
|
|
|
|
func (h *Head) gc() (actualInOrderMint, minOOOTime int64, minMmapFile int) {
|
2017-08-28 15:39:17 -07:00
|
|
|
|
// Only data strictly lower than this timestamp must be deleted.
|
|
|
|
|
mint := h.MinTime()
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// Only ooo m-map chunks strictly lower than or equal to this ref
|
|
|
|
|
// must be deleted.
|
|
|
|
|
minOOOMmapRef := chunks.ChunkDiskMapperRef(h.minOOOMmapRef.Load())
|
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.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
deleted, chunksRemoved, actualInOrderMint, minOOOTime, minMmapFile := h.series.gc(mint, minOOOMmapRef)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
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.chunksRemoved.Add(float64(chunksRemoved))
|
|
|
|
|
h.metrics.chunks.Sub(float64(chunksRemoved))
|
2020-07-27 21:42:42 -07:00
|
|
|
|
h.numSeries.Sub(uint64(seriesRemoved))
|
2017-03-04 07:50:48 -08:00
|
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
|
// Remove deleted series IDs from the postings lists.
|
|
|
|
|
h.postings.Delete(deleted)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
2021-09-15 23:50:03 -07:00
|
|
|
|
// Remove tombstones referring to the deleted series.
|
|
|
|
|
h.tombstones.DeleteTombstones(deleted)
|
|
|
|
|
h.tombstones.TruncateBefore(mint)
|
|
|
|
|
|
2019-04-09 06:16:24 -07:00
|
|
|
|
if h.wal != nil {
|
2022-10-10 08:08:46 -07:00
|
|
|
|
_, last, _ := wlog.Segments(h.wal.Dir())
|
2019-04-09 06:16:24 -07:00
|
|
|
|
h.deletedMtx.Lock()
|
|
|
|
|
// Keep series records until we're past segment 'last'
|
|
|
|
|
// because the WAL will still have samples records with
|
|
|
|
|
// this ref ID. If we didn't keep these series records then
|
|
|
|
|
// on start up when we replay the WAL, or any other code
|
|
|
|
|
// that reads the WAL, wouldn't be able to use those
|
|
|
|
|
// samples since we would have no labels for that ref ID.
|
|
|
|
|
for ref := range deleted {
|
2021-11-06 03:10:04 -07:00
|
|
|
|
h.deleted[chunks.HeadSeriesRef(ref)] = last
|
2019-04-09 06:16:24 -07:00
|
|
|
|
}
|
|
|
|
|
h.deletedMtx.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return actualInOrderMint, minOOOTime, minMmapFile
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 06:21:46 -07:00
|
|
|
|
// Tombstones returns a new reader over the head's tombstones
|
2019-09-19 02:15:41 -07:00
|
|
|
|
func (h *Head) Tombstones() (tombstones.Reader, error) {
|
2020-01-20 07:38:00 -08:00
|
|
|
|
return h.tombstones, nil
|
2017-08-05 04:31:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 01:04:48 -07:00
|
|
|
|
// NumSeries returns the number of active series in the head.
|
|
|
|
|
func (h *Head) NumSeries() uint64 {
|
2020-07-27 21:42:42 -07:00
|
|
|
|
return h.numSeries.Load()
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-23 01:04:48 -07:00
|
|
|
|
// Meta returns meta information about the head.
|
|
|
|
|
// The head is dynamic so will return dynamic results.
|
|
|
|
|
func (h *Head) Meta() BlockMeta {
|
|
|
|
|
var id [16]byte
|
|
|
|
|
copy(id[:], "______head______")
|
|
|
|
|
return BlockMeta{
|
|
|
|
|
MinTime: h.MinTime(),
|
|
|
|
|
MaxTime: h.MaxTime(),
|
|
|
|
|
ULID: ulid.ULID(id),
|
|
|
|
|
Stats: BlockStats{
|
|
|
|
|
NumSeries: h.NumSeries(),
|
|
|
|
|
},
|
2020-06-25 06:10:29 -07: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 {
|
2020-07-27 21:42:42 -07:00
|
|
|
|
return h.minTime.Load()
|
2016-12-14 09:38:46 -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 {
|
2020-07-27 21:42:42 -07:00
|
|
|
|
return h.maxTime.Load()
|
2018-11-07 07:52:41 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// MinOOOTime returns the lowest time bound on visible data in the out of order
|
|
|
|
|
// head.
|
|
|
|
|
func (h *Head) MinOOOTime() int64 {
|
|
|
|
|
return h.minOOOTime.Load()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MaxOOOTime returns the highest timestamp on visible data in the out of order
|
|
|
|
|
// head.
|
|
|
|
|
func (h *Head) MaxOOOTime() int64 {
|
|
|
|
|
return h.maxOOOTime.Load()
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-01 01:19:06 -07:00
|
|
|
|
// compactable returns whether the head has a compactable range.
|
|
|
|
|
// The head has a compactable range when the head time range is 1.5 times the chunk range.
|
|
|
|
|
// The 0.5 acts as a buffer of the appendable window.
|
|
|
|
|
func (h *Head) compactable() bool {
|
2020-07-27 21:42:42 -07:00
|
|
|
|
return h.MaxTime()-h.MinTime() > h.chunkRange.Load()/2*3
|
2016-12-14 09:38:46 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 12:19:39 -08:00
|
|
|
|
// Close flushes the WAL and closes the head.
|
2021-08-11 03:13:17 -07:00
|
|
|
|
// It also takes a snapshot of in-memory chunks if enabled.
|
2017-11-10 12:19:39 -08:00
|
|
|
|
func (h *Head) Close() error {
|
2020-05-22 02:03:23 -07:00
|
|
|
|
h.closedMtx.Lock()
|
|
|
|
|
defer h.closedMtx.Unlock()
|
|
|
|
|
h.closed = true
|
2017-08-05 04:31:48 -07:00
|
|
|
|
|
2020-10-28 08:24:58 -07:00
|
|
|
|
errs := tsdb_errors.NewMulti(h.chunkDiskMapper.Close())
|
2021-08-11 03:13:17 -07:00
|
|
|
|
if errs.Err() == nil && h.opts.EnableMemorySnapshotOnShutdown {
|
|
|
|
|
errs.Add(h.performChunkSnapshot())
|
|
|
|
|
}
|
2020-05-06 08:30:00 -07:00
|
|
|
|
if h.wal != nil {
|
2020-10-28 08:24:58 -07:00
|
|
|
|
errs.Add(h.wal.Close())
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if h.wbl != nil {
|
|
|
|
|
errs.Add(h.wbl.Close())
|
|
|
|
|
}
|
2021-09-13 06:00:41 -07:00
|
|
|
|
if errs.Err() == nil && h.opts.EnableMemorySnapshotOnShutdown {
|
|
|
|
|
errs.Add(h.performChunkSnapshot())
|
|
|
|
|
}
|
2020-10-28 08:24:58 -07:00
|
|
|
|
return errs.Err()
|
2016-12-31 06:35:08 -08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 02:37:29 -08:00
|
|
|
|
// String returns an human readable representation of the TSDB head. It's important to
|
|
|
|
|
// keep this function in order to avoid the struct dump when the head is stringified in
|
|
|
|
|
// errors or logs.
|
|
|
|
|
func (h *Head) String() string {
|
|
|
|
|
return "head"
|
2021-02-09 09:38:35 -08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 06:22:08 -07:00
|
|
|
|
func (h *Head) getOrCreate(hash uint64, lset labels.Labels) (*memSeries, bool, error) {
|
2021-03-18 08:23:50 -07:00
|
|
|
|
// Just using `getOrCreateWithID` below would be semantically sufficient, but we'd create
|
2017-09-18 03:28:56 -07:00
|
|
|
|
// 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 {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
return s, false, nil
|
2017-09-18 03:28:56 -07:00
|
|
|
|
}
|
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.
|
2021-11-06 03:10:04 -07:00
|
|
|
|
id := chunks.HeadSeriesRef(h.lastSeriesID.Inc())
|
2017-09-19 01:20:19 -07:00
|
|
|
|
|
|
|
|
|
return h.getOrCreateWithID(id, hash, lset)
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
func (h *Head) getOrCreateWithID(id chunks.HeadSeriesRef, hash uint64, lset labels.Labels) (*memSeries, bool, error) {
|
2021-03-18 08:23:50 -07:00
|
|
|
|
s, created, err := h.series.getOrSet(hash, lset, func() *memSeries {
|
2023-05-25 02:18:41 -07:00
|
|
|
|
return newMemSeries(lset, id, h.opts.IsolationDisabled)
|
2021-03-18 08:23:50 -07:00
|
|
|
|
})
|
2020-05-20 06:22:08 -07:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, false, err
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
if !created {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
return s, false, nil
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
|
h.metrics.seriesCreated.Inc()
|
2020-07-27 21:42:42 -07:00
|
|
|
|
h.numSeries.Inc()
|
2017-09-18 03:28:56 -07:00
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
h.postings.Add(storage.SeriesRef(id), lset)
|
2020-05-20 06:22:08 -07:00
|
|
|
|
return s, true, nil
|
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] {
|
2019-11-18 11:53:33 -08:00
|
|
|
|
if labels.Equal(s.lset, lset) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m seriesHashmap) set(hash uint64, s *memSeries) {
|
|
|
|
|
l := m[hash]
|
|
|
|
|
for i, prev := range l {
|
2019-11-18 11:53:33 -08:00
|
|
|
|
if labels.Equal(prev.lset, s.lset) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
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] {
|
2019-11-18 11:53:33 -08:00
|
|
|
|
if !labels.Equal(s.lset, lset) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
rem = append(rem, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(rem) == 0 {
|
|
|
|
|
delete(m, hash)
|
|
|
|
|
} else {
|
|
|
|
|
m[hash] = rem
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 23:12:43 -08:00
|
|
|
|
const (
|
|
|
|
|
// DefaultStripeSize is the default number of entries to allocate in the stripeSeries hash map.
|
|
|
|
|
DefaultStripeSize = 1 << 14
|
|
|
|
|
)
|
|
|
|
|
|
2021-11-17 02:21:27 -08:00
|
|
|
|
// stripeSeries holds series by HeadSeriesRef ("ID") and also by hash of their labels.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// ID-based lookups via getByID() are preferred over getByHash() for performance reasons.
|
2021-11-17 02:21:27 -08:00
|
|
|
|
// It locks modulo ranges of IDs and hashes to reduce lock contention.
|
2018-01-13 09:51:50 -08:00
|
|
|
|
// The locks are padded to not be on the same cache line. Filling the padded space
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// with the maps was profiled to be slower – likely due to the additional pointer
|
|
|
|
|
// dereferences.
|
|
|
|
|
type stripeSeries struct {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
size int
|
2021-11-17 02:21:27 -08:00
|
|
|
|
series []map[chunks.HeadSeriesRef]*memSeries // Sharded by ref. A series ref is the value of `size` when the series was being newly added.
|
|
|
|
|
hashes []seriesHashmap // Sharded by label hash.
|
|
|
|
|
locks []stripeLock // Sharded by ref for series access, by label hash for hashes access.
|
2020-05-20 06:22:08 -07:00
|
|
|
|
seriesLifecycleCallback SeriesLifecycleCallback
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type stripeLock struct {
|
|
|
|
|
sync.RWMutex
|
|
|
|
|
// Padding to avoid multiple locks being on the same cache line.
|
|
|
|
|
_ [40]byte
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 06:22:08 -07:00
|
|
|
|
func newStripeSeries(stripeSize int, seriesCallback SeriesLifecycleCallback) *stripeSeries {
|
2020-01-29 23:12:43 -08:00
|
|
|
|
s := &stripeSeries{
|
2020-05-20 06:22:08 -07:00
|
|
|
|
size: stripeSize,
|
2021-11-06 03:10:04 -07:00
|
|
|
|
series: make([]map[chunks.HeadSeriesRef]*memSeries, stripeSize),
|
2020-05-20 06:22:08 -07:00
|
|
|
|
hashes: make([]seriesHashmap, stripeSize),
|
|
|
|
|
locks: make([]stripeLock, stripeSize),
|
|
|
|
|
seriesLifecycleCallback: seriesCallback,
|
2020-01-29 23:12:43 -08:00
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
for i := range s.series {
|
2021-11-06 03:10:04 -07:00
|
|
|
|
s.series[i] = map[chunks.HeadSeriesRef]*memSeries{}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
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.
|
2021-11-06 03:10:04 -07:00
|
|
|
|
// note: returning map[chunks.HeadSeriesRef]struct{} would be more accurate,
|
|
|
|
|
// but the returned map goes into postings.Delete() which expects a map[storage.SeriesRef]struct
|
|
|
|
|
// and there's no easy way to cast maps.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// minMmapFile is the min mmap file number seen in the series (in-order and out-of-order) after gc'ing the series.
|
|
|
|
|
func (s *stripeSeries) gc(mint int64, minOOOMmapRef chunks.ChunkDiskMapperRef) (_ map[storage.SeriesRef]struct{}, _ int, _, _ int64, minMmapFile int) {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
var (
|
2021-11-28 23:54:23 -08:00
|
|
|
|
deleted = map[storage.SeriesRef]struct{}{}
|
|
|
|
|
deletedForCallback = []labels.Labels{}
|
|
|
|
|
rmChunks = 0
|
|
|
|
|
actualMint int64 = math.MaxInt64
|
2022-09-20 10:05:50 -07:00
|
|
|
|
minOOOTime int64 = math.MaxInt64
|
2017-09-05 02:45:18 -07:00
|
|
|
|
)
|
2022-09-20 10:05:50 -07:00
|
|
|
|
minMmapFile = math.MaxInt32
|
2017-09-05 02:45:18 -07:00
|
|
|
|
// 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.
|
2020-01-29 23:12:43 -08:00
|
|
|
|
for i := 0; i < s.size; i++ {
|
2017-09-05 02:45:18 -07:00
|
|
|
|
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()
|
2022-09-20 10:05:50 -07:00
|
|
|
|
rmChunks += series.truncateChunksBefore(mint, minOOOMmapRef)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if len(series.mmappedChunks) > 0 {
|
|
|
|
|
seq, _ := series.mmappedChunks[0].ref.Unpack()
|
|
|
|
|
if seq < minMmapFile {
|
|
|
|
|
minMmapFile = seq
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-28 02:19:41 -08:00
|
|
|
|
if series.ooo != nil && len(series.ooo.oooMmappedChunks) > 0 {
|
|
|
|
|
seq, _ := series.ooo.oooMmappedChunks[0].ref.Unpack()
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if seq < minMmapFile {
|
|
|
|
|
minMmapFile = seq
|
|
|
|
|
}
|
2022-12-28 02:19:41 -08:00
|
|
|
|
for _, ch := range series.ooo.oooMmappedChunks {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if ch.minTime < minOOOTime {
|
|
|
|
|
minOOOTime = ch.minTime
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-28 02:19:41 -08:00
|
|
|
|
if series.ooo != nil && series.ooo.oooHeadChunk != nil {
|
|
|
|
|
if series.ooo.oooHeadChunk.minTime < minOOOTime {
|
|
|
|
|
minOOOTime = series.ooo.oooHeadChunk.minTime
|
2022-09-20 10:05:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-28 02:19:41 -08:00
|
|
|
|
if len(series.mmappedChunks) > 0 || series.headChunk != nil || series.pendingCommit ||
|
|
|
|
|
(series.ooo != nil && (len(series.ooo.oooMmappedChunks) > 0 || series.ooo.oooHeadChunk != nil)) {
|
2020-11-25 05:03:30 -08:00
|
|
|
|
seriesMint := series.minTime()
|
|
|
|
|
if seriesMint < actualMint {
|
|
|
|
|
actualMint = seriesMint
|
|
|
|
|
}
|
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.
|
2020-01-29 23:12:43 -08:00
|
|
|
|
j := int(series.ref) & (s.size - 1)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
if i != j {
|
|
|
|
|
s.locks[j].Lock()
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
deleted[storage.SeriesRef(series.ref)] = struct{}{}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
s.hashes[i].del(hash, series.lset)
|
|
|
|
|
delete(s.series[j], series.ref)
|
2020-05-20 06:22:08 -07:00
|
|
|
|
deletedForCallback = append(deletedForCallback, series.lset)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
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()
|
2020-05-20 06:22:08 -07:00
|
|
|
|
|
|
|
|
|
s.seriesLifecycleCallback.PostDeletion(deletedForCallback...)
|
|
|
|
|
deletedForCallback = deletedForCallback[:0]
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-25 05:03:30 -08:00
|
|
|
|
if actualMint == math.MaxInt64 {
|
|
|
|
|
actualMint = mint
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return deleted, rmChunks, actualMint, minOOOTime, minMmapFile
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
func (s *stripeSeries) getByID(id chunks.HeadSeriesRef) *memSeries {
|
|
|
|
|
i := uint64(id) & uint64(s.size-1)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
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 {
|
2020-01-29 23:12:43 -08:00
|
|
|
|
i := hash & uint64(s.size-1)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
s.locks[i].RLock()
|
|
|
|
|
series := s.hashes[i].get(hash, lset)
|
|
|
|
|
s.locks[i].RUnlock()
|
|
|
|
|
|
|
|
|
|
return series
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 08:23:50 -07:00
|
|
|
|
func (s *stripeSeries) getOrSet(hash uint64, lset labels.Labels, createSeries func() *memSeries) (*memSeries, bool, error) {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
// PreCreation is called here to avoid calling it inside the lock.
|
|
|
|
|
// It is not necessary to call it just before creating a series,
|
|
|
|
|
// rather it gives a 'hint' whether to create a series or not.
|
2021-03-18 08:23:50 -07:00
|
|
|
|
preCreationErr := s.seriesLifecycleCallback.PreCreation(lset)
|
|
|
|
|
|
|
|
|
|
// Create the series, unless the PreCreation() callback as failed.
|
|
|
|
|
// If failed, we'll not allow to create a new series anyway.
|
|
|
|
|
var series *memSeries
|
|
|
|
|
if preCreationErr == nil {
|
|
|
|
|
series = createSeries()
|
|
|
|
|
}
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
2020-05-20 06:22:08 -07:00
|
|
|
|
i := hash & uint64(s.size-1)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
s.locks[i].Lock()
|
|
|
|
|
|
2021-03-18 08:23:50 -07:00
|
|
|
|
if prev := s.hashes[i].get(hash, lset); prev != nil {
|
2017-09-18 02:23:22 -07:00
|
|
|
|
s.locks[i].Unlock()
|
2020-05-20 06:22:08 -07:00
|
|
|
|
return prev, false, nil
|
|
|
|
|
}
|
2021-03-18 08:23:50 -07:00
|
|
|
|
if preCreationErr == nil {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
s.hashes[i].set(hash, series)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
s.locks[i].Unlock()
|
|
|
|
|
|
2021-03-18 08:23:50 -07:00
|
|
|
|
if preCreationErr != nil {
|
2020-05-20 06:22:08 -07:00
|
|
|
|
// The callback prevented creation of series.
|
2021-03-18 08:23:50 -07:00
|
|
|
|
return nil, false, preCreationErr
|
2020-05-20 06:22:08 -07:00
|
|
|
|
}
|
|
|
|
|
// Setting the series in the s.hashes marks the creation of series
|
|
|
|
|
// as any further calls to this methods would return that series.
|
|
|
|
|
s.seriesLifecycleCallback.PostCreation(series.lset)
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
i = uint64(series.ref) & uint64(s.size-1)
|
2017-09-05 02:45:18 -07:00
|
|
|
|
|
|
|
|
|
s.locks[i].Lock()
|
|
|
|
|
s.series[i][series.ref] = series
|
|
|
|
|
s.locks[i].Unlock()
|
|
|
|
|
|
2020-05-20 06:22:08 -07:00
|
|
|
|
return series, true, nil
|
2017-09-05 02:45:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
|
type sample struct {
|
|
|
|
|
t int64
|
2023-03-30 10:50:13 -07:00
|
|
|
|
f float64
|
2021-11-28 23:54:23 -08:00
|
|
|
|
h *histogram.Histogram
|
|
|
|
|
fh *histogram.FloatHistogram
|
2021-07-05 08:57:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
|
func newSample(t int64, v float64, h *histogram.Histogram, fh *histogram.FloatHistogram) tsdbutil.Sample {
|
|
|
|
|
return sample{t, v, h, fh}
|
2017-03-24 02:20:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
|
func (s sample) T() int64 { return s.t }
|
2023-03-30 10:50:13 -07:00
|
|
|
|
func (s sample) F() float64 { return s.f }
|
2021-11-28 23:54:23 -08:00
|
|
|
|
func (s sample) H() *histogram.Histogram { return s.h }
|
|
|
|
|
func (s sample) FH() *histogram.FloatHistogram { return s.fh }
|
2021-11-12 10:07:41 -08:00
|
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
|
func (s sample) Type() chunkenc.ValueType {
|
|
|
|
|
switch {
|
|
|
|
|
case s.h != nil:
|
|
|
|
|
return chunkenc.ValHistogram
|
|
|
|
|
case s.fh != nil:
|
|
|
|
|
return chunkenc.ValFloatHistogram
|
|
|
|
|
default:
|
|
|
|
|
return chunkenc.ValFloat
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-25 13:06:19 -07:00
|
|
|
|
|
2017-09-07 23:48:19 -07:00
|
|
|
|
// memSeries is the in-memory representation of a series. None of its methods
|
2018-01-13 09:51:50 -08:00
|
|
|
|
// are goroutine safe and it is the caller's responsibility to lock it.
|
2017-01-11 04:02:38 -08:00
|
|
|
|
type memSeries struct {
|
2020-03-16 05:59:22 -07:00
|
|
|
|
sync.RWMutex
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2021-11-17 05:05:10 -08:00
|
|
|
|
ref chunks.HeadSeriesRef
|
|
|
|
|
lset labels.Labels
|
2022-08-17 03:02:28 -07:00
|
|
|
|
meta *metadata.Metadata
|
2021-11-17 05:05:10 -08:00
|
|
|
|
|
|
|
|
|
// Immutable chunks on disk that have not yet gone into a block, in order of ascending time stamps.
|
|
|
|
|
// When compaction runs, chunks get moved into a block and all pointers are shifted like so:
|
|
|
|
|
//
|
|
|
|
|
// /------- let's say these 2 chunks get stored into a block
|
|
|
|
|
// | |
|
|
|
|
|
// before compaction: mmappedChunks=[p5,p6,p7,p8,p9] firstChunkID=5
|
|
|
|
|
// after compaction: mmappedChunks=[p7,p8,p9] firstChunkID=7
|
|
|
|
|
//
|
|
|
|
|
// pN is the pointer to the mmappedChunk referered to by HeadChunkID=N
|
2020-05-06 08:30:00 -07:00
|
|
|
|
mmappedChunks []*mmappedChunk
|
2022-09-20 10:05:50 -07:00
|
|
|
|
headChunk *memChunk // Most recent chunk in memory that's still being built.
|
|
|
|
|
firstChunkID chunks.HeadChunkID // HeadChunkID for mmappedChunks[0]
|
2021-11-17 05:05:10 -08:00
|
|
|
|
|
2022-12-28 02:19:41 -08:00
|
|
|
|
ooo *memSeriesOOOFields
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2022-09-27 01:22:22 -07:00
|
|
|
|
mmMaxTime int64 // Max time of any mmapped chunk, only used during WAL replay.
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
2023-05-25 02:18:41 -07:00
|
|
|
|
nextAt int64 // Timestamp at which to cut the next chunk.
|
2021-11-17 02:21:27 -08:00
|
|
|
|
|
2022-09-27 07:02:05 -07:00
|
|
|
|
// We keep the last value here (in addition to appending it to the chunk) so we can check for duplicates.
|
|
|
|
|
lastValue float64
|
2017-01-09 07:51:39 -08:00
|
|
|
|
|
2022-10-11 09:35:35 -07:00
|
|
|
|
// We keep the last histogram value here (in addition to appending it to the chunk) so we can check for duplicates.
|
2022-12-28 00:55:07 -08:00
|
|
|
|
lastHistogramValue *histogram.Histogram
|
|
|
|
|
lastFloatHistogramValue *histogram.FloatHistogram
|
2022-10-11 09:35:35 -07:00
|
|
|
|
|
2021-11-17 02:21:27 -08:00
|
|
|
|
// Current appender for the head chunk. Set when a new head chunk is cut.
|
|
|
|
|
// It is nil only if headChunk is nil. E.g. if there was an appender that created a new series, but rolled back the commit
|
|
|
|
|
// (the first sample would create a headChunk, hence appender, but rollback skipped it while the Append() call would create a series).
|
|
|
|
|
app chunkenc.Appender
|
2020-02-12 11:22:27 -08:00
|
|
|
|
|
2021-11-19 02:11:32 -08:00
|
|
|
|
// txs is nil if isolation is disabled.
|
2020-02-12 11:22:27 -08:00
|
|
|
|
txs *txRing
|
2021-08-30 06:38:44 -07:00
|
|
|
|
|
2022-08-17 03:02:28 -07:00
|
|
|
|
pendingCommit bool // Whether there are samples waiting to be committed to this series.
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-28 02:19:41 -08:00
|
|
|
|
// memSeriesOOOFields contains the fields required by memSeries
|
|
|
|
|
// to handle out-of-order data.
|
|
|
|
|
type memSeriesOOOFields struct {
|
|
|
|
|
oooMmappedChunks []*mmappedChunk // Immutable chunks on disk containing OOO samples.
|
|
|
|
|
oooHeadChunk *oooHeadChunk // Most recent chunk for ooo samples in memory that's still being built.
|
|
|
|
|
firstOOOChunkID chunks.HeadChunkID // HeadOOOChunkID for oooMmappedChunks[0].
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 02:18:41 -07:00
|
|
|
|
func newMemSeries(lset labels.Labels, id chunks.HeadSeriesRef, isolationDisabled bool) *memSeries {
|
2019-01-08 09:08:41 -08:00
|
|
|
|
s := &memSeries{
|
2023-05-25 02:18:41 -07:00
|
|
|
|
lset: lset,
|
|
|
|
|
ref: id,
|
|
|
|
|
nextAt: math.MinInt64,
|
2019-01-08 09:08:41 -08:00
|
|
|
|
}
|
2021-11-19 02:11:32 -08:00
|
|
|
|
if !isolationDisabled {
|
|
|
|
|
s.txs = newTxRing(4)
|
|
|
|
|
}
|
2019-01-08 09:08:41 -08:00
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 15:39:17 -07:00
|
|
|
|
func (s *memSeries) minTime() int64 {
|
2020-05-06 08:30:00 -07:00
|
|
|
|
if len(s.mmappedChunks) > 0 {
|
|
|
|
|
return s.mmappedChunks[0].minTime
|
2018-02-07 05:43:21 -08:00
|
|
|
|
}
|
2020-05-06 08:30:00 -07:00
|
|
|
|
if s.headChunk != nil {
|
|
|
|
|
return s.headChunk.minTime
|
|
|
|
|
}
|
|
|
|
|
return math.MinInt64
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *memSeries) maxTime() int64 {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
// The highest timestamps will always be in the regular (non-OOO) chunks, even if OOO is enabled.
|
2018-02-07 05:43:21 -08:00
|
|
|
|
c := s.head()
|
2021-08-17 10:08:16 -07:00
|
|
|
|
if c != nil {
|
|
|
|
|
return c.maxTime
|
2018-02-07 05:43:21 -08:00
|
|
|
|
}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
if len(s.mmappedChunks) > 0 {
|
|
|
|
|
return s.mmappedChunks[len(s.mmappedChunks)-1].maxTime
|
2018-02-07 05:43:21 -08:00
|
|
|
|
}
|
2021-08-17 10:08:16 -07:00
|
|
|
|
return math.MinInt64
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 00:02:13 -07:00
|
|
|
|
// truncateChunksBefore removes all chunks from the series that
|
|
|
|
|
// have no timestamp at or after mint.
|
|
|
|
|
// Chunk IDs remain unchanged.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
func (s *memSeries) truncateChunksBefore(mint int64, minOOOMmapRef chunks.ChunkDiskMapperRef) int {
|
|
|
|
|
var removedInOrder int
|
2020-05-06 08:30:00 -07:00
|
|
|
|
if s.headChunk != nil && s.headChunk.maxTime < mint {
|
|
|
|
|
// If head chunk is truncated, we can truncate all mmapped chunks.
|
2022-09-20 10:05:50 -07:00
|
|
|
|
removedInOrder = 1 + len(s.mmappedChunks)
|
|
|
|
|
s.firstChunkID += chunks.HeadChunkID(removedInOrder)
|
2018-10-31 06:28:56 -07:00
|
|
|
|
s.headChunk = nil
|
2020-05-06 08:30:00 -07:00
|
|
|
|
s.mmappedChunks = nil
|
|
|
|
|
}
|
|
|
|
|
if len(s.mmappedChunks) > 0 {
|
|
|
|
|
for i, c := range s.mmappedChunks {
|
|
|
|
|
if c.maxTime >= mint {
|
|
|
|
|
break
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
removedInOrder = i + 1
|
|
|
|
|
}
|
|
|
|
|
s.mmappedChunks = append(s.mmappedChunks[:0], s.mmappedChunks[removedInOrder:]...)
|
|
|
|
|
s.firstChunkID += chunks.HeadChunkID(removedInOrder)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var removedOOO int
|
2022-12-28 02:19:41 -08:00
|
|
|
|
if s.ooo != nil && len(s.ooo.oooMmappedChunks) > 0 {
|
|
|
|
|
for i, c := range s.ooo.oooMmappedChunks {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if c.ref.GreaterThan(minOOOMmapRef) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
removedOOO = i + 1
|
2020-05-06 08:30:00 -07:00
|
|
|
|
}
|
2022-12-28 02:19:41 -08:00
|
|
|
|
s.ooo.oooMmappedChunks = append(s.ooo.oooMmappedChunks[:0], s.ooo.oooMmappedChunks[removedOOO:]...)
|
|
|
|
|
s.ooo.firstOOOChunkID += chunks.HeadChunkID(removedOOO)
|
|
|
|
|
|
|
|
|
|
if len(s.ooo.oooMmappedChunks) == 0 && s.ooo.oooHeadChunk == nil {
|
|
|
|
|
s.ooo = nil
|
|
|
|
|
}
|
2018-10-31 06:28:56 -07:00
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
|
|
|
|
|
return removedInOrder + removedOOO
|
2017-08-28 15:39:17 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-12 11:22:27 -08:00
|
|
|
|
// cleanupAppendIDsBelow cleans up older appendIDs. Has to be called after
|
|
|
|
|
// acquiring lock.
|
|
|
|
|
func (s *memSeries) cleanupAppendIDsBelow(bound uint64) {
|
2021-11-19 02:11:32 -08:00
|
|
|
|
if s.txs != nil {
|
|
|
|
|
s.txs.cleanupAppendIDsBelow(bound)
|
|
|
|
|
}
|
2020-02-12 11:22:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 04:02:38 -08:00
|
|
|
|
func (s *memSeries) head() *memChunk {
|
2018-10-31 06:28:56 -07:00
|
|
|
|
return s.headChunk
|
2017-01-11 04:02:38 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type memChunk struct {
|
2017-11-30 06:34:49 -08:00
|
|
|
|
chunk chunkenc.Chunk
|
2017-01-11 04:02:38 -08:00
|
|
|
|
minTime, maxTime int64
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 10:05:50 -07:00
|
|
|
|
type oooHeadChunk struct {
|
|
|
|
|
chunk *OOOChunk
|
|
|
|
|
minTime, maxTime int64 // can probably be removed and pulled out of the chunk instead
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OverlapsClosedInterval returns true if the chunk overlaps [mint, maxt].
|
|
|
|
|
func (mc *oooHeadChunk) OverlapsClosedInterval(mint, maxt int64) bool {
|
|
|
|
|
return overlapsClosedInterval(mc.minTime, mc.maxTime, mint, maxt)
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 08:30:00 -07:00
|
|
|
|
// OverlapsClosedInterval returns true if the chunk overlaps [mint, maxt].
|
2018-07-02 01:23:36 -07:00
|
|
|
|
func (mc *memChunk) OverlapsClosedInterval(mint, maxt int64) bool {
|
2021-08-03 07:33:54 -07:00
|
|
|
|
return overlapsClosedInterval(mc.minTime, mc.maxTime, mint, maxt)
|
2017-01-09 07:51:39 -08:00
|
|
|
|
}
|
2017-11-30 06:34:49 -08:00
|
|
|
|
|
2021-08-03 07:33:54 -07:00
|
|
|
|
func overlapsClosedInterval(mint1, maxt1, mint2, maxt2 int64) bool {
|
|
|
|
|
return mint1 <= maxt2 && mint2 <= maxt1
|
2021-07-05 08:57:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
|
// mappedChunks describes a head chunk on disk that has been mmapped
|
2020-05-06 08:30:00 -07:00
|
|
|
|
type mmappedChunk struct {
|
2021-10-13 05:14:32 -07:00
|
|
|
|
ref chunks.ChunkDiskMapperRef
|
2020-05-06 08:30:00 -07:00
|
|
|
|
numSamples uint16
|
|
|
|
|
minTime, maxTime int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the chunk overlaps [mint, maxt].
|
|
|
|
|
func (mc *mmappedChunk) OverlapsClosedInterval(mint, maxt int64) bool {
|
2021-08-03 07:33:54 -07:00
|
|
|
|
return overlapsClosedInterval(mc.minTime, mc.maxTime, mint, maxt)
|
2020-05-20 06:22:08 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type noopSeriesLifecycleCallback struct{}
|
|
|
|
|
|
|
|
|
|
func (noopSeriesLifecycleCallback) PreCreation(labels.Labels) error { return nil }
|
|
|
|
|
func (noopSeriesLifecycleCallback) PostCreation(labels.Labels) {}
|
|
|
|
|
func (noopSeriesLifecycleCallback) PostDeletion(...labels.Labels) {}
|
2020-10-12 14:15:40 -07:00
|
|
|
|
|
|
|
|
|
func (h *Head) Size() int64 {
|
2022-09-20 10:05:50 -07:00
|
|
|
|
var walSize, wblSize int64
|
2020-10-12 14:15:40 -07:00
|
|
|
|
if h.wal != nil {
|
|
|
|
|
walSize, _ = h.wal.Size()
|
|
|
|
|
}
|
2022-09-20 10:05:50 -07:00
|
|
|
|
if h.wbl != nil {
|
|
|
|
|
wblSize, _ = h.wbl.Size()
|
|
|
|
|
}
|
2020-11-03 02:04:59 -08:00
|
|
|
|
cdmSize, _ := h.chunkDiskMapper.Size()
|
2022-09-20 10:05:50 -07:00
|
|
|
|
return walSize + wblSize + cdmSize
|
2020-10-12 14:15:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *RangeHead) Size() int64 {
|
|
|
|
|
return h.head.Size()
|
|
|
|
|
}
|
2021-06-05 07:29:32 -07:00
|
|
|
|
|
|
|
|
|
func (h *Head) startWALReplayStatus(startFrom, last int) {
|
|
|
|
|
h.stats.WALReplayStatus.Lock()
|
|
|
|
|
defer h.stats.WALReplayStatus.Unlock()
|
|
|
|
|
|
|
|
|
|
h.stats.WALReplayStatus.Min = startFrom
|
|
|
|
|
h.stats.WALReplayStatus.Max = last
|
|
|
|
|
h.stats.WALReplayStatus.Current = startFrom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Head) updateWALReplayStatusRead(current int) {
|
|
|
|
|
h.stats.WALReplayStatus.Lock()
|
|
|
|
|
defer h.stats.WALReplayStatus.Unlock()
|
|
|
|
|
|
|
|
|
|
h.stats.WALReplayStatus.Current = current
|
|
|
|
|
}
|