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

ensure hashmap init clears maps #794

Merged
merged 2 commits into from
Jul 30, 2024
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
1 change: 1 addition & 0 deletions internal/freelist/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (f *array) mergeSpans(ids common.Pgids) {
func NewArrayFreelist() Interface {
a := &array{
shared: newShared(),
ids: []common.Pgid{},
}
a.Interface = a
return a
Expand Down
91 changes: 66 additions & 25 deletions internal/freelist/freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"unsafe"

"github.com/stretchr/testify/require"

"go.etcd.io/bbolt/internal/common"
)

Expand Down Expand Up @@ -85,7 +87,7 @@ func TestFreelist_releaseRange(t *testing.T) {
title: "Single pending outsize minimum end range",
pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},
releaseRanges: []testRange{{1, 199}},
wantFree: nil,
wantFree: []common.Pgid{},
},
{
title: "Single pending with minimum begin range",
Expand All @@ -97,7 +99,7 @@ func TestFreelist_releaseRange(t *testing.T) {
title: "Single pending outside minimum begin range",
pagesIn: []testPage{{id: 3, n: 1, allocTxn: 100, freeTxn: 200}},
releaseRanges: []testRange{{101, 300}},
wantFree: nil,
wantFree: []common.Pgid{},
},
{
title: "Single pending in minimum range",
Expand All @@ -109,7 +111,7 @@ func TestFreelist_releaseRange(t *testing.T) {
title: "Single pending and read transaction at 199",
pagesIn: []testPage{{id: 3, n: 1, allocTxn: 199, freeTxn: 200}},
releaseRanges: []testRange{{100, 198}, {200, 300}},
wantFree: nil,
wantFree: []common.Pgid{},
},
{
title: "Adjacent pending and read transactions at 199, 200",
Expand All @@ -122,7 +124,7 @@ func TestFreelist_releaseRange(t *testing.T) {
{200, 199}, // Simulate the ranges db.freePages might produce.
{201, 300},
},
wantFree: nil,
wantFree: []common.Pgid{},
},
{
title: "Out of order ranges",
Expand All @@ -135,7 +137,7 @@ func TestFreelist_releaseRange(t *testing.T) {
{201, 200},
{200, 200},
},
wantFree: nil,
wantFree: []common.Pgid{},
},
{
title: "Multiple pending, read transaction at 150",
Expand All @@ -153,32 +155,71 @@ func TestFreelist_releaseRange(t *testing.T) {
}

for _, c := range releaseRangeTests {
f := newTestFreelist()
var ids []common.Pgid
for _, p := range c.pagesIn {
for i := uint64(0); i < uint64(p.n); i++ {
ids = append(ids, common.Pgid(uint64(p.id)+i))
t.Run(c.title, func(t *testing.T) {
f := newTestFreelist()
var ids []common.Pgid
for _, p := range c.pagesIn {
for i := uint64(0); i < uint64(p.n); i++ {
ids = append(ids, common.Pgid(uint64(p.id)+i))
}
}
f.Init(ids)
for _, p := range c.pagesIn {
f.Allocate(p.allocTxn, p.n)
}
}
f.Init(ids)
for _, p := range c.pagesIn {
f.Allocate(p.allocTxn, p.n)
}

for _, p := range c.pagesIn {
f.Free(p.freeTxn, common.NewPage(p.id, 0, 0, uint32(p.n-1)))
}
for _, p := range c.pagesIn {
f.Free(p.freeTxn, common.NewPage(p.id, 0, 0, uint32(p.n-1)))
}

for _, r := range c.releaseRanges {
f.releaseRange(r.begin, r.end)
}
for _, r := range c.releaseRanges {
f.releaseRange(r.begin, r.end)
}

if exp := common.Pgids(c.wantFree); !reflect.DeepEqual(exp, f.freePageIds()) {
t.Errorf("exp=%v; got=%v for %s", exp, f.freePageIds(), c.title)
}
require.Equal(t, common.Pgids(c.wantFree), f.freePageIds())
})
}
}

func TestFreeList_init(t *testing.T) {
buf := make([]byte, 4096)
f := newTestFreelist()
f.Init(common.Pgids{5, 6, 8})

p := common.LoadPage(buf)
f.Write(p)

f2 := newTestFreelist()
f2.Read(p)
require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())

// When initializing the freelist with an empty list of page ID,
// it should reset the freelist page IDs.
f2.Init([]common.Pgid{})
require.Equal(t, common.Pgids{}, f2.freePageIds())
}

func TestFreeList_reload(t *testing.T) {
buf := make([]byte, 4096)
f := newTestFreelist()
f.Init(common.Pgids{5, 6, 8})

p := common.LoadPage(buf)
f.Write(p)

f2 := newTestFreelist()
f2.Read(p)
require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())

f2.Free(common.Txid(5), common.NewPage(10, common.LeafPageFlag, 0, 2))

// reload shouldn't affect the pending list
f2.Reload(p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you reload the freelist from a page, it means you already read the freelist from the page in the first place.


require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())
require.Equal(t, []common.Pgid{10, 11, 12}, f2.pendingPageIds()[5].ids)
}

// Ensure that a freelist can deserialize from a freelist page.
func TestFreelist_read(t *testing.T) {
// Create a page.
Expand Down Expand Up @@ -263,7 +304,7 @@ func Test_freelist_ReadIDs_and_getFreePageIDs(t *testing.T) {
}

f2 := newTestFreelist()
var exp2 []common.Pgid
exp2 := []common.Pgid{}
f2.Init(exp2)

if got2 := f2.freePageIds(); !reflect.DeepEqual(got2, common.Pgids(exp2)) {
Expand Down
18 changes: 9 additions & 9 deletions internal/freelist/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ type hashMap struct {
}

func (f *hashMap) Init(pgids common.Pgids) {
// reset the counter when freelist init
f.freePagesCount = 0
f.freemaps = make(map[uint64]pidSet)
f.forwardMap = make(map[common.Pgid]uint64)
f.backwardMap = make(map[common.Pgid]uint64)

if len(pgids) == 0 {
return
}

size := uint64(1)
start := pgids[0]
// reset the counter when freelist init
f.freePagesCount = 0

if !sort.SliceIsSorted([]common.Pgid(pgids), func(i, j int) bool { return pgids[i] < pgids[j] }) {
panic("pgids not sorted")
}

f.freemaps = make(map[uint64]pidSet)
f.forwardMap = make(map[common.Pgid]uint64)
f.backwardMap = make(map[common.Pgid]uint64)
size := uint64(1)
start := pgids[0]

for i := 1; i < len(pgids); i++ {
// continuous page
Expand Down Expand Up @@ -117,7 +117,7 @@ func (f *hashMap) FreeCount() int {
func (f *hashMap) freePageIds() common.Pgids {
count := f.FreeCount()
if count == 0 {
return nil
return common.Pgids{}
}

m := make([]common.Pgid, 0, count)
Expand Down
2 changes: 1 addition & 1 deletion internal/freelist/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (t *shared) releaseRange(begin, end common.Txid) {
if begin > end {
return
}
var m common.Pgids
m := common.Pgids{}
for tid, txp := range t.pending {
if tid < begin || tid > end {
continue
Expand Down