Skip to content

Commit

Permalink
Fix: Wrap Around LeaseID
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTung97 committed Jul 8, 2021
1 parent 4505b29 commit b4bd13c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ func (l *leaseList) init(size uint32, expire uint32) {
l.expire = expire
}

func (l *leaseList) increase() {
l.nextLease++
if l.nextLease == 0 {
l.nextLease = 1
}
}

func (l *leaseList) getLease(hash uint32, now uint32) (uint32, bool) {
for i, e := range l.list {
if e.createdAt+l.expire <= now {
Expand All @@ -41,7 +48,7 @@ func (l *leaseList) getLease(hash uint32, now uint32) (uint32, bool) {
}
}

l.nextLease++
l.increase()
l.list[minIndex] = leaseEntry{
hash: hash,
lease: l.nextLease,
Expand Down
40 changes: 40 additions & 0 deletions lease_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memtable

import (
"math"
"testing"
"unsafe"
)
Expand Down Expand Up @@ -127,6 +128,45 @@ func TestLeaseList_GetLease_WhenFull(t *testing.T) {
assertEqualUint32(t, 7, id)
}

func TestLeaseList_GetLease_WhenFull_LeaseID_Check_Out_Of_Order(t *testing.T) {
var l leaseList
l.init(4, 4000)
l.getLease(100, 1000)
l.getLease(200, 1000)
l.getLease(300, 1000)
l.getLease(400, 1000)

assertEqualUint32(t, 4, l.list[3].lease)

affected := l.deleteLease(100, 1)
assertTrue(t, affected)

id, ok := l.getLease(500, 1000)
assertTrue(t, ok)
assertEqualUint32(t, 5, id)

id, ok = l.getLease(600, 1000)
assertTrue(t, ok)
assertEqualUint32(t, 6, id)

id, ok = l.getLease(300, 1200)
assertFalse(t, ok)
}

func TestLeaseList_SameHash_When_NextLease_Equal_Max(t *testing.T) {
var l leaseList
l.init(4, 4000)
l.nextLease = math.MaxUint32

id, ok := l.getLease(1234, 1000)
assertTrue(t, ok)
assertEqualUint32(t, 1, id)

id, ok = l.getLease(1234, 2000)
assertFalse(t, ok)
assertEqualUint32(t, 0, id)
}

func TestLeaseListSize(t *testing.T) {
size := unsafe.Sizeof(leaseList{})
if size != 64 {
Expand Down

0 comments on commit b4bd13c

Please sign in to comment.