From 1d9a461c233d8b7fe11448b7c19c0414cba8d2be Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 25 Mar 2023 11:57:37 -0700 Subject: [PATCH] all: follow-up after 34634ec357a9fc58722eabc6d790568d1cd7ea3d - Use windows.FlushFileBuffers() instead of windows.Fsync() at streamTracker.adviseDontNeed() for consistency with implementations for other architectures. - Use filepath.Base() instead of filepath.Split(), since the dir part isn't used. This simplifies the code a bit. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/70 --- lib/filestream/filestream_windows.go | 2 +- lib/fs/fs_windows.go | 32 ++++++++++++++-------------- lib/storage/part_header.go | 5 +---- lib/storage/partition.go | 6 +----- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/filestream/filestream_windows.go b/lib/filestream/filestream_windows.go index a46c935a9..1d37a61fd 100644 --- a/lib/filestream/filestream_windows.go +++ b/lib/filestream/filestream_windows.go @@ -8,7 +8,7 @@ import ( func (st *streamTracker) adviseDontNeed(n int, fdatasync bool) error { if fdatasync && st.fd > 0 { - if err := windows.Fsync(windows.Handle(st.fd)); err != nil { + if err := windows.FlushFileBuffers(windows.Handle(st.fd)); err != nil { return fmt.Errorf("windows.Fsync error: %w", err) } } diff --git a/lib/fs/fs_windows.go b/lib/fs/fs_windows.go index 27f8a3cd4..fff0783c7 100644 --- a/lib/fs/fs_windows.go +++ b/lib/fs/fs_windows.go @@ -58,8 +58,8 @@ func createFlockFile(flockFile string) (*os.File, error) { } var ( - fileMappingMU sync.Mutex - fileMappingByAddr = map[uintptr]windows.Handle{} + mmapByAddrLock sync.Mutex + mmapByAddr = map[uintptr]windows.Handle{} ) func mmap(fd int, length int) ([]byte, error) { @@ -77,15 +77,16 @@ func mmap(fd int, length int) ([]byte, error) { windows.CloseHandle(h) return nil, os.NewSyscallError("MapViewOfFile", errno) } - fileMappingMU.Lock() - fileMappingByAddr[addr] = h - fileMappingMU.Unlock() data := make([]byte, 0) hdr := (*reflect.SliceHeader)(unsafe.Pointer(&data)) hdr.Data = addr hdr.Len = length hdr.Cap = hdr.Len + mmapByAddrLock.Lock() + mmapByAddr[addr] = h + mmapByAddrLock.Unlock() + return data, nil } @@ -94,20 +95,20 @@ func mUnmap(data []byte) error { // In case of write, additional call FlushViewOfFile must be performed. header := (*reflect.SliceHeader)(unsafe.Pointer(&data)) addr := header.Data - fileMappingMU.Lock() - defer fileMappingMU.Unlock() - if err := windows.UnmapViewOfFile(addr); err != nil { - return err - } - handle, ok := fileMappingByAddr[addr] + mmapByAddrLock.Lock() + h, ok := mmapByAddr[addr] if !ok { logger.Fatalf("BUG: unmapping for non exist addr: %d", addr) } - delete(fileMappingByAddr, addr) + delete(mmapByAddr, addr) + mmapByAddrLock.Unlock() - e := windows.CloseHandle(handle) - return os.NewSyscallError("CloseHandle", e) + if err := windows.UnmapViewOfFile(addr); err != nil { + return fmt.Errorf("cannot unmap memory mapped file: %w", err) + } + errno := windows.CloseHandle(h) + return os.NewSyscallError("CloseHandle", errno) } func mustGetFreeSpace(path string) uint64 { @@ -115,8 +116,7 @@ func mustGetFreeSpace(path string) uint64 { r, _, err := procDisk.Call(uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(path))), uintptr(unsafe.Pointer(&freeBytes))) if r == 0 { - logger.Errorf("cannot get free space for path: %q : %s", path, err) - return 0 + logger.Panicf("FATAL: cannot get free space for %q : %s", path, err) } return uint64(freeBytes) } diff --git a/lib/storage/part_header.go b/lib/storage/part_header.go index 5d1412f98..25ef71963 100644 --- a/lib/storage/part_header.go +++ b/lib/storage/part_header.go @@ -83,10 +83,7 @@ func (ph *partHeader) ParseFromPath(path string) error { path = filepath.Clean(path) // Extract encoded part name. - dir, partName := filepath.Split(path) - if len(dir) == 0 { - return fmt.Errorf("cannot find encoded part name in the path %q", path) - } + partName := filepath.Base(path) // PartName must have the following form: // RowsCount_BlocksCount_MinTimestamp_MaxTimestamp_Garbage diff --git a/lib/storage/partition.go b/lib/storage/partition.go index c25959e2c..c7a320442 100644 --- a/lib/storage/partition.go +++ b/lib/storage/partition.go @@ -259,11 +259,7 @@ func openPartition(smallPartsPath, bigPartsPath string, s *Storage) (*partition, smallPartsPath = filepath.Clean(smallPartsPath) bigPartsPath = filepath.Clean(bigPartsPath) - dir, name := filepath.Split(smallPartsPath) - if len(dir) == 0 { - return nil, fmt.Errorf("cannot find partition name from smallPartsPath %q; must be in the form /path/to/smallparts/YYYY_MM", smallPartsPath) - } - + name := filepath.Base(smallPartsPath) if !strings.HasSuffix(bigPartsPath, name) { return nil, fmt.Errorf("patititon name in bigPartsPath %q doesn't match smallPartsPath %q; want %q", bigPartsPath, smallPartsPath, name) }