Skip to content

Commit

Permalink
Merge pull request #7245 from Lyndon-Li/issue-fix-7244
Browse files Browse the repository at this point in the history
Issue 7244: delete incomplete snapshot automatically for kopia uploader
  • Loading branch information
Lyndon-Li authored Dec 22, 2023
2 parents ee345cf + 60d2c62 commit f5e36c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/7245-Lyndon-Li
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue #7244. By the end of the upload, check the outstanding incomplete snapshots and delete them by calling ApplyRetentionPolicy
6 changes: 6 additions & 0 deletions pkg/uploader/kopia/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
)

// All function mainly used to make testing more convenient
var applyRetentionPolicyFunc = policy.ApplyRetentionPolicy
var treeForSourceFunc = policy.TreeForSource
var setPolicyFunc = policy.SetPolicy
var saveSnapshotFunc = snapshot.SaveSnapshot
Expand Down Expand Up @@ -291,6 +292,11 @@ func SnapshotSource(
return "", 0, errors.Wrapf(err, "Failed to save kopia manifest %v", manifest.ID)
}

_, err = applyRetentionPolicyFunc(ctx, rep, sourceInfo, true)
if err != nil {
return "", 0, errors.Wrapf(err, "Failed to apply kopia retention policy for si %v", sourceInfo)
}

if err = rep.Flush(ctx); err != nil {
return "", 0, errors.Wrapf(err, "Failed to flush kopia repository")
}
Expand Down
1 change: 1 addition & 0 deletions pkg/uploader/kopia/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func injectSnapshotFuncs() *snapshotMockes {
repoWriterMock: &repomocks.RepositoryWriter{},
}

applyRetentionPolicyFunc = s.policyMock.ApplyRetentionPolicy
setPolicyFunc = s.policyMock.SetPolicy
treeForSourceFunc = s.policyMock.TreeForSource
loadSnapshotFunc = s.snapshotMock.LoadSnapshot
Expand Down
9 changes: 5 additions & 4 deletions pkg/uploader/mocks/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package mocks
import (
"context"

"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/snapshot/policy"
"github.com/stretchr/testify/mock"

Expand Down Expand Up @@ -56,15 +57,15 @@ func (_m *Policy) TreeForSource(ctx context.Context, rep repo.Repository, si sna
}

// ApplyRetentionPolicy provides a mock function with given fields: ctx, rep, sourceInfo, reallyDelete
func (_m *Policy) ApplyRetentionPolicy(ctx context.Context, rep repo.RepositoryWriter, sourceInfo snapshot.SourceInfo, reallyDelete bool) ([]*snapshot.Manifest, error) {
func (_m *Policy) ApplyRetentionPolicy(ctx context.Context, rep repo.RepositoryWriter, sourceInfo snapshot.SourceInfo, reallyDelete bool) ([]manifest.ID, error) {
ret := _m.Called(ctx, rep, sourceInfo, reallyDelete)

var r0 []*snapshot.Manifest
if rf, ok := ret.Get(0).(func(context.Context, repo.RepositoryWriter, snapshot.SourceInfo, bool) []*snapshot.Manifest); ok {
var r0 []manifest.ID
if rf, ok := ret.Get(0).(func(context.Context, repo.RepositoryWriter, snapshot.SourceInfo, bool) []manifest.ID); ok {
r0 = rf(ctx, rep, sourceInfo, reallyDelete)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*snapshot.Manifest)
r0 = ret.Get(0).([]manifest.ID)
}
}

Expand Down

0 comments on commit f5e36c1

Please sign in to comment.