prometheus/tsdb/tsdbblockutil.go
Bartlomiej Plotka 34426766d8 Unify Iterator interfaces. All point to storage now.
This is part of https://github.com/prometheus/prometheus/pull/5882 that can be done to simplify things.
All todos I added will be fixed in follow up PRs.

* querier.Querier, querier.Appender, querier.SeriesSet, and querier.Series interfaces merged
with storage interface.go. All imports that.
* querier.SeriesIterator replaced by chunkenc.Iterator
* Added chunkenc.Iterator.Seek method and tests for xor implementation (?)
* Since we properly handle SelectParams for Select methods I adjusted min max
based on that. This should help in terms of performance for queries with functions like offset.
* added Seek to deletedIterator and test.
* storage/tsdb was removed as it was only a unnecessary glue with incompatible structs.

No logic was changed, only different source of abstractions, so no need for benchmarks.

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
2020-02-17 18:03:54 +00:00

85 lines
2.2 KiB
Go

// 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"
"os"
"path/filepath"
"github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/pkg/labels"
)
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, DefaultStripeSize)
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, ExponentialBlockRanges(DefaultBlockDuration, 3, 5), 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
}