From 4139adee1b9522ee8f8ed55f6553c9401fa70ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Mon, 19 Aug 2024 18:34:15 -0400 Subject: [PATCH] lxd/instances: Remove old retry logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber Sponsored-by: https://webdock.io (cherry picked from commit 7c60b4a520c7554bb4a8103783419ed144e511e1) Signed-off-by: hamistao License: Apache-2.0 --- lxd/instances_get.go | 44 ++++++++++---------------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) diff --git a/lxd/instances_get.go b/lxd/instances_get.go index df0a3d3b9449..a230052bd53f 100644 --- a/lxd/instances_get.go +++ b/lxd/instances_get.go @@ -17,17 +17,14 @@ import ( "github.com/canonical/lxd/lxd/cluster" "github.com/canonical/lxd/lxd/db" dbCluster "github.com/canonical/lxd/lxd/db/cluster" - "github.com/canonical/lxd/lxd/db/query" "github.com/canonical/lxd/lxd/instance" "github.com/canonical/lxd/lxd/instance/instancetype" "github.com/canonical/lxd/lxd/request" "github.com/canonical/lxd/lxd/response" - "github.com/canonical/lxd/lxd/state" "github.com/canonical/lxd/shared" "github.com/canonical/lxd/shared/api" "github.com/canonical/lxd/shared/entity" "github.com/canonical/lxd/shared/filter" - "github.com/canonical/lxd/shared/logger" "github.com/canonical/lxd/shared/version" ) @@ -222,33 +219,12 @@ func urlInstanceTypeDetect(r *http.Request) (instancetype.Type, error) { func instancesGet(d *Daemon, r *http.Request) response.Response { s := d.State() - for i := 0; i < 100; i++ { - result, err := doInstancesGet(s, r) - if err == nil { - return response.SyncResponse(true, result) - } - - if !query.IsRetriableError(err) { - logger.Debugf("DBERR: containersGet: error %q", err) - return response.SmartError(err) - } - // 100 ms may seem drastic, but we really don't want to thrash - // perhaps we should use a random amount - time.Sleep(100 * time.Millisecond) - } - - logger.Debugf("DBERR: containersGet, db is locked") - logger.Debugf(logger.GetStack()) - return response.InternalError(fmt.Errorf("DB is locked")) -} - -func doInstancesGet(s *state.State, r *http.Request) (any, error) { resultFullList := []*api.InstanceFull{} resultMu := sync.Mutex{} instanceType, err := urlInstanceTypeDetect(r) if err != nil { - return nil, err + return response.BadRequest(err) } // Parse the recursion field. @@ -261,7 +237,7 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { filterStr := r.FormValue("filter") clauses, err := filter.Parse(filterStr, filter.QueryOperatorSet()) if err != nil { - return nil, fmt.Errorf("Invalid filter: %w", err) + return response.BadRequest(fmt.Errorf("Invalid filter: %w", err)) } mustLoadObjects := recursion > 0 || (recursion == 0 && clauses != nil && len(clauses.Clauses) > 0) @@ -271,7 +247,7 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { allProjects := shared.IsTrue(r.FormValue("all-projects")) if allProjects && projectName != "" { - return nil, api.StatusErrorf(http.StatusBadRequest, "Cannot specify a project when requesting all projects") + return response.BadRequest(fmt.Errorf("Cannot specify a project when requesting all projects")) } else if !allProjects && projectName == "" { projectName = api.ProjectDefaultName } @@ -304,12 +280,12 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { return nil }) if err != nil { - return nil, err + return response.SmartError(err) } userHasPermission, err := s.Authorizer.GetPermissionChecker(r.Context(), auth.EntitlementCanView, entity.TypeInstance) if err != nil { - return nil, err + return response.SmartError(err) } // Removes instances the user doesn't have access to. @@ -436,7 +412,7 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { for _, projectName := range filteredProjects { insts, err := instanceLoadNodeProjectAll(r.Context(), s, projectName, instanceType) if err != nil { - return nil, fmt.Errorf("Failed loading instances for project %q: %w", projectName, err) + return response.InternalError(fmt.Errorf("Failed loading instances for project %q: %w", projectName, err)) } for _, inst := range insts { @@ -506,7 +482,7 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { if clauses != nil && len(clauses.Clauses) > 0 { resultFullList, err = instance.FilterFull(resultFullList, *clauses) if err != nil { - return nil, err + return response.SmartError(err) } } @@ -524,7 +500,7 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { resultList = append(resultList, url.String()) } - return resultList, nil + return response.SyncResponse(true, resultList) } if recursion == 1 { @@ -533,10 +509,10 @@ func doInstancesGet(s *state.State, r *http.Request) (any, error) { resultList = append(resultList, &resultFullList[i].Instance) } - return resultList, nil + return response.SyncResponse(true, resultList) } - return resultFullList, nil + return response.SyncResponse(true, resultFullList) } // Fetch information about the containers on the given remote node, using the