diff options
author | James Fargher <jfargher@gitlab.com> | 2023-09-26 05:46:35 +0300 |
---|---|---|
committer | James Fargher <jfargher@gitlab.com> | 2023-09-28 02:23:48 +0300 |
commit | f34c1c8ba5156eb1f50479c2f152d224876d0a90 (patch) | |
tree | 3cd22df3b405cc3b2c2470363954af4c56d6ee7d | |
parent | b6c5124c4462e86f40b473ba379d0c5aab4152d2 (diff) |
backup: Use manifests on restore when available
When a backup ID is specified on restore and a manifest can be found,
then we now prefer to restore from the manifest file.
Changelog: added
-rw-r--r-- | internal/backup/locator.go | 23 | ||||
-rw-r--r-- | internal/backup/locator_test.go | 122 |
2 files changed, 143 insertions, 2 deletions
diff --git a/internal/backup/locator.go b/internal/backup/locator.go index 638b49309..1e1c42550 100644 --- a/internal/backup/locator.go +++ b/internal/backup/locator.go @@ -316,9 +316,28 @@ func (l ManifestLocator) FindLatest(ctx context.Context, repo storage.Repository return l.Fallback.FindLatest(ctx, repo) } -// Find passes through to Fallback +// Find loads the manifest for the provided repo and backupID. If this manifest +// does not exist, the fallback is used. func (l ManifestLocator) Find(ctx context.Context, repo storage.Repository, backupID string) (*Backup, error) { - return l.Fallback.Find(ctx, repo, backupID) + f, err := l.Sink.GetReader(ctx, manifestPath(repo, backupID)) + switch { + case errors.Is(err, ErrDoesntExist): + return l.Fallback.Find(ctx, repo, backupID) + case err != nil: + return nil, fmt.Errorf("manifest: find: %w", err) + } + defer f.Close() + + var backup Backup + + if err := toml.NewDecoder(f).Decode(&backup); err != nil { + return nil, fmt.Errorf("manifest: find: %w", err) + } + + backup.ID = backupID + backup.Repository = repo + + return &backup, nil } func manifestPath(repo storage.Repository, backupID string) string { diff --git a/internal/backup/locator_test.go b/internal/backup/locator_test.go index 7aed83608..90cac3664 100644 --- a/internal/backup/locator_test.go +++ b/internal/backup/locator_test.go @@ -11,9 +11,11 @@ import ( "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v16/internal/git" "gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest" + "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage" "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg" + "gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb" ) func TestLegacyLocator(t *testing.T) { @@ -483,3 +485,123 @@ custom_hooks_path = '%[1]s/%[2]s/002.custom_hooks.tar' `, repo.RelativePath, backupID), string(manifest)) }) } + +func TestManifestLocator_Find(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + desc string + repo storage.Repository + backupID string + setup func(t *testing.T, ctx context.Context, backupPath string) + expectedBackup *Backup + }{ + { + desc: "finds manifest", + repo: &gitalypb.Repository{ + StorageName: "default", + RelativePath: "vanity/repo.git", + }, + backupID: "abc123", + setup: func(t *testing.T, ctx context.Context, backupPath string) { + testhelper.WriteFiles(t, backupPath, map[string]any{ + "vanity/repo/LATEST": "abc123", + "vanity/repo/abc123/LATEST": "002", + "manifests/default/vanity/repo.git/abc123.toml": `object_format = 'sha1' + +[[steps]] +bundle_path = 'path/to/001.bundle' +ref_path = 'path/to/001.refs' +custom_hooks_path = 'path/to/001.custom_hooks.tar' + +[[steps]] +bundle_path = 'path/to/002.bundle' +ref_path = 'path/to/002.refs' +previous_ref_path = 'path/to/001.refs' +custom_hooks_path = 'path/to/002.custom_hooks.tar' +`, + }) + }, + expectedBackup: &Backup{ + ID: "abc123", + Repository: &gitalypb.Repository{ + StorageName: "default", + RelativePath: "vanity/repo.git", + }, + ObjectFormat: "sha1", + Steps: []Step{ + { + BundlePath: "path/to/001.bundle", + RefPath: "path/to/001.refs", + CustomHooksPath: "path/to/001.custom_hooks.tar", + }, + { + BundlePath: "path/to/002.bundle", + RefPath: "path/to/002.refs", + PreviousRefPath: "path/to/001.refs", + CustomHooksPath: "path/to/002.custom_hooks.tar", + }, + }, + }, + }, + { + desc: "fallback", + repo: &gitalypb.Repository{ + StorageName: "default", + RelativePath: "vanity/repo.git", + }, + backupID: "abc123", + setup: func(t *testing.T, ctx context.Context, backupPath string) { + testhelper.WriteFiles(t, backupPath, map[string]any{ + "vanity/repo/LATEST": "abc123", + "vanity/repo/abc123/LATEST": "002", + }) + }, + expectedBackup: &Backup{ + ID: "abc123", + Repository: &gitalypb.Repository{ + StorageName: "default", + RelativePath: "vanity/repo.git", + }, + ObjectFormat: "sha1", + Steps: []Step{ + { + BundlePath: "vanity/repo/abc123/001.bundle", + RefPath: "vanity/repo/abc123/001.refs", + CustomHooksPath: "vanity/repo/abc123/001.custom_hooks.tar", + }, + { + BundlePath: "vanity/repo/abc123/002.bundle", + RefPath: "vanity/repo/abc123/002.refs", + PreviousRefPath: "vanity/repo/abc123/001.refs", + CustomHooksPath: "vanity/repo/abc123/002.custom_hooks.tar", + }, + }, + }, + }, + } { + tc := tc + t.Run(tc.desc, func(t *testing.T) { + t.Parallel() + + ctx := testhelper.Context(t) + backupPath := testhelper.TempDir(t) + + tc.setup(t, ctx, backupPath) + + sink := NewFilesystemSink(backupPath) + var l Locator = PointerLocator{ + Sink: sink, + } + l = ManifestLocator{ + Sink: sink, + Fallback: l, + } + + backup, err := l.Find(ctx, tc.repo, tc.backupID) + require.NoError(t, err) + + require.Equal(t, tc.expectedBackup, backup) + }) + } +} |