Skip to content

Commit

Permalink
meta/redis: fix some transactions that are not wrapped by multi/exec (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SandyXSD committed Jun 19, 2024
1 parent 790db41 commit ab13802
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,23 @@ func (m *redisMeta) incrCounter(name string, value int64) (int64, error) {

func (m *redisMeta) setIfSmall(name string, value, diff int64) (bool, error) {
var changed bool
ctx := Background
name = m.prefix + name
err := m.txn(Background, func(tx *redis.Tx) error {
err := m.txn(ctx, func(tx *redis.Tx) error {
changed = false
old, err := tx.Get(Background, name).Int64()
old, err := tx.Get(ctx, name).Int64()
if err != nil && err != redis.Nil {
return err
}
if old > value-diff {
return nil
} else {
changed = true
return tx.Set(Background, name, value, 0).Err()
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, name, value, 0)
return nil
})
return err
}
}, name)

Expand Down Expand Up @@ -1184,7 +1189,11 @@ func (m *redisMeta) doReadlink(ctx Context, inode Ino, noatime bool) (atime int6
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
return tx.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0).Err()
_, e = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0)
return nil
})
return e
}, m.inodeKey(inode))
atime = attr.Atime
return
Expand Down Expand Up @@ -3386,7 +3395,11 @@ func (m *redisMeta) doRepair(ctx Context, inode Ino, attr *Attr) syscall.Errno {
attr.Nlink++
}
}
return tx.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0).Err()
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0)
return nil
})
return err
}, m.inodeKey(inode), m.entryKey(inode)))
}

Expand Down Expand Up @@ -3435,10 +3448,13 @@ func (m *redisMeta) doSetXattr(ctx Context, inode Ino, name string, value []byte
} else if !ok {
return ENOATTR
}
_, err := m.rdb.HSet(ctx, key, name, value).Result()
_, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.HSet(ctx, key, name, value)
return nil
})
return err
default: // XattrCreateOrReplace
_, err := m.rdb.HSet(ctx, key, name, value).Result()
_, err := tx.HSet(ctx, key, name, value).Result()
return err
}
}, key))
Expand Down Expand Up @@ -4413,7 +4429,10 @@ func (m *redisMeta) doTouchAtime(ctx Context, inode Ino, attr *Attr, now time.Ti
}
attr.Atime = now.Unix()
attr.Atimensec = uint32(now.Nanosecond())
if err = tx.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0).Err(); err == nil {
if _, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0)
return nil
}); err == nil {
updated = true
}
return err
Expand Down

0 comments on commit ab13802

Please sign in to comment.