Skip to content

Commit

Permalink
perf: optimize the trigger retention API
Browse files Browse the repository at this point in the history
Enhance the API for triggering retention by optimizing it from synchronous to asynchronous to solve the problem of slow response in the case of a large number of tasks.

Signed-off-by: chlins <[email protected]>
  • Loading branch information
chlins committed Nov 9, 2023
1 parent 5c02fd8 commit 0ad3a8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
57 changes: 45 additions & 12 deletions src/controller/retention/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
"github.com/goharbor/harbor/src/controller/event/operator"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/logger"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/lib/retry"
"github.com/goharbor/harbor/src/pkg"
"github.com/goharbor/harbor/src/pkg/project"
"github.com/goharbor/harbor/src/pkg/repository"
Expand Down Expand Up @@ -80,6 +83,7 @@ type defaultController struct {
projectManager project.Manager
repositoryMgr repository.Manager
scheduler scheduler.Scheduler
wp *lib.WorkerPool
}

const (
Expand Down Expand Up @@ -248,21 +252,49 @@ func (r *defaultController) TriggerRetentionExec(ctx context.Context, policyID i
"dry_run": dryRun,
"operator": operator.FromContext(ctx),
}

id, err := r.execMgr.Create(ctx, job.RetentionVendorType, policyID, trigger, extra)
if num, err := r.launcher.Launch(ctx, p, id, dryRun); err != nil {
if err1 := r.execMgr.StopAndWait(ctx, id, 10*time.Second); err1 != nil {
logger.Errorf("failed to stop the retention execution %d: %v", id, err1)
}
if err1 := r.execMgr.MarkError(ctx, id, err.Error()); err1 != nil {
logger.Errorf("failed to mark error for the retention execution %d: %v", id, err1)
}
if err != nil {
return 0, err
} else if num == 0 {
// no candidates, mark the execution as done directly
if err := r.execMgr.MarkDone(ctx, id, "no resources for retention"); err != nil {
logger.Errorf("failed to mark done for the execution %d: %v", id, err)
}
}

go func() {
r.wp.GetWorker()
defer r.wp.ReleaseWorker()
// copy the context to request a new ormer
ctx = orm.Copy(ctx)
// as we start a new transaction in the goroutine, the execution record may not
// be inserted yet, wait until it is ready before continue
if err := retry.Retry(func() error {
_, err := r.execMgr.Get(ctx, id)
return err
}); err != nil {
markErr := r.execMgr.MarkError(ctx, id, fmt.Sprintf(
"failed to wait the execution record to be inserted: %v", err))
if markErr != nil {
logger.Errorf("failed to mark the status of execution %d to error: %v", id, markErr)
}
return

Check warning on line 277 in src/controller/retention/controller.go

View check run for this annotation

Codecov / codecov/patch

src/controller/retention/controller.go#L272-L277

Added lines #L272 - L277 were not covered by tests
}

if num, err := r.launcher.Launch(ctx, p, id, dryRun); err != nil {
logger.Errorf("failed to launch the retention jobs, err: %v", err)

if err = r.execMgr.StopAndWait(ctx, id, 10*time.Second); err != nil {
logger.Errorf("failed to stop the retention execution %d: %v", id, err)
}

Check warning on line 285 in src/controller/retention/controller.go

View check run for this annotation

Codecov / codecov/patch

src/controller/retention/controller.go#L281-L285

Added lines #L281 - L285 were not covered by tests

if err = r.execMgr.MarkError(ctx, id, err.Error()); err != nil {
logger.Errorf("failed to mark error for the retention execution %d: %v", id, err)
}

Check warning on line 289 in src/controller/retention/controller.go

View check run for this annotation

Codecov / codecov/patch

src/controller/retention/controller.go#L287-L289

Added lines #L287 - L289 were not covered by tests
} else if num == 0 {
// no candidates, mark the execution as done directly
if err := r.execMgr.MarkDone(ctx, id, "no resources for retention"); err != nil {
logger.Errorf("failed to mark done for the execution %d: %v", id, err)
}

Check warning on line 294 in src/controller/retention/controller.go

View check run for this annotation

Codecov / codecov/patch

src/controller/retention/controller.go#L293-L294

Added lines #L293 - L294 were not covered by tests
}
}()

return id, err
}

Expand Down Expand Up @@ -434,5 +466,6 @@ func NewController() Controller {
projectManager: pkg.ProjectMgr,
repositoryMgr: pkg.RepositoryMgr,
scheduler: scheduler.Sched,
wp: lib.NewWorkerPool(10),
}
}
2 changes: 2 additions & 0 deletions src/controller/retention/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/lib"
"github.com/goharbor/harbor/src/lib/orm"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/retention"
Expand Down Expand Up @@ -238,6 +239,7 @@ func (s *ControllerTestSuite) TestExecution() {
projectManager: projectMgr,
repositoryMgr: repositoryMgr,
scheduler: retentionScheduler,
wp: lib.NewWorkerPool(10),
}

p1 := &policy.Metadata{
Expand Down

0 comments on commit 0ad3a8d

Please sign in to comment.