Skip to content

Commit

Permalink
meta: move touchAtime to base meta (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD authored May 11, 2023
1 parent f558e43 commit 49d994d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 123 deletions.
35 changes: 26 additions & 9 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type engine interface {
doSetXattr(ctx Context, inode Ino, name string, value []byte, flags uint32) syscall.Errno
doRemoveXattr(ctx Context, inode Ino, name string) syscall.Errno
doRepair(ctx Context, inode Ino, attr *Attr) syscall.Errno
doTouchAtime(ctx Context, inode Ino, attr *Attr, ts time.Time) (bool, error)

doGetParents(ctx Context, inode Ino) map[Ino]int
doUpdateDirStat(ctx Context, batch map[Ino]dirStat) error
Expand All @@ -107,7 +108,6 @@ type engine interface {
scanPendingFiles(Context, pendingFileScan) error

GetSession(sid uint64, detail bool) (*Session, error)
touchAtime(ctx Context, inode Ino, attr *Attr) syscall.Errno
}

type trashSliceScan func(ss []Slice, ts int64) (clean bool, err error)
Expand Down Expand Up @@ -1495,19 +1495,38 @@ func (m *baseMeta) Rename(ctx Context, parentSrc Ino, nameSrc string, parentDst
return st
}

// caller makes sure inode is not special inode.
func (m *baseMeta) touchAtime(ctx Context, inode Ino, attr *Attr) {
if (m.conf.AtimeMode == NoAtime) || m.conf.ReadOnly {
return
}

if attr == nil {
attr = new(Attr)
m.of.Check(inode, attr)
}
now := time.Now()
if attr.Full && !m.atimeNeedsUpdate(attr, now) {
return
}

updated, err := m.en.doTouchAtime(ctx, inode, attr, now)
if updated {
m.of.Update(inode, attr)
} else if err != nil {
logger.Warnf("Update atime of inode %d: %s", inode, err)
}
}

func (m *baseMeta) Open(ctx Context, inode Ino, flags uint32, attr *Attr) (rerr syscall.Errno) {
if m.conf.ReadOnly && flags&(syscall.O_WRONLY|syscall.O_RDWR|syscall.O_TRUNC|syscall.O_APPEND) != 0 {
return syscall.EROFS
}

defer func() {
if rerr == 0 {
if err := m.en.touchAtime(ctx, inode, attr); err != 0 {
logger.Warnf("open %v update atime: %s", inode, err)
}
m.touchAtime(ctx, inode, attr)
}
}()

if m.conf.OpenCache > 0 && m.of.OpenCheck(inode, attr) {
return 0
}
Expand Down Expand Up @@ -1575,9 +1594,7 @@ func (m *baseMeta) Readdir(ctx Context, inode Ino, plus uint8, entries *[]*Entry
var attr Attr
defer func() {
if rerr == 0 {
if err := m.en.touchAtime(ctx, inode, &attr); err != 0 {
logger.Warnf("readdir %v update atime: %s", inode, err)
}
m.touchAtime(ctx, inode, &attr)
}
}()
inode = m.checkRoot(inode)
Expand Down
51 changes: 10 additions & 41 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2129,9 +2129,7 @@ func (m *redisMeta) doDeleteSustainedInode(sid uint64, inode Ino) error {
func (m *redisMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (rerr syscall.Errno) {
defer func() {
if rerr == 0 {
if err := m.touchAtime(ctx, inode, nil); err != 0 {
logger.Warnf("read %v update atime: %s", inode, err)
}
m.touchAtime(ctx, inode, nil)
}
}()
f := m.of.find(inode)
Expand Down Expand Up @@ -4287,52 +4285,23 @@ func (m *redisMeta) doAttachDirNode(ctx Context, parent Ino, dstIno Ino, name st
}, m.inodeKey(parent), m.entryKey(parent)))
}

// caller makes sure inode is not special inode.
func (m *redisMeta) touchAtime(ctx Context, ino Ino, cur *Attr) (rerr syscall.Errno) {
if (m.conf.AtimeMode != StrictAtime && m.conf.AtimeMode != RelAtime) || m.conf.ReadOnly {
return 0
}

var attr *Attr
newAttr := &Attr{}
if cur != nil {
attr = cur
} else if m.of.Check(ino, newAttr) {
attr = newAttr
}

now := time.Now()
if attr != nil && !m.atimeNeedsUpdate(attr, now) {
return 0
}

updated := false
defer func() {
if rerr == 0 && m.of.IsOpen(ino) && updated {
m.of.Update(ino, attr)
}
}()

return errno(m.txn(ctx, func(tx *redis.Tx) error {
if attr == nil {
attr = newAttr
}
a, err := tx.Get(ctx, m.inodeKey(ino)).Bytes()
func (m *redisMeta) doTouchAtime(ctx Context, inode Ino, attr *Attr, now time.Time) (bool, error) {
var updated bool
err := m.txn(ctx, func(tx *redis.Tx) error {
a, err := tx.Get(ctx, m.inodeKey(inode)).Bytes()
if err != nil {
return err
}
m.parseAttr(a, attr)

if !m.atimeNeedsUpdate(attr, now) {
return nil
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, m.inodeKey(ino), m.marshal(attr), 0)
return nil
})
updated = true
if err = tx.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0).Err(); err == nil {
updated = true
}
return err
}, m.inodeKey(ino)))
}, m.inodeKey(inode))
return updated, err
}
49 changes: 12 additions & 37 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2065,9 +2065,7 @@ func (m *dbMeta) doDeleteSustainedInode(sid uint64, inode Ino) error {
func (m *dbMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (rerr syscall.Errno) {
defer func() {
if rerr == 0 {
if err := m.touchAtime(ctx, inode, nil); err != 0 {
logger.Warnf("read %v update atime: %s", inode, err)
}
m.touchAtime(ctx, inode, nil)
}
}()
f := m.of.find(inode)
Expand Down Expand Up @@ -3923,36 +3921,10 @@ func (m *dbMeta) doAttachDirNode(ctx Context, parent Ino, inode Ino, name string
}, parent))
}

func (m *dbMeta) touchAtime(ctx Context, ino Ino, cur *Attr) (rerr syscall.Errno) {
if (m.conf.AtimeMode != StrictAtime && m.conf.AtimeMode != RelAtime) || m.conf.ReadOnly {
return 0
}

var curNode = node{Inode: ino}
var attr *Attr
newAttr := &Attr{}
if cur != nil {
attr = cur
} else if m.of.Check(ino, newAttr) {
attr = newAttr
}

now := time.Now()
if attr != nil && !m.atimeNeedsUpdate(attr, now) {
return 0
}

updated := false
defer func() {
if rerr == 0 && m.of.IsOpen(ino) && updated {
m.of.Update(ino, attr)
}
}()

return errno(m.txn(func(s *xorm.Session) error {
if attr == nil {
attr = newAttr
}
func (m *dbMeta) doTouchAtime(ctx Context, inode Ino, attr *Attr, now time.Time) (bool, error) {
var updated bool
err := m.txn(func(s *xorm.Session) error {
curNode := node{Inode: inode}
ok, err := s.ForUpdate().Get(&curNode)
if err != nil {
return err
Expand All @@ -3961,13 +3933,16 @@ func (m *dbMeta) touchAtime(ctx Context, ino Ino, cur *Attr) (rerr syscall.Errno
return syscall.ENOENT
}
m.parseAttr(&curNode, attr)

if !m.atimeNeedsUpdate(attr, now) {
return nil
}
curNode.Atime = now.Unix()*1e6 + int64(now.Nanosecond())/1e3
_, err = s.Cols("atime").Update(&curNode, &node{Inode: ino})
updated = true
attr.Atime = curNode.Atime / 1e6
attr.Atimensec = uint32(curNode.Atime % 1e6 * 1000)
if _, err = s.Cols("atime").Update(&curNode, &node{Inode: inode}); err == nil {
updated = true
}
return err
}))
})
return updated, err
}
44 changes: 8 additions & 36 deletions pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -1819,9 +1819,7 @@ func (m *kvMeta) doDeleteSustainedInode(sid uint64, inode Ino) error {
func (m *kvMeta) Read(ctx Context, inode Ino, indx uint32, slices *[]Slice) (rerr syscall.Errno) {
defer func() {
if rerr == 0 {
if err := m.touchAtime(ctx, inode, nil); err != 0 {
logger.Warnf("read %v update atime: %s", inode, err)
}
m.touchAtime(ctx, inode, nil)
}
}()
f := m.of.find(inode)
Expand Down Expand Up @@ -3409,48 +3407,22 @@ func (m *kvMeta) doAttachDirNode(ctx Context, parent Ino, inode Ino, name string
}, parent))
}

func (m *kvMeta) touchAtime(_ctx Context, ino Ino, cur *Attr) (rerr syscall.Errno) {
if (m.conf.AtimeMode != StrictAtime && m.conf.AtimeMode != RelAtime) || m.conf.ReadOnly {
return 0
}

var attr *Attr
newAttr := &Attr{}
if cur != nil {
attr = cur
} else if m.of.Check(ino, newAttr) {
attr = newAttr
}

now := time.Now()
if attr != nil && !m.atimeNeedsUpdate(attr, now) {
return 0
}

updated := false
defer func() {
if rerr == 0 && m.of.IsOpen(ino) && updated {
m.of.Update(ino, attr)
}
}()

return errno(m.client.txn(func(tx *kvTxn) error {
if attr == nil {
attr = newAttr
}
a := tx.get(m.inodeKey(ino))
func (m *kvMeta) doTouchAtime(ctx Context, inode Ino, attr *Attr, now time.Time) (bool, error) {
var updated bool
err := m.txn(func(tx *kvTxn) error {
a := tx.get(m.inodeKey(inode))
if a == nil {
return syscall.ENOENT
}
m.parseAttr(a, attr)

if !m.atimeNeedsUpdate(attr, now) {
return nil
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
tx.set(m.inodeKey(ino), m.marshal(attr))
tx.set(m.inodeKey(inode), m.marshal(attr))
updated = true
return nil
}, 0))
})
return updated, err
}

0 comments on commit 49d994d

Please sign in to comment.