mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/fs: move common code outside arch-specific implementations of mustRemoveDirAtomic()
This is a follow-up for 73b6c23271
Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/70
This commit is contained in:
parent
a5a57a7c2a
commit
e4c615e777
5 changed files with 8 additions and 24 deletions
|
@ -219,7 +219,12 @@ func IsEmptyDir(path string) bool {
|
|||
// If the process crashes after the step 1, then the directory must be removed
|
||||
// on the next process start by calling MustRemoveTemporaryDirs on the parent directory.
|
||||
func MustRemoveDirAtomic(dir string) {
|
||||
if !IsPathExist(dir) {
|
||||
return
|
||||
}
|
||||
mustRemoveDirAtomic(dir)
|
||||
parentDir := filepath.Dir(dir)
|
||||
MustSyncPath(parentDir)
|
||||
}
|
||||
|
||||
var atomicDirRemoveCounter = uint64(time.Now().UnixNano())
|
||||
|
|
|
@ -6,7 +6,6 @@ package fs
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
|
@ -18,15 +17,10 @@ func freeSpace(stat unix.Statfs_t) uint64 {
|
|||
}
|
||||
|
||||
func mustRemoveDirAtomic(dir string) {
|
||||
if !IsPathExist(dir) {
|
||||
return
|
||||
}
|
||||
n := atomic.AddUint64(&atomicDirRemoveCounter, 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)
|
||||
parentDir := filepath.Dir(dir)
|
||||
MustSyncPath(parentDir)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package fs
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
|
@ -15,15 +14,10 @@ func freeSpace(stat unix.Statfs_t) uint64 {
|
|||
}
|
||||
|
||||
func mustRemoveDirAtomic(dir string) {
|
||||
if !IsPathExist(dir) {
|
||||
return
|
||||
}
|
||||
n := atomic.AddUint64(&atomicDirRemoveCounter, 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)
|
||||
parentDir := filepath.Dir(dir)
|
||||
MustSyncPath(parentDir)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package fs
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
||||
|
@ -11,17 +10,12 @@ import (
|
|||
)
|
||||
|
||||
func mustRemoveDirAtomic(dir string) {
|
||||
if !IsPathExist(dir) {
|
||||
return
|
||||
}
|
||||
n := atomic.AddUint64(&atomicDirRemoveCounter, 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)
|
||||
parentDir := filepath.Dir(dir)
|
||||
MustSyncPath(parentDir)
|
||||
}
|
||||
|
||||
func mmap(fd int, length int) (data []byte, err error) {
|
||||
|
|
|
@ -25,17 +25,14 @@ func mustSyncPath(path string) {
|
|||
}
|
||||
|
||||
func mustRemoveDirAtomic(dir string) {
|
||||
if !IsPathExist(dir) {
|
||||
return
|
||||
}
|
||||
n := atomic.AddUint64(&atomicDirRemoveCounter, 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)
|
||||
}
|
||||
err := os.RemoveAll(tmpDir)
|
||||
if err != nil {
|
||||
logger.Warnf("cannot remove dir: %q: %s, restart VictoriaMetrics process to complete file deletion", tmpDir, err)
|
||||
if err := os.RemoveAll(tmpDir); err != nil {
|
||||
logger.Warnf("cannot remove dir: %q: %s; restart VictoriaMetrics to complete dir removal; "+
|
||||
"see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/70#issuecomment-1491529183", tmpDir, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue