Skip to content

Commit

Permalink
refactor error handling for DirHash and DirSize
Browse files Browse the repository at this point in the history
Signed-off-by: Krasi Georgiev <8903888+krasi-georgiev@users.noreply.github.com>
  • Loading branch information
krasi-georgiev committed Jul 9, 2019
1 parent 32ae42e commit fb75682
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
8 changes: 3 additions & 5 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ func TestBlockSize(t *testing.T) {
testutil.Ok(t, blockInit.Close())
}()
expSizeInit = blockInit.Size()
actSizeInit, err := testutil.DirSize(blockInit.Dir())
testutil.Ok(t, err)
actSizeInit := testutil.DirSize(t, blockInit.Dir())
testutil.Equals(t, expSizeInit, actSizeInit)
}

Expand All @@ -185,7 +184,7 @@ func TestBlockSize(t *testing.T) {
testutil.Ok(t, blockInit.Delete(1, 10, labels.NewMustRegexpMatcher("", ".*")))
expAfterDelete := blockInit.Size()
testutil.Assert(t, expAfterDelete > expSizeInit, "after a delete the block size should be bigger as the tombstone file should grow %v > %v", expAfterDelete, expSizeInit)
actAfterDelete, err := testutil.DirSize(blockDirInit)
actAfterDelete := testutil.DirSize(t, blockDirInit)
testutil.Ok(t, err)
testutil.Equals(t, expAfterDelete, actAfterDelete, "after a delete reported block size doesn't match actual disk size")

Expand All @@ -199,8 +198,7 @@ func TestBlockSize(t *testing.T) {
testutil.Ok(t, blockAfterCompact.Close())
}()
expAfterCompact := blockAfterCompact.Size()
actAfterCompact, err := testutil.DirSize(blockAfterCompact.Dir())
testutil.Ok(t, err)
actAfterCompact := testutil.DirSize(t, blockAfterCompact.Dir())
testutil.Assert(t, actAfterDelete > actAfterCompact, "after a delete and compaction the block size should be smaller %v,%v", actAfterDelete, actAfterCompact)
testutil.Equals(t, expAfterCompact, actAfterCompact, "after a delete and compaction reported block size doesn't match actual disk size")
}
Expand Down
18 changes: 6 additions & 12 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,7 @@ func TestSizeRetention(t *testing.T) {
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 the actual internal metrics.
actSize, err := testutil.DirSize(db.Dir())
testutil.Ok(t, err)
actSize := testutil.DirSize(t, db.Dir())
testutil.Equals(t, expSize, actSize, "registered size doesn't match actual disk size")

// Decrease the max bytes limit so that a delete is triggered.
Expand All @@ -1128,8 +1127,7 @@ func TestSizeRetention(t *testing.T) {
actBlocks := db.Blocks()
expSize = int64(prom_testutil.ToFloat64(db.metrics.blocksBytes))
actRetentCount := int(prom_testutil.ToFloat64(db.metrics.sizeRetentionCount))
actSize, err = testutil.DirSize(db.Dir())
testutil.Ok(t, err)
actSize = testutil.DirSize(t, db.Dir())

testutil.Equals(t, 1, actRetentCount, "metric retention count mismatch")
testutil.Equals(t, actSize, expSize, "metric db size doesn't match actual disk size")
Expand Down Expand Up @@ -2274,26 +2272,23 @@ func TestDBReadOnly(t *testing.T) {
testutil.Ok(t, err)
dbWritable.DisableCompactions()

dbSizeBeforeAppend, err := testutil.DirSize(dbWritable.Dir())
testutil.Ok(t, err)
dbSizeBeforeAppend := testutil.DirSize(t, dbWritable.Dir())
app := dbWritable.Appender()
_, err = app.Add(labels.FromStrings("foo", "bar"), dbWritable.Head().MaxTime()+1, 0)
testutil.Ok(t, err)
testutil.Ok(t, app.Commit())
expSeriesCount++

expBlocks = dbWritable.Blocks()
expDbSize, err := testutil.DirSize(dbWritable.Dir())
testutil.Ok(t, err)
expDbSize := testutil.DirSize(t, dbWritable.Dir())
testutil.Assert(t, expDbSize > dbSizeBeforeAppend, "db size didn't increase after an append")

q, err := dbWritable.Querier(math.MinInt64, math.MaxInt64)
testutil.Ok(t, err)
expSeries = query(t, q, matchAll)

testutil.Ok(t, dbWritable.Close()) // Close here to allow getting the dir hash for windows.
expDBHash, err = testutil.DirHash(dbWritable.Dir())
testutil.Ok(t, err)
expDBHash = testutil.DirHash(t, dbWritable.Dir())
}

// Open a read only db and ensure that the API returns the same result as the normal DB.
Expand All @@ -2314,8 +2309,7 @@ func TestDBReadOnly(t *testing.T) {
q, err := dbReadOnly.Querier(math.MinInt64, math.MaxInt64)
testutil.Ok(t, err)
readOnlySeries := query(t, q, matchAll)
readOnlyDBHash, err := testutil.DirHash(dbDir)
testutil.Ok(t, err)
readOnlyDBHash := testutil.DirHash(t, dbDir)

testutil.Equals(t, expSeriesCount, len(readOnlySeries), "total series mismatch")
testutil.Equals(t, expSeries, readOnlySeries, "series mismatch")
Expand Down
50 changes: 22 additions & 28 deletions testutil/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"strconv"
"testing"
)

const (
Expand Down Expand Up @@ -133,56 +134,49 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) {
}

// DirSize returns the size in bytes of all files in a directory.
func DirSize(path string) (int64, error) {
func DirSize(t *testing.T, path string) int64 {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Ok(t, err)
if !info.IsDir() {
size += info.Size()
}
return nil
})
return size, err
Ok(t, err)
return size
}

// DirHash returns a hash of all files attribites and their content within a directory.
func DirHash(path string) ([]byte, error) {
func DirHash(t *testing.T, path string) []byte {
hash := sha256.New()
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Ok(t, err)

if info.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
Ok(t, err)
defer f.Close()

if _, err := io.Copy(hash, f); err != nil {
return err
}
_, err = io.Copy(hash, f)
Ok(t, err)

_, err = io.WriteString(hash, strconv.Itoa(int(info.Size())))
Ok(t, err)

_, err = io.WriteString(hash, info.Name())
Ok(t, err)

if _, err := io.WriteString(hash, strconv.Itoa(int(info.Size()))); err != nil {
return err
}
if _, err := io.WriteString(hash, info.Name()); err != nil {
return err
}
modTime, err := info.ModTime().GobEncode()
if err != nil {
return err
}
if _, err := io.WriteString(hash, string(modTime)); err != nil {
return err
}
Ok(t, err)

_, err = io.WriteString(hash, string(modTime))
Ok(t, err)
return nil
})
Ok(t, err)

return hash.Sum(nil), err
return hash.Sum(nil)
}
6 changes: 2 additions & 4 deletions wal/wal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,8 @@ func TestCompression(t *testing.T) {
testutil.Ok(t, os.RemoveAll(dirUnCompressed))
}()

uncompressedSize, err := testutil.DirSize(dirUnCompressed)
testutil.Ok(t, err)
compressedSize, err := testutil.DirSize(dirCompressed)
testutil.Ok(t, err)
uncompressedSize := testutil.DirSize(t, dirUnCompressed)
compressedSize := testutil.DirSize(t, dirCompressed)

testutil.Assert(t, float64(uncompressedSize)*0.75 > float64(compressedSize), "Compressing zeroes should save at least 25%% space - uncompressedSize: %d, compressedSize: %d", uncompressedSize, compressedSize)
}
Expand Down

0 comments on commit fb75682

Please sign in to comment.