-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
WIP: Defer last commit info #16063
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
Closed
Closed
WIP: Defer last commit info #16063
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
326cb03
Defer last commit info
zeripath 10b56e1
Make Queue requests cancelable
zeripath 67b817e
reload in two seconds instead
zeripath 44b4254
fix copyright notice
zeripath dcd7397
default to use a level queue
zeripath c920249
Merge remote-tracking branch 'origin/main' into defer-last-commit-info
zeripath bcded8a
fixup! Merge remote-tracking branch 'origin/main' into defer-last-com…
zeripath 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
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
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,177 @@ | ||
// Copyright 2019 Gitea. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// +build gogit | ||
|
||
package repository | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
"path/filepath" | ||
"sync" | ||
|
||
"code.gitea.io/gitea/modules/cache" | ||
"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" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
var lock = sync.Mutex{} | ||
var table = map[CommitCacheRequest]bool{} | ||
var lastCommitQueue queue.UniqueQueue | ||
|
||
// CommitCacheRequest represents a cache request | ||
type CommitCacheRequest struct { | ||
Repo string | ||
CommitID string | ||
TreePath string | ||
Recursive bool | ||
} | ||
|
||
// Do runs the cache request uniquely ensuring that only one cache request is running for this request triple | ||
func (req *CommitCacheRequest) Do() error { | ||
ctx, cancel, _ := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("Cache: %s:%s:%s:%t", req.Repo, req.CommitID, req.TreePath, req.Recursive)) | ||
defer cancel() | ||
|
||
recursive := req.Recursive | ||
req.Recursive = false | ||
|
||
repo, err := git.OpenRepository(filepath.Join(setting.RepoRootPath, req.Repo+".git")) | ||
if err != nil { | ||
return err | ||
} | ||
commit, err := repo.GetCommit(req.CommitID) | ||
if err != nil { | ||
if git.IsErrNotExist(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
lccache := git.NewLastCommitCache(req.Repo, repo, setting.LastCommitCacheTTLSeconds, cache.GetCache()) | ||
|
||
directories := []string{req.TreePath} | ||
for len(directories) > 0 { | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
default: | ||
} | ||
|
||
req.TreePath = directories[len(directories)-1] | ||
next, err := req.doTree(ctx, repo, commit, recursive, lccache) | ||
if err != nil { | ||
return err | ||
} | ||
directories = append(next, directories[:len(directories)-1]...) | ||
} | ||
return nil | ||
} | ||
|
||
func (req *CommitCacheRequest) doTree(ctx context.Context, repo *git.Repository, commit *git.Commit, recursive bool, lccache *git.LastCommitCache) ([]string, error) { | ||
tree, err := commit.Tree.SubTree(req.TreePath) | ||
if err != nil { | ||
if git.IsErrNotExist(err) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
entries, err := tree.ListEntries() | ||
if err != nil { | ||
if git.IsErrNotExist(err) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
directories := make([]string, 0, len(entries)) | ||
|
||
commitNodeIndex, commitGraphFile := repo.CommitNodeIndex() | ||
if commitGraphFile != nil { | ||
defer commitGraphFile.Close() | ||
} | ||
|
||
commitNode, err := commitNodeIndex.Get(commit.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lock.Lock() | ||
if has := table[*req]; has { | ||
lock.Unlock() | ||
if recursive { | ||
for _, entry := range entries { | ||
if entry.IsDir() { | ||
directories = append(directories, path.Join(req.TreePath, entry.Name())) | ||
} | ||
} | ||
} | ||
return directories, nil | ||
} | ||
table[*req] = true | ||
lock.Unlock() | ||
defer func() { | ||
lock.Lock() | ||
delete(table, *req) | ||
lock.Unlock() | ||
}() | ||
|
||
entryPaths := make([]string, 0, len(entries)) | ||
for _, entry := range entries { | ||
if recursive && entry.IsDir() { | ||
directories = append(directories, path.Join(req.TreePath, entry.Name())) | ||
} | ||
_, ok := lccache.GetCachedCommitID(req.CommitID, path.Join(req.TreePath, entry.Name())) | ||
if !ok { | ||
entryPaths = append(entryPaths, entry.Name()) | ||
} | ||
} | ||
|
||
if len(entryPaths) == 0 { | ||
return directories, nil | ||
} | ||
|
||
commits, err := git.GetLastCommitForPaths(ctx, commitNode, req.TreePath, entryPaths) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for entryPath, entryCommit := range commits { | ||
if err := lccache.Put(commit.ID.String(), path.Join(req.TreePath, entryPath), entryCommit.ID().String()); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return directories, nil | ||
} | ||
|
||
func handle(data ...queue.Data) { | ||
for _, datum := range data { | ||
req := datum.(*CommitCacheRequest) | ||
if err := req.Do(); err != nil { | ||
log.Error("Unable to process commit cache request for %s:%s:%s:%t: %v", req.Repo, req.CommitID, req.TreePath, req.Recursive, err) | ||
} | ||
} | ||
} | ||
|
||
// Init initialises the queue | ||
func Init() error { | ||
lastCommitQueue = queue.CreateUniqueQueue("last_commit_queue", handle, &CommitCacheRequest{}).(queue.UniqueQueue) | ||
|
||
return nil | ||
} | ||
|
||
// UpdateCache queues the the request | ||
func UpdateCache(repo, commitID, treePath string, recursive bool) error { | ||
return lastCommitQueue.Push(&CommitCacheRequest{ | ||
Repo: repo, | ||
CommitID: commitID, | ||
TreePath: treePath, | ||
Recursive: recursive, | ||
}) | ||
} |
Oops, something went wrong.
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.