Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DB performance #1811

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/incusd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ func (d *Daemon) init() error {
}

// Setup the networks.
if !d.db.Cluster.LocalNodeIsEvacuated() {
if !d.serverClustered || !d.db.Cluster.LocalNodeIsEvacuated() {
logger.Infof("Initializing networks")

err = networkStartup(d.State())
Expand Down
29 changes: 13 additions & 16 deletions cmd/incusd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,13 @@ func eventsSocket(s *state.State, r *http.Request, w http.ResponseWriter) error
l := logger.AddContext(logger.Ctx{"remote": r.RemoteAddr})

var excludeLocations []string
// Get the current local serverName and store it for the events.
// We do that now to avoid issues with changes to the name and to limit
// the number of DB access to just one per connection.
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
if isClusterNotification(r) {
ctx := r.Context()

// Try and match cluster member certificate fingerprint to member name.
fingerprint, found := ctx.Value(request.CtxUsername).(string)
if found {
if isClusterNotification(r) {
ctx := r.Context()

// Try and match cluster member certificate fingerprint to member name.
fingerprint, found := ctx.Value(request.CtxUsername).(string)
if found {
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
cert, err := cluster.GetCertificateByFingerprintPrefix(context.Background(), tx.Tx(), fingerprint)
if err != nil {
return fmt.Errorf("Failed matching client certificate to cluster member: %w", err)
Expand All @@ -127,14 +124,14 @@ func eventsSocket(s *state.State, r *http.Request, w http.ResponseWriter) error
// Add the cluster member client's name to the excluded locations so that we can avoid
// looping the event back to them when they send us an event via recvFunc.
excludeLocations = append(excludeLocations, cert.Name)

return nil
})
if err != nil {
l.Warn("Failed setting up event connection", logger.Ctx{"err": err})
return nil
}
}

return nil
})
if err != nil {
l.Warn("Failed setting up event connection", logger.Ctx{"err": err})
return nil
}

var recvFunc events.EventHandler
Expand Down
2 changes: 1 addition & 1 deletion cmd/incusd/instance_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func instanceStatePut(d *Daemon, r *http.Request) response.Response {
}

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/incusd/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func instanceShouldAutoStart(inst instance.Instance) bool {

func instancesStart(s *state.State, instances []instance.Instance) {
// Check if the cluster is currently evacuated.
if s.DB.Cluster.LocalNodeIsEvacuated() {
if s.ServerClustered && s.DB.Cluster.LocalNodeIsEvacuated() {
return
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/incusd/instances_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func ensureDownloadedImageFitWithinBudget(ctx context.Context, s *state.State, r
}

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

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

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

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

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

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

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

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

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

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