Skip to content

Commit

Permalink
storage_volumes: Optimize db transactions
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hipp <thomashipp@gmail.com>
  • Loading branch information
monstermunchkin committed Jan 24, 2024
1 parent 0baa929 commit 561194e
Showing 1 changed file with 43 additions and 51 deletions.
94 changes: 43 additions & 51 deletions cmd/incusd/storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,19 +692,15 @@ func storagePoolVolumesPost(d *Daemon, r *http.Request) response.Response {
}

var poolID int64
var dbVolume *db.StorageVolume

err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
poolID, err = tx.GetStoragePoolID(ctx, poolName)
if err != nil {
return err
}

return err
})
if err != nil {
return response.SmartError(err)
}

// Check if destination volume exists.
var dbVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
// Check if destination volume exists.
dbVolume, err = tx.GetStoragePoolVolume(ctx, poolID, projectName, db.StoragePoolVolumeTypeCustom, req.Name, true)
if err != nil && !response.IsNotFoundError(err) {
return err
Expand Down Expand Up @@ -1152,29 +1148,29 @@ func storagePoolVolumePost(d *Daemon, r *http.Request) response.Response {
}

if srcPool.Driver().Info().Name == "ceph" {
var srcPoolID int64
var dbVolume *db.StorageVolume
var volumeNotFound bool
var targetIsSet bool

err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
// Load source volume.
srcPoolID, err = tx.GetStoragePoolID(ctx, srcPoolName)
srcPoolID, err := tx.GetStoragePoolID(ctx, srcPoolName)
if err != nil {
return err
}

return err
})
if err != nil {
return response.SmartError(err)
}
dbVolume, err = tx.GetStoragePoolVolume(ctx, srcPoolID, projectName, db.StoragePoolVolumeTypeCustom, volumeName, true)
if err != nil {
// Check if the user provided an incorrect target query parameter and return a helpful error message.
_, volumeNotFound = api.StatusErrorMatch(err, http.StatusNotFound)
targetIsSet = r.URL.Query().Get("target") != ""

var dbVolume *db.StorageVolume
return err
}

err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
dbVolume, err = tx.GetStoragePoolVolume(ctx, srcPoolID, projectName, db.StoragePoolVolumeTypeCustom, volumeName, true)
return err
return nil
})
if err != nil {
// Check if the user provided an incorrect target query parameter and return a helpful error message.
_, volumeNotFound := api.StatusErrorMatch(err, http.StatusNotFound)
targetIsSet := r.URL.Query().Get("target") != ""

if s.ServerClustered && targetIsSet && volumeNotFound {
return response.NotFound(fmt.Errorf("Storage volume not found on this cluster member"))
}
Expand Down Expand Up @@ -1312,28 +1308,29 @@ func storagePoolVolumePost(d *Daemon, r *http.Request) response.Response {
return response.SmartError(fmt.Errorf("Volume is used by Incus itself and cannot be renamed"))
}

var srcPoolID int64
var dbVolume *db.StorageVolume
var volumeNotFound bool
var targetIsSet bool

err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Load source volume.
srcPoolID, err = tx.GetStoragePoolID(ctx, srcPoolName)

return err
})
if err != nil {
return response.SmartError(err)
}
srcPoolID, err := tx.GetStoragePoolID(ctx, srcPoolName)
if err != nil {
return err
}

var dbVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
dbVolume, err = tx.GetStoragePoolVolume(ctx, srcPoolID, projectName, volumeType, volumeName, true)
return err
if err != nil {
// Check if the user provided an incorrect target query parameter and return a helpful error message.
_, volumeNotFound = api.StatusErrorMatch(err, http.StatusNotFound)
targetIsSet = r.URL.Query().Get("target") != ""

return err
}

return nil
})
if err != nil {
// Check if the user provided an incorrect target query parameter and return a helpful error message.
_, volumeNotFound := api.StatusErrorMatch(err, http.StatusNotFound)
targetIsSet := r.URL.Query().Get("target") != ""

if s.ServerClustered && targetIsSet && volumeNotFound {
return response.NotFound(fmt.Errorf("Storage volume not found on this cluster member"))
}
Expand Down Expand Up @@ -1718,21 +1715,16 @@ func storagePoolVolumeGet(d *Daemon, r *http.Request) response.Response {
return resp
}

var poolID int64
var dbVolume *db.StorageVolume

err = s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Get the ID of the storage pool the storage volume is supposed to be attached to.
poolID, err = tx.GetStoragePoolID(ctx, poolName)

return err
})
if err != nil {
return response.SmartError(err)
}
poolID, err := tx.GetStoragePoolID(ctx, poolName)
if err != nil {
return err
}

// Get the storage volume.
var dbVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
// Get the storage volume.
dbVolume, err = tx.GetStoragePoolVolume(ctx, poolID, volumeProjectName, volumeType, volumeName, true)
return err
})
Expand Down

0 comments on commit 561194e

Please sign in to comment.