2017-05-26 08:56:31 -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.
|
|
|
|
|
2017-05-16 20:06:56 -07:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2017-05-23 04:57:45 -07:00
|
|
|
"fmt"
|
2017-05-24 01:12:56 -07:00
|
|
|
"io"
|
2017-05-16 20:06:56 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-24 01:12:56 -07:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-05-16 20:06:56 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const tombstoneFilename = "tombstones"
|
|
|
|
|
2017-05-23 04:57:45 -07:00
|
|
|
const (
|
|
|
|
// MagicTombstone is 4 bytes at the head of a tombstone file.
|
|
|
|
MagicTombstone = 0x130BA30
|
|
|
|
|
|
|
|
tombstoneFormatV1 = 1
|
|
|
|
)
|
|
|
|
|
2017-10-09 06:21:46 -07:00
|
|
|
// TombstoneReader gives access to tombstone intervals by series reference.
|
2017-09-04 07:08:38 -07:00
|
|
|
type TombstoneReader interface {
|
|
|
|
Get(ref uint64) Intervals
|
2017-10-09 06:21:46 -07:00
|
|
|
|
|
|
|
Close() error
|
2017-09-04 07:08:38 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:54:24 -07:00
|
|
|
func writeTombstoneFile(dir string, tr tombstoneReader) error {
|
2017-05-16 20:06:56 -07:00
|
|
|
path := filepath.Join(dir, tombstoneFilename)
|
|
|
|
tmp := path + ".tmp"
|
2017-08-26 09:04:00 -07:00
|
|
|
hash := newCRC32()
|
2017-05-16 20:06:56 -07:00
|
|
|
|
|
|
|
f, err := os.Create(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 01:12:56 -07:00
|
|
|
defer f.Close()
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
buf := encbuf{b: make([]byte, 3*binary.MaxVarintLen64)}
|
2017-05-23 04:57:45 -07:00
|
|
|
buf.reset()
|
|
|
|
// Write the meta.
|
|
|
|
buf.putBE32(MagicTombstone)
|
|
|
|
buf.putByte(tombstoneFormatV1)
|
2017-05-24 01:12:56 -07:00
|
|
|
_, err = f.Write(buf.get())
|
2017-05-23 04:57:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
mw := io.MultiWriter(f, hash)
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
for k, v := range tr {
|
|
|
|
for _, itv := range v {
|
|
|
|
buf.reset()
|
2017-09-04 07:08:38 -07:00
|
|
|
buf.putUvarint64(k)
|
2017-08-25 01:11:46 -07:00
|
|
|
buf.putVarint64(itv.Mint)
|
|
|
|
buf.putVarint64(itv.Maxt)
|
2017-05-24 01:12:56 -07:00
|
|
|
|
|
|
|
_, err = mw.Write(buf.get())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
_, err = f.Write(hash.Sum(nil))
|
2017-05-16 20:06:56 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return renameFile(tmp, path)
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
// Stone holds the information on the posting and time-range
|
2017-05-16 20:06:56 -07:00
|
|
|
// that is deleted.
|
2017-05-26 08:56:31 -07:00
|
|
|
type Stone struct {
|
2017-09-04 07:08:38 -07:00
|
|
|
ref uint64
|
2017-08-25 01:11:46 -07:00
|
|
|
intervals Intervals
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:54:24 -07:00
|
|
|
func readTombstones(dir string) (tombstoneReader, error) {
|
2017-05-16 20:06:56 -07:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(dir, tombstoneFilename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
if len(b) < 5 {
|
|
|
|
return nil, errors.Wrap(errInvalidSize, "tombstones header")
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
d := &decbuf{b: b[:len(b)-4]} // 4 for the checksum.
|
2017-05-23 04:57:45 -07:00
|
|
|
if mg := d.be32(); mg != MagicTombstone {
|
|
|
|
return nil, fmt.Errorf("invalid magic number %x", mg)
|
|
|
|
}
|
2017-05-24 01:12:56 -07:00
|
|
|
if flag := d.byte(); flag != tombstoneFormatV1 {
|
|
|
|
return nil, fmt.Errorf("invalid tombstone format %x", flag)
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
if d.err() != nil {
|
|
|
|
return nil, d.err()
|
|
|
|
}
|
|
|
|
|
2017-05-24 01:12:56 -07:00
|
|
|
// Verify checksum
|
2017-08-26 09:04:00 -07:00
|
|
|
hash := newCRC32()
|
2017-05-24 01:12:56 -07:00
|
|
|
if _, err := hash.Write(d.get()); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "write to hash")
|
|
|
|
}
|
|
|
|
if binary.BigEndian.Uint32(b[len(b)-4:]) != hash.Sum32() {
|
|
|
|
return nil, errors.New("checksum did not match")
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-26 08:56:31 -07:00
|
|
|
stonesMap := newEmptyTombstoneReader()
|
2017-05-24 01:12:56 -07:00
|
|
|
for d.len() > 0 {
|
2017-09-04 07:08:38 -07:00
|
|
|
k := d.uvarint64()
|
2017-05-24 01:12:56 -07:00
|
|
|
mint := d.varint64()
|
|
|
|
maxt := d.varint64()
|
|
|
|
if d.err() != nil {
|
|
|
|
return nil, d.err()
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2017-05-21 22:01:50 -07:00
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
stonesMap.add(k, Interval{mint, maxt})
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 22:54:24 -07:00
|
|
|
return newTombstoneReader(stonesMap), nil
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
type tombstoneReader map[uint64]Intervals
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
func newTombstoneReader(ts map[uint64]Intervals) tombstoneReader {
|
2017-05-23 22:54:24 -07:00
|
|
|
return tombstoneReader(ts)
|
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2017-05-23 22:54:24 -07:00
|
|
|
func newEmptyTombstoneReader() tombstoneReader {
|
2017-09-04 07:08:38 -07:00
|
|
|
return tombstoneReader(make(map[uint64]Intervals))
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
func (t tombstoneReader) Get(ref uint64) Intervals {
|
2017-05-23 22:54:24 -07:00
|
|
|
return t[ref]
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
func (t tombstoneReader) add(ref uint64, itv Interval) {
|
2017-05-26 08:56:31 -07:00
|
|
|
t[ref] = t[ref].add(itv)
|
|
|
|
}
|
|
|
|
|
2017-10-09 06:21:46 -07:00
|
|
|
func (tombstoneReader) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
// Interval represents a single time-interval.
|
|
|
|
type Interval struct {
|
|
|
|
Mint, Maxt int64
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
func (tr Interval) inBounds(t int64) bool {
|
|
|
|
return t >= tr.Mint && t <= tr.Maxt
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
func (tr Interval) isSubrange(dranges Intervals) bool {
|
2017-05-22 04:12:36 -07:00
|
|
|
for _, r := range dranges {
|
2017-08-25 01:11:46 -07:00
|
|
|
if r.inBounds(tr.Mint) && r.inBounds(tr.Maxt) {
|
2017-05-17 02:19:42 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
// Intervals represents a set of increasing and non-overlapping time-intervals.
|
|
|
|
type Intervals []Interval
|
2017-05-22 04:12:36 -07:00
|
|
|
|
2017-05-17 02:19:42 -07:00
|
|
|
// This adds the new time-range to the existing ones.
|
2017-05-21 10:50:05 -07:00
|
|
|
// The existing ones must be sorted.
|
2017-08-25 01:11:46 -07:00
|
|
|
func (itvs Intervals) add(n Interval) Intervals {
|
2017-05-22 04:12:36 -07:00
|
|
|
for i, r := range itvs {
|
2017-05-17 02:19:42 -07:00
|
|
|
// TODO(gouthamve): Make this codepath easier to digest.
|
2017-08-25 01:11:46 -07:00
|
|
|
if r.inBounds(n.Mint-1) || r.inBounds(n.Mint) {
|
|
|
|
if n.Maxt > r.Maxt {
|
|
|
|
itvs[i].Maxt = n.Maxt
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
j := 0
|
2017-05-22 04:12:36 -07:00
|
|
|
for _, r2 := range itvs[i+1:] {
|
2017-08-25 01:11:46 -07:00
|
|
|
if n.Maxt < r2.Mint {
|
2017-05-17 02:19:42 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 0 {
|
2017-08-25 01:11:46 -07:00
|
|
|
if itvs[i+j].Maxt > n.Maxt {
|
|
|
|
itvs[i].Maxt = itvs[i+j].Maxt
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
itvs = append(itvs[:i+1], itvs[i+j+1:]...)
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
return itvs
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
if r.inBounds(n.Maxt+1) || r.inBounds(n.Maxt) {
|
|
|
|
if n.Mint < r.Maxt {
|
|
|
|
itvs[i].Mint = n.Mint
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
return itvs
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
2017-05-22 04:12:36 -07:00
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
if n.Mint < r.Mint {
|
|
|
|
newRange := make(Intervals, i, len(itvs[:i])+1)
|
2017-05-22 04:12:36 -07:00
|
|
|
copy(newRange, itvs[:i])
|
2017-05-17 02:19:42 -07:00
|
|
|
newRange = append(newRange, n)
|
2017-05-22 04:12:36 -07:00
|
|
|
newRange = append(newRange, itvs[i:]...)
|
2017-05-17 02:19:42 -07:00
|
|
|
|
|
|
|
return newRange
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 04:12:36 -07:00
|
|
|
itvs = append(itvs, n)
|
|
|
|
return itvs
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|