Skip to content

Commit

Permalink
lxd: Add reverter to storagePoolVolumeUpdateUsers
Browse files Browse the repository at this point in the history
storagePoolVolumeUpdateUsers is used in two places, one of which does not
currently use a reverter. If that update fails, the instances/profiles
could be left in an inconsistent state.

This pushes the reverter inside the function, so that it is safe to call
without needing a reverter.

My hunch is that this is also slightly less slow in case of failure.

Signed-off-by: Wesley Hershberger <[email protected]>
  • Loading branch information
MggMuggins committed Dec 20, 2024
1 parent 48b02f2 commit b3f47fc
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lxd/storage_volumes_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import (
storagePools "github.com/canonical/lxd/lxd/storage"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/revert"
"github.com/canonical/lxd/shared/version"
)

var supportedVolumeTypes = []int{cluster.StoragePoolVolumeTypeContainer, cluster.StoragePoolVolumeTypeVM, cluster.StoragePoolVolumeTypeCustom, cluster.StoragePoolVolumeTypeImage}

func storagePoolVolumeUpdateUsers(s *state.State, projectName string, oldPoolName string, oldVol *api.StorageVolume, newPoolName string, newVol *api.StorageVolume) error {
revert := revert.New()
defer revert.Fail()

// Update all instances that are using the volume with a local (non-expanded) device.
err := storagePools.VolumeUsedByInstanceDevices(s, oldPoolName, projectName, oldVol, false, func(dbInst db.InstanceArgs, project api.Project, usedByDevices []string) error {
inst, err := instance.Load(s, dbInst, project)
Expand Down Expand Up @@ -58,6 +63,17 @@ func storagePoolVolumeUpdateUsers(s *state.State, projectName string, oldPoolNam
return err
}

revert.Add(func() {
err := inst.Update(dbInst, false)
if err != nil {
logger.Error("Failed to revert instance update", logger.Ctx{
"project": dbInst.Project,
"instance": dbInst.Name,
"error": err,
})
}
})

return nil
})
if err != nil {
Expand Down Expand Up @@ -99,12 +115,29 @@ func storagePoolVolumeUpdateUsers(s *state.State, projectName string, oldPoolNam
return err
}

revert.Add(func() {
original := api.ProfilePut{
Config: profile.Config,
Description: profile.Description,
Devices: profile.Devices,
}
err := doProfileUpdate(s, p, profile.Name, profileID, &profile, original)
if err != nil {
logger.Error("Failed reverting profile update", logger.Ctx{
"project": p.Name,
"profile": profile.Name,
"error": err,
})
}
})

return nil
})
if err != nil {
return err
}

revert.Success()
return nil
}

Expand Down

0 comments on commit b3f47fc

Please sign in to comment.