Skip to content

Commit

Permalink
meta: clean symlink cache (#5131)
Browse files Browse the repository at this point in the history
Signed-off-by: jiefenghuang <[email protected]>
  • Loading branch information
jiefenghuang authored Sep 4, 2024
1 parent 44eebbc commit e58fa03
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
53 changes: 48 additions & 5 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ import (
)

const (
inodeBatch = 1 << 10
sliceIdBatch = 4 << 10
nlocks = 1024
inodeBatch = 1 << 10
sliceIdBatch = 4 << 10
nlocks = 1024
maxSymCacheNum = int32(10000)
)

var maxCompactSlices = 1000
Expand Down Expand Up @@ -144,6 +145,47 @@ type cchunk struct {
slices int
}

type symlinkCache struct {
*sync.Map
size atomic.Int32
cap int32
}

func newSymlinkCache(cap int32) *symlinkCache {
return &symlinkCache{
Map: &sync.Map{},
cap: cap,
}
}

func (symCache *symlinkCache) Store(inode Ino, path []byte) {
if _, loaded := symCache.Swap(inode, path); !loaded {
symCache.size.Add(1)
}
}

func (symCache *symlinkCache) clean() {
for {
time.Sleep(time.Minute)
symCache.doClean()
}
}

func (symCache *symlinkCache) doClean() {
if symCache.size.Load() < int32(float64(symCache.cap)*0.75) {
return
}

todo := symCache.size.Load() / 5
cnt := int32(0)
symCache.Range(func(key, value interface{}) bool {
symCache.Delete(key)
symCache.size.Add(-1)
cnt++
return cnt < todo
})
}

type baseMeta struct {
sync.Mutex
addr string
Expand All @@ -159,7 +201,7 @@ type baseMeta struct {
compacting map[uint64]bool
maxDeleting chan struct{}
dslices chan Slice // slices to delete
symlinks *sync.Map
symlinks *symlinkCache
msgCallbacks *msgCallbacks
reloadCb []func(*Format)
umounting bool
Expand Down Expand Up @@ -200,7 +242,7 @@ func newBaseMeta(addr string, conf *Config) *baseMeta {
removedFiles: make(map[Ino]bool),
compacting: make(map[uint64]bool),
maxDeleting: make(chan struct{}, 100),
symlinks: &sync.Map{},
symlinks: newSymlinkCache(maxSymCacheNum),
fsStat: new(fsStat),
dirStats: make(map[Ino]dirStat),
dirParents: make(map[Ino]Ino),
Expand Down Expand Up @@ -417,6 +459,7 @@ func (m *baseMeta) NewSession(record bool) error {
go m.cleanupDeletedFiles()
go m.cleanupSlices()
go m.cleanupTrash()
go m.symlinks.clean()
}
return nil
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/meta/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/juicedata/juicefs/pkg/utils"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func testConfig() *Config {
Expand Down Expand Up @@ -3190,3 +3191,28 @@ func testAtime(t *testing.T, m Meta) {
}
}
}

func TestSymlinkCache(t *testing.T) {
cache := newSymlinkCache(10000)

job := make(chan Ino)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for ino := range job {
cache.Store(ino, []byte(fmt.Sprintf("file%d", ino)))
}
}()
}

for i := 0; i < 10000; i++ {
job <- Ino(i)
}
close(job)
wg.Wait()

cache.doClean()
require.Equal(t, int32(8000), cache.size.Load())
}

0 comments on commit e58fa03

Please sign in to comment.