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

meta: show detailed plock records for sessions #2627

Merged
merged 2 commits into from
Aug 30, 2022
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
2 changes: 1 addition & 1 deletion pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ type Flock struct {
type Plock struct {
Inode Ino
Owner uint64
Records []byte // FIXME: loadLocks
Records []plockRecord
}

// Session contains detailed information of a client session
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (m *redisMeta) getSession(sid string, detail bool) (*Session, error) {
if isFlock {
s.Flocks = append(s.Flocks, Flock{Ino(inode), owner, v})
} else {
s.Plocks = append(s.Plocks, Plock{Ino(inode), owner, []byte(v)})
s.Plocks = append(s.Plocks, Plock{Ino(inode), owner, loadLocks([]byte(v))})
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/meta/redis_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (r *redisMeta) Getlk(ctx Context, inode Ino, owner uint64, ltype *uint32, s
ls := loadLocks([]byte(d))
for _, l := range ls {
// find conflicted locks
if (*ltype == F_WRLCK || l.ltype == F_WRLCK) && *end >= l.start && *start <= l.end {
*ltype = l.ltype
*start = l.start
*end = l.end
if (*ltype == F_WRLCK || l.Type == F_WRLCK) && *end >= l.Start && *start <= l.End {
*ltype = l.Type
*start = l.Start
*end = l.End
sid, _ := strconv.Atoi(strings.Split(k, "_")[0])
if uint64(sid) == r.sid {
*pid = l.pid
*pid = l.Pid
} else {
*pid = 0
}
Expand Down Expand Up @@ -178,7 +178,7 @@ func (r *redisMeta) Setlk(ctx Context, inode Ino, owner uint64, block bool, ltyp
ls := loadLocks([]byte(d))
for _, l := range ls {
// find conflicted locks
if (ltype == F_WRLCK || l.ltype == F_WRLCK) && end >= l.start && start <= l.end {
if (ltype == F_WRLCK || l.Type == F_WRLCK) && end >= l.Start && start <= l.End {
return syscall.EAGAIN
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (m *dbMeta) getSession(row interface{}, detail bool) (*Session, error) {
}
s.Plocks = make([]Plock, 0, len(prows))
for _, prow := range prows {
s.Plocks = append(s.Plocks, Plock{prow.Inode, uint64(prow.Owner), prow.Records})
s.Plocks = append(s.Plocks, Plock{prow.Inode, uint64(prow.Owner), loadLocks(prow.Records)})
}
return nil
})
Expand Down
12 changes: 6 additions & 6 deletions pkg/meta/sql_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func (m *dbMeta) Getlk(ctx Context, inode Ino, owner_ uint64, ltype *uint32, sta
ls := loadLocks(d)
for _, l := range ls {
// find conflicted locks
if (*ltype == F_WRLCK || l.ltype == F_WRLCK) && *end >= l.start && *start <= l.end {
*ltype = l.ltype
*start = l.start
*end = l.end
if (*ltype == F_WRLCK || l.Type == F_WRLCK) && *end >= l.Start && *start <= l.End {
*ltype = l.Type
*start = l.Start
*end = l.End
if k.sid == m.sid {
*pid = l.pid
*pid = l.Pid
} else {
*pid = 0
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func (m *dbMeta) Setlk(ctx Context, inode Ino, owner_ uint64, block bool, ltype
ls := loadLocks(d)
for _, l := range ls {
// find conflicted locks
if (ltype == F_WRLCK || l.ltype == F_WRLCK) && end >= l.start && start <= l.end {
if (ltype == F_WRLCK || l.Type == F_WRLCK) && end >= l.Start && start <= l.End {
return syscall.EAGAIN
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (m *kvMeta) getSession(sid uint64, detail bool) (*Session, error) {
ls := unmarshalPlock(v)
for o, l := range ls {
if o.sid == sid {
s.Plocks = append(s.Plocks, Plock{inode, o.sid, l})
s.Plocks = append(s.Plocks, Plock{inode, o.sid, loadLocks(l)})
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/meta/tkv_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ func (m *kvMeta) Getlk(ctx Context, inode Ino, owner uint64, ltype *uint32, star
ls := loadLocks(records)
for _, l := range ls {
// find conflicted locks
if (*ltype == F_WRLCK || l.ltype == F_WRLCK) && *end >= l.start && *start <= l.end {
*ltype = l.ltype
*start = l.start
*end = l.end
if (*ltype == F_WRLCK || l.Type == F_WRLCK) && *end >= l.Start && *start <= l.End {
*ltype = l.Type
*start = l.Start
*end = l.End
if o.sid == m.sid {
*pid = l.pid
*pid = l.Pid
} else {
*pid = 0
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (m *kvMeta) Setlk(ctx Context, inode Ino, owner uint64, block bool, ltype u
ls := loadLocks(d)
for _, l := range ls {
// find conflicted locks
if (ltype == F_WRLCK || l.ltype == F_WRLCK) && end >= l.start && start <= l.end {
if (ltype == F_WRLCK || l.Type == F_WRLCK) && end >= l.Start && start <= l.End {
return syscall.EAGAIN
}
}
Expand Down
56 changes: 28 additions & 28 deletions pkg/meta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func lookupSubdir(m Meta, subdir string) (Ino, error) {
}

type plockRecord struct {
ltype uint32
pid uint32
start uint64
end uint64
Type uint32
Pid uint32
Start uint64
End uint64
}

func loadLocks(d []byte) []plockRecord {
Expand All @@ -174,58 +174,58 @@ func loadLocks(d []byte) []plockRecord {
func dumpLocks(ls []plockRecord) []byte {
wb := utils.NewBuffer(uint32(len(ls)) * 24)
for _, l := range ls {
wb.Put32(l.ltype)
wb.Put32(l.pid)
wb.Put64(l.start)
wb.Put64(l.end)
wb.Put32(l.Type)
wb.Put32(l.Pid)
wb.Put64(l.Start)
wb.Put64(l.End)
}
return wb.Bytes()
}

func updateLocks(ls []plockRecord, nl plockRecord) []plockRecord {
// ls is ordered by l.start without overlap
size := len(ls)
for i := 0; i < size && nl.start <= nl.end; i++ {
for i := 0; i < size && nl.Start <= nl.End; i++ {
l := ls[i]
if nl.start < l.start && nl.end >= l.start {
if nl.Start < l.Start && nl.End >= l.Start {
// split nl
ls = append(ls, nl)
ls[len(ls)-1].end = l.start - 1
nl.start = l.start
ls[len(ls)-1].End = l.Start - 1
nl.Start = l.Start
}
if nl.start > l.start && nl.start <= l.end {
if nl.Start > l.Start && nl.Start <= l.End {
// split l
l.end = nl.start - 1
l.End = nl.Start - 1
ls = append(ls, l)
ls[i].start = nl.start
ls[i].Start = nl.Start
l = ls[i]
}
if nl.start == l.start {
ls[i].ltype = nl.ltype // update l
ls[i].pid = nl.pid
if l.end > nl.end {
if nl.Start == l.Start {
ls[i].Type = nl.Type // update l
ls[i].Pid = nl.Pid
if l.End > nl.End {
// split l
ls[i].end = nl.end
l.start = nl.end + 1
ls[i].End = nl.End
l.Start = nl.End + 1
ls = append(ls, l)
}
nl.start = ls[i].end + 1
nl.Start = ls[i].End + 1
}
}
if nl.start <= nl.end {
if nl.Start <= nl.End {
ls = append(ls, nl)
}
sort.Slice(ls, func(i, j int) bool { return ls[i].start < ls[j].start })
sort.Slice(ls, func(i, j int) bool { return ls[i].Start < ls[j].Start })
for i := 0; i < len(ls); {
if ls[i].ltype == F_UNLCK || ls[i].start > ls[i].end {
if ls[i].Type == F_UNLCK || ls[i].Start > ls[i].End {
// remove empty one
copy(ls[i:], ls[i+1:])
ls = ls[:len(ls)-1]
} else {
if i+1 < len(ls) && ls[i].ltype == ls[i+1].ltype && ls[i].pid == ls[i+1].pid && ls[i].end+1 == ls[i+1].start {
if i+1 < len(ls) && ls[i].Type == ls[i+1].Type && ls[i].Pid == ls[i+1].Pid && ls[i].End+1 == ls[i+1].Start {
// combine continuous range
ls[i].end = ls[i+1].end
ls[i+1].start = ls[i+1].end + 1
ls[i].End = ls[i+1].End
ls[i+1].Start = ls[i+1].End + 1
}
i++
}
Expand Down