@ -25,6 +25,8 @@ import (
"testing"
"time"
"github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/go-kit/kit/log"
"github.com/oklog/ulid"
"github.com/pkg/errors"
@ -1111,11 +1113,49 @@ func TestSizeRetention(t *testing.T) {
createBlock ( t , db . Dir ( ) , genSeries ( 100 , 10 , m . MinTime , m . MaxTime ) )
}
headBlocks := [ ] * BlockMeta {
{ MinTime : 700 , MaxTime : 800 } ,
}
// Add some data to the WAL.
headApp := db . Head ( ) . Appender ( )
for _ , m := range headBlocks {
series := genSeries ( 100 , 10 , m . MinTime , m . MaxTime )
for _ , s := range series {
it := s . Iterator ( )
for it . Next ( ) {
tim , v := it . At ( )
_ , err := headApp . Add ( s . Labels ( ) , tim , v )
testutil . Ok ( t , err )
}
testutil . Ok ( t , it . Err ( ) )
}
}
testutil . Ok ( t , headApp . Commit ( ) )
// Test that registered size matches the actual disk size.
testutil . Ok ( t , db . reload ( ) ) // Reload the db to register the new db size.
testutil . Equals ( t , len ( blocks ) , len ( db . Blocks ( ) ) ) // Ensure all blocks are registered.
expSize := int64 ( prom_testutil . ToFloat64 ( db . metrics . blocksBytes ) ) // Use the actual internal metrics.
actSize := testutil . DirSize ( t , db . Dir ( ) )
testutil . Ok ( t , db . reload ( ) ) // Reload the db to register the new db size.
testutil . Equals ( t , len ( blocks ) , len ( db . Blocks ( ) ) ) // Ensure all blocks are registered.
blockSize := int64 ( prom_testutil . ToFloat64 ( db . metrics . blocksBytes ) ) // Use the the actual internal metrics.
walSize , err := db . Head ( ) . wal . Size ( )
testutil . Ok ( t , err )
// Expected size should take into account block size + WAL size
expSize := blockSize + walSize
actSize , err := fileutil . DirSize ( db . Dir ( ) )
testutil . Ok ( t , err )
testutil . Equals ( t , expSize , actSize , "registered size doesn't match actual disk size" )
// Create a WAL checkpoint, and compare sizes.
first , last , err := db . Head ( ) . wal . Segments ( )
testutil . Ok ( t , err )
_ , err = wal . Checkpoint ( db . Head ( ) . wal , first , last - 1 , func ( x uint64 ) bool { return false } , 0 )
testutil . Ok ( t , err )
blockSize = int64 ( prom_testutil . ToFloat64 ( db . metrics . blocksBytes ) ) // Use the the actual internal metrics.
walSize , err = db . Head ( ) . wal . Size ( )
testutil . Ok ( t , err )
expSize = blockSize + walSize
actSize , err = fileutil . DirSize ( db . Dir ( ) )
testutil . Ok ( t , err )
testutil . Equals ( t , expSize , actSize , "registered size doesn't match actual disk size" )
// Decrease the max bytes limit so that a delete is triggered.
@ -1127,9 +1167,14 @@ func TestSizeRetention(t *testing.T) {
expBlocks := blocks [ 1 : ]
actBlocks := db . Blocks ( )
expSize = int64 ( prom_testutil . ToFloat64 ( db . metrics . blocksBytes ) )
blockSize = int64 ( prom_testutil . ToFloat64 ( db . metrics . blocksBytes ) )
walSize , err = db . Head ( ) . wal . Size ( )
testutil . Ok ( t , err )
// Expected size should take into account block size + WAL size
expSize = blockSize + walSize
actRetentCount := int ( prom_testutil . ToFloat64 ( db . metrics . sizeRetentionCount ) )
actSize = testutil . DirSize ( t , db . Dir ( ) )
actSize , err = fileutil . DirSize ( db . Dir ( ) )
testutil . Ok ( t , err )
testutil . Equals ( t , 1 , actRetentCount , "metric retention count mismatch" )
testutil . Equals ( t , actSize , expSize , "metric db size doesn't match actual disk size" )
@ -1137,7 +1182,6 @@ func TestSizeRetention(t *testing.T) {
testutil . Equals ( t , len ( blocks ) - 1 , len ( actBlocks ) , "new block count should be decreased from:%v to:%v" , len ( blocks ) , len ( blocks ) - 1 )
testutil . Equals ( t , expBlocks [ 0 ] . MaxTime , actBlocks [ 0 ] . meta . MaxTime , "maxT mismatch of the first block" )
testutil . Equals ( t , expBlocks [ len ( expBlocks ) - 1 ] . MaxTime , actBlocks [ len ( actBlocks ) - 1 ] . meta . MaxTime , "maxT mismatch of the last block" )
}
func TestSizeRetentionMetric ( t * testing . T ) {
@ -2298,7 +2342,8 @@ func TestDBReadOnly(t *testing.T) {
testutil . Ok ( t , err )
dbWritable . DisableCompactions ( )
dbSizeBeforeAppend := testutil . DirSize ( t , dbWritable . Dir ( ) )
dbSizeBeforeAppend , err := fileutil . DirSize ( dbWritable . Dir ( ) )
testutil . Ok ( t , err )
app := dbWritable . Appender ( )
_ , err = app . Add ( labels . FromStrings ( "foo" , "bar" ) , dbWritable . Head ( ) . MaxTime ( ) + 1 , 0 )
testutil . Ok ( t , err )
@ -2306,7 +2351,8 @@ func TestDBReadOnly(t *testing.T) {
expSeriesCount ++
expBlocks = dbWritable . Blocks ( )
expDbSize := testutil . DirSize ( t , dbWritable . Dir ( ) )
expDbSize , err := fileutil . DirSize ( dbWritable . Dir ( ) )
testutil . Ok ( t , err )
testutil . Assert ( t , expDbSize > dbSizeBeforeAppend , "db size didn't increase after an append" )
q , err := dbWritable . Querier ( math . MinInt64 , math . MaxInt64 )