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

DB commit unit test #27

Merged
merged 1 commit into from
Oct 7, 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
38 changes: 33 additions & 5 deletions alloc/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ func (sa SnapshotAllocator) Copy(data []byte) (types.NodeAddress, []byte, error)
// Deallocate marks node for deallocation.
func (sa SnapshotAllocator) Deallocate(nodeAddress types.NodeAddress, srcSnapshotID types.SnapshotID) error {
if srcSnapshotID == sa.snapshotID {
sa.DeallocateImmediately(nodeAddress)
delete(sa.dirtyNodes, nodeAddress)
sa.allocator.Deallocate(nodeAddress)
return nil
}

Expand All @@ -137,8 +138,35 @@ func (sa SnapshotAllocator) Deallocate(nodeAddress types.NodeAddress, srcSnapsho
return nil
}

// DeallocateImmediately dealocates node immediately.
func (sa SnapshotAllocator) DeallocateImmediately(nodeAddress types.NodeAddress) {
delete(sa.dirtyNodes, nodeAddress)
sa.allocator.Deallocate(nodeAddress)
// NewImmediateSnapshotAllocator creates new immediate snapshot deallocator.
func NewImmediateSnapshotAllocator(
snapshotID types.SnapshotID,
parentSnapshotAllocator types.SnapshotAllocator,
) ImmediateSnapshotAllocator {
return ImmediateSnapshotAllocator{
snapshotID: snapshotID,
parentSnapshotAllocator: parentSnapshotAllocator,
}
}

// ImmediateSnapshotAllocator deallocates nodes immediately instead of adding them to deallocation list.
type ImmediateSnapshotAllocator struct {
snapshotID types.SnapshotID
parentSnapshotAllocator types.SnapshotAllocator
}

// Allocate allocates new node.
func (sa ImmediateSnapshotAllocator) Allocate() (types.NodeAddress, []byte, error) {
return sa.parentSnapshotAllocator.Allocate()
}

// Copy allocates new node and copies content from existing one.
func (sa ImmediateSnapshotAllocator) Copy(data []byte) (types.NodeAddress, []byte, error) {
return sa.parentSnapshotAllocator.Copy(data)
}

// Deallocate marks node for deallocation.
func (sa ImmediateSnapshotAllocator) Deallocate(nodeAddress types.NodeAddress, _ types.SnapshotID) error {
// using sa.snapshotID instead of the snapshotID argument causes immediate deallocation.
return sa.parentSnapshotAllocator.Deallocate(nodeAddress, sa.snapshotID)
}
25 changes: 14 additions & 11 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ type SpaceToCommit struct {

// Snapshot represents snapshot.
type Snapshot struct {
SnapshotID types.SnapshotID
SingularityNode *types.SingularityNode
SnapshotInfo *types.SnapshotInfo
Snapshots *space.Space[types.SnapshotID, types.SnapshotInfo]
Spaces *space.Space[types.SpaceID, types.SpaceInfo]
SpacesToCommit map[types.SpaceID]SpaceToCommit
Allocator types.SnapshotAllocator
SnapshotID types.SnapshotID
SingularityNode *types.SingularityNode
SnapshotInfo *types.SnapshotInfo
Snapshots *space.Space[types.SnapshotID, types.SnapshotInfo]
Spaces *space.Space[types.SpaceID, types.SpaceInfo]
DeallocationLists *space.Space[types.SnapshotID, types.NodeAddress]
SpacesToCommit map[types.SpaceID]SpaceToCommit
Allocator types.SnapshotAllocator
}

// New creates new database.
Expand Down Expand Up @@ -246,6 +247,7 @@ func (db *DB) prepareNextSnapshot(singularityNode types.SingularityNode) error {
deallocationLists,
db.listNodeAllocator,
)
immediateAllocator := alloc.NewImmediateSnapshotAllocator(snapshotID, allocator)
*deallocationLists = *space.New[types.SnapshotID, types.NodeAddress](space.Config[types.SnapshotID, types.NodeAddress]{
SnapshotID: snapshotID,
HashMod: &snapshotInfo.DeallocationRoot.HashMod,
Expand All @@ -255,7 +257,7 @@ func (db *DB) prepareNextSnapshot(singularityNode types.SingularityNode) error {
},
PointerNodeAllocator: db.pointerNodeAllocator,
DataNodeAllocator: db.snapshotToNodeNodeAllocator,
Allocator: allocator,
Allocator: immediateAllocator,
})

snapshots := space.New[types.SnapshotID, types.SnapshotInfo](space.Config[types.SnapshotID, types.SnapshotInfo]{
Expand All @@ -267,7 +269,7 @@ func (db *DB) prepareNextSnapshot(singularityNode types.SingularityNode) error {
},
PointerNodeAllocator: db.pointerNodeAllocator,
DataNodeAllocator: db.snapshotInfoNodeAllocator,
Allocator: allocator,
Allocator: immediateAllocator,
})

if singularityNode.SnapshotRoot.State != types.StateFree {
Expand Down Expand Up @@ -297,8 +299,9 @@ func (db *DB) prepareNextSnapshot(singularityNode types.SingularityNode) error {
DataNodeAllocator: db.spaceInfoNodeAllocator,
Allocator: allocator,
}),
SpacesToCommit: map[types.SpaceID]SpaceToCommit{},
Allocator: allocator,
DeallocationLists: deallocationLists,
SpacesToCommit: map[types.SpaceID]SpaceToCommit{},
Allocator: allocator,
}

return nil
Expand Down
Loading