Skip to content
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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ linters:
# Known findings as of 2026-10-24 in code other open PRs are changing; delete each rule once fixed.
rules:
# errcheck is enforced only in packages already cleaned up; widen this as others are.
- path-except: ^(internal/(journal|bitmap|refcount|trim|superblock|storage|node|metrics|lock|inode|extent)|cmd/crctest)/
- path-except: ^(internal/(journal|bitmap|refcount|trim|superblock|storage|node|metrics|lock|inode|extent|snapshot)|cmd/crctest)/
linters: [errcheck]
- path: ^internal/goclusterfs/fs\.go$
linters: [unused]
Expand Down
23 changes: 18 additions & 5 deletions internal/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"iter"
"log"
"sync"
"time"

Expand Down Expand Up @@ -271,6 +272,18 @@ func NewRegistry(dev *storage.Device, offset, size uint64, lockMgr *lock.Manager
}
}

// unlock releases the registry lock. ErrNotLockOwner means a double release:
// the extra one may have dropped another holder's lock early.
func (r *Registry) unlock() {
err := r.lockMgr.Release(lock.LockTypeSnapshot, 0)
switch {
case errors.Is(err, lock.ErrNotLockOwner):
log.Printf("[ERROR] snapshot registry: released the registry lock that this node does not hold (double release or lock bookkeeping bug)")
case err != nil:
log.Printf("[WARN] snapshot registry: releasing the registry lock failed: %v", err)
}
}

// Create creates a new snapshot descriptor.
//
// Parameters:
Expand All @@ -297,7 +310,7 @@ func (r *Registry) Create(ctx context.Context, name string, targetInode uint32,
if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil {
return nil, fmt.Errorf("failed to acquire snapshot lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeSnapshot, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down Expand Up @@ -435,7 +448,7 @@ func (r *Registry) UpdateParentID(ctx context.Context, snapshotID, newParentID u
if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil {
return fmt.Errorf("failed to acquire snapshot lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeSnapshot, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down Expand Up @@ -481,7 +494,7 @@ func (r *Registry) updateState(ctx context.Context, snapshotID uint64, newState,
if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil {
return fmt.Errorf("failed to acquire snapshot lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeSnapshot, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down Expand Up @@ -515,7 +528,7 @@ func (r *Registry) SetRefCountJobID(ctx context.Context, snapshotID, jobID uint6
if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil {
return fmt.Errorf("failed to acquire snapshot lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeSnapshot, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down Expand Up @@ -549,7 +562,7 @@ func (r *Registry) Remove(ctx context.Context, snapshotID uint64) error {
if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil {
return fmt.Errorf("failed to acquire snapshot lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeSnapshot, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down
130 changes: 92 additions & 38 deletions internal/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ func createTestDevice(tb testing.TB, size int64) (*storage.Device, func()) {
}

if err := f.Truncate(size); err != nil {
f.Close()
os.Remove(testFile)
_ = f.Close()
_ = os.Remove(testFile)
tb.Skipf("Cannot truncate test file: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
tb.Fatal(err)
}

dev, err := storage.Open(testFile)
if err != nil {
os.Remove(testFile)
_ = os.Remove(testFile)
tb.Skipf("O_DIRECT not supported: %v", err)
}

cleanup := func() {
dev.Close()
os.Remove(testFile)
_ = dev.Close()
_ = os.Remove(testFile)
}

return dev, cleanup
Expand Down Expand Up @@ -310,8 +312,12 @@ func TestRegistryGetByName(t *testing.T) {
ctx := context.Background()

// Create snapshots
registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly)
if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil {
t.Fatal(err)
}

// Get by name
desc, err := registry.GetByName(ctx, "snap2")
Expand Down Expand Up @@ -364,9 +370,15 @@ func TestRegistryList(t *testing.T) {
ctx := context.Background()

// Create several snapshots
registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly)
registry.Create(ctx, "hidden", 300, InvalidSnapshotID, 3000, 150, 3072, FlagHidden)
if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "hidden", 300, InvalidSnapshotID, 3000, 150, 3072, FlagHidden); err != nil {
t.Fatal(err)
}

// List should not include hidden
snapshots, err := registry.List(ctx)
Expand All @@ -386,9 +398,15 @@ func TestRegistryListForInode(t *testing.T) {
ctx := context.Background()

// Create snapshots for different inodes
registry.Create(ctx, "snap1-a", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
registry.Create(ctx, "snap1-b", 100, InvalidSnapshotID, 1001, 51, 1025, FlagReadOnly)
registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly)
if _, err := registry.Create(ctx, "snap1-a", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "snap1-b", 100, InvalidSnapshotID, 1001, 51, 1025, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil {
t.Fatal(err)
}

// List for inode 100
snapshots, err := registry.ListForInode(ctx, 100)
Expand Down Expand Up @@ -449,7 +467,9 @@ func TestRegistryDelete(t *testing.T) {
// Create and activate a snapshot
desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID,
1000, 50, 1024, FlagReadOnly)
registry.UpdateState(ctx, desc.SnapshotID, StateActive)
if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}

// Delete (mark for deletion)
err := registry.Delete(ctx, desc.SnapshotID)
Expand Down Expand Up @@ -480,8 +500,12 @@ func TestRegistryRemove(t *testing.T) {
// Create, activate, and delete a snapshot
desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID,
1000, 50, 1024, FlagReadOnly)
registry.UpdateState(ctx, desc.SnapshotID, StateActive)
registry.Delete(ctx, desc.SnapshotID)
if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}
if err := registry.Delete(ctx, desc.SnapshotID); err != nil {
t.Fatal(err)
}

// Remove permanently
err := registry.Remove(ctx, desc.SnapshotID)
Expand Down Expand Up @@ -510,7 +534,9 @@ func TestRegistryRemoveNotDeleting(t *testing.T) {
// Create an active snapshot
desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID,
1000, 50, 1024, FlagReadOnly)
registry.UpdateState(ctx, desc.SnapshotID, StateActive)
if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}

// Try to remove without deleting first
err := registry.Remove(ctx, desc.SnapshotID)
Expand Down Expand Up @@ -551,8 +577,12 @@ func TestRegistryInitialize(t *testing.T) {
ctx := context.Background()

// Create some snapshots
registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly)
if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
t.Fatal(err)
}
if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil {
t.Fatal(err)
}

// Initialize should clear everything
err := registry.Initialize(ctx)
Expand Down Expand Up @@ -580,17 +610,25 @@ func TestRegistryScan(t *testing.T) {

// Create snapshots in various states
desc1, _ := registry.Create(ctx, "active1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
registry.UpdateState(ctx, desc1.SnapshotID, StateActive)
if err := registry.UpdateState(ctx, desc1.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}

desc2, _ := registry.Create(ctx, "active2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly)
registry.UpdateState(ctx, desc2.SnapshotID, StateActive)
if err := registry.UpdateState(ctx, desc2.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}

_, _ = registry.Create(ctx, "creating", 300, InvalidSnapshotID, 3000, 75, 1536, FlagReadOnly)
// Leave in creating state

desc4, _ := registry.Create(ctx, "deleting", 400, InvalidSnapshotID, 4000, 25, 512, FlagReadOnly)
registry.UpdateState(ctx, desc4.SnapshotID, StateActive)
registry.Delete(ctx, desc4.SnapshotID)
if err := registry.UpdateState(ctx, desc4.SnapshotID, StateActive); err != nil {
t.Fatal(err)
}
if err := registry.Delete(ctx, desc4.SnapshotID); err != nil {
t.Fatal(err)
}

// Scan
stats, err := registry.Scan(ctx)
Expand Down Expand Up @@ -676,7 +714,7 @@ func TestRegistryPersistence(t *testing.T) {
}

testFile := filepath.Join(tmpDir, "test_snapshot_persist.bin")
defer os.Remove(testFile)
defer func() { _ = os.Remove(testFile) }()

var snapshotID uint64

Expand All @@ -687,16 +725,18 @@ func TestRegistryPersistence(t *testing.T) {
t.Skipf("Cannot create test file: %v", err)
}
if err := f.Truncate(10 << 20); err != nil {
f.Close()
_ = f.Close()
t.Skipf("Cannot truncate: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
t.Fatal(err)
}

dev, err := storage.Open(testFile)
if err != nil {
t.Skipf("O_DIRECT not supported: %v", err)
}
defer dev.Close()
defer func() { _ = dev.Close() }()

registry := NewRegistry(dev, 0, 10*SectorSize, nil)
ctx := context.Background()
Expand All @@ -708,21 +748,25 @@ func TestRegistryPersistence(t *testing.T) {
}
snapshotID = desc.SnapshotID

registry.UpdateState(ctx, snapshotID, StateActive)
if err := registry.UpdateState(ctx, snapshotID, StateActive); err != nil {
t.Fatal(err)
}
}()

// Reopen and verify
dev, err := storage.Open(testFile)
if err != nil {
t.Skipf("Cannot reopen: %v", err)
}
defer dev.Close()
defer func() { _ = dev.Close() }()

registry := NewRegistry(dev, 0, 10*SectorSize, nil)
ctx := context.Background()

// Scan to rebuild state
registry.Scan(ctx)
if _, err := registry.Scan(ctx); err != nil {
t.Fatal(err)
}

// Retrieve
desc, err := registry.Get(ctx, snapshotID)
Expand Down Expand Up @@ -864,8 +908,10 @@ func BenchmarkCreate(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i),
InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
if _, err := registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i),
InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -886,7 +932,9 @@ func BenchmarkGet(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
registry.Get(ctx, ids[i%100])
if _, err := registry.Get(ctx, ids[i%100]); err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -899,13 +947,17 @@ func BenchmarkList(b *testing.B) {

// Pre-create snapshots
for i := 0; i < 100; i++ {
registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i),
InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly)
if _, err := registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i),
InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil {
b.Fatal(err)
}
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
registry.List(ctx)
if _, err := registry.List(ctx); err != nil {
b.Fatal(err)
}
}
}

Expand Down Expand Up @@ -1051,7 +1103,9 @@ func TestRegistryIDsAfterOlderImagesAndSlotReuse(t *testing.T) {
continue
}
var n int
fmt.Sscanf(d.GetName(), "old-%d", &n)
if _, err := fmt.Sscanf(d.GetName(), "old-%d", &n); err != nil {
t.Fatal(err)
}
d.SnapshotID = uint64(n + 1)
if err := old.writeDescriptor(slot, d); err != nil {
t.Fatal(err)
Expand Down
16 changes: 9 additions & 7 deletions internal/snapshot/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,28 @@ func setupTestDevice(t *testing.T) (*storage.Device, string, func()) {
devPath := filepath.Join(tmpDir, "test.img")
f, err := os.Create(devPath)
if err != nil {
os.RemoveAll(tmpDir)
_ = os.RemoveAll(tmpDir)
t.Fatalf("Failed to create device file: %v", err)
}

if err := f.Truncate(testDeviceSize); err != nil {
f.Close()
os.RemoveAll(tmpDir)
_ = f.Close()
_ = os.RemoveAll(tmpDir)
t.Fatalf("Failed to truncate device: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
t.Fatal(err)
}

dev, err := storage.Open(devPath)
if err != nil {
os.RemoveAll(tmpDir)
_ = os.RemoveAll(tmpDir)
t.Fatalf("Failed to open device: %v", err)
}

cleanup := func() {
dev.Close()
os.RemoveAll(tmpDir)
_ = dev.Close()
_ = os.RemoveAll(tmpDir)
}

return dev, devPath, cleanup
Expand Down
Loading