Added CreateBlock and CreateHead functions to new file (#6331)

* Added CreateBlock and CreateHead functions to new file to make it reusable across packages.

Signed-off-by: Dipack P Panjabi <dipack.panjabi@gmail.com>
This commit is contained in:
Dipack P Panjabi 2019-11-21 07:10:25 -05:00 committed by Krasimir Georgiev
parent 7bf17b654c
commit e2dd5b61ef
2 changed files with 89 additions and 1 deletions

View file

@ -43,12 +43,17 @@ import (
"golang.org/x/sync/errgroup"
)
// Default duration of a block in milliseconds - 2h.
const (
DefaultBlockDuration = int64(2 * 60 * 60 * 1000)
)
// DefaultOptions used for the DB. They are sane for setups using
// millisecond precision timestamps.
var DefaultOptions = &Options{
WALSegmentSize: wal.DefaultSegmentSize,
RetentionDuration: 15 * 24 * 60 * 60 * 1000, // 15 days in milliseconds
BlockRanges: ExponentialBlockRanges(int64(2*time.Hour)/1e6, 3, 5),
BlockRanges: ExponentialBlockRanges(DefaultBlockDuration, 3, 5),
NoLockfile: false,
AllowOverlappingBlocks: false,
WALCompression: false,

83
tsdb/tsdbblockutil.go Normal file
View file

@ -0,0 +1,83 @@
// Copyright 2019 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 (
"context"
"fmt"
"github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/pkg/labels"
"os"
"path/filepath"
)
var InvalidTimesError = fmt.Errorf("max time is lesser than min time")
type MetricSample struct {
TimestampMs int64
Value float64
Labels labels.Labels
}
// CreateHead creates a TSDB writer head to write the sample data to.
func CreateHead(samples []*MetricSample, chunkRange int64, logger log.Logger) (*Head, error) {
head, err := NewHead(nil, logger, nil, chunkRange)
if err != nil {
return nil, err
}
app := head.Appender()
for _, sample := range samples {
_, err = app.Add(sample.Labels, sample.TimestampMs, sample.Value)
if err != nil {
return nil, err
}
}
err = app.Commit()
if err != nil {
return nil, err
}
return head, nil
}
// CreateBlock creates a chunkrange block from the samples passed to it, and writes it to disk.
func CreateBlock(samples []*MetricSample, dir string, mint, maxt int64, logger log.Logger) (string, error) {
chunkRange := maxt - mint
if chunkRange == 0 {
chunkRange = DefaultBlockDuration
}
if chunkRange < 0 {
return "", InvalidTimesError
}
head, err := CreateHead(samples, chunkRange, logger)
if err != nil {
return "", err
}
compactor, err := NewLeveledCompactor(context.Background(), nil, logger, DefaultOptions.BlockRanges, nil)
if err != nil {
return "", err
}
err = os.MkdirAll(dir, 0777)
if err != nil {
return "", err
}
ulid, err := compactor.Write(dir, head, mint, maxt, nil)
if err != nil {
return "", err
}
return filepath.Join(dir, ulid.String()), nil
}