-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Reduce unnecessary DB queries for Actions tasks #25199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lunny
merged 48 commits into
go-gitea:main
from
sillyguodong:feature/fetch_with_task_index
Jul 24, 2023
Merged
Changes from all commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
82cbd09
fetch task with index
sillyguodong da135c4
add test
sillyguodong 65395a1
Increase function no need return
sillyguodong 2f460ab
Merge branch 'feature/fetch_with_task_index' of github.com:sillyguodo…
sillyguodong 964e860
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 148606c
use version
sillyguodong 7caf9d2
update
sillyguodong b718d8b
go mod
sillyguodong 94c3bee
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 6b4fbad
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong d59c383
go mod
sillyguodong 23f8721
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 72ea6ac
use db to store tasks version
sillyguodong e905fed
support re-run job
sillyguodong c430b28
log error
sillyguodong 483a01a
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 96915c6
typo
sillyguodong 1861d25
fix migrations
sillyguodong 46edcba
Merge branch 'main' into feature/fetch_with_task_index
silverwind ff179fd
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 0771112
fix review
sillyguodong b61682b
delete fmt
sillyguodong 67a4216
fix if cond
sillyguodong 0d5b917
delete empty line
sillyguodong 5285ed4
update
sillyguodong c8e9d93
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 3e12fd3
adjust some logic
sillyguodong 7d66700
fix
sillyguodong 9b025bc
comment
sillyguodong 8c7051a
typo
sillyguodong be3576b
context
sillyguodong 4283ebe
another context
sillyguodong 9837fb5
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 98cec83
no introduce new ctx any more
sillyguodong 32a4778
Merge branch 'main' into feature/fetch_with_task_index
sillyguodong 1e8be9a
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 75d7109
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 23ec84e
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 56fe8de
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 900d1e8
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 06ad0cc
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 43f076f
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot 8590ce4
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot f82fffb
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot dd94ee7
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot fc22e0c
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot f3a43f9
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot f840c1b
Merge branch 'main' into feature/fetch_with_task_index
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
) | ||
|
||
// ActionTasksVersion | ||
// If both ownerID and repoID is zero, its scope is global. | ||
// If ownerID is not zero and repoID is zero, its scope is org (there is no user-level runner currrently). | ||
// If ownerID is zero and repoID is not zero, its scope is repo. | ||
type ActionTasksVersion struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
OwnerID int64 `xorm:"UNIQUE(owner_repo)"` | ||
RepoID int64 `xorm:"INDEX UNIQUE(owner_repo)"` | ||
Version int64 | ||
CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
UpdatedUnix timeutil.TimeStamp `xorm:"updated"` | ||
} | ||
|
||
func init() { | ||
db.RegisterModel(new(ActionTasksVersion)) | ||
} | ||
|
||
func GetTasksVersionByScope(ctx context.Context, ownerID, repoID int64) (int64, error) { | ||
var tasksVersion ActionTasksVersion | ||
has, err := db.GetEngine(ctx).Where("owner_id = ? AND repo_id = ?", ownerID, repoID).Get(&tasksVersion) | ||
if err != nil { | ||
return 0, err | ||
} else if !has { | ||
return 0, nil | ||
} | ||
return tasksVersion.Version, err | ||
} | ||
|
||
func insertTasksVersion(ctx context.Context, ownerID, repoID int64) (*ActionTasksVersion, error) { | ||
tasksVersion := &ActionTasksVersion{ | ||
OwnerID: ownerID, | ||
RepoID: repoID, | ||
Version: 1, | ||
} | ||
if _, err := db.GetEngine(ctx).Insert(tasksVersion); err != nil { | ||
return nil, err | ||
} | ||
return tasksVersion, nil | ||
} | ||
|
||
func increaseTasksVersionByScope(ctx context.Context, ownerID, repoID int64) error { | ||
result, err := db.GetEngine(ctx).Exec("UPDATE action_tasks_version SET version = version + 1 WHERE owner_id = ? AND repo_id = ?", ownerID, repoID) | ||
if err != nil { | ||
return err | ||
} | ||
affected, err := result.RowsAffected() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if affected == 0 { | ||
// if update sql does not affect any rows, the database may be broken, | ||
// so re-insert the row of version data here. | ||
if _, err := insertTasksVersion(ctx, ownerID, repoID); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IncreaseTaskVersion(ctx context.Context, ownerID, repoID int64) error { | ||
ctx, commiter, err := db.TxContext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer commiter.Close() | ||
|
||
// 1. increase global | ||
if err := increaseTasksVersionByScope(ctx, 0, 0); err != nil { | ||
log.Error("IncreaseTasksVersionByScope(Global): %v", err) | ||
return err | ||
} | ||
|
||
// 2. increase owner | ||
if ownerID > 0 { | ||
if err := increaseTasksVersionByScope(ctx, ownerID, 0); err != nil { | ||
log.Error("IncreaseTasksVersionByScope(Owner): %v", err) | ||
return err | ||
} | ||
} | ||
|
||
// 3. increase repo | ||
if repoID > 0 { | ||
if err := increaseTasksVersionByScope(ctx, 0, repoID); err != nil { | ||
log.Error("IncreaseTasksVersionByScope(Repo): %v", err) | ||
return err | ||
} | ||
} | ||
|
||
return commiter.Commit() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_21 //nolint | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
func CreateActionTasksVersionTable(x *xorm.Engine) error { | ||
type ActionTasksVersion struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
OwnerID int64 `xorm:"UNIQUE(owner_repo)"` | ||
RepoID int64 `xorm:"INDEX UNIQUE(owner_repo)"` | ||
Version int64 | ||
CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
UpdatedUnix timeutil.TimeStamp `xorm:"updated"` | ||
} | ||
|
||
return x.Sync(new(ActionTasksVersion)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.