Skip to content

Commit

Permalink
mfs: clean directory cache on flush
Browse files Browse the repository at this point in the history
Flushing represents a sort of "I'm done" with the folder.

It is less likely that old references to the folder are used after it has been
flushed. As such, we take the chance to clean up the cache then.
  • Loading branch information
hsanjuan committed Dec 16, 2024
1 parent c022f6f commit fbe0229
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The following emojis are used to highlight certain changes:

### Fixed

* `mfs`: directory cache is now cleared every time the directory node is read, somewhat limiting unbounded growth and time to sync it to the underlying unixfs.
* `mfs`: directory cache is now cleared on Flush(), liberating the memory used by the otherwise ever-growing cache. References to directories and sub-directories should be renewed after flushing.

### Security

Expand Down
10 changes: 7 additions & 3 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (d *Directory) Unlink(name string) error {
}

func (d *Directory) Flush() error {
nd, err := d.GetNode()
nd, err := d.getNode(true)
if err != nil {
return err
}
Expand All @@ -349,7 +349,7 @@ func (d *Directory) AddChild(name string, nd ipld.Node) error {
return d.unixfsDir.AddChild(d.ctx, name, nd)
}

func (d *Directory) syncCache(clean bool) error {
func (d *Directory) cacheSync(clean bool) error {
for name, entry := range d.entriesCache {
nd, err := entry.GetNode()
if err != nil {
Expand Down Expand Up @@ -385,10 +385,14 @@ func (d *Directory) Path() string {
}

func (d *Directory) GetNode() (ipld.Node, error) {
return d.getNode(false)
}

func (d *Directory) getNode(cacheClean bool) (ipld.Node, error) {
d.lock.Lock()
defer d.lock.Unlock()

err := d.syncCache(true)
err := d.cacheSync(cacheClean)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions mfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,9 @@ func (fi *File) setNodeData(data []byte) error {
}

fi.nodeLock.Lock()
defer fi.nodeLock.Unlock()
fi.node = nd
parent := fi.inode.parent
name := fi.inode.name

fi.nodeLock.Unlock()
return parent.updateChildEntry(child{name, fi.node})
}
11 changes: 1 addition & 10 deletions mfs/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,7 @@ func (kr *Root) Flush() error {
func (kr *Root) FlushMemFree(ctx context.Context) error {
dir := kr.GetDirectory()

if err := dir.Flush(); err != nil {
return err
}

dir.lock.Lock()
defer dir.lock.Unlock()

clear(dir.entriesCache)

return nil
return dir.Flush()

Check warning on line 173 in mfs/root.go

View check run for this annotation

Codecov / codecov/patch

mfs/root.go#L173

Added line #L173 was not covered by tests
}

// updateChildEntry implements the `parent` interface, and signals
Expand Down

0 comments on commit fbe0229

Please sign in to comment.