Skip to content
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

Skip failed LFS objects and continue when migrating LFS #31697

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions modules/lfs/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package lfs

import (
"fmt"
)

type ErrLFS struct {
OID string
Err error
}

type ErrLFSDownload struct {
ErrLFS
}

type ErrLFSUpload struct {
ErrLFS
}

type ErrLFSVerify struct {
ErrLFS
}

func (e *ErrLFS) Error() string {
return fmt.Sprintf("LFS error for object %s: %v", e.OID, e.Err)
}

func (e *ErrLFS) Unwrap() error {
return e.Err
}

func (e *ErrLFSDownload) Error() string {
return fmt.Sprintf("LFS error while downloading [%s]: %s", e.OID, e.Err)
}

func (e *ErrLFSUpload) Error() string {
return fmt.Sprintf("LFS error while uploading [%s]: %s", e.OID, e.Err)
}

func (e *ErrLFSVerify) Error() string {
return fmt.Sprintf("LFS error while verifying [%s]: %s", e.OID, e.Err)
}

func (e *ErrLFSDownload) Is(target error) bool {
_, ok := target.(*ErrLFSDownload)
return ok
}

func (e *ErrLFSUpload) Is(target error) bool {
_, ok := target.(*ErrLFSUpload)
return ok
}

func (e *ErrLFSVerify) Is(target error) bool {
_, ok := target.(*ErrLFSVerify)
return ok
}
52 changes: 42 additions & 10 deletions modules/lfs/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@

url := fmt.Sprintf("%s/objects/batch", c.endpoint)

request := &BatchRequest{operation, c.transferNames(), nil, objects}
payload := new(bytes.Buffer)
err := json.NewEncoder(payload).Encode(request)
err := json.NewEncoder(payload).Encode(&BatchRequest{
Operation: operation,
Transfers: c.transferNames(),
Objects: objects,
})
if err != nil {
log.Error("Error encoding json: %v", err)
return nil, err
Expand Down Expand Up @@ -134,15 +137,19 @@
return fmt.Errorf("TransferAdapter not found: %s", result.Transfer)
}

var resultErrors []error
for _, object := range result.Objects {
if object.Error != nil {
objectError := errors.New(object.Error.Message)
log.Trace("Error on object %v: %v", object.Pointer, objectError)
log.Trace("Error on object %v: %v", object.Pointer, object.Error.Message)
if uc != nil {
objectError := &ErrLFSUpload{ErrLFS{OID: object.Oid, Err: object.Error}}
resultErrors = append(resultErrors, objectError)
if _, err := uc(object.Pointer, objectError); err != nil {
return err
}
} else {
objectError := &ErrLFSDownload{ErrLFS{OID: object.Oid, Err: object.Error}}
resultErrors = append(resultErrors, objectError)
if err := dc(object.Pointer, nil, objectError); err != nil {
return err
}
Expand All @@ -164,18 +171,33 @@

content, err := uc(object.Pointer, nil)
if err != nil {
return err
if errors.Is(err, &ErrLFSUpload{}) {
resultErrors = append(resultErrors, err)
continue
} else {

Check failure on line 177 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 177 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 177 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return err
}
}

err = transferAdapter.Upload(ctx, link, object.Pointer, content)
if err != nil {
return err
if errors.Is(err, &ErrLFSUpload{}) {
resultErrors = append(resultErrors, err)
continue
} else {

Check failure on line 187 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 187 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 187 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return err
}
}

link, ok = object.Actions["verify"]
if ok {
if err := transferAdapter.Verify(ctx, link, object.Pointer); err != nil {
return err
if errors.Is(err, &ErrLFSVerify{}) {
resultErrors = append(resultErrors, err)
continue
} else {

Check failure on line 198 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 198 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 198 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return err
}
}
}
} else {
Expand All @@ -191,16 +213,26 @@

content, err := transferAdapter.Download(ctx, link)
if err != nil {
return err
if errors.Is(err, &ErrLFSDownload{}) {
resultErrors = append(resultErrors, err)
continue
} else {

Check failure on line 219 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 219 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 219 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return err
}
}

if err := dc(object.Pointer, content, nil); err != nil {
return err
if errors.Is(err, &ErrLFSDownload{}) {
resultErrors = append(resultErrors, err)
continue
} else {

Check failure on line 228 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 228 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 228 in modules/lfs/http_client.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
return err
}
}
}
}

return nil
return errors.Join(resultErrors...)
}

// createRequest creates a new request, and sets the headers.
Expand Down
5 changes: 5 additions & 0 deletions modules/lfs/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package lfs

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -64,6 +65,10 @@ type ObjectError struct {
Message string `json:"message"`
}

func (oe *ObjectError) Error() string {
return fmt.Sprintf("code: %d, message: %s", oe.Code, oe.Message)
}

// PointerBlob associates a Git blob with a Pointer.
type PointerBlob struct {
Hash string
Expand Down
33 changes: 24 additions & 9 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"context"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -178,9 +179,16 @@
errChan := make(chan error, 1)
go lfs.SearchPointerBlobs(ctx, gitRepo, pointerChan, errChan)

var lfsDownloadErrors []error

downloadObjects := func(pointers []lfs.Pointer) error {
err := lfsClient.Download(ctx, pointers, func(p lfs.Pointer, content io.ReadCloser, objectError error) error {
if objectError != nil {
if errors.Is(objectError, &lfs.ErrLFSDownload{}) {
log.Trace("Repo[%-v]: Ignoring LFS error: %v", repo, objectError)
lfsDownloadErrors = append(lfsDownloadErrors, objectError)
return nil
}
return objectError
}

Expand Down Expand Up @@ -208,10 +216,13 @@
default:
}
}
if errors.Is(err, &lfs.ErrLFSDownload{}) {
return nil
}
return err
}

var batch []lfs.Pointer
var downloadBatch []lfs.Pointer
for pointerBlob := range pointerChan {
meta, err := git_model.GetLFSMetaObjectByOid(ctx, repo.ID, pointerBlob.Oid)
if err != nil && err != git_model.ErrLFSObjectNotExist {
Expand All @@ -233,28 +244,32 @@

if exist {
log.Trace("Repo[%-v]: LFS object %-v already present; creating meta object", repo, pointerBlob.Pointer)
_, err := git_model.NewLFSMetaObject(ctx, repo.ID, pointerBlob.Pointer)
m, err := git_model.NewLFSMetaObject(ctx, repo.ID, pointerBlob.Pointer)
if err != nil {
log.Error("Repo[%-v]: Error creating LFS meta object %-v: %v", repo, pointerBlob.Pointer, err)
return err
}
if m.Existing {
log.Trace("Repo[%-v]: LFS meta object %-v was already present", repo, m)
}
continue
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this continue is not needed but make the code more redable ... at least a bit

} else {

Check failure on line 256 in modules/repository/repo.go

View workflow job for this annotation

GitHub Actions / lint-backend

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 256 in modules/repository/repo.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)

Check failure on line 256 in modules/repository/repo.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
if setting.LFS.MaxFileSize > 0 && pointerBlob.Size > setting.LFS.MaxFileSize {
log.Info("Repo[%-v]: LFS object %-v download denied because of LFS_MAX_FILE_SIZE=%d < size %d", repo, pointerBlob.Pointer, setting.LFS.MaxFileSize, pointerBlob.Size)
continue
}

batch = append(batch, pointerBlob.Pointer)
if len(batch) >= lfsClient.BatchSize() {
if err := downloadObjects(batch); err != nil {
downloadBatch = append(downloadBatch, pointerBlob.Pointer)
if len(downloadBatch) >= lfsClient.BatchSize() {
if err := downloadObjects(downloadBatch); err != nil {
return err
}
batch = nil
downloadBatch = nil
}
}
}
if len(batch) > 0 {
if err := downloadObjects(batch); err != nil {
if len(downloadBatch) > 0 {
if err := downloadObjects(downloadBatch); err != nil {
return err
}
}
Expand All @@ -265,7 +280,7 @@
return err
}

return nil
return errors.Join(lfsDownloadErrors...)
}

// shortRelease to reduce load memory, this struct can replace repo_model.Release
Expand Down
1 change: 1 addition & 0 deletions services/repository/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
lfsClient := lfs.NewClient(endpoint, httpTransport)
if err = repo_module.StoreMissingLfsObjectsInRepository(ctx, repo, gitRepo, lfsClient); err != nil {
log.Error("Failed to store missing LFS objects for repository: %v", err)
// TODO: check for lfs.ErrLFSDownload errors and display them
}
}
}
Expand Down
Loading