Skip to content
Merged
102 changes: 102 additions & 0 deletions internal/bitmap/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"fmt"
"log"
"math/bits"
"slices"
"sync"
"time"

"github.com/supporttools/GoClusterFS/internal/lock"
"github.com/supporttools/GoClusterFS/internal/storage"
Expand Down Expand Up @@ -50,6 +52,8 @@ type TrimQueuer interface {
Queue(blockNum uint64)
// QueueRange adds multiple contiguous freed blocks to the TRIM queue.
QueueRange(startBlock, count uint64)
// SetFreeGuard makes the manager discard only blocks the guard confirms are free.
SetFreeGuard(guard func(ctx context.Context, blocks []uint64, discard func(free []uint64)) error)
}

// Region manages block allocation using a bitmap stored on disk.
Expand Down Expand Up @@ -274,6 +278,104 @@ func (r *Region) SetTrimManager(tm TrimQueuer) {
r.mu.Lock()
defer r.mu.Unlock()
r.trimMgr = tm
if tm != nil {
tm.SetFreeGuard(r.withFreeBlocks)
}
}

// withFreeBlocks marks the blocks that are still free as allocated, calls
// discard without the bitmap lock, then frees them again. A discard can outlast
// the lock's stale threshold, so no lock is held while it runs. A crash during
// the discard leaves the blocks allocated and unreferenced, which fsck reports.
func (r *Region) withFreeBlocks(ctx context.Context, blocks []uint64, discard func(free []uint64)) error {
reserved, err := r.flipBits(ctx, blocks, true)
if err != nil {
r.releaseReserved(ctx, reserved)
return err
}
if len(reserved) > 0 {
discard(reserved)
r.releaseReserved(ctx, reserved)
}
return nil
}

const (
// releaseTimeout bounds each attempt to take the bitmap lock when freeing reserved blocks.
releaseTimeout = 30 * time.Second
// releaseDeadline bounds all attempts, so a failing device cannot hang unmount.
releaseDeadline = 5 * time.Minute
)

// releaseReserved frees blocks reserved for a discard. ctx may have run out
// during a slow discard, and fsck cannot repair a leaked reservation, so it
// retries until every block is free or releaseDeadline passes. Nothing else
// references these blocks.
func (r *Region) releaseReserved(ctx context.Context, blocks []uint64) {
ctx = context.WithoutCancel(ctx)
deadline := time.Now().Add(releaseDeadline)
for attempt := 1; len(blocks) > 0; attempt++ {
if time.Now().After(deadline) {
log.Printf("[ERROR] bitmap: gave up freeing blocks %v reserved for TRIM after %v; fsck will report them as leaked", blocks, releaseDeadline)
return
}
attemptCtx, cancel := context.WithTimeout(ctx, releaseTimeout)
freed, err := r.flipBits(attemptCtx, blocks, false)
cancel()
blocks = slices.DeleteFunc(blocks, func(b uint64) bool { return slices.Contains(freed, b) })
if err == nil {
return
}
log.Printf("[WARN] bitmap: freeing %d blocks reserved for TRIM failed (attempt %d), retrying: %v", len(blocks), attempt, err)
time.Sleep(time.Second)
}
}

// flipBits marks each block allocated (or free) whose bit says otherwise, under
// the bitmap lock, and returns the blocks it changed, even on error.
func (r *Region) flipBits(ctx context.Context, blocks []uint64, allocated bool) ([]uint64, error) {
if r.lockMgr != nil {
if err := r.lockMgr.Acquire(ctx, lock.LockTypeBlockBitmap, 0, lock.LockModeExclusive); err != nil {
return nil, fmt.Errorf("failed to acquire bitmap lock: %w", err)
}
defer r.unlock()
}

r.mu.Lock()
defer r.mu.Unlock()

bySector := make(map[uint64][]uint64)
for _, b := range blocks {
if b < r.totalBlocks {
bySector[b/BitsPerSector] = append(bySector[b/BitsPerSector], b)
}
}

var changed []uint64
for sectorIdx, sectorBlocks := range bySector {
data, err := r.readSector(sectorIdx)
if err != nil {
return changed, fmt.Errorf("failed to read bitmap sector %d: %w", sectorIdx, err)
}
var flipped []uint64
for _, b := range sectorBlocks {
bit := b % BitsPerSector
mask := byte(1) << (bit % 8)
if (data[bit/8]&mask != 0) == allocated {
continue
}
data[bit/8] ^= mask
flipped = append(flipped, b)
}
if len(flipped) == 0 {
continue
}
if err := r.writeSector(sectorIdx, data); err != nil {
return changed, fmt.Errorf("failed to write bitmap sector %d: %w", sectorIdx, err)
}
changed = append(changed, flipped...)
}
return changed, nil
}

// Scan reads the entire bitmap and returns free/allocated counts.
Expand Down
265 changes: 265 additions & 0 deletions internal/bitmap/trim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package bitmap

import (
"context"
"sync"
"testing"
"time"

"github.com/supporttools/GoClusterFS/internal/lock"
"github.com/supporttools/GoClusterFS/internal/trim"
)

type discardRecorder struct {
mu sync.Mutex
discarded map[uint64]bool
}

func (d *discardRecorder) discard(offset, length uint64) error {
d.mu.Lock()
defer d.mu.Unlock()
for b := offset / trim.BlockSize; b < (offset+length)/trim.BlockSize; b++ {
d.discarded[b] = true
}
return nil
}

func (d *discardRecorder) has(block uint64) bool {
d.mu.Lock()
defer d.mu.Unlock()
return d.discarded[block]
}

func newTrimmedRegion(t *testing.T, batch int, maxAge time.Duration) (*Region, *trim.Manager, *discardRecorder, func()) {
t.Helper()
dev, cleanup := createTestDevice(t, 10<<20)
rec := &discardRecorder{discarded: make(map[uint64]bool)}
tm := trim.NewManager(dev, trim.Config{BatchSize: batch, MaxAge: maxAge, Enabled: true, Discard: rec.discard})
region := NewRegion(dev, 0, 1000, nil, [16]byte{1})
region.SetTrimManager(tm)
tm.Start()
return region, tm, rec, func() {
_ = tm.Stop()
cleanup()
}
}

func waitFor(t *testing.T, cond func() bool) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for !cond() {
if time.Now().After(deadline) {
t.Fatal("condition not met within 5s")
}
time.Sleep(5 * time.Millisecond)
}
}

func TestTrimSkipsBlockReallocatedBeforeFlush(t *testing.T) {
region, _, rec, cleanup := newTrimmedRegion(t, 1000, 50*time.Millisecond)
defer cleanup()
ctx := context.Background()

blocks, err := region.Alloc(ctx, 3)
if err != nil {
t.Fatal(err)
}
reused, stillFree := blocks[0], blocks[2]
for _, b := range []uint64{reused, stillFree} {
if err := region.Free(ctx, b); err != nil {
t.Fatal(err)
}
}
again, err := region.Alloc(ctx, 1)
if err != nil {
t.Fatal(err)
}
if again[0] != reused {
t.Fatalf("Alloc after Free got block %d, want the freed block %d", again[0], reused)
}

// Ranges are discarded in block order, so once stillFree is discarded the
// flush is past reused.
waitFor(t, func() bool { return rec.has(stillFree) })
if rec.has(reused) {
t.Errorf("block %d was discarded after it was allocated again", reused)
}
}

func TestTrimSkipsBlockAnotherNodeAllocated(t *testing.T) {
region, tm, rec, cleanup := newTrimmedRegion(t, 1000, time.Hour)
defer cleanup()
other := NewRegion(region.dev, 0, 1000, nil, [16]byte{2})
ctx := context.Background()

blocks, err := region.Alloc(ctx, 1)
if err != nil {
t.Fatal(err)
}
if err := region.Free(ctx, blocks[0]); err != nil {
t.Fatal(err)
}
theirs, err := other.Alloc(ctx, 1)
if err != nil {
t.Fatal(err)
}
if theirs[0] != blocks[0] {
t.Fatalf("other node got block %d, want the freed block %d", theirs[0], blocks[0])
}

if err := tm.Stop(); err != nil {
t.Fatal(err)
}
if rec.has(blocks[0]) {
t.Errorf("block %d was discarded after another node allocated it", blocks[0])
}
}

func TestTrimDiscardsFreedBlocks(t *testing.T) {
region, tm, rec, cleanup := newTrimmedRegion(t, 1000, time.Hour)
defer cleanup()
ctx := context.Background()

blocks, err := region.Alloc(ctx, 3)
if err != nil {
t.Fatal(err)
}
for _, b := range blocks {
if err := region.Free(ctx, b); err != nil {
t.Fatal(err)
}
}

if err := tm.Stop(); err != nil {
t.Fatal(err)
}
for _, b := range blocks {
if !rec.has(b) {
t.Errorf("free block %d was not discarded", b)
}
}
}

// TestTrimBatchFlushFromFree checks that a full batch does not flush inside
// Free, which holds the bitmap lock the flush needs.
func TestTrimBatchFlushFromFree(t *testing.T) {
region, _, rec, cleanup := newTrimmedRegion(t, 1, time.Hour)
defer cleanup()
ctx := context.Background()

blocks, err := region.Alloc(ctx, 1)
if err != nil {
t.Fatal(err)
}

done := make(chan error, 1)
go func() { done <- region.Free(ctx, blocks[0]) }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("Free did not return: the batch flush deadlocked on the bitmap lock")
}

waitFor(t, func() bool { return rec.has(blocks[0]) })
}

// TestTrimDiscardHoldsNoBitmapLock runs the guard with real lock managers on two
// nodes. A discard can outlast the lock's 15s stale threshold, so the block must
// stay reserved without the lock held, and other nodes must keep allocating.
func TestTrimDiscardHoldsNoBitmapLock(t *testing.T) {
dev, cleanup := createTestDevice(t, 10<<20)
defer cleanup()
lockA := lock.NewManager(dev, 1<<20, 1<<20, [16]byte{1}, 0)
lockB := lock.NewManager(dev, 1<<20, 1<<20, [16]byte{2}, 1)

started := make(chan struct{})
finish := make(chan struct{})
var once sync.Once
tm := trim.NewManager(dev, trim.Config{BatchSize: 1000, MaxAge: time.Hour, Enabled: true, Discard: func(offset, length uint64) error {
once.Do(func() { close(started) })
<-finish
return nil
}})
a := NewRegion(dev, 0, 1000, lockA, [16]byte{1})
a.SetTrimManager(tm)
tm.Start()
b := NewRegion(dev, 0, 1000, lockB, [16]byte{2})
ctx := context.Background()

blocks, err := a.Alloc(ctx, 1)
if err != nil {
t.Fatal(err)
}
if err := a.Free(ctx, blocks[0]); err != nil {
t.Fatal(err)
}

stopped := make(chan error, 1)
go func() { stopped <- tm.Stop() }()
<-started

if free, err := b.IsFree(blocks[0]); err != nil || free {
t.Errorf("block %d being discarded: IsFree = %v, %v; want reserved", blocks[0], free, err)
}
allocCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
theirs, err := b.Alloc(allocCtx, 1)
cancel()
if err != nil {
t.Fatalf("node B could not allocate during node A's discard: %v", err)
}
if theirs[0] == blocks[0] {
t.Errorf("node B allocated block %d while node A was discarding it", blocks[0])
}

close(finish)
if err := <-stopped; err != nil {
t.Fatal(err)
}
if free, err := a.IsFree(blocks[0]); err != nil || !free {
t.Errorf("block %d after discard: IsFree = %v, %v; want free again", blocks[0], free, err)
}
}

// TestTrimReleasesReservationAfterSlowDiscard checks that the reserved blocks are
// freed even when the flush's context ran out during the discard and another
// node holds the bitmap lock at that moment.
func TestTrimReleasesReservationAfterSlowDiscard(t *testing.T) {
dev, cleanup := createTestDevice(t, 10<<20)
defer cleanup()
lockA := lock.NewManager(dev, 1<<20, 1<<20, [16]byte{1}, 0)
lockB := lock.NewManager(dev, 1<<20, 1<<20, [16]byte{2}, 1)
a := NewRegion(dev, 0, 1000, lockA, [16]byte{1})
bg := context.Background()

blocks, err := a.Alloc(bg, 1)
if err != nil {
t.Fatal(err)
}
if err := a.Free(bg, blocks[0]); err != nil {
t.Fatal(err)
}

ctx, cancel := context.WithTimeout(bg, 100*time.Millisecond)
defer cancel()
err = a.withFreeBlocks(ctx, blocks, func([]uint64) {
if err := lockB.Acquire(bg, lock.LockTypeBlockBitmap, 0, lock.LockModeExclusive); err != nil {
t.Errorf("node B lock: %v", err)
return
}
<-ctx.Done()
go func() {
time.Sleep(time.Second)
_ = lockB.Release(lock.LockTypeBlockBitmap, 0)
}()
})
if err != nil {
t.Fatal(err)
}

if free, err := a.IsFree(blocks[0]); err != nil || !free {
t.Errorf("block %d after a discard that outlasted the context: IsFree = %v, %v; want free", blocks[0], free, err)
}
}
Loading
Loading