Skip to content

Commit

Permalink
Deleting latest snapshot is forbidden (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Nov 30, 2024
1 parent a592e4c commit bca0d2f
Showing 1 changed file with 43 additions and 64 deletions.
107 changes: 43 additions & 64 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ func (db *DB) deleteSnapshot(
deallocationHashBuff []byte,
deallocationHashMatches []uint64,
) error {
if snapshotID == db.snapshotInfo.PreviousSnapshotID {
return errors.New("deleting latest snapshot is forbidden")
}

var snapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
err := db.snapshots.Find(&snapshotInfoValue, snapshotID, tx, walRecorder, allocator, snapshotID, space.StageData,
snapshotHashBuff, snapshotHashMatches)
Expand All @@ -318,51 +322,36 @@ func (db *DB) deleteSnapshot(
return err
}

var nextSnapshotInfo *types.SnapshotInfo
var nextDeallocationListRoot types.NodeRoot
var nextDeallocationLists *space.Space[types.SnapshotID, types.NodeAddress]

//nolint:nestif
if snapshotInfo.NextSnapshotID < db.singularityNode.LastSnapshotID {
var nextSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
err := db.snapshots.Find(&nextSnapshotInfoValue, snapshotID, tx, walRecorder, allocator,
snapshotInfo.NextSnapshotID, space.StageData, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}
var nextSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
if err := db.snapshots.Find(&nextSnapshotInfoValue, snapshotID, tx, walRecorder, allocator,
snapshotInfo.NextSnapshotID, space.StageData, snapshotHashBuff, snapshotHashMatches); err != nil {
return err
}

exists, err := nextSnapshotInfoValue.Exists(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}
if !exists {
return errors.Errorf("next snapshot %d does not exist", snapshotID)
}
tmpNextSnapshotInfo, err := nextSnapshotInfoValue.Value(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}
nextSnapshotInfo = &tmpNextSnapshotInfo
exists, err = nextSnapshotInfoValue.Exists(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}
if !exists {
return errors.Errorf("next snapshot %d does not exist", snapshotID)
}
nextSnapshotInfo, err := nextSnapshotInfoValue.Value(snapshotID, tx, walRecorder, allocator, snapshotHashBuff,
snapshotHashMatches)
if err != nil {
return err
}

nextDeallocationListRoot = types.NodeRoot{
Pointer: &nextSnapshotInfo.DeallocationRoot,
}
nextDeallocationLists = space.New[types.SnapshotID, types.NodeAddress](
space.Config[types.SnapshotID, types.NodeAddress]{
SpaceRoot: nextDeallocationListRoot,
State: db.config.State,
DataNodeAssistant: db.snapshotToNodeNodeAssistant,
},
)
} else {
nextSnapshotInfo = &db.snapshotInfo
nextDeallocationListRoot = types.NodeRoot{
Pointer: &db.snapshotInfo.DeallocationRoot,
}
nextDeallocationLists = db.deallocationLists
nextDeallocationListRoot := types.NodeRoot{
Pointer: &nextSnapshotInfo.DeallocationRoot,
}
nextDeallocationLists := space.New[types.SnapshotID, types.NodeAddress](
space.Config[types.SnapshotID, types.NodeAddress]{
SpaceRoot: nextDeallocationListRoot,
State: db.config.State,
DataNodeAssistant: db.snapshotToNodeNodeAssistant,
},
)

deallocationListsRoot := types.NodeRoot{
Pointer: &snapshotInfo.DeallocationRoot,
Expand Down Expand Up @@ -422,35 +411,25 @@ func (db *DB) deleteSnapshot(
}

nextSnapshotInfo.DeallocationRoot = snapshotInfo.DeallocationRoot
nextSnapshotInfo.PreviousSnapshotID = snapshotInfo.PreviousSnapshotID
if err := nextSnapshotInfoValue.Set(
snapshotID,
tx,
walRecorder,
allocator,
nextSnapshotInfo,
snapshotHashBuff,
snapshotHashMatches,
); err != nil {
return err
}

space.Deallocate(
nextDeallocationListRoot.Pointer,
deallocator,
db.config.State,
)

nextSnapshotInfo.PreviousSnapshotID = snapshotInfo.PreviousSnapshotID

if snapshotInfo.NextSnapshotID < db.singularityNode.LastSnapshotID {
var nextSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
err := db.snapshots.Find(&nextSnapshotInfoValue, snapshotID, tx, walRecorder, allocator,
snapshotInfo.NextSnapshotID, space.StageData, snapshotHashBuff, snapshotHashMatches)
if err != nil {
return err
}
if err := nextSnapshotInfoValue.Set(
snapshotID,
tx,
walRecorder,
allocator,
*nextSnapshotInfo,
snapshotHashBuff,
snapshotHashMatches,
); err != nil {
return err
}
}

//nolint:nestif
if snapshotInfo.PreviousSnapshotID > 0 {
var previousSnapshotInfoValue space.Entry[types.SnapshotID, types.SnapshotInfo]
Expand Down

0 comments on commit bca0d2f

Please sign in to comment.