Skip to content

Commit

Permalink
meta: update atime in transaction
Browse files Browse the repository at this point in the history
Signed-off-by: Eryu Guan <[email protected]>
  • Loading branch information
eryugey committed May 9, 2023
1 parent 13bd564 commit c19aef5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 69 deletions.
29 changes: 9 additions & 20 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4269,34 +4269,23 @@ func (m *redisMeta) touchAtime(ctx Context, ino Ino, cur *Attr) (rerr syscall.Er
}
}()

now := time.Now()

// Already got attr, update atime without transaction
if attr != nil {
if !m.atimeNeedsUpdate(attr, now) {
return 0
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
updated = true
return errno(m.rdb.Set(ctx, m.inodeKey(ino), m.marshal(attr), 0).Err())
}

// Should get attr first, do it in transaction
return errno(m.txn(ctx, func(tx *redis.Tx) error {
attr = newAttr
a, err := tx.Get(ctx, m.inodeKey(ino)).Bytes()
if err != nil {
return err
if attr == nil {
attr = newAttr
a, err := tx.Get(ctx, m.inodeKey(ino)).Bytes()
if err != nil {
return err
}
m.parseAttr(a, attr)
}
m.parseAttr(a, attr)

now := time.Now()
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 {
_, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, m.inodeKey(ino), m.marshal(attr), 0)
return nil
})
Expand Down
37 changes: 12 additions & 25 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3929,38 +3929,25 @@ func (m *dbMeta) touchAtime(ctx Context, ino Ino, cur *Attr) (rerr syscall.Errno
}
}()

now := time.Now()

// Already got attr, update atime without transaction
if attr != nil {
if !m.atimeNeedsUpdate(attr, now) {
return 0
}
curNode.Atime = now.Unix()*1e6 + int64(now.Nanosecond())/1e3
s := m.db.NewSession()
defer s.Close()
_, err := s.Cols("atime").Update(&curNode, &node{Inode: ino})
updated = true
return errno(err)
}

// Should get attr first, do it in transaction
return errno(m.txn(func(s *xorm.Session) error {
attr = newAttr
ok, err := s.ForUpdate().Get(&curNode)
if err != nil {
return err
}
if !ok {
return syscall.ENOENT
if attr == nil {
attr = newAttr
ok, err := s.ForUpdate().Get(&curNode)
if err != nil {
return err
}
if !ok {
return syscall.ENOENT
}
m.parseAttr(&curNode, attr)
}

m.parseAttr(&curNode, attr)
now := time.Now()
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})
_, err := s.Cols("atime").Update(&curNode, &node{Inode: ino})
updated = true
return err
}))
Expand Down
33 changes: 9 additions & 24 deletions pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3408,30 +3408,15 @@ func (m *kvMeta) touchAtime(_ctx Context, ino Ino, cur *Attr) (rerr syscall.Errn
}
}()

now := time.Now()

// Already got attr, update atime without transaction
if attr != nil {
if !m.atimeNeedsUpdate(attr, now) {
return 0
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
updated = true
return errno(m.client.txn(func(tx *kvTxn) error {
tx.set(m.inodeKey(ino), m.marshal(attr))
return nil
}, 0))
}

// Should get attr first, do it in transaction
return errno(m.txn(func(tx *kvTxn) error {
attr = newAttr
a := tx.get(m.inodeKey(ino))
if a == nil {
return syscall.ENOENT
return errno(m.client.txn(func(tx *kvTxn) error {
if attr == nil {
attr = newAttr
a := tx.get(m.inodeKey(ino))
if a == nil {
return syscall.ENOENT
}
m.parseAttr(a, attr)
}
m.parseAttr(a, attr)

now := time.Now()
if !m.atimeNeedsUpdate(attr, now) {
Expand All @@ -3442,5 +3427,5 @@ func (m *kvMeta) touchAtime(_ctx Context, ino Ino, cur *Attr) (rerr syscall.Errn
tx.set(m.inodeKey(ino), m.marshal(attr))
updated = true
return nil
}))
}, 0))
}

0 comments on commit c19aef5

Please sign in to comment.