Skip to content

Commit 13a32f2

Browse files
committed
Add free disk space validation for fallback sync
1 parent 489486b commit 13a32f2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pbm/restore/physical.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strconv"
1919
"strings"
2020
"sync"
21+
"syscall"
2122
"time"
2223

2324
"github.com/mongodb/mongo-tools/common/db"
@@ -2500,6 +2501,15 @@ func (r *PhysRestore) prepareBackup(ctx context.Context, backupName string) erro
25002501
}
25012502

25022503
setName := mapRevRS(r.nodeInfo.SetName)
2504+
2505+
if r.fallback && r.bcp.RS(setName) != nil {
2506+
err = r.checkDiskSpace(r.bcp.RS(setName).Size)
2507+
if err != nil {
2508+
return errors.Wrap(err, "not enough disk space for fallback strategy, "+
2509+
"consider using --fallback-enabled=false")
2510+
}
2511+
}
2512+
25032513
var ok bool
25042514
for _, v := range r.bcp.Replsets {
25052515
if v.Name == setName {
@@ -2547,6 +2557,25 @@ func (r *PhysRestore) checkMongod(needVersion string) (version string, err error
25472557
return v, nil
25482558
}
25492559

2560+
// checkDiskSpace compares available disk space on the node with the backup size.
2561+
// It returns error in case when backup size is larger than free space on the disk.
2562+
// It also includes min disk space requirement of 10GiB.
2563+
func (r *PhysRestore) checkDiskSpace(bcpSize int64) error {
2564+
var stat syscall.Statfs_t
2565+
err := syscall.Statfs(r.dbpath, &stat)
2566+
if err != nil {
2567+
return errors.Wrap(err, "get statfs")
2568+
}
2569+
2570+
free := int64(stat.Bavail) * int64(stat.Bsize)
2571+
tenGiB := 10 * (1 << 30)
2572+
if bcpSize+int64(tenGiB) >= free {
2573+
return errors.Errorf("not enough disk space, free=%d, backup size=%d", free, bcpSize)
2574+
}
2575+
2576+
return nil
2577+
}
2578+
25502579
// MarkFailed sets the restore and rs state as failed with the given message
25512580
func (r *PhysRestore) MarkFailed(e error) {
25522581
err := util.RetryableWrite(r.stg,

0 commit comments

Comments
 (0)