Skip to content

Commit

Permalink
Use batch database operations instead of one by one to optimze api pu…
Browse files Browse the repository at this point in the history
…lls (#32680)

Resolve #31492

The response time for the Pull Requests API has improved significantly,
dropping from over `2000ms` to about `350ms` on my local machine. It's
about `6` times faster.

A key area for further optimization lies in batch-fetching data for
`apiPullRequest.ChangedFiles, apiPullRequest.Additions, and
apiPullRequest.Deletions`.

Tests `TestAPIViewPulls` does exist and new tests added.

- This PR also fixes some bugs in `GetDiff` functions.
- This PR also fixes data inconsistent in test data. For a pull request,
the head branch's reference should be equal to the reference in
`pull/xxx/head`.
  • Loading branch information
lunny authored Dec 10, 2024
1 parent 2ac6f2b commit fbe6d9d
Show file tree
Hide file tree
Showing 15 changed files with 566 additions and 93 deletions.
20 changes: 18 additions & 2 deletions models/fixtures/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
reviewer_id: 1
issue_id: 2
content: "Demo Review"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -12,6 +13,7 @@
reviewer_id: 534543
issue_id: 534543
content: "Invalid Review #1"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -20,6 +22,7 @@
reviewer_id: 1
issue_id: 343545
content: "Invalid Review #2"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -28,6 +31,7 @@
reviewer_id: 1
issue_id: 2
content: "Pending Review"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -36,6 +40,7 @@
reviewer_id: 1
issue_id: 3
content: "New review 1"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -61,8 +66,8 @@
type: 1
reviewer_id: 4
issue_id: 3
original_author_id: 0
content: "New review 5"
original_author_id: 0
commit_id: 8091a55037cd59e47293aca02981b5a67076b364
stale: true
updated_unix: 946684813
Expand All @@ -73,16 +78,17 @@
reviewer_id: 2
issue_id: 3
content: "New review 3 rejected"
original_author_id: 0
updated_unix: 946684814
created_unix: 946684814
original_author_id: 0

-
id: 10
type: 3
reviewer_id: 100
issue_id: 3
content: "a deleted user's review"
original_author_id: 0
official: true
updated_unix: 946684815
created_unix: 946684815
Expand Down Expand Up @@ -112,6 +118,7 @@
reviewer_id: 5
issue_id: 11
content: "old review from user5"
original_author_id: 0
updated_unix: 946684820
created_unix: 946684820

Expand All @@ -121,6 +128,7 @@
reviewer_id: 5
issue_id: 11
content: "duplicate review from user5 (latest)"
original_author_id: 0
updated_unix: 946684830
created_unix: 946684830

Expand All @@ -130,6 +138,7 @@
reviewer_id: 6
issue_id: 11
content: "singular review from org6 and final review for this pr"
original_author_id: 0
updated_unix: 946684831
created_unix: 946684831

Expand All @@ -139,6 +148,7 @@
reviewer_id: 20
issue_id: 20
content: "review request for user20"
original_author_id: 0
updated_unix: 946684832
created_unix: 946684832

Expand All @@ -148,6 +158,7 @@
reviewer_id: 20
issue_id: 20
content: "review approved by user20"
original_author_id: 0
updated_unix: 946684833
created_unix: 946684833

Expand All @@ -158,6 +169,7 @@
reviewer_team_id: 5
issue_id: 20
content: "review request for team5"
original_author_id: 0
updated_unix: 946684834
created_unix: 946684834

Expand All @@ -168,6 +180,7 @@
reviewer_team_id: 0
issue_id: 20
content: "review request for user15"
original_author_id: 0
updated_unix: 946684835
created_unix: 946684835

Expand All @@ -177,6 +190,7 @@
reviewer_id: 1
issue_id: 2
content: "Review Comment"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810

Expand All @@ -186,6 +200,7 @@
reviewer_id: 5
issue_id: 3
content: "reviewed by user5"
original_author_id: 0
commit_id: 4a357436d925b5c974181ff12a994538ddc5a269
updated_unix: 946684816
created_unix: 946684816
Expand All @@ -196,5 +211,6 @@
reviewer_id: 5
issue_id: 3
content: "review request for user5"
original_author_id: 0
updated_unix: 946684817
created_unix: 946684817
59 changes: 59 additions & 0 deletions models/issues/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -240,6 +241,64 @@ func (prs PullRequestList) GetIssueIDs() []int64 {
})
}

func (prs PullRequestList) LoadReviewCommentsCounts(ctx context.Context) (map[int64]int, error) {
issueIDs := prs.GetIssueIDs()
countsMap := make(map[int64]int, len(issueIDs))
counts := make([]struct {
IssueID int64
Count int
}, 0, len(issueIDs))
if err := db.GetEngine(ctx).Select("issue_id, count(*) as count").
Table("comment").In("issue_id", issueIDs).And("type = ?", CommentTypeReview).
GroupBy("issue_id").Find(&counts); err != nil {
return nil, err
}
for _, c := range counts {
countsMap[c.IssueID] = c.Count
}
return countsMap, nil
}

func (prs PullRequestList) LoadReviews(ctx context.Context) (ReviewList, error) {
issueIDs := prs.GetIssueIDs()
reviews := make([]*Review, 0, len(issueIDs))

subQuery := builder.Select("max(id) as id").
From("review").
Where(builder.In("issue_id", issueIDs)).
And(builder.In("`type`", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest)).
And(builder.Eq{
"dismissed": false,
"original_author_id": 0,
"reviewer_team_id": 0,
}).
GroupBy("issue_id, reviewer_id")
// Get latest review of each reviewer, sorted in order they were made
if err := db.GetEngine(ctx).In("id", subQuery).OrderBy("review.updated_unix ASC").Find(&reviews); err != nil {
return nil, err
}

teamReviewRequests := make([]*Review, 0, 5)
subQueryTeam := builder.Select("max(id) as id").
From("review").
Where(builder.In("issue_id", issueIDs)).
And(builder.Eq{
"original_author_id": 0,
}).And(builder.Neq{
"reviewer_team_id": 0,
}).
GroupBy("issue_id, reviewer_team_id")
if err := db.GetEngine(ctx).In("id", subQueryTeam).OrderBy("review.updated_unix ASC").Find(&teamReviewRequests); err != nil {
return nil, err
}

if len(teamReviewRequests) > 0 {
reviews = append(reviews, teamReviewRequests...)
}

return reviews, nil
}

// HasMergedPullRequestInRepo returns whether the user(poster) has merged pull-request in the repo
func HasMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (bool, error) {
return db.GetEngine(ctx).
Expand Down
64 changes: 64 additions & 0 deletions models/issues/pull_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package issues_test

import (
"testing"

"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"

"github.com/stretchr/testify/assert"
)

func TestPullRequestList_LoadAttributes(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
assert.NoError(t, issues_model.PullRequestList(prs).LoadAttributes(db.DefaultContext))
for _, pr := range prs {
assert.NotNil(t, pr.Issue)
assert.Equal(t, pr.IssueID, pr.Issue.ID)
}

assert.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes(db.DefaultContext))
}

func TestPullRequestList_LoadReviewCommentsCounts(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
reviewComments, err := issues_model.PullRequestList(prs).LoadReviewCommentsCounts(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, reviewComments, 2)
for _, pr := range prs {
assert.EqualValues(t, reviewComments[pr.IssueID], 1)
}
}

func TestPullRequestList_LoadReviews(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
reviewList, err := issues_model.PullRequestList(prs).LoadReviews(db.DefaultContext)
assert.NoError(t, err)
// 1, 7, 8, 9, 10, 22
assert.Len(t, reviewList, 6)
assert.EqualValues(t, 1, reviewList[0].ID)
assert.EqualValues(t, 7, reviewList[1].ID)
assert.EqualValues(t, 8, reviewList[2].ID)
assert.EqualValues(t, 9, reviewList[3].ID)
assert.EqualValues(t, 10, reviewList[4].ID)
assert.EqualValues(t, 22, reviewList[5].ID)
}
20 changes: 2 additions & 18 deletions models/issues/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestPullRequestsNewest(t *testing.T) {
func TestLoadRequestedReviewers(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
assert.NoError(t, pull.LoadIssue(db.DefaultContext))
issue := pull.Issue
assert.NoError(t, issue.LoadRepo(db.DefaultContext))
Expand All @@ -93,7 +93,7 @@ func TestLoadRequestedReviewers(t *testing.T) {
assert.NotNil(t, comment)

assert.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext))
assert.Len(t, pull.RequestedReviewers, 1)
assert.Len(t, pull.RequestedReviewers, 6)

comment, err = issues_model.RemoveReviewRequest(db.DefaultContext, issue, user1, &user_model.User{})
assert.NoError(t, err)
Expand Down Expand Up @@ -234,22 +234,6 @@ func TestPullRequest_UpdateCols(t *testing.T) {
unittest.CheckConsistencyFor(t, pr)
}

func TestPullRequestList_LoadAttributes(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
assert.NoError(t, issues_model.PullRequestList(prs).LoadAttributes(db.DefaultContext))
for _, pr := range prs {
assert.NotNil(t, pr.Issue)
assert.Equal(t, pr.IssueID, pr.Issue.ID)
}

assert.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes(db.DefaultContext))
}

// TODO TestAddTestPullRequestTask

func TestPullRequest_IsWorkInProgress(t *testing.T) {
Expand Down
11 changes: 3 additions & 8 deletions models/issues/review_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,9 @@ func (reviews ReviewList) LoadReviewersTeams(ctx context.Context) error {
}
}

teamsMap := make(map[int64]*organization_model.Team, 0)
for _, teamID := range reviewersTeamsIDs {
team, err := organization_model.GetTeamByID(ctx, teamID)
if err != nil {
return err
}

teamsMap[teamID] = team
teamsMap, err := organization_model.GetTeamsByIDs(ctx, reviewersTeamsIDs)
if err != nil {
return err
}

for _, review := range reviews {
Expand Down
Loading

0 comments on commit fbe6d9d

Please sign in to comment.