2020-11-25 21:07:06 -08:00
|
|
|
// Copyright 2020 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"math"
|
2021-06-29 02:23:38 -07:00
|
|
|
"time"
|
2020-11-25 21:07:06 -08:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2020-11-25 21:07:06 -08:00
|
|
|
"github.com/pkg/errors"
|
2021-10-22 01:19:38 -07:00
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
|
|
|
"github.com/prometheus/prometheus/model/textparse"
|
2020-11-25 21:07:06 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb"
|
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getMinAndMaxTimestamps(p textparse.Parser) (int64, int64, error) {
|
|
|
|
var maxt, mint int64 = math.MinInt64, math.MaxInt64
|
|
|
|
|
|
|
|
for {
|
|
|
|
entry, err := p.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, errors.Wrap(err, "next")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry != textparse.EntrySeries {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ts, _ := p.Series()
|
|
|
|
if ts == nil {
|
|
|
|
return 0, 0, errors.Errorf("expected timestamp for series got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
if *ts > maxt {
|
|
|
|
maxt = *ts
|
|
|
|
}
|
|
|
|
if *ts < mint {
|
|
|
|
mint = *ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxt == math.MinInt64 {
|
|
|
|
maxt = 0
|
|
|
|
}
|
|
|
|
if mint == math.MaxInt64 {
|
|
|
|
mint = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxt, mint, nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:28:37 -07:00
|
|
|
func getCompatibleBlockDuration(maxBlockDuration int64) int64 {
|
2020-11-25 21:07:06 -08:00
|
|
|
blockDuration := tsdb.DefaultBlockDuration
|
2021-06-29 02:23:38 -07:00
|
|
|
if maxBlockDuration > tsdb.DefaultBlockDuration {
|
|
|
|
ranges := tsdb.ExponentialBlockRanges(tsdb.DefaultBlockDuration, 10, 3)
|
|
|
|
idx := len(ranges) - 1 // Use largest range if user asked for something enormous.
|
|
|
|
for i, v := range ranges {
|
|
|
|
if v > maxBlockDuration {
|
|
|
|
idx = i - 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blockDuration = ranges[idx]
|
|
|
|
}
|
2021-10-21 14:28:37 -07:00
|
|
|
return blockDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBlocks(input []byte, mint, maxt, maxBlockDuration int64, maxSamplesInAppender int, outputDir string, humanReadable, quiet bool) (returnErr error) {
|
|
|
|
blockDuration := getCompatibleBlockDuration(maxBlockDuration)
|
2020-11-25 21:07:06 -08:00
|
|
|
mint = blockDuration * (mint / blockDuration)
|
|
|
|
|
|
|
|
db, err := tsdb.OpenDBReadOnly(outputDir, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
returnErr = tsdb_errors.NewMulti(returnErr, db.Close()).Err()
|
|
|
|
}()
|
|
|
|
|
2020-12-29 17:06:41 -08:00
|
|
|
var (
|
|
|
|
wroteHeader bool
|
|
|
|
nextSampleTs int64 = math.MaxInt64
|
|
|
|
)
|
2020-12-09 13:58:23 -08:00
|
|
|
|
2020-11-25 21:07:06 -08:00
|
|
|
for t := mint; t <= maxt; t = t + blockDuration {
|
2020-12-29 17:06:41 -08:00
|
|
|
tsUpper := t + blockDuration
|
|
|
|
if nextSampleTs != math.MaxInt64 && nextSampleTs >= tsUpper {
|
|
|
|
// The next sample is not in this timerange, we can avoid parsing
|
|
|
|
// the file for this timerange.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nextSampleTs = math.MaxInt64
|
|
|
|
|
2020-11-25 21:07:06 -08:00
|
|
|
err := func() error {
|
2021-02-16 00:06:02 -08:00
|
|
|
// To prevent races with compaction, a block writer only allows appending samples
|
|
|
|
// that are at most half a block size older than the most recent sample appended so far.
|
|
|
|
// However, in the way we use the block writer here, compaction doesn't happen, while we
|
|
|
|
// also need to append samples throughout the whole block range. To allow that, we
|
|
|
|
// pretend that the block is twice as large here, but only really add sample in the
|
|
|
|
// original interval later.
|
|
|
|
w, err := tsdb.NewBlockWriter(log.NewNopLogger(), outputDir, 2*blockDuration)
|
2020-11-25 21:07:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "block writer")
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = tsdb_errors.NewMulti(err, w.Close()).Err()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
app := w.Appender(ctx)
|
2020-12-08 12:55:45 -08:00
|
|
|
p := textparse.NewOpenMetricsParser(input)
|
2020-11-25 21:07:06 -08:00
|
|
|
samplesCount := 0
|
|
|
|
for {
|
|
|
|
e, err := p.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parse")
|
|
|
|
}
|
|
|
|
if e != textparse.EntrySeries {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ts, v := p.Series()
|
|
|
|
if ts == nil {
|
2020-12-29 17:06:41 -08:00
|
|
|
l := labels.Labels{}
|
|
|
|
p.Metric(&l)
|
2020-11-25 21:07:06 -08:00
|
|
|
return errors.Errorf("expected timestamp for series %v, got none", l)
|
|
|
|
}
|
2020-12-29 17:06:41 -08:00
|
|
|
if *ts < t {
|
2020-11-25 21:07:06 -08:00
|
|
|
continue
|
|
|
|
}
|
2020-12-29 17:06:41 -08:00
|
|
|
if *ts >= tsUpper {
|
|
|
|
if *ts < nextSampleTs {
|
|
|
|
nextSampleTs = *ts
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
l := labels.Labels{}
|
|
|
|
p.Metric(&l)
|
2020-11-25 21:07:06 -08:00
|
|
|
|
2021-02-18 04:07:00 -08:00
|
|
|
if _, err := app.Append(0, l, *ts, v); err != nil {
|
2020-11-25 21:07:06 -08:00
|
|
|
return errors.Wrap(err, "add sample")
|
|
|
|
}
|
|
|
|
|
|
|
|
samplesCount++
|
|
|
|
if samplesCount < maxSamplesInAppender {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we arrive here, the samples count is greater than the maxSamplesInAppender.
|
|
|
|
// Therefore the old appender is committed and a new one is created.
|
|
|
|
// This prevents keeping too many samples lined up in an appender and thus in RAM.
|
|
|
|
if err := app.Commit(); err != nil {
|
|
|
|
return errors.Wrap(err, "commit")
|
|
|
|
}
|
|
|
|
|
|
|
|
app = w.Appender(ctx)
|
|
|
|
samplesCount = 0
|
|
|
|
}
|
2020-12-09 13:58:23 -08:00
|
|
|
|
2020-11-25 21:07:06 -08:00
|
|
|
if err := app.Commit(); err != nil {
|
|
|
|
return errors.Wrap(err, "commit")
|
|
|
|
}
|
2020-12-09 13:58:23 -08:00
|
|
|
|
|
|
|
block, err := w.Flush(ctx)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2021-06-10 10:34:07 -07:00
|
|
|
if quiet {
|
|
|
|
break
|
|
|
|
}
|
2020-12-09 13:58:23 -08:00
|
|
|
blocks, err := db.Blocks()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "get blocks")
|
|
|
|
}
|
|
|
|
for _, b := range blocks {
|
|
|
|
if b.Meta().ULID == block {
|
|
|
|
printBlocks([]tsdb.BlockReader{b}, !wroteHeader, humanReadable)
|
|
|
|
wroteHeader = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tsdb.ErrNoSeriesAppended:
|
|
|
|
default:
|
2020-11-25 21:07:06 -08:00
|
|
|
return errors.Wrap(err, "flush")
|
|
|
|
}
|
2020-12-09 13:58:23 -08:00
|
|
|
|
2020-11-25 21:07:06 -08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "process blocks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-29 02:23:38 -07:00
|
|
|
func backfill(maxSamplesInAppender int, input []byte, outputDir string, humanReadable, quiet bool, maxBlockDuration time.Duration) (err error) {
|
2020-12-08 12:55:45 -08:00
|
|
|
p := textparse.NewOpenMetricsParser(input)
|
2020-11-25 21:07:06 -08:00
|
|
|
maxt, mint, err := getMinAndMaxTimestamps(p)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting min and max timestamp")
|
|
|
|
}
|
2021-06-29 02:23:38 -07:00
|
|
|
return errors.Wrap(createBlocks(input, mint, maxt, int64(maxBlockDuration/time.Millisecond), maxSamplesInAppender, outputDir, humanReadable, quiet), "block creation")
|
2020-11-25 21:07:06 -08:00
|
|
|
}
|