Skip to content

Commit

Permalink
Remove old retry logic (from Incus) (#14319)
Browse files Browse the repository at this point in the history
Just a cherry pick for
lxc/incus@7c60b4a
  • Loading branch information
tomponline authored Oct 22, 2024
2 parents fdb9168 + 4139ade commit 5386396
Showing 1 changed file with 10 additions and 34 deletions.
44 changes: 10 additions & 34 deletions lxd/instances_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 5386396

Please sign in to comment.