-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tsdb): allow backups during snapshotting, and don't leak tmp files (
#20527) Co-authored-by: davidby-influx <[email protected]>
- Loading branch information
1 parent
4239d03
commit 743aef4
Showing
8 changed files
with
131 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package tsm1 | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/influxdata/influxdb/v2/logger" | ||
"github.com/influxdata/influxdb/v2/models" | ||
"github.com/influxdata/influxdb/v2/tsdb" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEngine_ConcurrentShardSnapshots(t *testing.T) { | ||
tmpDir, err := ioutil.TempDir("", "shard_test") | ||
require.NoError(t, err, "error creating temporary directory") | ||
defer os.RemoveAll(tmpDir) | ||
|
||
tmpShard := filepath.Join(tmpDir, "shard") | ||
tmpWal := filepath.Join(tmpDir, "wal") | ||
|
||
sfile := NewSeriesFile(tmpDir) | ||
defer sfile.Close() | ||
|
||
opts := tsdb.NewEngineOptions() | ||
opts.Config.WALDir = filepath.Join(tmpDir, "wal") | ||
opts.SeriesIDSets = seriesIDSets([]*tsdb.SeriesIDSet{}) | ||
|
||
sh := tsdb.NewShard(1, tmpShard, tmpWal, sfile, opts) | ||
require.NoError(t, sh.Open(), "error opening shard") | ||
defer sh.Close() | ||
|
||
points := make([]models.Point, 0, 10000) | ||
for i := 0; i < cap(points); i++ { | ||
points = append(points, models.MustNewPoint( | ||
"cpu", | ||
models.NewTags(map[string]string{"host": "server"}), | ||
map[string]interface{}{"value": 1.0}, | ||
time.Unix(int64(i), 0), | ||
)) | ||
} | ||
err = sh.WritePoints(points) | ||
require.NoError(t, err) | ||
|
||
engineInterface, err := sh.Engine() | ||
require.NoError(t, err, "error retrieving shard engine") | ||
|
||
// Get the struct underlying the interface. Not a recommended practice. | ||
realEngineStruct, ok := (engineInterface).(*Engine) | ||
if !ok { | ||
t.Log("Engine type does not permit simulating Cache race conditions") | ||
return | ||
} | ||
// fake a race condition in snapshotting the cache. | ||
realEngineStruct.Cache.snapshotting = true | ||
defer func() { | ||
realEngineStruct.Cache.snapshotting = false | ||
}() | ||
|
||
snapshotFunc := func(skipCacheOk bool) { | ||
if f, err := sh.CreateSnapshot(skipCacheOk); err == nil { | ||
require.NoError(t, os.RemoveAll(f), "error cleaning up TestEngine_ConcurrentShardSnapshots") | ||
} else if err == ErrSnapshotInProgress { | ||
if skipCacheOk { | ||
t.Fatalf("failing to ignore this error,: %s", err.Error()) | ||
} | ||
} else { | ||
t.Fatalf("error creating shard snapshot: %s", err.Error()) | ||
} | ||
} | ||
|
||
// Permit skipping cache in the snapshot | ||
snapshotFunc(true) | ||
// do not permit skipping the cache in the snapshot | ||
snapshotFunc(false) | ||
realEngineStruct.Cache.snapshotting = false | ||
} | ||
|
||
// NewSeriesFile returns a new instance of SeriesFile with a temporary file path. | ||
func NewSeriesFile(tmpDir string) *tsdb.SeriesFile { | ||
dir, err := ioutil.TempDir(tmpDir, "tsdb-series-file-") | ||
if err != nil { | ||
panic(err) | ||
} | ||
f := tsdb.NewSeriesFile(dir) | ||
f.Logger = logger.New(os.Stdout) | ||
if err := f.Open(); err != nil { | ||
panic(err) | ||
} | ||
return f | ||
} | ||
|
||
type seriesIDSets []*tsdb.SeriesIDSet | ||
|
||
func (a seriesIDSets) ForEach(f func(ids *tsdb.SeriesIDSet)) error { | ||
for _, v := range a { | ||
f(v) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters