Skip to content

Commit

Permalink
Allocate nextTrash inode in mknod to avoid generating too many tx…
Browse files Browse the repository at this point in the history
…n conflicts (#5330)

Signed-off-by: Changxin Miao <[email protected]>
  • Loading branch information
polyrabbit authored Nov 29, 2024
1 parent 065f8ab commit 4826ace
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
10 changes: 2 additions & 8 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2324,14 +2324,8 @@ func (m *baseMeta) checkTrash(parent Ino, trash *Ino) syscall.Errno {

st := m.en.doLookup(Background, TrashInode, name, trash, nil)
if st == syscall.ENOENT {
next, err := m.en.incrCounter("nextTrash", 1)
if err == nil {
*trash = TrashInode + Ino(next)
attr := Attr{Typ: TypeDirectory, Nlink: 2, Length: 4 << 10, Parent: TrashInode, Full: true}
st = m.en.doMknod(Background, TrashInode, name, TypeDirectory, 0555, 0, "", trash, &attr)
} else {
st = errno(err)
}
attr := Attr{Typ: TypeDirectory, Nlink: 2, Length: 4 << 10, Parent: TrashInode, Full: true}
st = m.en.doMknod(Background, TrashInode, name, TypeDirectory, 0555, 0, "", trash, &attr)
}

m.Lock()
Expand Down
10 changes: 10 additions & 0 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ func (m *redisMeta) usedSpaceKey() string {
return m.prefix + usedSpace
}

func (m *redisMeta) nextTrashKey() string {
return m.prefix + "nextTrash"
}

func (m *redisMeta) dirDataLengthKey() string {
return m.prefix + "dirDataLength"
}
Expand Down Expand Up @@ -1248,6 +1252,12 @@ func (m *redisMeta) doMknod(ctx Context, parent Ino, name string, _type uint8, m
*inode = foundIno
}
return syscall.EEXIST
} else if parent == TrashInode {
if next, err := tx.Incr(ctx, m.nextTrashKey()).Result(); err != nil { // Some inode will be wasted if conflict happens
return err
} else {
*inode = TrashInode + Ino(next)
}
}

mode &= 07777
Expand Down
46 changes: 28 additions & 18 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,26 +701,30 @@ func (m *dbMeta) getCounter(name string) (v int64, err error) {
return
}

func (m *dbMeta) incrCounter(name string, value int64) (int64, error) {
var v int64
err := m.txn(func(s *xorm.Session) error {
var c = counter{Name: name}
ok, err := s.ForUpdate().Get(&c)
if err != nil {
return err
}
v = c.Value + value
if value > 0 {
c.Value = v
if ok {
_, err = s.Cols("value").Update(&c, &counter{Name: name})
} else {
err = mustInsert(s, &c)
}
}
func (m *dbMeta) incrCounter(name string, value int64) (v int64, err error) {
err = m.txn(func(s *xorm.Session) error {
v, err = m.incrSessionCounter(s, name, value)
return err
})
return v, err
return
}

func (m *dbMeta) incrSessionCounter(s *xorm.Session, name string, value int64) (v int64, err error) {
var c = counter{Name: name}
ok, err := s.ForUpdate().Get(&c)
if err != nil {
return
}
v = c.Value + value
if value > 0 {
c.Value = v
if ok {
_, err = s.Cols("value").Update(&c, &counter{Name: name})
} else {
err = mustInsert(s, &c)
}
}
return
}

func (m *dbMeta) setIfSmall(name string, value, diff int64) (bool, error) {
Expand Down Expand Up @@ -1317,6 +1321,12 @@ func (m *dbMeta) doMknod(ctx Context, parent Ino, name string, _type uint8, mode
*inode = foundIno
}
return syscall.EEXIST
} else if parent == TrashInode {
if next, err := m.incrSessionCounter(s, "nextTrash", 1); err != nil {
return err
} else {
*inode = TrashInode + Ino(next)
}
}

n := node{Inode: *inode}
Expand Down
4 changes: 4 additions & 0 deletions pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,10 @@ func (m *kvMeta) doMknod(ctx Context, parent Ino, name string, _type uint8, mode
*inode = foundIno
}
return syscall.EEXIST
} else if parent == TrashInode { // user's inode is allocated by prefetch, trash inode is allocated on demand
key := m.counterKey("nextTrash")
next := tx.incrBy(key, 1)
*inode = TrashInode + Ino(next)
}

mode &= 07777
Expand Down

0 comments on commit 4826ace

Please sign in to comment.