Skip to content

Commit ea84f83

Browse files
authored
Merge pull request #1811 from stgraber/main
Improve DB performance
2 parents 6e7d679 + 1ddba88 commit ea84f83

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

cmd/incusd/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ func (d *Daemon) init() error {
15721572
}
15731573

15741574
// Setup the networks.
1575-
if !d.db.Cluster.LocalNodeIsEvacuated() {
1575+
if !d.serverClustered || !d.db.Cluster.LocalNodeIsEvacuated() {
15761576
logger.Infof("Initializing networks")
15771577

15781578
err = networkStartup(d.State())

cmd/incusd/events.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,13 @@ func eventsSocket(s *state.State, r *http.Request, w http.ResponseWriter) error
109109
l := logger.AddContext(logger.Ctx{"remote": r.RemoteAddr})
110110

111111
var excludeLocations []string
112-
// Get the current local serverName and store it for the events.
113-
// We do that now to avoid issues with changes to the name and to limit
114-
// the number of DB access to just one per connection.
115-
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
116-
if isClusterNotification(r) {
117-
ctx := r.Context()
118-
119-
// Try and match cluster member certificate fingerprint to member name.
120-
fingerprint, found := ctx.Value(request.CtxUsername).(string)
121-
if found {
112+
if isClusterNotification(r) {
113+
ctx := r.Context()
114+
115+
// Try and match cluster member certificate fingerprint to member name.
116+
fingerprint, found := ctx.Value(request.CtxUsername).(string)
117+
if found {
118+
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
122119
cert, err := cluster.GetCertificateByFingerprintPrefix(context.Background(), tx.Tx(), fingerprint)
123120
if err != nil {
124121
return fmt.Errorf("Failed matching client certificate to cluster member: %w", err)
@@ -127,14 +124,14 @@ func eventsSocket(s *state.State, r *http.Request, w http.ResponseWriter) error
127124
// Add the cluster member client's name to the excluded locations so that we can avoid
128125
// looping the event back to them when they send us an event via recvFunc.
129126
excludeLocations = append(excludeLocations, cert.Name)
127+
128+
return nil
129+
})
130+
if err != nil {
131+
l.Warn("Failed setting up event connection", logger.Ctx{"err": err})
132+
return nil
130133
}
131134
}
132-
133-
return nil
134-
})
135-
if err != nil {
136-
l.Warn("Failed setting up event connection", logger.Ctx{"err": err})
137-
return nil
138135
}
139136

140137
var recvFunc events.EventHandler

cmd/incusd/instance_state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func instanceStatePut(d *Daemon, r *http.Request) response.Response {
177177
}
178178

179179
// Check if the cluster member is evacuated.
180-
if s.DB.Cluster.LocalNodeIsEvacuated() && req.Action != "stop" {
180+
if s.ServerClustered && req.Action != "stop" && s.DB.Cluster.LocalNodeIsEvacuated() {
181181
return response.Forbidden(fmt.Errorf("Cluster member is evacuated"))
182182
}
183183

cmd/incusd/instances.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func instanceShouldAutoStart(inst instance.Instance) bool {
209209

210210
func instancesStart(s *state.State, instances []instance.Instance) {
211211
// Check if the cluster is currently evacuated.
212-
if s.DB.Cluster.LocalNodeIsEvacuated() {
212+
if s.ServerClustered && s.DB.Cluster.LocalNodeIsEvacuated() {
213213
return
214214
}
215215

cmd/incusd/instances_post.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func ensureDownloadedImageFitWithinBudget(ctx context.Context, s *state.State, r
9393
}
9494

9595
func createFromImage(s *state.State, r *http.Request, p api.Project, profiles []api.Profile, img *api.Image, imgAlias string, req *api.InstancesPost) response.Response {
96-
if s.DB.Cluster.LocalNodeIsEvacuated() {
96+
if s.ServerClustered && s.DB.Cluster.LocalNodeIsEvacuated() {
9797
return response.Forbidden(fmt.Errorf("Cluster member is evacuated"))
9898
}
9999

@@ -156,7 +156,7 @@ func createFromImage(s *state.State, r *http.Request, p api.Project, profiles []
156156
}
157157

158158
func createFromNone(s *state.State, r *http.Request, projectName string, profiles []api.Profile, req *api.InstancesPost) response.Response {
159-
if s.DB.Cluster.LocalNodeIsEvacuated() {
159+
if s.ServerClustered && s.DB.Cluster.LocalNodeIsEvacuated() {
160160
return response.Forbidden(fmt.Errorf("Cluster member is evacuated"))
161161
}
162162

@@ -209,7 +209,7 @@ func createFromNone(s *state.State, r *http.Request, projectName string, profile
209209
}
210210

211211
func createFromMigration(ctx context.Context, s *state.State, r *http.Request, projectName string, profiles []api.Profile, req *api.InstancesPost) response.Response {
212-
if s.DB.Cluster.LocalNodeIsEvacuated() && r != nil && r.Context().Value(request.CtxProtocol) != "cluster" {
212+
if s.ServerClustered && r != nil && r.Context().Value(request.CtxProtocol) != "cluster" && s.DB.Cluster.LocalNodeIsEvacuated() {
213213
return response.Forbidden(fmt.Errorf("Cluster member is evacuated"))
214214
}
215215

@@ -474,7 +474,7 @@ func createFromMigration(ctx context.Context, s *state.State, r *http.Request, p
474474
}
475475

476476
func createFromCopy(ctx context.Context, s *state.State, r *http.Request, projectName string, profiles []api.Profile, req *api.InstancesPost) response.Response {
477-
if s.DB.Cluster.LocalNodeIsEvacuated() {
477+
if s.ServerClustered && s.DB.Cluster.LocalNodeIsEvacuated() {
478478
return response.Forbidden(fmt.Errorf("Cluster member is evacuated"))
479479
}
480480

@@ -956,12 +956,12 @@ func instancesPost(d *Daemon, r *http.Request) response.Response {
956956
var targetMemberInfo *db.NodeInfo
957957
var targetGroupName string
958958

959-
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
960-
target := request.QueryParam(r, "target")
961-
if !s.ServerClustered && target != "" {
962-
return api.StatusErrorf(http.StatusBadRequest, "Target only allowed when clustered")
963-
}
959+
target := request.QueryParam(r, "target")
960+
if !s.ServerClustered && target != "" {
961+
return response.BadRequest(fmt.Errorf("Target only allowed when clustered"))
962+
}
964963

964+
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
965965
dbProject, err := dbCluster.GetProject(ctx, tx.Tx(), targetProjectName)
966966
if err != nil {
967967
return fmt.Errorf("Failed loading project: %w", err)

0 commit comments

Comments
 (0)