Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta: slowdown dump process to reduce memory usage #4253

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Details: https://juicefs.com/docs/community/metadata_dump_load`,
Name: "keep-secret-key",
Usage: "keep secret keys intact (WARNING: Be careful as they may be leaked)",
},
&cli.BoolFlag{
Name: "fast",
Usage: "speedup dump by load all metadata into memory",
},
},
}
}
Expand Down Expand Up @@ -101,7 +105,7 @@ func dump(ctx *cli.Context) (err error) {
if st := m.Chroot(meta.Background, metaConf.Subdir); st != 0 {
return st
}
if err := m.DumpMeta(w, 1, ctx.Bool("keep-secret-key")); err != nil {
if err := m.DumpMeta(w, 1, ctx.Bool("keep-secret-key"), ctx.Bool("fast")); err != nil {
return err
}
logger.Infof("Dump metadata into %s succeed", dst)
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ type Meta interface {
HandleQuota(ctx Context, cmd uint8, dpath string, quotas map[string]*Quota, strict, repair bool) error

// Dump the tree under root, which may be modified by checkRoot
DumpMeta(w io.Writer, root Ino, keepSecret bool) error
DumpMeta(w io.Writer, root Ino, keepSecret, fast bool) error
LoadMeta(r io.Reader) error

// getBase return the base engine.
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/load_dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func testDump(t *testing.T, m Meta, root Ino, expect, result string) {
if _, err = m.Load(true); err != nil {
t.Fatalf("load setting: %s", err)
}
if err = m.DumpMeta(fp, root, false); err != nil {
if err = m.DumpMeta(fp, root, false, true); err != nil {
t.Fatalf("dump meta: %s", err)
}
cmd := exec.Command("diff", expect, result)
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3845,7 +3845,7 @@ func (m *redisMeta) dumpDir(inode Ino, tree *DumpedEntry, bw *bufio.Writer, dept
return nil
}

func (m *redisMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
func (m *redisMeta) DumpMeta(w io.Writer, root Ino, keepSecret, fast bool) (err error) {
defer func() {
if p := recover(); p != nil {
if e, ok := p.(error); ok {
Expand Down
7 changes: 5 additions & 2 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3524,7 +3524,7 @@ func (m *dbMeta) makeSnap(ses *xorm.Session, bar *utils.Bar) error {
return nil
}

func (m *dbMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
func (m *dbMeta) DumpMeta(w io.Writer, root Ino, keepSecret, fast bool) (err error) {
defer func() {
if p := recover(); p != nil {
if e, ok := p.(error); ok {
Expand All @@ -3539,7 +3539,7 @@ func (m *dbMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
var tree, trash *DumpedEntry
root = m.checkRoot(root)
return m.roTxn(func(s *xorm.Session) error {
if root == RootInode {
if root == RootInode && fast {
defer func() { m.snap = nil }()
bar := progress.AddCountBar("Snapshot keys", 0)
if err = m.makeSnap(s, bar); err != nil {
Expand All @@ -3552,6 +3552,9 @@ func (m *dbMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
if tree, err = m.dumpEntry(s, root, TypeDirectory); err != nil {
return err
}
if trash, err = m.dumpEntry(s, TrashInode, TypeDirectory); err != nil {
davies marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
if tree == nil {
return errors.New("The entry of the root inode was not found")
Expand Down
13 changes: 11 additions & 2 deletions pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ func (m *kvMeta) dumpDir(inode Ino, tree *DumpedEntry, bw *bufio.Writer, depth i
return nil
}

func (m *kvMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
func (m *kvMeta) DumpMeta(w io.Writer, root Ino, keepSecret, fast bool) (err error) {
defer func() {
if p := recover(); p != nil {
debug.PrintStack()
Expand Down Expand Up @@ -2986,7 +2986,7 @@ func (m *kvMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
var tree, trash *DumpedEntry
root = m.checkRoot(root)

if root == 1 { // make snap
if root == 1 && fast { // make snap
m.snap = make(map[Ino]*DumpedEntry)
defer func() {
m.snap = nil
Expand Down Expand Up @@ -3072,6 +3072,15 @@ func (m *kvMeta) DumpMeta(w io.Writer, root Ino, keepSecret bool) (err error) {
if err = m.dumpEntry(root, tree); err != nil {
return err
}
trash = &DumpedEntry{
Attr: &DumpedAttr{
Inode: TrashInode,
Type: "directory",
},
}
if err = m.dumpEntry(TrashInode, trash); err != nil {
return err
}
}

if tree == nil || tree.Attr == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/vfs/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func backup(m meta.Meta, blob object.ObjectStorage, now time.Time) error {
defer os.Remove(fp.Name())
defer fp.Close()
zw := gzip.NewWriter(fp)
err = m.DumpMeta(zw, 0, false) // force dump the whole tree
err = m.DumpMeta(zw, 0, false, false) // force dump the whole tree
_ = zw.Close()
if err != nil {
return err
Expand Down
Loading