Skip to content

Commit

Permalink
test: check post-compaction series file sizes before reopening (#22534)
Browse files Browse the repository at this point in the history
  • Loading branch information
danxmoran authored Sep 21, 2021
1 parent 59f0f13 commit 233f277
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
4 changes: 4 additions & 0 deletions cmd/influxd/inspect/build_tsi/build_tsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func Test_BuildTSI_Invalid_Compact_Series_Specific_Shard(t *testing.T) {
}

func Test_BuildTSI_Valid_Compact_Series(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("mmap implementation on Windows prevents series-file from shrinking during compaction")
}

tempDir := newTempDirectory(t, "", "build-tsi")
defer os.RemoveAll(tempDir)

Expand Down
74 changes: 39 additions & 35 deletions tsdb/series_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/tsdb"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -179,6 +180,22 @@ func TestSeriesFile_Compaction(t *testing.T) {
sfile := MustOpenSeriesFile(t)
defer sfile.Close()

var segmentPaths []string
for _, p := range sfile.Partitions() {
for _, ss := range p.Segments() {
segmentPaths = append(segmentPaths, ss.Path())
}
}

sfileSize := func() (res int64) {
for _, p := range segmentPaths {
fi, err := os.Stat(p)
require.NoError(t, err)
res += fi.Size()
}
return
}

// Generate a bunch of keys.
var mms [][]byte
var tagSets []models.Tags
Expand All @@ -189,70 +206,57 @@ func TestSeriesFile_Compaction(t *testing.T) {

// Add all to the series file.
ids, err := sfile.CreateSeriesListIfNotExists(mms, tagSets)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

// Delete a subset of keys.
for i, id := range ids {
if i%10 == 0 {
if err := sfile.DeleteSeriesID(id); err != nil {
t.Fatal(err)
}
require.NoError(t, sfile.DeleteSeriesID(id))
}
}

// Compute total size of all series data.
origSize, err := sfile.FileSize()
if err != nil {
t.Fatal(err)
}
// Check total series count.
require.Equal(t, 1000, int(sfile.SeriesCount()))

// Compact all segments.
var paths []string
for _, p := range sfile.Partitions() {
for _, ss := range p.Segments() {
if err := ss.CompactToPath(ss.Path()+".tmp", p.Index()); err != nil {
t.Fatal(err)
}
require.NoError(t, ss.CompactToPath(ss.Path()+".tmp", p.Index()))
paths = append(paths, ss.Path())
}
}

// Close index.
if err := sfile.SeriesFile.Close(); err != nil {
t.Fatal(err)
}
require.NoError(t, sfile.SeriesFile.Close())

// Compute total size of all series data.
origSize := sfileSize()

// Overwrite files.
for _, path := range paths {
if err := os.Rename(path+".tmp", path); err != nil {
t.Fatal(err)
}
require.NoError(t, os.Rename(path+".tmp", path))
}

// Check size of compacted series data.
// We do this before reopening the index because on Windows, opening+mmap'ing the series
// file will cause the file to grow back to its original size.
newSize := sfileSize()

// Verify new size is smaller.
require.Greater(t, origSize, newSize)

// Reopen index.
sfile.SeriesFile = tsdb.NewSeriesFile(sfile.SeriesFile.Path())
if err := sfile.SeriesFile.Open(); err != nil {
t.Fatal(err)
}
require.NoError(t, sfile.SeriesFile.Open())

// Ensure series status is correct.
for i, id := range ids {
if got, want := sfile.IsDeleted(id), (i%10) == 0; got != want {
t.Fatalf("IsDeleted(%d)=%v, want %v", id, got, want)
}
}

// Verify new size is smaller.
newSize, err := sfile.FileSize()
if err != nil {
t.Fatal(err)
} else if newSize >= origSize {
t.Fatalf("expected new size (%d) to be smaller than original size (%d)", newSize, origSize)
require.Equal(t, (i%10) == 0, sfile.IsDeleted(id))
}

t.Logf("original size: %d, new size: %d", origSize, newSize)
// Check total series count.
require.Equal(t, 900, int(sfile.SeriesCount()))
}

var cachedCompactionSeriesFile *SeriesFile
Expand Down

0 comments on commit 233f277

Please sign in to comment.