Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert current write lock into read lock #4179

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/meta/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,12 @@ func testLocks(t *testing.T, m Meta) {
if st := m.Flock(ctx, inode, o1, syscall.F_WRLCK, false); st != 0 {
t.Fatalf("flock wlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_RDLCK, false); st != 0 {
t.Fatalf("flock rlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_WRLCK, false); st != 0 {
t.Fatalf("flock wlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_UNLCK, false); st != 0 {
t.Fatalf("flock unlock: %s", st)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/redis_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (r *redisMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, bl
if err != nil {
return err
}
delete(owners, lkey)
if ltype == F_RDLCK {
for _, v := range owners {
if v == "W" {
Expand All @@ -67,7 +68,6 @@ func (r *redisMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, bl
})
return err
}
delete(owners, lkey)
if len(owners) > 0 {
return syscall.EAGAIN
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/meta/sql_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ func (m *dbMeta) Flock(ctx Context, inode Ino, owner_ uint64, ltype uint32, bloc
locks[key{l.Sid, l.Owner}] = l
}

me := key{m.sid, owner}
flk, ok := locks[me]
delete(locks, me)
var typec byte = 'W'
if ltype == F_RDLCK {
for _, l := range locks {
if l.Ltype == 'W' {
return syscall.EAGAIN
}
}
return mustInsert(s, flock{Inode: inode, Owner: owner, Ltype: 'R', Sid: m.sid})
}
me := key{m.sid, owner}
flk, ok := locks[me]
delete(locks, me)
if len(locks) > 0 {
typec = 'R'
} else if len(locks) > 0 {
return syscall.EAGAIN
}
var n int64
if ok {
if flk.Ltype != 'W' {
n, err = s.Cols("Ltype").Update(&flock{Ltype: 'W'}, &flock{Inode: inode, Owner: owner, Sid: m.sid})
if flk.Ltype != typec {
n, err = s.Cols("Ltype").Update(&flock{Ltype: typec}, &flock{Inode: inode, Owner: owner, Sid: m.sid})
} else {
n = 1
}
} else {
n, err = s.InsertOne(&flock{Inode: inode, Owner: owner, Ltype: 'W', Sid: m.sid})
n, err = s.InsertOne(&flock{Inode: inode, Owner: owner, Ltype: typec, Sid: m.sid})
}
if err == nil && n == 0 {
err = fmt.Errorf("insert/update failed")
Expand Down
4 changes: 2 additions & 2 deletions pkg/meta/tkv_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (m *kvMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, block
case F_UNLCK:
delete(ls, lkey)
case F_RDLCK:
for _, l := range ls {
if l == 'W' {
for o, l := range ls {
if l == 'W' && o != lkey {
return syscall.EAGAIN
}
}
Expand Down