Skip to content

Commit cbbd0dc

Browse files
authored
Merge pull request #1314 from stgraber/profiles
Profile performance improvements
2 parents 36c3c6d + 8261a78 commit cbbd0dc

File tree

14 files changed

+111
-30
lines changed

14 files changed

+111
-30
lines changed

cmd/incusd/api_internal_recover.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ func internalRecoverScan(ctx context.Context, s *state.State, userPools []api.St
8080
return err
8181
}
8282

83+
profileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
84+
if err != nil {
85+
return err
86+
}
87+
8388
profileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
8489
if err != nil {
8590
return err
@@ -92,7 +97,7 @@ func internalRecoverScan(ctx context.Context, s *state.State, userPools []api.St
9297
projectProfiles[profile.Project] = []*api.Profile{}
9398
}
9499

95-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
100+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
96101
if err != nil {
97102
return err
98103
}

cmd/incusd/instance_patch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,18 @@ func instancePatch(d *Daemon, r *http.Request) response.Response {
192192
return err
193193
}
194194

195+
profileConfigs, err := cluster.GetConfig(ctx, tx.Tx(), "profile")
196+
if err != nil {
197+
return err
198+
}
199+
195200
profileDevices, err := cluster.GetDevices(ctx, tx.Tx(), "profile")
196201
if err != nil {
197202
return err
198203
}
199204

200205
for _, profile := range profiles {
201-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
206+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
202207
if err != nil {
203208
return err
204209
}

cmd/incusd/instance_post.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,18 @@ func migrateInstance(ctx context.Context, s *state.State, inst instance.Instance
636636
return err
637637
}
638638

639+
profileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
640+
if err != nil {
641+
return err
642+
}
643+
639644
profileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
640645
if err != nil {
641646
return err
642647
}
643648

644649
for _, profile := range rawProfiles {
645-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
650+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
646651
if err != nil {
647652
return err
648653
}

cmd/incusd/instance_put.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,18 @@ func instancePut(d *Daemon, r *http.Request) response.Response {
138138
return err
139139
}
140140

141+
profileConfigs, err := cluster.GetConfig(ctx, tx.Tx(), "profile")
142+
if err != nil {
143+
return err
144+
}
145+
141146
profileDevices, err := cluster.GetDevices(ctx, tx.Tx(), "profile")
142147
if err != nil {
143148
return err
144149
}
145150

146151
for _, profile := range profiles {
147-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
152+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
148153
if err != nil {
149154
return err
150155
}

cmd/incusd/instances_post.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
993993
return err
994994
}
995995

996+
dbProfileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
997+
if err != nil {
998+
return err
999+
}
1000+
9961001
dbProfileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
9971002
if err != nil {
9981003
return err
@@ -1009,7 +1014,7 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
10091014
return fmt.Errorf("Requested profile %q doesn't exist", profileName)
10101015
}
10111016

1012-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), dbProfileDevices)
1017+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), dbProfileConfigs, dbProfileDevices)
10131018
if err != nil {
10141019
return err
10151020
}

cmd/incusd/profiles.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ func profilesGet(d *Daemon, r *http.Request) response.Response {
190190
}
191191

192192
if recursion {
193+
profileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
194+
if err != nil {
195+
return err
196+
}
197+
193198
profileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
194199
if err != nil {
195200
return err
@@ -201,7 +206,7 @@ func profilesGet(d *Daemon, r *http.Request) response.Response {
201206
continue
202207
}
203208

204-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
209+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
205210
if err != nil {
206211
return err
207212
}
@@ -437,12 +442,17 @@ func profileGet(d *Daemon, r *http.Request) response.Response {
437442
return fmt.Errorf("Fetch profile: %w", err)
438443
}
439444

445+
profileConfigs, err := dbCluster.GetConfig(ctx, tx.Tx(), "profile")
446+
if err != nil {
447+
return err
448+
}
449+
440450
profileDevices, err := dbCluster.GetDevices(ctx, tx.Tx(), "profile")
441451
if err != nil {
442452
return err
443453
}
444454

445-
resp, err = profile.ToAPI(ctx, tx.Tx(), profileDevices)
455+
resp, err = profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
446456
if err != nil {
447457
return err
448458
}
@@ -533,7 +543,7 @@ func profilePut(d *Daemon, r *http.Request) response.Response {
533543
return fmt.Errorf("Failed to retrieve profile %q: %w", name, err)
534544
}
535545

536-
profile, err = current.ToAPI(ctx, tx.Tx(), nil)
546+
profile, err = current.ToAPI(ctx, tx.Tx(), nil, nil)
537547
if err != nil {
538548
return err
539549
}
@@ -638,7 +648,7 @@ func profilePatch(d *Daemon, r *http.Request) response.Response {
638648
return fmt.Errorf("Failed to retrieve profile=%q: %w", name, err)
639649
}
640650

641-
profile, err = current.ToAPI(ctx, tx.Tx(), nil)
651+
profile, err = current.ToAPI(ctx, tx.Tx(), nil, nil)
642652
if err != nil {
643653
return err
644654
}

internal/server/backup/backup_config_utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ func ConfigToInstanceDBArgs(state *state.State, c *config.Config, projectName st
5151
return err
5252
}
5353

54+
// Get all the profile configs.
55+
profileConfigs, err := cluster.GetConfig(ctx, tx.Tx(), "profile")
56+
if err != nil {
57+
return err
58+
}
59+
5460
// Get all the profile devices.
5561
profileDevices, err := cluster.GetDevices(ctx, tx.Tx(), "profile")
5662
if err != nil {
5763
return err
5864
}
5965

6066
for _, profile := range profiles {
61-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
67+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
6268
if err != nil {
6369
return err
6470
}

internal/server/db/cluster/instances.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,19 @@ type InstanceFilter struct {
7777
}
7878

7979
// ToAPI converts the database Instance to API type.
80-
func (i *Instance) ToAPI(ctx context.Context, tx *sql.Tx, instanceDevices map[int][]Device, profileDevices map[int][]Device) (*api.Instance, error) {
80+
func (i *Instance) ToAPI(ctx context.Context, tx *sql.Tx, instanceDevices map[int][]Device, profileConfigs map[int]map[string]string, profileDevices map[int][]Device) (*api.Instance, error) {
8181
profiles, err := GetInstanceProfiles(ctx, tx, i.ID)
8282
if err != nil {
8383
return nil, err
8484
}
8585

86+
if profileConfigs == nil {
87+
profileConfigs, err = GetConfig(ctx, tx, "profile")
88+
if err != nil {
89+
return nil, err
90+
}
91+
}
92+
8693
if profileDevices == nil {
8794
profileDevices, err = GetDevices(ctx, tx, "profile")
8895
if err != nil {
@@ -93,7 +100,7 @@ func (i *Instance) ToAPI(ctx context.Context, tx *sql.Tx, instanceDevices map[in
93100
apiProfiles := make([]api.Profile, 0, len(profiles))
94101
profileNames := make([]string, 0, len(profiles))
95102
for _, p := range profiles {
96-
apiProfile, err := p.ToAPI(ctx, tx, profileDevices)
103+
apiProfile, err := p.ToAPI(ctx, tx, profileConfigs, profileDevices)
97104
if err != nil {
98105
return nil, err
99106
}

internal/server/db/cluster/profiles.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,31 @@ type ProfileFilter struct {
5252
}
5353

5454
// ToAPI returns a cluster Profile as an API struct.
55-
func (p *Profile) ToAPI(ctx context.Context, tx *sql.Tx, profileDevices map[int][]Device) (*api.Profile, error) {
56-
config, err := GetProfileConfig(ctx, tx, p.ID)
57-
if err != nil {
58-
return nil, err
55+
func (p *Profile) ToAPI(ctx context.Context, tx *sql.Tx, profileConfigs map[int]map[string]string, profileDevices map[int][]Device) (*api.Profile, error) {
56+
var err error
57+
58+
var dbConfig map[string]string
59+
if profileConfigs != nil {
60+
dbConfig = profileConfigs[p.ID]
61+
if dbConfig == nil {
62+
dbConfig = map[string]string{}
63+
}
64+
} else {
65+
dbConfig, err = GetProfileConfig(ctx, tx, p.ID)
66+
if err != nil {
67+
return nil, err
68+
}
5969
}
6070

61-
var devices map[string]Device
71+
var dbDevices map[string]Device
6272
if profileDevices != nil {
63-
devices = map[string]Device{}
73+
dbDevices = map[string]Device{}
6474

6575
for _, dev := range profileDevices[p.ID] {
66-
devices[dev.Name] = dev
76+
dbDevices[dev.Name] = dev
6777
}
6878
} else {
69-
devices, err = GetProfileDevices(ctx, tx, p.ID)
79+
dbDevices, err = GetProfileDevices(ctx, tx, p.ID)
7080
if err != nil {
7181
return nil, err
7282
}
@@ -76,8 +86,8 @@ func (p *Profile) ToAPI(ctx context.Context, tx *sql.Tx, profileDevices map[int]
7686
Name: p.Name,
7787
ProfilePut: api.ProfilePut{
7888
Description: p.Description,
79-
Config: config,
80-
Devices: DevicesToAPI(devices),
89+
Config: dbConfig,
90+
Devices: DevicesToAPI(dbDevices),
8191
},
8292
Project: p.Project,
8393
}

internal/server/db/instances.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ func (c *ClusterTx) instanceProfilesFill(ctx context.Context, snapshotsMode bool
534534
return fmt.Errorf("Failed loading profiles: %w", err)
535535
}
536536

537+
// Get all the profile configs.
538+
profileConfigs, err := cluster.GetConfig(context.TODO(), c.Tx(), "profile")
539+
if err != nil {
540+
return fmt.Errorf("Failed loading profile configs: %w", err)
541+
}
542+
537543
// Get all the profile devices.
538544
profileDevices, err := cluster.GetDevices(context.TODO(), c.Tx(), "profile")
539545
if err != nil {
@@ -549,7 +555,7 @@ func (c *ClusterTx) instanceProfilesFill(ctx context.Context, snapshotsMode bool
549555
continue
550556
}
551557

552-
profilesByID[profile.ID], err = profile.ToAPI(context.TODO(), c.tx, profileDevices)
558+
profilesByID[profile.ID], err = profile.ToAPI(context.TODO(), c.tx, profileConfigs, profileDevices)
553559
if err != nil {
554560
return err
555561
}

internal/server/db/profiles.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *ClusterTx) GetProfile(ctx context.Context, project, name string) (int64
6161
profile := profiles[0]
6262
id := int64(profile.ID)
6363

64-
result, err := profile.ToAPI(ctx, c.tx, nil)
64+
result, err := profile.ToAPI(ctx, c.tx, nil, nil)
6565
if err != nil {
6666
return -1, nil, err
6767
}
@@ -78,14 +78,20 @@ func (c *ClusterTx) GetProfiles(ctx context.Context, projectName string, profile
7878
return nil, err
7979
}
8080

81+
// Get all the profile configs.
82+
profileConfigs, err := cluster.GetConfig(ctx, c.Tx(), "profile")
83+
if err != nil {
84+
return nil, err
85+
}
86+
8187
// Get all the profile devices.
8288
profileDevices, err := cluster.GetDevices(ctx, c.Tx(), "profile")
8389
if err != nil {
8490
return nil, err
8591
}
8692

8793
for i, profile := range dbProfiles {
88-
apiProfile, err := profile.ToAPI(ctx, c.tx, profileDevices)
94+
apiProfile, err := profile.ToAPI(ctx, c.tx, profileConfigs, profileDevices)
8995
if err != nil {
9096
return nil, err
9197
}

internal/server/project/permissions.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,14 +1257,19 @@ func fetchProject(tx *db.ClusterTx, projectName string, skipIfNoLimits bool) (*p
12571257
return nil, fmt.Errorf("Fetch profiles from database: %w", err)
12581258
}
12591259

1260+
dbProfileConfigs, err := cluster.GetConfig(ctx, tx.Tx(), "profile")
1261+
if err != nil {
1262+
return nil, fmt.Errorf("Fetch profile configs from database: %w", err)
1263+
}
1264+
12601265
dbProfileDevices, err := cluster.GetDevices(ctx, tx.Tx(), "profile")
12611266
if err != nil {
12621267
return nil, fmt.Errorf("Fetch profile devices from database: %w", err)
12631268
}
12641269

12651270
profiles := make([]api.Profile, 0, len(dbProfiles))
12661271
for _, profile := range dbProfiles {
1267-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), dbProfileDevices)
1272+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), dbProfileConfigs, dbProfileDevices)
12681273
if err != nil {
12691274
return nil, err
12701275
}
@@ -1284,7 +1289,7 @@ func fetchProject(tx *db.ClusterTx, projectName string, skipIfNoLimits bool) (*p
12841289

12851290
instances := make([]api.Instance, 0, len(dbInstances))
12861291
for _, instance := range dbInstances {
1287-
apiInstance, err := instance.ToAPI(ctx, tx.Tx(), dbInstanceDevices, dbProfileDevices)
1292+
apiInstance, err := instance.ToAPI(ctx, tx.Tx(), dbInstanceDevices, dbProfileConfigs, dbProfileDevices)
12881293
if err != nil {
12891294
return nil, fmt.Errorf("Failed to get API data for instance %q in project %q: %w", instance.Name, instance.Project, err)
12901295
}

internal/server/scriptlet/instance_placement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func InstancePlacementRun(ctx context.Context, l logger.Logger, s *state.State,
267267

268268
// Convert the []Instances into []api.Instances.
269269
for _, obj := range objects {
270-
instance, err := obj.ToAPI(ctx, tx.Tx(), objectDevices, nil)
270+
instance, err := obj.ToAPI(ctx, tx.Tx(), objectDevices, nil, nil)
271271
if err != nil {
272272
return err
273273
}

internal/server/storage/utils.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,14 +821,20 @@ func VolumeUsedByProfileDevices(s *state.State, poolName string, projectName str
821821
return fmt.Errorf("Failed loading profiles: %w", err)
822822
}
823823

824+
// Get all the profile configs.
825+
profileConfigs, err := cluster.GetConfig(ctx, tx.Tx(), "profile")
826+
if err != nil {
827+
return fmt.Errorf("Failed loading profile configs: %w", err)
828+
}
829+
824830
// Get all the profile devices.
825831
profileDevices, err := cluster.GetDevices(ctx, tx.Tx(), "profile")
826832
if err != nil {
827-
return fmt.Errorf("Failed loading profiles: %w", err)
833+
return fmt.Errorf("Failed loading profile devices: %w", err)
828834
}
829835

830836
for _, profile := range dbProfiles {
831-
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileDevices)
837+
apiProfile, err := profile.ToAPI(ctx, tx.Tx(), profileConfigs, profileDevices)
832838
if err != nil {
833839
return fmt.Errorf("Failed getting API Profile %q: %w", profile.Name, err)
834840
}

0 commit comments

Comments
 (0)