Skip to content

Commit d128a10

Browse files
authored
Merge pull request #823 from ahrtr/rollback_alloc_20240819_1.3
[1.3] Rollback alloc map: remove all page ids which are allocated by the txid
2 parents 8c9b349 + 94db72d commit d128a10

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

freelist.go

+8
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ func (f *freelist) rollback(txid txid) {
252252
}
253253
// Remove pages from pending list and mark as free if allocated by txid.
254254
delete(f.pending, txid)
255+
256+
// Remove pgids which are allocated by this txid
257+
for pgid, tid := range f.allocs {
258+
if tid == txid {
259+
delete(f.allocs, pgid)
260+
}
261+
}
262+
255263
f.mergeSpans(m)
256264
}
257265

freelist_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"sort"
88
"testing"
99
"unsafe"
10+
11+
"github.com/stretchr/testify/require"
1012
)
1113

1214
// TestFreelistType is used as a env variable for test to indicate the backend type
@@ -253,6 +255,38 @@ func TestFreelistArray_allocate(t *testing.T) {
253255
}
254256
}
255257

258+
func Test_Freelist_Rollback(t *testing.T) {
259+
f := newTestFreelist()
260+
261+
f.readIDs([]pgid{3, 5, 6, 7, 12, 13})
262+
263+
f.free(100, &page{
264+
id: 20,
265+
flags: 0,
266+
count: 0,
267+
overflow: 1,
268+
})
269+
f.allocate(100, 3)
270+
f.free(100, &page{
271+
id: 25,
272+
flags: 0,
273+
count: 0,
274+
overflow: 0,
275+
})
276+
f.allocate(100, 2)
277+
278+
require.Equal(t, map[pgid]txid{5: 100, 12: 100}, f.allocs)
279+
require.Equal(t, map[txid]*txPending{100: {
280+
ids: []pgid{20, 21, 25},
281+
alloctx: []txid{0, 0, 0},
282+
}}, f.pending)
283+
284+
f.rollback(100)
285+
286+
require.Equal(t, map[pgid]txid{}, f.allocs)
287+
require.Equal(t, map[txid]*txPending{}, f.pending)
288+
}
289+
256290
// Ensure that a freelist can deserialize from a freelist page.
257291
func TestFreelist_read(t *testing.T) {
258292
// Create a page.

0 commit comments

Comments
 (0)