mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-18 15:51:11 +01:00
6f9c278559
# ⚠️ Breaking Many deprecated queue config options are removed (actually, they should have been removed in 1.18/1.19). If you see the fatal message when starting Gitea: "Please update your app.ini to remove deprecated config options", please follow the error messages to remove these options from your app.ini. Example: ``` 2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]` 2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]` 2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options ``` Many options in `[queue]` are are dropped, including: `WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`, `BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed from app.ini. # The problem The old queue package has some legacy problems: * complexity: I doubt few people could tell how it works. * maintainability: Too many channels and mutex/cond are mixed together, too many different structs/interfaces depends each other. * stability: due to the complexity & maintainability, sometimes there are strange bugs and difficult to debug, and some code doesn't have test (indeed some code is difficult to test because a lot of things are mixed together). * general applicability: although it is called "queue", its behavior is not a well-known queue. * scalability: it doesn't seem easy to make it work with a cluster without breaking its behaviors. It came from some very old code to "avoid breaking", however, its technical debt is too heavy now. It's a good time to introduce a better "queue" package. # The new queue package It keeps using old config and concept as much as possible. * It only contains two major kinds of concepts: * The "base queue": channel, levelqueue, redis * They have the same abstraction, the same interface, and they are tested by the same testing code. * The "WokerPoolQueue", it uses the "base queue" to provide "worker pool" function, calls the "handler" to process the data in the base queue. * The new code doesn't do "PushBack" * Think about a queue with many workers, the "PushBack" can't guarantee the order for re-queued unhandled items, so in new code it just does "normal push" * The new code doesn't do "pause/resume" * The "pause/resume" was designed to handle some handler's failure: eg: document indexer (elasticsearch) is down * If a queue is paused for long time, either the producers blocks or the new items are dropped. * The new code doesn't do such "pause/resume" trick, it's not a common queue's behavior and it doesn't help much. * If there are unhandled items, the "push" function just blocks for a few seconds and then re-queue them and retry. * The new code doesn't do "worker booster" * Gitea's queue's handlers are light functions, the cost is only the go-routine, so it doesn't make sense to "boost" them. * The new code only use "max worker number" to limit the concurrent workers. * The new "Push" never blocks forever * Instead of creating more and more blocking goroutines, return an error is more friendly to the server and to the end user. There are more details in code comments: eg: the "Flush" problem, the strange "code.index" hanging problem, the "immediate" queue problem. Almost ready for review. TODO: * [x] add some necessary comments during review * [x] add some more tests if necessary * [x] update documents and config options * [x] test max worker / active worker * [x] re-run the CI tasks to see whether any test is flaky * [x] improve the `handleOldLengthConfiguration` to provide more friendly messages * [x] fine tune default config values (eg: length?) ## Code coverage: ![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
261 lines
7.7 KiB
Go
261 lines
7.7 KiB
Go
// Copyright 2021 Gitea. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package automerge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
|
pull_model "code.gitea.io/gitea/models/pull"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/process"
|
|
"code.gitea.io/gitea/modules/queue"
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
)
|
|
|
|
// prAutoMergeQueue represents a queue to handle update pull request tests
|
|
var prAutoMergeQueue *queue.WorkerPoolQueue[string]
|
|
|
|
// Init runs the task queue to that handles auto merges
|
|
func Init() error {
|
|
prAutoMergeQueue = queue.CreateUniqueQueue("pr_auto_merge", handler)
|
|
if prAutoMergeQueue == nil {
|
|
return fmt.Errorf("Unable to create pr_auto_merge Queue")
|
|
}
|
|
go graceful.GetManager().RunWithShutdownFns(prAutoMergeQueue.Run)
|
|
return nil
|
|
}
|
|
|
|
// handle passed PR IDs and test the PRs
|
|
func handler(items ...string) []string {
|
|
for _, s := range items {
|
|
var id int64
|
|
var sha string
|
|
if _, err := fmt.Sscanf(s, "%d_%s", &id, &sha); err != nil {
|
|
log.Error("could not parse data from pr_auto_merge queue (%v): %v", s, err)
|
|
continue
|
|
}
|
|
handlePull(id, sha)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func addToQueue(pr *issues_model.PullRequest, sha string) {
|
|
log.Trace("Adding pullID: %d to the pull requests patch checking queue with sha %s", pr.ID, sha)
|
|
if err := prAutoMergeQueue.Push(fmt.Sprintf("%d_%s", pr.ID, sha)); err != nil {
|
|
log.Error("Error adding pullID: %d to the pull requests patch checking queue %v", pr.ID, err)
|
|
}
|
|
}
|
|
|
|
// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
|
|
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
|
|
err = db.WithTx(ctx, func(ctx context.Context) error {
|
|
lastCommitStatus, err := pull_service.GetPullRequestCommitStatusState(ctx, pull)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// we don't need to schedule
|
|
if lastCommitStatus.IsSuccess() {
|
|
return nil
|
|
}
|
|
|
|
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message); err != nil {
|
|
return err
|
|
}
|
|
scheduled = true
|
|
|
|
_, err = issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRScheduledToAutoMerge, pull, doer)
|
|
return err
|
|
})
|
|
return scheduled, err
|
|
}
|
|
|
|
// RemoveScheduledAutoMerge cancels a previously scheduled pull request
|
|
func RemoveScheduledAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest) error {
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
if err := pull_model.DeleteScheduledAutoMerge(ctx, pull.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err := issues_model.CreateAutoMergeComment(ctx, issues_model.CommentTypePRUnScheduledToAutoMerge, pull, doer)
|
|
return err
|
|
})
|
|
}
|
|
|
|
// MergeScheduledPullRequest merges a previously scheduled pull request when all checks succeeded
|
|
func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model.Repository) error {
|
|
pulls, err := getPullRequestsByHeadSHA(ctx, sha, repo, func(pr *issues_model.PullRequest) bool {
|
|
return !pr.HasMerged && pr.CanAutoMerge()
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pr := range pulls {
|
|
addToQueue(pr, sha)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) {
|
|
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
refs, err := gitRepo.GetRefsBySha(sha, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pulls := make(map[int64]*issues_model.PullRequest)
|
|
|
|
for _, ref := range refs {
|
|
// Each pull branch starts with refs/pull/ we then go from there to find the index of the pr and then
|
|
// use that to get the pr.
|
|
if strings.HasPrefix(ref, git.PullPrefix) {
|
|
parts := strings.Split(ref[len(git.PullPrefix):], "/")
|
|
|
|
// e.g. 'refs/pull/1/head' would be []string{"1", "head"}
|
|
if len(parts) != 2 {
|
|
log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo)
|
|
continue
|
|
}
|
|
|
|
prIndex, err := strconv.ParseInt(parts[0], 10, 64)
|
|
if err != nil {
|
|
log.Error("getPullRequestsByHeadSHA found broken pull ref [%s] on repo [%-v]", ref, repo)
|
|
continue
|
|
}
|
|
|
|
p, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, prIndex)
|
|
if err != nil {
|
|
// If there is no pull request for this branch, we don't try to merge it.
|
|
if issues_model.IsErrPullRequestNotExist(err) {
|
|
continue
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if filter(p) {
|
|
pulls[p.ID] = p
|
|
}
|
|
}
|
|
}
|
|
|
|
return pulls, nil
|
|
}
|
|
|
|
func handlePull(pullID int64, sha string) {
|
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(),
|
|
fmt.Sprintf("Handle AutoMerge of PR[%d] with sha[%s]", pullID, sha))
|
|
defer finished()
|
|
|
|
pr, err := issues_model.GetPullRequestByID(ctx, pullID)
|
|
if err != nil {
|
|
log.Error("GetPullRequestByID[%d]: %v", pullID, err)
|
|
return
|
|
}
|
|
|
|
// Check if there is a scheduled pr in the db
|
|
exists, scheduledPRM, err := pull_model.GetScheduledMergeByPullID(ctx, pr.ID)
|
|
if err != nil {
|
|
log.Error("%-v GetScheduledMergeByPullID: %v", pr, err)
|
|
return
|
|
}
|
|
if !exists {
|
|
return
|
|
}
|
|
|
|
// Get all checks for this pr
|
|
// We get the latest sha commit hash again to handle the case where the check of a previous push
|
|
// did not succeed or was not finished yet.
|
|
|
|
if err = pr.LoadHeadRepo(ctx); err != nil {
|
|
log.Error("%-v LoadHeadRepo: %v", pr, err)
|
|
return
|
|
}
|
|
|
|
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
|
|
if err != nil {
|
|
log.Error("OpenRepository %-v: %v", pr.HeadRepo, err)
|
|
return
|
|
}
|
|
defer headGitRepo.Close()
|
|
|
|
headBranchExist := headGitRepo.IsBranchExist(pr.HeadBranch)
|
|
|
|
if pr.HeadRepo == nil || !headBranchExist {
|
|
log.Warn("Head branch of auto merge %-v does not exist [HeadRepoID: %d, Branch: %s]", pr, pr.HeadRepoID, pr.HeadBranch)
|
|
return
|
|
}
|
|
|
|
// Check if all checks succeeded
|
|
pass, err := pull_service.IsPullCommitStatusPass(ctx, pr)
|
|
if err != nil {
|
|
log.Error("%-v IsPullCommitStatusPass: %v", pr, err)
|
|
return
|
|
}
|
|
if !pass {
|
|
log.Info("Scheduled auto merge %-v has unsuccessful status checks", pr)
|
|
return
|
|
}
|
|
|
|
// Merge if all checks succeeded
|
|
doer, err := user_model.GetUserByID(ctx, scheduledPRM.DoerID)
|
|
if err != nil {
|
|
log.Error("Unable to get scheduled User[%d]: %v", scheduledPRM.DoerID, err)
|
|
return
|
|
}
|
|
|
|
perm, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
|
|
if err != nil {
|
|
log.Error("GetUserRepoPermission %-v: %v", pr.HeadRepo, err)
|
|
return
|
|
}
|
|
|
|
if err := pull_service.CheckPullMergable(ctx, doer, &perm, pr, pull_service.MergeCheckTypeGeneral, false); err != nil {
|
|
if errors.Is(pull_service.ErrUserNotAllowedToMerge, err) {
|
|
log.Info("%-v was scheduled to automerge by an unauthorized user", pr)
|
|
return
|
|
}
|
|
log.Error("%-v CheckPullMergable: %v", pr, err)
|
|
return
|
|
}
|
|
|
|
var baseGitRepo *git.Repository
|
|
if pr.BaseRepoID == pr.HeadRepoID {
|
|
baseGitRepo = headGitRepo
|
|
} else {
|
|
if err = pr.LoadBaseRepo(ctx); err != nil {
|
|
log.Error("%-v LoadBaseRepo: %v", pr, err)
|
|
return
|
|
}
|
|
|
|
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
|
if err != nil {
|
|
log.Error("OpenRepository %-v: %v", pr.BaseRepo, err)
|
|
return
|
|
}
|
|
defer baseGitRepo.Close()
|
|
}
|
|
|
|
if err := pull_service.Merge(ctx, pr, doer, baseGitRepo, scheduledPRM.MergeStyle, "", scheduledPRM.Message, true); err != nil {
|
|
log.Error("pull_service.Merge: %v", err)
|
|
return
|
|
}
|
|
}
|