Skip to content

Commit

Permalink
Prefetch next inode number
Browse files Browse the repository at this point in the history
Signed-off-by: Changxin Miao <[email protected]>
  • Loading branch information
polyrabbit committed Sep 3, 2024
1 parent 44eebbc commit 949a8ee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
43 changes: 35 additions & 8 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ type baseMeta struct {
dirParents map[Ino]Ino // directory inode -> parent inode
dirQuotas map[Ino]*Quota // directory inode -> quota

freeMu sync.Mutex
freeInodes freeID
freeSlices freeID
freeMu sync.Mutex
freeInodes freeID
freeSlices freeID
prefetchMu sync.Mutex
prefetchedInode uint64

usedSpaceG prometheus.Gauge
usedInodesG prometheus.Gauge
Expand Down Expand Up @@ -948,22 +950,47 @@ func (m *baseMeta) nextInode() (Ino, error) {
m.freeMu.Lock()
defer m.freeMu.Unlock()
if m.freeInodes.next >= m.freeInodes.maxid {
v, err := m.en.incrCounter("nextInode", inodeBatch)
if err != nil {
return 0, err
m.prefetchMu.Lock() // Wait until prefetchInodes() is done
nextLimit := m.prefetchedInode
m.prefetchMu.Unlock()
if nextLimit <= m.freeInodes.maxid {
v, err := m.en.incrCounter("nextInode", inodeBatch)
if err != nil {
return 0, err
}
nextLimit = uint64(v)
} else {
logger.Debugf("Prefetch hit, next limit: %d", nextLimit)
}
m.freeInodes.next = uint64(v) - inodeBatch
m.freeInodes.maxid = uint64(v)
m.freeInodes.next = nextLimit - inodeBatch
m.freeInodes.maxid = nextLimit
}
n := m.freeInodes.next
m.freeInodes.next++
for n <= 1 {
n = m.freeInodes.next
m.freeInodes.next++
}
if m.freeInodes.maxid-m.freeInodes.next < uint64(utils.JitterIt(inodeBatch*0.1)) {
go m.prefetchInodes()
}
return Ino(n), nil
}

func (m *baseMeta) prefetchInodes() {
m.prefetchMu.Lock()
defer m.prefetchMu.Unlock()
if m.prefetchedInode > m.freeInodes.maxid {
return // Someone else has done the job
}
v, err := m.en.incrCounter("nextInode", inodeBatch)
if err == nil {
m.prefetchedInode = uint64(v)
} else {
logger.Warnf("prefetchInodes: %s, current limit: %d", err, m.freeInodes.maxid)
}
}

func (m *baseMeta) Mknod(ctx Context, parent Ino, name string, _type uint8, mode, cumask uint16, rdev uint32, path string, inode *Ino, attr *Attr) syscall.Errno {
if _type < TypeFile || _type > TypeSocket {
return syscall.EINVAL
Expand Down
6 changes: 5 additions & 1 deletion pkg/utils/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
)

func SleepWithJitter(d time.Duration) {
time.Sleep(d + JitterIt(d))
}

func JitterIt[T float64 | time.Duration](d T) T {
j := int64(d / 20) // +- 5%
time.Sleep(d + time.Duration(rand.Int63n(2*j+1)-j))
return d + T(rand.Int63n(2*j+1)-j)
}

0 comments on commit 949a8ee

Please sign in to comment.