rename createPopulatedBlock to createBlock and use it instead ot createEmptyBlock (#488)

Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
Krasi Georgiev 2018-12-29 14:20:51 +03:00 committed by GitHub
parent a90a7195d4
commit eb6586f513
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 75 deletions

View file

@ -21,7 +21,6 @@ import (
"testing" "testing"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/tsdb/index"
"github.com/prometheus/tsdb/labels" "github.com/prometheus/tsdb/labels"
"github.com/prometheus/tsdb/testutil" "github.com/prometheus/tsdb/testutil"
) )
@ -46,60 +45,40 @@ func TestSetCompactionFailed(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
defer os.RemoveAll(tmpdir) defer os.RemoveAll(tmpdir)
b := createEmptyBlock(t, tmpdir, &BlockMeta{Version: 2}) blockDir := createBlock(t, tmpdir, 0, 0, 0)
b, err := OpenBlock(blockDir, nil)
testutil.Ok(t, err)
testutil.Equals(t, false, b.meta.Compaction.Failed) testutil.Equals(t, false, b.meta.Compaction.Failed)
testutil.Ok(t, b.setCompactionFailed()) testutil.Ok(t, b.setCompactionFailed())
testutil.Equals(t, true, b.meta.Compaction.Failed) testutil.Equals(t, true, b.meta.Compaction.Failed)
testutil.Ok(t, b.Close()) testutil.Ok(t, b.Close())
b, err = OpenBlock(tmpdir, nil) b, err = OpenBlock(blockDir, nil)
testutil.Ok(t, err) testutil.Ok(t, err)
testutil.Equals(t, true, b.meta.Compaction.Failed) testutil.Equals(t, true, b.meta.Compaction.Failed)
testutil.Ok(t, b.Close())
} }
// createEmpty block creates a block with the given meta but without any data. // createBlock creates a block with nSeries series, filled with
func createEmptyBlock(t *testing.T, dir string, meta *BlockMeta) *Block {
testutil.Ok(t, os.MkdirAll(dir, 0777))
testutil.Ok(t, writeMetaFile(dir, meta))
ir, err := index.NewWriter(filepath.Join(dir, indexFilename))
testutil.Ok(t, err)
testutil.Ok(t, ir.Close())
testutil.Ok(t, os.MkdirAll(chunkDir(dir), 0777))
testutil.Ok(t, writeTombstoneFile(dir, newMemTombstones()))
b, err := OpenBlock(dir, nil)
testutil.Ok(t, err)
return b
}
// createPopulatedBlock creates a block with nSeries series, filled with
// samples of the given mint,maxt time range and returns its dir. // samples of the given mint,maxt time range and returns its dir.
func createPopulatedBlock(tb testing.TB, dir string, nSeries int, mint, maxt int64) string { func createBlock(tb testing.TB, dir string, nSeries int, mint, maxt int64) string {
head, err := NewHead(nil, nil, nil, 2*60*60*1000) head, err := NewHead(nil, nil, nil, 2*60*60*1000)
testutil.Ok(tb, err) testutil.Ok(tb, err)
defer head.Close() defer head.Close()
lbls, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), nSeries) lbls, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), nSeries)
testutil.Ok(tb, err) testutil.Ok(tb, err)
refs := make([]uint64, nSeries) var ref uint64
for ts := mint; ts <= maxt; ts++ { for ts := mint; ts <= maxt; ts++ {
app := head.Appender() app := head.Appender()
for i, lbl := range lbls { for _, lbl := range lbls {
if refs[i] != 0 { err := app.AddFast(ref, ts, rand.Float64())
err := app.AddFast(refs[i], ts, rand.Float64()) if err == nil {
if err == nil { continue
continue
}
} }
ref, err := app.Add(lbl, int64(ts), rand.Float64()) ref, err = app.Add(lbl, int64(ts), rand.Float64())
testutil.Ok(tb, err) testutil.Ok(tb, err)
refs[i] = ref
} }
err := app.Commit() err := app.Commit()
testutil.Ok(tb, err) testutil.Ok(tb, err)

View file

@ -81,22 +81,24 @@ func TestDB_reloadOrder(t *testing.T) {
defer close() defer close()
defer db.Close() defer db.Close()
metas := []*BlockMeta{ metas := []BlockMeta{
{ULID: ulid.MustNew(100, nil), MinTime: 90, MaxTime: 100}, {MinTime: 90, MaxTime: 100},
{ULID: ulid.MustNew(200, nil), MinTime: 70, MaxTime: 80}, {MinTime: 70, MaxTime: 80},
{ULID: ulid.MustNew(300, nil), MinTime: 100, MaxTime: 110}, {MinTime: 100, MaxTime: 110},
} }
for _, m := range metas { for _, m := range metas {
bdir := filepath.Join(db.Dir(), m.ULID.String()) createBlock(t, db.Dir(), 1, m.MinTime, m.MaxTime)
createEmptyBlock(t, bdir, m)
} }
testutil.Ok(t, db.reload()) testutil.Ok(t, db.reload())
blocks := db.Blocks() blocks := db.Blocks()
testutil.Equals(t, 3, len(blocks)) testutil.Equals(t, 3, len(blocks))
testutil.Equals(t, *metas[1], blocks[0].Meta()) testutil.Equals(t, metas[1].MinTime, blocks[0].Meta().MinTime)
testutil.Equals(t, *metas[0], blocks[1].Meta()) testutil.Equals(t, metas[1].MaxTime, blocks[0].Meta().MaxTime)
testutil.Equals(t, *metas[2], blocks[2].Meta()) testutil.Equals(t, metas[0].MinTime, blocks[1].Meta().MinTime)
testutil.Equals(t, metas[0].MaxTime, blocks[1].Meta().MaxTime)
testutil.Equals(t, metas[2].MinTime, blocks[2].Meta().MinTime)
testutil.Equals(t, metas[2].MaxTime, blocks[2].Meta().MaxTime)
} }
func TestDataAvailableOnlyAfterCommit(t *testing.T) { func TestDataAvailableOnlyAfterCommit(t *testing.T) {
@ -822,6 +824,7 @@ func TestTombstoneClean(t *testing.T) {
func TestTombstoneCleanFail(t *testing.T) { func TestTombstoneCleanFail(t *testing.T) {
db, close := openTestDB(t, nil) db, close := openTestDB(t, nil)
defer db.Close()
defer close() defer close()
var expectedBlockDirs []string var expectedBlockDirs []string
@ -830,15 +833,9 @@ func TestTombstoneCleanFail(t *testing.T) {
// totalBlocks should be >=2 so we have enough blocks to trigger compaction failure. // totalBlocks should be >=2 so we have enough blocks to trigger compaction failure.
totalBlocks := 2 totalBlocks := 2
for i := 0; i < totalBlocks; i++ { for i := 0; i < totalBlocks; i++ {
entropy := rand.New(rand.NewSource(time.Now().UnixNano())) blockDir := createBlock(t, db.Dir(), 0, 0, 0)
uid := ulid.MustNew(ulid.Now(), entropy) block, err := OpenBlock(blockDir, nil)
meta := &BlockMeta{ testutil.Ok(t, err)
Version: 2,
ULID: uid,
}
blockDir := filepath.Join(db.Dir(), uid.String())
block := createEmptyBlock(t, blockDir, meta)
// Add some some fake tombstones to trigger the compaction. // Add some some fake tombstones to trigger the compaction.
tomb := newMemTombstones() tomb := newMemTombstones()
tomb.addInterval(0, Interval{0, 1}) tomb.addInterval(0, Interval{0, 1})
@ -880,14 +877,8 @@ func (c *mockCompactorFailing) Write(dest string, b BlockReader, mint, maxt int6
return ulid.ULID{}, fmt.Errorf("the compactor already did the maximum allowed blocks so it is time to fail") return ulid.ULID{}, fmt.Errorf("the compactor already did the maximum allowed blocks so it is time to fail")
} }
entropy := rand.New(rand.NewSource(time.Now().UnixNano())) block, err := OpenBlock(createBlock(c.t, dest, 0, 0, 0), nil)
uid := ulid.MustNew(ulid.Now(), entropy) testutil.Ok(c.t, err)
meta := &BlockMeta{
Version: 2,
ULID: uid,
}
block := createEmptyBlock(c.t, filepath.Join(dest, meta.ULID.String()), meta)
testutil.Ok(c.t, block.Close()) // Close block as we won't be using anywhere. testutil.Ok(c.t, block.Close()) // Close block as we won't be using anywhere.
c.blocks = append(c.blocks, block) c.blocks = append(c.blocks, block)
@ -1286,12 +1277,7 @@ func TestInitializeHeadTimestamp(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
id := ulid.MustNew(2000, nil) createBlock(t, dir, 1, 1000, 2000)
createEmptyBlock(t, path.Join(dir, id.String()), &BlockMeta{
ULID: id,
MinTime: 1000,
MaxTime: 2000,
})
db, err := Open(dir, nil, nil, nil) db, err := Open(dir, nil, nil, nil)
testutil.Ok(t, err) testutil.Ok(t, err)
@ -1304,12 +1290,7 @@ func TestInitializeHeadTimestamp(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
id := ulid.MustNew(2000, nil) createBlock(t, dir, 1, 1000, 6000)
createEmptyBlock(t, path.Join(dir, id.String()), &BlockMeta{
ULID: id,
MinTime: 1000,
MaxTime: 6000,
})
testutil.Ok(t, os.MkdirAll(path.Join(dir, "wal"), 0777)) testutil.Ok(t, os.MkdirAll(path.Join(dir, "wal"), 0777))
w, err := wal.New(nil, nil, path.Join(dir, "wal")) w, err := wal.New(nil, nil, path.Join(dir, "wal"))
@ -1496,7 +1477,7 @@ func TestBlockRanges(t *testing.T) {
// Test that the compactor doesn't create overlapping blocks // Test that the compactor doesn't create overlapping blocks
// when a non standard block already exists. // when a non standard block already exists.
firstBlockMaxT := int64(3) firstBlockMaxT := int64(3)
createPopulatedBlock(t, dir, 1, 0, firstBlockMaxT) createBlock(t, dir, 1, 0, firstBlockMaxT)
db, err := Open(dir, logger, nil, DefaultOptions) db, err := Open(dir, logger, nil, DefaultOptions)
if err != nil { if err != nil {
t.Fatalf("Opening test storage failed: %s", err) t.Fatalf("Opening test storage failed: %s", err)
@ -1545,7 +1526,7 @@ func TestBlockRanges(t *testing.T) {
testutil.Ok(t, db.Close()) testutil.Ok(t, db.Close())
thirdBlockMaxt := secondBlockMaxt + 2 thirdBlockMaxt := secondBlockMaxt + 2
createPopulatedBlock(t, dir, 1, secondBlockMaxt+1, thirdBlockMaxt) createBlock(t, dir, 1, secondBlockMaxt+1, thirdBlockMaxt)
db, err = Open(dir, logger, nil, DefaultOptions) db, err = Open(dir, logger, nil, DefaultOptions)
if err != nil { if err != nil {

View file

@ -1232,7 +1232,7 @@ func BenchmarkPersistedQueries(b *testing.B) {
dir, err := ioutil.TempDir("", "bench_persisted") dir, err := ioutil.TempDir("", "bench_persisted")
testutil.Ok(b, err) testutil.Ok(b, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
block, err := OpenBlock(createPopulatedBlock(b, dir, nSeries, 1, int64(nSamples)), nil) block, err := OpenBlock(createBlock(b, dir, nSeries, 1, int64(nSamples)), nil)
testutil.Ok(b, err) testutil.Ok(b, err)
defer block.Close() defer block.Close()