Never list workers in Update()/storeUpdate() transactions (#228)

* POST /v1/workers: do not list workers in a single update txn

* schedulingLoopIteration(): do not list workers in a single update txn

* .golangci.yml: remove mentions of fully deprecated linters
This commit is contained in:
Nikolay Edigaryev
2024-12-05 16:59:50 +04:00
committed by GitHub
parent d94690176e
commit d7b6f477e1
3 changed files with 99 additions and 53 deletions
+36 -16
View File
@@ -35,13 +35,48 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder
}
worker.CreatedAt = currentTime
// License capacity check
if err := controller.storeView(func(txn storepkg.Transaction) responder.Responder {
_, err := txn.GetWorker(worker.Name)
if err != nil && !errors.Is(err, storepkg.ErrNotFound) {
controller.logger.Errorf("failed to check if the worker "+
"with name %q exists in the DB: %v", worker.Name, err)
return responder.Code(http.StatusInternalServerError)
}
if err == nil {
// We will be re-creating a worker with
// the same name, no capacity change
return nil
}
// We will be adding a new worker, check if the license capacity allows that
workers, err := txn.ListWorkers()
if err != nil {
controller.logger.Errorf("failed to count the number of workers in the DB: %v", err)
return responder.Code(http.StatusInternalServerError)
}
if uint(len(workers)+1) > controller.maxWorkersPerLicense {
return responder.JSON(http.StatusConflict, NewErrorResponse("cannot register a new worker "+
"because the license capacity of %d workers has been reached, "+
"consider upgrading at https://tart.run/licensing/", controller.maxWorkersPerLicense))
}
return nil
}); err != nil {
return err
}
return controller.storeUpdate(func(txn storepkg.Transaction) responder.Responder {
// In case there already exist a worker with the same name,
// allow overwriting it if the request comes from a worker
// with the same machine ID
dbWorker, err := txn.GetWorker(worker.Name)
if err != nil && !errors.Is(err, storepkg.ErrNotFound) {
controller.logger.Errorf("failed to check if the worker exists in the DB: %v", err)
controller.logger.Errorf("failed to check if the worker "+
"with name %q exists in the DB: %v", worker.Name, err)
return responder.Code(http.StatusInternalServerError)
}
@@ -50,21 +85,6 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder
NewErrorResponse("this worker is managed from a different machine ID, "+
"delete this worker first to be able to re-create it"))
}
if errors.Is(err, storepkg.ErrNotFound) {
// We will be adding a new worker, check if the license capacity allows that
workers, err := txn.ListWorkers()
if err != nil {
controller.logger.Errorf("failed to count the number of workers in the DB: %v", err)
return responder.Code(http.StatusInternalServerError)
}
if uint(len(workers)+1) > controller.maxWorkersPerLicense {
return responder.JSON(http.StatusConflict, NewErrorResponse("cannot register a new worker "+
"because the license capacity of %d workers has been reached, "+
"consider upgrading at https://tart.run/licensing/", controller.maxWorkersPerLicense))
}
}
if err := txn.SetWorker(worker); err != nil {
return responder.Error(err)