mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
use a db copy instead of creating it again.
Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
parent
8ffd705346
commit
1b0d85bbf2
124
compact_test.go
124
compact_test.go
|
@ -15,7 +15,6 @@ package tsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
@ -28,6 +27,7 @@ import (
|
||||||
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
|
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/tsdb/chunks"
|
"github.com/prometheus/tsdb/chunks"
|
||||||
|
"github.com/prometheus/tsdb/fileutil"
|
||||||
"github.com/prometheus/tsdb/labels"
|
"github.com/prometheus/tsdb/labels"
|
||||||
"github.com/prometheus/tsdb/testutil"
|
"github.com/prometheus/tsdb/testutil"
|
||||||
)
|
)
|
||||||
|
@ -748,70 +748,76 @@ func TestDisableAutoCompactions(t *testing.T) {
|
||||||
// TestCancelCompactions ensures that when the db is closed
|
// TestCancelCompactions ensures that when the db is closed
|
||||||
// any running compaction is cancelled to unblock closing the db.
|
// any running compaction is cancelled to unblock closing the db.
|
||||||
func TestCancelCompactions(t *testing.T) {
|
func TestCancelCompactions(t *testing.T) {
|
||||||
createTestDb := func() (*DB, func()) {
|
tmpdir, err := ioutil.TempDir("", "testCancelCompaction")
|
||||||
tmpdir, err := ioutil.TempDir("", "testCancelCompaction")
|
testutil.Ok(t, err)
|
||||||
testutil.Ok(t, err)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
// Create some blocks to fall within the compaction range.
|
// Create some blocks to fall within the compaction range.
|
||||||
createBlock(t, tmpdir, 10000, 0, 1000)
|
createBlock(t, tmpdir, 7000, 0, 1000)
|
||||||
createBlock(t, tmpdir, 10000, 1000, 2000)
|
createBlock(t, tmpdir, 7000, 1000, 2000)
|
||||||
createBlock(t, tmpdir, 1, 2000, 2001) // The most recent block is ignored so can be e small one.
|
createBlock(t, tmpdir, 1, 2000, 2001) // The most recent block is ignored so can be e small one.
|
||||||
db, err := Open(tmpdir, log.NewNopLogger(), nil, &Options{BlockRanges: []int64{1, 2000}})
|
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch")
|
|
||||||
testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch")
|
|
||||||
|
|
||||||
return db, func() {
|
// Copy the db so we have an exact copy to compare compaction times.
|
||||||
os.RemoveAll(tmpdir)
|
tmpdirCopy := tmpdir + "Copy"
|
||||||
}
|
err = fileutil.CopyDirs(tmpdir, tmpdirCopy)
|
||||||
}
|
testutil.Ok(t, err)
|
||||||
// First lets mesure the compaction time without interupting it.
|
defer os.RemoveAll(tmpdirCopy)
|
||||||
|
|
||||||
|
// Measure the compaction time without interupting it.
|
||||||
var timeCompactionUninterrupted time.Duration
|
var timeCompactionUninterrupted time.Duration
|
||||||
{
|
{
|
||||||
db, delete := createTestDb()
|
db, err := Open(tmpdir, log.NewNopLogger(), nil, &Options{BlockRanges: []int64{1, 2000}})
|
||||||
defer delete()
|
testutil.Ok(t, err)
|
||||||
db.compactc <- struct{}{} // Trigger a compaction.
|
db.DisableCompactions()
|
||||||
var start time.Time
|
testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch")
|
||||||
for {
|
testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch")
|
||||||
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
|
go func() {
|
||||||
start = time.Now()
|
var start time.Time
|
||||||
break
|
for {
|
||||||
}
|
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
|
||||||
time.Sleep(3 * time.Millisecond)
|
start = time.Now()
|
||||||
}
|
break
|
||||||
|
}
|
||||||
for {
|
|
||||||
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran) == 1 {
|
|
||||||
timeCompactionUninterrupted = time.Since(start)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
time.Sleep(3 * time.Millisecond)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Closing the db in the middle of compaction should take half the time.
|
|
||||||
{
|
|
||||||
db, delete := createTestDb()
|
|
||||||
defer delete()
|
|
||||||
|
|
||||||
db.compactc <- struct{}{} // Trigger a compaction.
|
|
||||||
dbClosed := make(chan struct{})
|
|
||||||
for {
|
|
||||||
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
|
|
||||||
time.Sleep(3 * time.Millisecond)
|
time.Sleep(3 * time.Millisecond)
|
||||||
go func() {
|
|
||||||
testutil.Ok(t, db.Close())
|
|
||||||
close(dbClosed)
|
|
||||||
}()
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
start := time.Now()
|
for {
|
||||||
<-dbClosed
|
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran) == 1 {
|
||||||
actT := time.Since(start)
|
timeCompactionUninterrupted = time.Since(start)
|
||||||
fmt.Println(timeCompactionUninterrupted)
|
break
|
||||||
fmt.Println(actT)
|
}
|
||||||
expT := time.Duration(timeCompactionUninterrupted / 2)
|
time.Sleep(3 * time.Millisecond)
|
||||||
testutil.Assert(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT)
|
}
|
||||||
|
testutil.Ok(t, db.Close())
|
||||||
|
}()
|
||||||
|
db.compact()
|
||||||
|
}
|
||||||
|
// Measure the compaction time when closing the db in the middle of compaction.
|
||||||
|
{
|
||||||
|
db, err := Open(tmpdirCopy, log.NewNopLogger(), nil, &Options{BlockRanges: []int64{1, 2000}})
|
||||||
|
testutil.Ok(t, err)
|
||||||
|
db.DisableCompactions()
|
||||||
|
testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch")
|
||||||
|
testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch")
|
||||||
|
go func() {
|
||||||
|
dbClosed := make(chan struct{})
|
||||||
|
for {
|
||||||
|
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
|
||||||
|
time.Sleep(3 * time.Millisecond)
|
||||||
|
go func() {
|
||||||
|
testutil.Ok(t, db.Close())
|
||||||
|
close(dbClosed)
|
||||||
|
}()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
<-dbClosed
|
||||||
|
actT := time.Since(start)
|
||||||
|
expT := time.Duration(timeCompactionUninterrupted / 2) // Closing the db in the middle of compaction should less than half the time.
|
||||||
|
testutil.Assert(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT)
|
||||||
|
}()
|
||||||
|
db.compact()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue