Skip to content

Commit

Permalink
lxd/project/limits: Use InstanceList for queries
Browse files Browse the repository at this point in the history
This at least gets us from `O(n)` queries to `O(1)` (where n is instances).
InstanceList does a lot of unnecessary faffing around with profiles that
ends up getting thrown out (looks like about 4 queries whose results end
up being dropped).

Signed-off-by: Wesley Hershberger <wesley.hershberger@canonical.com>
  • Loading branch information
MggMuggins committed Oct 23, 2024
1 parent b7d1b5d commit 6d46fd6
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions lxd/project/limits/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,43 +1288,39 @@ func fetchProject(globalConfig map[string]any, tx *db.ClusterTx, projectName str
profiles = append(profiles, *apiProfile)
}

drivers, err := tx.GetStoragePoolDrivers(ctx)
if err != nil {
return nil, fmt.Errorf("Fetch storage pools from database: %w", err)
}

dbInstances, err := cluster.GetInstances(ctx, tx.Tx(), cluster.InstanceFilter{Project: &projectName})
if err != nil {
return nil, fmt.Errorf("Fetch project instances from database: %w", err)
info := &projectInfo{
Project: *project,
Profiles: profiles,
}

dbInstanceDevices, err := cluster.GetDevices(ctx, tx.Tx(), "instance")
if err != nil {
return nil, fmt.Errorf("Fetch instance devices from database: %w", err)
instanceFilter := cluster.InstanceFilter{
Project: &projectName,
}

instances := make([]api.Instance, 0, len(dbInstances))
for _, instance := range dbInstances {
apiInstance, err := instance.ToAPI(ctx, tx.Tx(), globalConfig, dbInstanceDevices, dbProfileConfigs, dbProfileDevices)
instanceFunc := func(inst db.InstanceArgs, project api.Project) error {
apiInstance, err := inst.ToAPI()
if err != nil {
return nil, fmt.Errorf("Failed to get API data for instance %q in project %q: %w", instance.Name, instance.Project, err)
return err
}

instances = append(instances, *apiInstance)
info.Instances = append(info.Instances, *apiInstance)

return nil
}

volumes, err := tx.GetCustomVolumesInProject(ctx, projectName)
err = tx.InstanceList(ctx, instanceFunc, instanceFilter)
if err != nil {
return nil, fmt.Errorf("Fetch project custom volumes from database: %w", err)
return nil, fmt.Errorf("Fetch instances from database: %w", err)
}

info := &projectInfo{
Project: *project,
Profiles: profiles,
Instances: instances,
Volumes: volumes,
info.StoragePoolDrivers, err = tx.GetStoragePoolDrivers(ctx)
if err != nil {
return nil, fmt.Errorf("Fetch storage pools from database: %w", err)
}

StoragePoolDrivers: drivers,
info.Volumes, err = tx.GetCustomVolumesInProject(ctx, projectName)
if err != nil {
return nil, fmt.Errorf("Fetch project custom volumes from database: %w", err)
}

return info, nil
Expand Down

0 comments on commit 6d46fd6

Please sign in to comment.