2018-05-17 06:02:47 -07:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-11-30 06:46:16 -08:00
|
|
|
"math"
|
2018-05-17 06:02:47 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-08-13 01:34:14 -07:00
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/wal"
|
2018-05-17 06:02:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// CheckpointStats returns stats about a created checkpoint.
|
|
|
|
type CheckpointStats struct {
|
|
|
|
DroppedSeries int
|
|
|
|
DroppedSamples int
|
|
|
|
DroppedTombstones int
|
2018-06-18 04:52:57 -07:00
|
|
|
TotalSeries int // Processed series including dropped ones.
|
2018-12-03 14:14:37 -08:00
|
|
|
TotalSamples int // Processed samples including dropped ones.
|
2018-08-02 14:46:45 -07:00
|
|
|
TotalTombstones int // Processed tombstones including dropped ones.
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
2018-10-11 08:23:52 -07:00
|
|
|
// LastCheckpoint returns the directory name and index of the most recent checkpoint.
|
2018-05-17 06:02:47 -07:00
|
|
|
// If dir does not contain any checkpoints, ErrNotFound is returned.
|
|
|
|
func LastCheckpoint(dir string) (string, int, error) {
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
// Traverse list backwards since there may be multiple checkpoints left.
|
|
|
|
for i := len(files) - 1; i >= 0; i-- {
|
|
|
|
fi := files[i]
|
|
|
|
|
|
|
|
if !strings.HasPrefix(fi.Name(), checkpointPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return "", 0, errors.Errorf("checkpoint %s is not a directory", fi.Name())
|
|
|
|
}
|
2018-10-11 08:23:52 -07:00
|
|
|
idx, err := strconv.Atoi(fi.Name()[len(checkpointPrefix):])
|
2018-05-17 06:02:47 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-30 06:46:16 -08:00
|
|
|
return filepath.Join(dir, fi.Name()), idx, nil
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
return "", 0, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2018-10-11 08:23:52 -07:00
|
|
|
// DeleteCheckpoints deletes all checkpoints in a directory below a given index.
|
|
|
|
func DeleteCheckpoints(dir string, maxIndex int) error {
|
2019-03-19 06:31:57 -07:00
|
|
|
var errs tsdb_errors.MultiError
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if !strings.HasPrefix(fi.Name(), checkpointPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-11 08:23:52 -07:00
|
|
|
index, err := strconv.Atoi(fi.Name()[len(checkpointPrefix):])
|
|
|
|
if err != nil || index >= maxIndex {
|
2018-05-17 06:02:47 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(filepath.Join(dir, fi.Name())); err != nil {
|
|
|
|
errs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkpointPrefix = "checkpoint."
|
|
|
|
|
2018-10-11 08:23:52 -07:00
|
|
|
// Checkpoint creates a compacted checkpoint of segments in range [first, last] in the given WAL.
|
2018-05-17 06:02:47 -07:00
|
|
|
// It includes the most recent checkpoint if it exists.
|
|
|
|
// All series not satisfying keep and samples below mint are dropped.
|
|
|
|
//
|
|
|
|
// The checkpoint is stored in a directory named checkpoint.N in the same
|
|
|
|
// segmented format as the original WAL itself.
|
|
|
|
// This makes it easy to read it through the WAL package and concatenate
|
|
|
|
// it with the original WAL.
|
2018-10-11 08:23:52 -07:00
|
|
|
func Checkpoint(w *wal.WAL, from, to int, keep func(id uint64) bool, mint int64) (*CheckpointStats, error) {
|
2018-05-17 06:02:47 -07:00
|
|
|
stats := &CheckpointStats{}
|
2018-11-30 06:46:16 -08:00
|
|
|
var sgmReader io.ReadCloser
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
{
|
2018-11-30 06:46:16 -08:00
|
|
|
|
|
|
|
var sgmRange []wal.SegmentRange
|
2018-10-11 08:23:52 -07:00
|
|
|
dir, idx, err := LastCheckpoint(w.Dir())
|
2018-05-17 06:02:47 -07:00
|
|
|
if err != nil && err != ErrNotFound {
|
|
|
|
return nil, errors.Wrap(err, "find last checkpoint")
|
|
|
|
}
|
2018-10-11 08:23:52 -07:00
|
|
|
last := idx + 1
|
2018-05-17 06:02:47 -07:00
|
|
|
if err == nil {
|
2018-10-11 08:23:52 -07:00
|
|
|
if from > last {
|
|
|
|
return nil, fmt.Errorf("unexpected gap to last checkpoint. expected:%v, requested:%v", last, from)
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
// Ignore WAL files below the checkpoint. They shouldn't exist to begin with.
|
2018-10-11 08:23:52 -07:00
|
|
|
from = last
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2018-11-30 06:46:16 -08:00
|
|
|
sgmRange = append(sgmRange, wal.SegmentRange{Dir: dir, Last: math.MaxInt32})
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
2018-11-30 06:46:16 -08:00
|
|
|
sgmRange = append(sgmRange, wal.SegmentRange{Dir: w.Dir(), First: from, Last: to})
|
|
|
|
sgmReader, err = wal.NewSegmentsRangeReader(sgmRange...)
|
2018-05-17 06:02:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "create segment reader")
|
|
|
|
}
|
2018-11-30 06:46:16 -08:00
|
|
|
defer sgmReader.Close()
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
2019-01-07 00:43:33 -08:00
|
|
|
cpdir := filepath.Join(w.Dir(), fmt.Sprintf(checkpointPrefix+"%06d", to))
|
2018-05-17 06:02:47 -07:00
|
|
|
cpdirtmp := cpdir + ".tmp"
|
|
|
|
|
|
|
|
if err := os.MkdirAll(cpdirtmp, 0777); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "create checkpoint dir")
|
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
cp, err := wal.New(nil, nil, cpdirtmp, w.CompressionEnabled())
|
2018-05-17 06:02:47 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "open checkpoint")
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:43:33 -08:00
|
|
|
// Ensures that an early return caused by an error doesn't leave any tmp files.
|
|
|
|
defer func() {
|
|
|
|
cp.Close()
|
|
|
|
os.RemoveAll(cpdirtmp)
|
|
|
|
}()
|
|
|
|
|
2018-11-30 06:46:16 -08:00
|
|
|
r := wal.NewReader(sgmReader)
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
var (
|
|
|
|
series []RefSeries
|
|
|
|
samples []RefSample
|
|
|
|
tstones []Stone
|
|
|
|
dec RecordDecoder
|
|
|
|
enc RecordEncoder
|
|
|
|
buf []byte
|
|
|
|
recs [][]byte
|
|
|
|
)
|
|
|
|
for r.Next() {
|
|
|
|
series, samples, tstones = series[:0], samples[:0], tstones[:0]
|
|
|
|
|
|
|
|
// We don't reset the buffer since we batch up multiple records
|
|
|
|
// before writing them to the checkpoint.
|
2018-06-18 04:52:57 -07:00
|
|
|
// Remember where the record for this iteration starts.
|
2018-05-17 06:02:47 -07:00
|
|
|
start := len(buf)
|
|
|
|
rec := r.Record()
|
|
|
|
|
|
|
|
switch dec.Type(rec) {
|
|
|
|
case RecordSeries:
|
|
|
|
series, err = dec.Series(rec, series)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode series")
|
|
|
|
}
|
|
|
|
// Drop irrelevant series in place.
|
|
|
|
repl := series[:0]
|
|
|
|
for _, s := range series {
|
|
|
|
if keep(s.Ref) {
|
|
|
|
repl = append(repl, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(repl) > 0 {
|
|
|
|
buf = enc.Series(repl, buf)
|
|
|
|
}
|
|
|
|
stats.TotalSeries += len(series)
|
|
|
|
stats.DroppedSeries += len(series) - len(repl)
|
|
|
|
|
|
|
|
case RecordSamples:
|
|
|
|
samples, err = dec.Samples(rec, samples)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode samples")
|
|
|
|
}
|
|
|
|
// Drop irrelevant samples in place.
|
|
|
|
repl := samples[:0]
|
|
|
|
for _, s := range samples {
|
|
|
|
if s.T >= mint {
|
|
|
|
repl = append(repl, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(repl) > 0 {
|
|
|
|
buf = enc.Samples(repl, buf)
|
|
|
|
}
|
|
|
|
stats.TotalSamples += len(samples)
|
|
|
|
stats.DroppedSamples += len(samples) - len(repl)
|
|
|
|
|
|
|
|
case RecordTombstones:
|
|
|
|
tstones, err = dec.Tombstones(rec, tstones)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "decode deletes")
|
|
|
|
}
|
|
|
|
// Drop irrelevant tombstones in place.
|
|
|
|
repl := tstones[:0]
|
|
|
|
for _, s := range tstones {
|
|
|
|
for _, iv := range s.intervals {
|
|
|
|
if iv.Maxt >= mint {
|
|
|
|
repl = append(repl, s)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(repl) > 0 {
|
|
|
|
buf = enc.Tombstones(repl, buf)
|
|
|
|
}
|
|
|
|
stats.TotalTombstones += len(tstones)
|
|
|
|
stats.DroppedTombstones += len(tstones) - len(repl)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("invalid record type")
|
|
|
|
}
|
|
|
|
if len(buf[start:]) == 0 {
|
|
|
|
continue // All contents discarded.
|
|
|
|
}
|
|
|
|
recs = append(recs, buf[start:])
|
|
|
|
|
|
|
|
// Flush records in 1 MB increments.
|
|
|
|
if len(buf) > 1*1024*1024 {
|
|
|
|
if err := cp.Log(recs...); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "flush records")
|
|
|
|
}
|
|
|
|
buf, recs = buf[:0], recs[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we hit any corruption during checkpointing, repairing is not an option.
|
|
|
|
// The head won't know which series records are lost.
|
|
|
|
if r.Err() != nil {
|
|
|
|
return nil, errors.Wrap(r.Err(), "read segments")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush remaining records.
|
|
|
|
if err := cp.Log(recs...); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "flush records")
|
|
|
|
}
|
|
|
|
if err := cp.Close(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "close checkpoint")
|
|
|
|
}
|
2018-08-02 14:46:45 -07:00
|
|
|
if err := fileutil.Replace(cpdirtmp, cpdir); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "rename checkpoint directory")
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2018-11-30 06:46:16 -08:00
|
|
|
|
2018-05-17 06:02:47 -07:00
|
|
|
return stats, nil
|
|
|
|
}
|