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"
|
|
|
|
"hash/crc32"
|
2017-05-24 08:12:56 +00:00
|
|
|
"io"
|
2017-05-17 03:06:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-24 08:12:56 +00:00
|
|
|
|
|
|
|
"github.com/pkg/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.
|
|
|
|
MagicTombstone = 0x130BA30
|
|
|
|
|
|
|
|
tombstoneFormatV1 = 1
|
|
|
|
)
|
|
|
|
|
2017-05-24 05:54: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-05-23 11:57:45 +00:00
|
|
|
hash := crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
2017-05-17 03:06:56 +00:00
|
|
|
|
|
|
|
f, err := os.Create(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 08:12:56 +00:00
|
|
|
defer f.Close()
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
buf := encbuf{b: make([]byte, 3*binary.MaxVarintLen64)}
|
2017-05-23 11:57:45 +00:00
|
|
|
buf.reset()
|
|
|
|
// Write the meta.
|
|
|
|
buf.putBE32(MagicTombstone)
|
|
|
|
buf.putByte(tombstoneFormatV1)
|
2017-05-24 08:12:56 +00:00
|
|
|
_, 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
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
for k, v := range tr {
|
|
|
|
for _, itv := range v {
|
|
|
|
buf.reset()
|
|
|
|
buf.putUvarint32(k)
|
|
|
|
buf.putVarint64(itv.mint)
|
|
|
|
buf.putVarint64(itv.maxt)
|
|
|
|
|
|
|
|
_, err = mw.Write(buf.get())
|
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-22 11:12:36 +00:00
|
|
|
ref uint32
|
|
|
|
intervals intervals
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TombstoneReader is the iterator over tombstones.
|
|
|
|
type TombstoneReader interface {
|
2017-05-26 15:56:31 +00:00
|
|
|
Get(ref uint32) intervals
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 05:54:24 +00:00
|
|
|
func readTombstones(dir string) (tombstoneReader, error) {
|
2017-05-17 03:06:56 +00:00
|
|
|
b, err := ioutil.ReadFile(filepath.Join(dir, tombstoneFilename))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
if len(b) < 5 {
|
|
|
|
return nil, errors.Wrap(errInvalidSize, "tombstones header")
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
d := &decbuf{b: b[:len(b)-4]} // 4 for the checksum.
|
2017-05-23 11:57:45 +00:00
|
|
|
if mg := d.be32(); mg != MagicTombstone {
|
|
|
|
return nil, fmt.Errorf("invalid magic number %x", mg)
|
|
|
|
}
|
2017-05-24 08:12:56 +00:00
|
|
|
if flag := d.byte(); flag != tombstoneFormatV1 {
|
|
|
|
return nil, fmt.Errorf("invalid tombstone format %x", flag)
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
if d.err() != nil {
|
|
|
|
return nil, d.err()
|
|
|
|
}
|
|
|
|
|
2017-05-24 08:12:56 +00:00
|
|
|
// Verify checksum
|
|
|
|
hash := crc32.New(crc32.MakeTable(crc32.Castagnoli))
|
|
|
|
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-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
stonesMap := newEmptyTombstoneReader()
|
2017-05-24 08:12:56 +00:00
|
|
|
for d.len() > 0 {
|
|
|
|
k := d.uvarint32()
|
|
|
|
mint := d.varint64()
|
|
|
|
maxt := d.varint64()
|
|
|
|
if d.err() != nil {
|
|
|
|
return nil, d.err()
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-05-22 05:01:50 +00:00
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
stonesMap.add(k, interval{mint, maxt})
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 05:54:24 +00:00
|
|
|
return newTombstoneReader(stonesMap), nil
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 05:54:24 +00:00
|
|
|
type tombstoneReader map[uint32]intervals
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2017-05-24 05:54:24 +00:00
|
|
|
func newTombstoneReader(ts map[uint32]intervals) tombstoneReader {
|
|
|
|
return tombstoneReader(ts)
|
|
|
|
}
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2017-05-24 05:54:24 +00:00
|
|
|
func newEmptyTombstoneReader() tombstoneReader {
|
|
|
|
return tombstoneReader(make(map[uint32]intervals))
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
func (t tombstoneReader) Get(ref uint32) intervals {
|
2017-05-24 05:54:24 +00:00
|
|
|
return t[ref]
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2017-05-26 15:56:31 +00:00
|
|
|
func (t tombstoneReader) add(ref uint32, itv interval) {
|
|
|
|
t[ref] = t[ref].add(itv)
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:12:36 +00:00
|
|
|
type interval struct {
|
2017-05-17 09:19:42 +00:00
|
|
|
mint, maxt int64
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:12:36 +00:00
|
|
|
func (tr interval) inBounds(t int64) bool {
|
2017-05-17 09:19:42 +00:00
|
|
|
return t >= tr.mint && t <= tr.maxt
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:12:36 +00:00
|
|
|
func (tr interval) isSubrange(dranges intervals) bool {
|
|
|
|
for _, r := range dranges {
|
2017-05-17 09:19:42 +00:00
|
|
|
if r.inBounds(tr.mint) && r.inBounds(tr.maxt) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:12:36 +00:00
|
|
|
type intervals []interval
|
|
|
|
|
2017-05-17 09:19:42 +00:00
|
|
|
// This adds the new time-range to the existing ones.
|
2017-05-21 17:50:05 +00:00
|
|
|
// The existing ones must be sorted.
|
2017-05-22 11:12:36 +00:00
|
|
|
func (itvs intervals) add(n interval) intervals {
|
|
|
|
for i, r := range itvs {
|
2017-05-17 09:19:42 +00:00
|
|
|
// TODO(gouthamve): Make this codepath easier to digest.
|
2017-05-22 11:12:36 +00:00
|
|
|
if r.inBounds(n.mint-1) || r.inBounds(n.mint) {
|
2017-05-17 09:19:42 +00:00
|
|
|
if n.maxt > r.maxt {
|
2017-05-22 11:12:36 +00:00
|
|
|
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-05-17 09:19:42 +00:00
|
|
|
if n.maxt < r2.mint {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j != 0 {
|
2017-05-22 11:12:36 +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-05-22 11:12:36 +00:00
|
|
|
if r.inBounds(n.maxt+1) || r.inBounds(n.maxt) {
|
2017-05-17 09:19:42 +00:00
|
|
|
if n.mint < r.maxt {
|
2017-05-22 11:12:36 +00:00
|
|
|
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-05-17 09:19:42 +00:00
|
|
|
if n.mint < r.mint {
|
2017-05-22 11:12:36 +00:00
|
|
|
newRange := make(intervals, i, len(itvs[:i])+1)
|
|
|
|
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
|
|
|
}
|