2017-05-26 15:56:31 +00: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.
|
|
|
|
|
2017-05-17 03:06:56 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2017-05-23 11:57:45 +00:00
|
|
|
"fmt"
|
2017-05-24 08:12:56 +00:00
|
|
|
"io"
|
2017-05-17 03:06:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-06-08 11:52:01 +00:00
|
|
|
"sync"
|
2018-09-20 08:33:52 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-02-22 17:11:11 +00:00
|
|
|
"github.com/prometheus/tsdb/encoding"
|
2019-04-08 12:06:40 +00:00
|
|
|
tsdb_errors "github.com/prometheus/tsdb/errors"
|
2017-05-17 03:06:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const tombstoneFilename = "tombstones"
|
|
|
|
|
2017-05-23 11:57:45 +00:00
|
|
|
const (
|
|
|
|
// MagicTombstone is 4 bytes at the head of a tombstone file.
|
2018-11-09 11:37:02 +00:00
|
|
|
MagicTombstone = 0x0130BA30
|
2017-05-23 11:57:45 +00:00
|
|
|
|
|
|
|
tombstoneFormatV1 = 1
|
|
|
|
)
|
|
|
|
|
2017-10-09 13:21:46 +00:00
|
|
|
// TombstoneReader gives access to tombstone intervals by series reference.
|
2017-09-04 14:08:38 +00:00
|
|
|
type TombstoneReader interface {
|
2017-11-13 12:32:24 +00:00
|
|
|
// Get returns deletion intervals for the series with the given reference.
|
|
|
|
Get(ref uint64) (Intervals, error)
|
2017-10-09 13:21:46 +00:00
|
|
|
|
2017-11-13 12:32:24 +00:00
|
|
|
// Iter calls the given function for each encountered interval.
|
|
|
|
Iter(func(uint64, Intervals) error) error
|
|
|
|
|
2018-09-27 11:43:22 +00:00
|
|
|
// Total returns the total count of tombstones.
|
|
|
|
Total() uint64
|
|
|
|
|
2017-11-13 12:32:24 +00:00
|
|
|
// Close any underlying resources
|
2017-10-09 13:21:46 +00:00
|
|
|
Close() error
|
2017-09-04 14:08:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 12:32:24 +00:00
|
|
|
func writeTombstoneFile(dir string, tr TombstoneReader) error {
|
2017-05-17 03:06:56 +00:00
|
|
|
path := filepath.Join(dir, tombstoneFilename)
|
|
|
|
tmp := path + ".tmp"
|
2017-08-26 16:04:00 +00:00
|
|
|
hash := newCRC32()
|
2017-05-17 03:06:56 +00:00
|
|
|
|
|
|
|
f, err := os.Create(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-10-31 14:37:41 +00:00
|
|
|
defer func() {
|
|
|
|
if f != nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}()
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
buf := encoding.Encbuf{B: make([]byte, 3*binary.MaxVarintLen64)}
|
|
|
|
buf.Reset()
|
2017-05-23 11:57:45 +00:00
|
|
|
// Write the meta.
|
2019-02-22 17:11:11 +00:00
|
|
|
buf.PutBE32(MagicTombstone)
|
|
|
|
buf.PutByte(tombstoneFormatV1)
|
|
|
|
_, err = f.Write(buf.Get())
|
2017-05-23 11:57:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
mw := io.MultiWriter(f, hash)
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2018-09-20 08:33:52 +00:00
|
|
|
if err := tr.Iter(func(ref uint64, ivs Intervals) error {
|
2017-11-13 12:32:24 +00:00
|
|
|
for _, iv := range ivs {
|
2019-02-22 17:11:11 +00:00
|
|
|
buf.Reset()
|
2017-11-13 12:32:24 +00:00
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
buf.PutUvarint64(ref)
|
|
|
|
buf.PutVarint64(iv.Mint)
|
|
|
|
buf.PutVarint64(iv.Maxt)
|
2017-05-24 08:12:56 +00:00
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
_, err = mw.Write(buf.Get())
|
2017-05-24 08:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-11-13 12:32:24 +00:00
|
|
|
return nil
|
2018-09-20 08:33:52 +00:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("error writing tombstones: %v", err)
|
|
|
|
}
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
_, err = f.Write(hash.Sum(nil))
|
2017-05-17 03:06:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:06:40 +00:00
|
|
|
var merr tsdb_errors.MultiError
|
|
|
|
if merr.Add(f.Sync()); merr.Err() != nil {
|
|
|
|
merr.Add(f.Close())
|
|
|
|
return merr.Err()
|
|
|
|
}
|
|
|
|
|
2017-10-31 14:37:41 +00:00
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f = nil
|
2017-05-17 03:06:56 +00:00
|
|
|
return renameFile(tmp, path)
|
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
// Stone holds the information on the posting and time-range
|
2017-05-17 03:06:56 +00:00
|
|
|
// that is deleted.
|
2017-05-26 15:56:31 +00:00
|
|
|
type Stone struct {
|
2017-09-04 14:08:38 +00:00
|
|
|
ref uint64
|
2017-08-25 08:11:46 +00:00
|
|
|
intervals Intervals
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:03:52 +00:00
|
|
|
func readTombstones(dir string) (TombstoneReader, SizeReader, error) {
|
2017-05-17 03:06:56 +00:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(dir, tombstoneFilename))
|
2017-12-05 16:17:33 +00:00
|
|
|
if os.IsNotExist(err) {
|
2019-01-16 10:03:52 +00:00
|
|
|
return newMemTombstones(), nil, nil
|
2017-12-05 16:17:33 +00:00
|
|
|
} else if err != nil {
|
2019-01-16 10:03:52 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sr := &TombstoneFile{
|
|
|
|
size: int64(len(b)),
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
if len(b) < 5 {
|
2019-02-22 17:11:11 +00:00
|
|
|
return nil, sr, errors.Wrap(encoding.ErrInvalidSize, "tombstones header")
|
2017-05-26 15:56:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
d := &encoding.Decbuf{B: b[:len(b)-4]} // 4 for the checksum.
|
|
|
|
if mg := d.Be32(); mg != MagicTombstone {
|
2019-01-16 10:03:52 +00:00
|
|
|
return nil, sr, fmt.Errorf("invalid magic number %x", mg)
|
2017-05-23 11:57:45 +00:00
|
|
|
}
|
2019-02-22 17:11:11 +00:00
|
|
|
if flag := d.Byte(); flag != tombstoneFormatV1 {
|
2019-01-16 10:03:52 +00:00
|
|
|
return nil, sr, fmt.Errorf("invalid tombstone format %x", flag)
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
if d.Err() != nil {
|
|
|
|
return nil, sr, d.Err()
|
2017-05-26 15:56:31 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 12:50:04 +00:00
|
|
|
// Verify checksum.
|
2017-08-26 16:04:00 +00:00
|
|
|
hash := newCRC32()
|
2019-02-22 17:11:11 +00:00
|
|
|
if _, err := hash.Write(d.Get()); err != nil {
|
2019-01-16 10:03:52 +00:00
|
|
|
return nil, sr, errors.Wrap(err, "write to hash")
|
2017-05-24 08:12:56 +00:00
|
|
|
}
|
|
|
|
if binary.BigEndian.Uint32(b[len(b)-4:]) != hash.Sum32() {
|
2019-01-16 10:03:52 +00:00
|
|
|
return nil, sr, errors.New("checksum did not match")
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 16:40:01 +00:00
|
|
|
stonesMap := newMemTombstones()
|
2017-11-13 12:32:24 +00:00
|
|
|
|
2019-02-22 17:11:11 +00:00
|
|
|
for d.Len() > 0 {
|
|
|
|
k := d.Uvarint64()
|
|
|
|
mint := d.Varint64()
|
|
|
|
maxt := d.Varint64()
|
|
|
|
if d.Err() != nil {
|
|
|
|
return nil, sr, d.Err()
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-05-22 05:01:50 +00:00
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
stonesMap.addInterval(k, Interval{mint, maxt})
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:03:52 +00:00
|
|
|
return stonesMap, sr, nil
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
type memTombstones struct {
|
2018-07-10 13:24:13 +00:00
|
|
|
intvlGroups map[uint64]Intervals
|
|
|
|
mtx sync.RWMutex
|
2018-06-08 11:52:01 +00:00
|
|
|
}
|
2017-11-13 12:32:24 +00:00
|
|
|
|
2018-11-14 16:40:01 +00:00
|
|
|
// newMemTombstones creates new in memory TombstoneReader
|
|
|
|
// that allows adding new intervals.
|
|
|
|
func newMemTombstones() *memTombstones {
|
2018-07-10 13:24:13 +00:00
|
|
|
return &memTombstones{intvlGroups: make(map[uint64]Intervals)}
|
2017-05-24 05:54:24 +00:00
|
|
|
}
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
func (t *memTombstones) Get(ref uint64) (Intervals, error) {
|
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
2018-07-10 13:24:13 +00:00
|
|
|
return t.intvlGroups[ref], nil
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
func (t *memTombstones) Iter(f func(uint64, Intervals) error) error {
|
2018-06-25 13:52:11 +00:00
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
2018-07-10 13:24:13 +00:00
|
|
|
for ref, ivs := range t.intvlGroups {
|
2017-11-13 12:32:24 +00:00
|
|
|
if err := f(ref, ivs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2018-09-27 11:43:22 +00:00
|
|
|
func (t *memTombstones) Total() uint64 {
|
|
|
|
t.mtx.RLock()
|
|
|
|
defer t.mtx.RUnlock()
|
|
|
|
|
|
|
|
total := uint64(0)
|
|
|
|
for _, ivs := range t.intvlGroups {
|
|
|
|
total += uint64(len(ivs))
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
// addInterval to an existing memTombstones
|
2018-07-06 12:30:27 +00:00
|
|
|
func (t *memTombstones) addInterval(ref uint64, itvs ...Interval) {
|
2018-06-08 11:52:01 +00:00
|
|
|
t.mtx.Lock()
|
|
|
|
defer t.mtx.Unlock()
|
2018-06-25 13:52:11 +00:00
|
|
|
for _, itv := range itvs {
|
2018-07-10 13:24:13 +00:00
|
|
|
t.intvlGroups[ref] = t.intvlGroups[ref].add(itv)
|
2018-06-25 13:52:11 +00:00
|
|
|
}
|
2017-05-26 15:56:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 10:03:52 +00:00
|
|
|
// TombstoneFile holds information about the tombstone file.
|
|
|
|
type TombstoneFile struct {
|
|
|
|
size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the tombstone file size.
|
|
|
|
func (t *TombstoneFile) Size() int64 {
|
|
|
|
return t.size
|
|
|
|
}
|
|
|
|
|
2018-11-14 16:40:01 +00:00
|
|
|
func (*memTombstones) Close() error {
|
2017-10-09 13:21:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
// Interval represents a single time-interval.
|
|
|
|
type Interval struct {
|
|
|
|
Mint, Maxt int64
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
func (tr Interval) inBounds(t int64) bool {
|
|
|
|
return t >= tr.Mint && t <= tr.Maxt
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
func (tr Interval) isSubrange(dranges Intervals) bool {
|
2017-05-22 11:12:36 +00:00
|
|
|
for _, r := range dranges {
|
2017-08-25 08:11:46 +00:00
|
|
|
if r.inBounds(tr.Mint) && r.inBounds(tr.Maxt) {
|
2017-05-17 09:19:42 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
// Intervals represents a set of increasing and non-overlapping time-intervals.
|
|
|
|
type Intervals []Interval
|
2017-05-22 11:12:36 +00:00
|
|
|
|
2018-06-08 11:52:01 +00:00
|
|
|
// add the new time-range to the existing ones.
|
2017-05-21 17:50:05 +00:00
|
|
|
// The existing ones must be sorted.
|
2017-08-25 08:11:46 +00:00
|
|
|
func (itvs Intervals) add(n Interval) Intervals {
|
2017-05-22 11:12:36 +00:00
|
|
|
for i, r := range itvs {
|
2017-05-17 09:19:42 +00:00
|
|
|
// TODO(gouthamve): Make this codepath easier to digest.
|
2017-08-25 08:11:46 +00:00
|
|
|
if r.inBounds(n.Mint-1) || r.inBounds(n.Mint) {
|
|
|
|
if n.Maxt > r.Maxt {
|
|
|
|
itvs[i].Maxt = n.Maxt
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
j := 0
|
2017-05-22 11:12:36 +00:00
|
|
|
for _, r2 := range itvs[i+1:] {
|
2017-08-25 08:11:46 +00:00
|
|
|
if n.Maxt < r2.Mint {
|
2017-05-17 09:19:42 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 0 {
|
2017-08-25 08:11:46 +00:00
|
|
|
if itvs[i+j].Maxt > n.Maxt {
|
|
|
|
itvs[i].Maxt = itvs[i+j].Maxt
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
2017-05-22 11:12:36 +00:00
|
|
|
itvs = append(itvs[:i+1], itvs[i+j+1:]...)
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
2017-05-22 11:12:36 +00:00
|
|
|
return itvs
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
if r.inBounds(n.Maxt+1) || r.inBounds(n.Maxt) {
|
|
|
|
if n.Mint < r.Maxt {
|
|
|
|
itvs[i].Mint = n.Mint
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
2017-05-22 11:12:36 +00:00
|
|
|
return itvs
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
2017-05-22 11:12:36 +00:00
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
if n.Mint < r.Mint {
|
|
|
|
newRange := make(Intervals, i, len(itvs[:i])+1)
|
2017-05-22 11:12:36 +00:00
|
|
|
copy(newRange, itvs[:i])
|
2017-05-17 09:19:42 +00:00
|
|
|
newRange = append(newRange, n)
|
2017-05-22 11:12:36 +00:00
|
|
|
newRange = append(newRange, itvs[i:]...)
|
2017-05-17 09:19:42 +00:00
|
|
|
|
|
|
|
return newRange
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:12:36 +00:00
|
|
|
itvs = append(itvs, n)
|
|
|
|
return itvs
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|