mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-27 02:46:47 +01:00
334cd92a6c
### Describe Your Changes fsync() ensures that the data is written to disk. In production this is needed for data durability. However, during the development, when the unit tests are run, this level of durability is not needed. Therefore fsync() can be disabled which will makes test runs two times faster. The disabling is done by setting the `DISABLE_FSYNC_FOR_TESTING` environment variable. The valid values for this variable are the same as the values of the arg of `go doc strconv.ParseBool`: ``` 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. ``` Any other value means `false`. The variable is set for all test build targets. Compare running times: Build Target | DISABLE_FSYNC_FOR_TESTING=0 | DISABLE_FSYNC_FOR_TESTING=1 ----------------- | ------------------------------------------------ | ------------------------------------------------- make test | 1m5s | 0m22s make test-race | 3m1s | 1m42s make test-pure | 1m7s | 0m20s make test-full | 1m21s | 0m32s make test-full-386 | 1m42s | 0m36s When running tests for a given package, fsync can be disabled as follows: ```shell DISABLE_FSYNC_FOR_TESTING=1 go test ./lib/storage ``` Disabling fsync() is intended for testing purposes only and the name of the variables reflects that. What could also have been done but haven't: - lib/filestream/filestream.go: `Writer.MustFlush()` also uses f.Sync() but nothing has been done to it, because the Writer.MustFlush() is not used anywhere in the VM codebase. A side question: what is the general policy for the unused code? - lib/filestream/filestream.go: Writer.Write() calls `adviceDontNeed()` which calls unix.Fdatasync(). Disabling it could potentially improve running time, but running tests with this code disabled has shown otherwise. ### Checklist The following checks are **mandatory**: - [ x] My change adheres [VictoriaMetrics contributing guidelines](https://docs.victoriametrics.com/contributing/). --------- Signed-off-by: Artem Fetishev <wwctrsrx@gmail.com>
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func mustRemoveDirAtomic(dir string) {
|
|
n := atomicDirRemoveCounter.Add(1)
|
|
tmpDir := fmt.Sprintf("%s.must-remove.%d", dir, n)
|
|
if err := os.Rename(dir, tmpDir); err != nil {
|
|
logger.Panicf("FATAL: cannot move %s to %s: %s", dir, tmpDir, err)
|
|
}
|
|
MustRemoveAll(tmpDir)
|
|
}
|
|
|
|
func mmap(fd int, length int) (data []byte, err error) {
|
|
return unix.Mmap(fd, 0, length, unix.PROT_READ, unix.MAP_SHARED)
|
|
|
|
}
|
|
func mUnmap(data []byte) error {
|
|
return unix.Munmap(data)
|
|
}
|
|
|
|
func mustSyncPath(path string) {
|
|
d, err := os.Open(path)
|
|
if err != nil {
|
|
logger.Panicf("FATAL: cannot open file for fsync: %s", err)
|
|
}
|
|
if !disableFSyncForTesting {
|
|
if err := d.Sync(); err != nil {
|
|
_ = d.Close()
|
|
logger.Panicf("FATAL: cannot flush %q to storage: %s", path, err)
|
|
}
|
|
}
|
|
if err := d.Close(); err != nil {
|
|
logger.Panicf("FATAL: cannot close %q: %s", path, err)
|
|
}
|
|
}
|
|
|
|
func createFlockFile(flockFile string) (*os.File, error) {
|
|
flockF, err := os.Create(flockFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot create lock file %q: %w", flockFile, err)
|
|
}
|
|
|
|
flock := unix.Flock_t{
|
|
Type: unix.F_WRLCK,
|
|
Start: 0,
|
|
Len: 0,
|
|
Whence: 0,
|
|
}
|
|
if err := unix.FcntlFlock(flockF.Fd(), unix.F_SETLK, &flock); err != nil {
|
|
return nil, fmt.Errorf("cannot acquire lock on file %q: %w", flockFile, err)
|
|
}
|
|
return flockF, nil
|
|
}
|
|
|
|
func mustGetFreeSpace(path string) uint64 {
|
|
var stat unix.Statvfs_t
|
|
if err := unix.Statvfs(path, &stat); err != nil {
|
|
logger.Panicf("FATAL: cannot determine free disk space on %q: %s", path, err)
|
|
}
|
|
return freeSpace(stat)
|
|
}
|
|
|
|
func freeSpace(stat unix.Statvfs_t) uint64 {
|
|
return uint64(stat.Bavail) * uint64(stat.Bsize)
|
|
}
|