Skip to content

Commit

Permalink
export fields of plock records
Browse files Browse the repository at this point in the history
  • Loading branch information
davies committed Aug 30, 2022
1 parent e8b62a7 commit 817f758
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
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
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
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

0 comments on commit 817f758

Please sign in to comment.