- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 6.2k
Add doctor command for full GC of LFS #21978
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
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            16 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      bed6a51
              
                Add doctor command for full GC of LFS
              
              
                zeripath 3a3ac24
              
                Merge branch 'main' into full-gc-lfs
              
              
                zeripath cc31e60
              
                fix fmt
              
              
                zeripath 4130d88
              
                Merge remote-tracking branch 'zeripath/full-gc-lfs' into full-gc-lfs
              
              
                zeripath 2d36bf7
              
                use IsObjectExist instead
              
              
                zeripath 36e25fa
              
                Merge branch 'main' into full-gc-lfs
              
              
                zeripath 24ae9d5
              
                Merge remote-tracking branch 'origin/main' into full-gc-lfs
              
              
                zeripath 3b4deb6
              
                update date of header
              
              
                zeripath 8aea5a7
              
                slight adjust select query
              
              
                zeripath 4b5fc12
              
                as per wolfogre
              
              
                zeripath c9f992d
              
                Merge branch 'main' into full-gc-lfs
              
              
                zeripath 7f7288b
              
                Merge remote-tracking branch 'origin/main' into full-gc-lfs
              
              
                zeripath 16c082f
              
                add olderthan option
              
              
                zeripath 46a1a5e
              
                add comment
              
              
                zeripath 598d95f
              
                Merge branch 'main' into full-gc-lfs
              
              
                lunny f135557
              
                Merge branch 'main' into full-gc-lfs
              
              
                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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2022 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|  | ||
| package doctor | ||
|  | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
|  | ||
| "code.gitea.io/gitea/modules/log" | ||
| "code.gitea.io/gitea/modules/setting" | ||
| "code.gitea.io/gitea/services/repository" | ||
| ) | ||
|  | ||
| func init() { | ||
| Register(&Check{ | ||
| Title: "Garbage collect LFS", | ||
| Name: "gc-lfs", | ||
| IsDefault: false, | ||
| Run: garbageCollectLFSCheck, | ||
| AbortIfFailed: false, | ||
| SkipDatabaseInitialization: false, | ||
| Priority: 1, | ||
| }) | ||
| } | ||
|  | ||
| func garbageCollectLFSCheck(ctx context.Context, logger log.Logger, autofix bool) error { | ||
| if !setting.LFS.StartServer { | ||
| return fmt.Errorf("LFS support is disabled") | ||
| } | ||
|  | ||
| if err := repository.GarbageCollectLFSMetaObjects(ctx, logger, autofix); err != nil { | ||
|         
                  zeripath marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| return err | ||
| } | ||
|  | ||
| return checkStorage(&checkStorageOptions{LFS: true})(ctx, logger, autofix) | ||
| } | ||
  
    
      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 2022 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|  | ||
| package repository | ||
|  | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|  | ||
| "code.gitea.io/gitea/models/db" | ||
| git_model "code.gitea.io/gitea/models/git" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| "code.gitea.io/gitea/modules/git" | ||
| "code.gitea.io/gitea/modules/lfs" | ||
| "code.gitea.io/gitea/modules/log" | ||
|  | ||
| "xorm.io/builder" | ||
| ) | ||
|  | ||
| func GarbageCollectLFSMetaObjects(ctx context.Context, logger log.Logger, autofix bool) error { | ||
| log.Trace("Doing: GarbageCollectLFSMetaObjects") | ||
|  | ||
| if err := db.Iterate( | ||
| ctx, | ||
| builder.And(builder.Gt{"id": 0}), | ||
| func(ctx context.Context, repo *repo_model.Repository) error { | ||
| return GarbageCollectLFSMetaObjectsForRepo(ctx, repo, logger, autofix) | ||
| }, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|  | ||
| log.Trace("Finished: GarbageCollectLFSMetaObjects") | ||
| return nil | ||
| } | ||
|  | ||
| func GarbageCollectLFSMetaObjectsForRepo(ctx context.Context, repo *repo_model.Repository, logger log.Logger, autofix bool) error { | ||
| if logger != nil { | ||
| logger.Info("Checking %-v", repo) | ||
| } | ||
| total, orphaned, collected, deleted := 0, 0, 0, 0 | ||
| if logger != nil { | ||
| defer func() { | ||
| if orphaned == 0 { | ||
| logger.Info("Found %d total LFSMetaObjects in %-v", total, repo) | ||
| } else if !autofix { | ||
| logger.Info("Found %d/%d orphaned LFSMetaObjects in %-v", orphaned, total, repo) | ||
| } else { | ||
| logger.Info("Collected %d/%d orphaned/%d total LFSMetaObjects in %-v. %d removed from storage.", collected, orphaned, total, repo, deleted) | ||
| } | ||
| }() | ||
| } | ||
|  | ||
| gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) | ||
| if err != nil { | ||
| log.Error("Unable to open git repository %-v: %v", repo, err) | ||
| return err | ||
| } | ||
| defer gitRepo.Close() | ||
|  | ||
| store := lfs.NewContentStore() | ||
|  | ||
| return git_model.IterateLFSMetaObjectsForRepo(ctx, repo.ID, func(ctx context.Context, metaObject *git_model.LFSMetaObject, count int64) error { | ||
| total++ | ||
| pointerSha := git.ComputeBlobHash([]byte(metaObject.Pointer.StringContent())) | ||
|  | ||
| if gitRepo.IsObjectExist(pointerSha.String()) { | ||
| return nil | ||
| } | ||
| orphaned++ | ||
|  | ||
| if !autofix { | ||
| return nil | ||
| } | ||
| // Non-existent pointer file | ||
| _, err = git_model.RemoveLFSMetaObjectByOidFn(repo.ID, metaObject.Oid, func(count int64) error { | ||
| if count > 0 { | ||
| return nil | ||
| } | ||
|  | ||
| if err := store.Delete(metaObject.RelativePath()); err != nil { | ||
| log.Error("Unable to remove lfs metaobject %s from store: %v", metaObject.Oid, err) | ||
| } | ||
| deleted++ | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to remove meta-object %s in %s: %w", metaObject.Oid, repo.FullName(), err) | ||
| } | ||
| collected++ | ||
|  | ||
| return nil | ||
| }, &git_model.IterateLFSMetaObjectsForRepoOptions{ | ||
| // Only attempt to garbage collect lfs meta objects older than a week as the order of git lfs upload | ||
| // and git object upload is not necessarily guaranteed. It's possible to imagine a situation whereby | ||
| // an LFS object is uploaded but the git branch is not uploaded immediately, or there are some rapid | ||
| // changes in new branches that might lead to lfs objects becoming temporarily unassociated with git | ||
| // objects. | ||
| // | ||
| // It is likely that a week is potentially excessive but it should definitely be enough that any | ||
| // unassociated LFS object is genuinely unassociated. | ||
| OlderThan: time.Now().Add(-24 * 7 * time.Hour), | ||
| }) | ||
| } | 
  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.