export makeTagKV, fetchPrevSnapshotManifest (#1750)

## Type of change

- [x] 🐹 Trivial/Minor

## Issue(s)

* #1725
This commit is contained in:
Keepers 2022-12-09 12:34:29 -07:00 committed by GitHub
parent edd0708285
commit 8b2489b60c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 12 deletions

View File

@ -42,7 +42,13 @@ func serviceCatTag(p path.Path) string {
return p.Service().String() + p.Category().String()
}
func makeTagKV(k string) (string, string) {
// MakeTagKV normalizes the provided key to protect it from clobbering
// similarly named tags from non-user input (user inputs are still open
// to collisions amongst eachother).
// Returns the normalized Key plus a default value. If you're embedding a
// key-only tag, the returned default value msut be used instead of an
// empty string.
func MakeTagKV(k string) (string, string) {
return userTagPrefix + k, defaultTagValue
}
@ -53,12 +59,12 @@ func tagsFromStrings(oc *OwnersCats) map[string]string {
res := make(map[string]string, len(oc.ServiceCats)+len(oc.ResourceOwners))
for k := range oc.ServiceCats {
tk, tv := makeTagKV(k)
tk, tv := MakeTagKV(k)
res[tk] = tv
}
for k := range oc.ResourceOwners {
tk, tv := makeTagKV(k)
tk, tv := MakeTagKV(k)
res[tk] = tv
}
@ -181,13 +187,14 @@ func fetchPrevManifests(
return manifestsSinceLastComplete(mans), nil
}
// FetchPrevSnapshotManifests returns a set of manifests for complete and maybe
// fetchPrevSnapshotManifests returns a set of manifests for complete and maybe
// incomplete snapshots for the given (resource owner, service, category)
// tuples. Up to two manifests can be returned per tuple: one complete and one
// incomplete. An incomplete manifest may be returned if it is newer than the
// newest complete manifest for the tuple. Manifests are deduped such that if
// multiple tuples match the same manifest it will only be returned once.
func FetchPrevSnapshotManifests(
// External callers can access this via wrapper.FetchPrevSnapshotManifests().
func fetchPrevSnapshotManifests(
ctx context.Context,
sm snapshotManager,
oc *OwnersCats,
@ -199,10 +206,10 @@ func FetchPrevSnapshotManifests(
// we can pass in. Can be expanded to return more than the most recent
// snapshots, but may require more memory at runtime.
for serviceCat := range oc.ServiceCats {
serviceTagKey, serviceTagValue := makeTagKV(serviceCat)
serviceTagKey, serviceTagValue := MakeTagKV(serviceCat)
for resourceOwner := range oc.ResourceOwners {
resourceOwnerTagKey, resourceOwnerTagValue := makeTagKV(resourceOwner)
resourceOwnerTagKey, resourceOwnerTagValue := MakeTagKV(resourceOwner)
tags := map[string]string{
serviceTagKey: serviceTagValue,

View File

@ -79,7 +79,7 @@ func newManifestInfo(
structTags := make(map[string]struct{}, len(tags))
for _, t := range tags {
tk, _ := makeTagKV(t)
tk, _ := MakeTagKV(t)
structTags[tk] = struct{}{}
}
@ -442,7 +442,7 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshots() {
}
}
snaps := FetchPrevSnapshotManifests(ctx, msm, test.input)
snaps := fetchPrevSnapshotManifests(ctx, msm, test.input)
expected := make([]*snapshot.Manifest, 0, len(test.expectedIdxs))
for _, i := range test.expectedIdxs {
@ -532,7 +532,7 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshotsWorksWithErrors() {
},
}
snaps := FetchPrevSnapshotManifests(ctx, msm, input)
snaps := fetchPrevSnapshotManifests(ctx, msm, input)
// Only 1 snapshot should be chosen because the other two attempts fail.
// However, which one is returned is non-deterministic because maps are used.

View File

@ -548,7 +548,7 @@ func (w Wrapper) makeSnapshotWithRoot(
) (*BackupStats, error) {
var man *snapshot.Manifest
prevSnaps := FetchPrevSnapshotManifests(ctx, w.c, oc)
prevSnaps := fetchPrevSnapshotManifests(ctx, w.c, oc)
bc := &stats.ByteCounter{}
@ -782,3 +782,20 @@ func (w Wrapper) DeleteSnapshot(
return nil
}
// FetchPrevSnapshotManifests returns a set of manifests for complete and maybe
// incomplete snapshots for the given (resource owner, service, category)
// tuples. Up to two manifests can be returned per tuple: one complete and one
// incomplete. An incomplete manifest may be returned if it is newer than the
// newest complete manifest for the tuple. Manifests are deduped such that if
// multiple tuples match the same manifest it will only be returned once.
func (w Wrapper) FetchPrevSnapshotManifests(
ctx context.Context,
oc OwnersCats,
) ([]*snapshot.Manifest, error) {
if w.c == nil {
return nil, errors.WithStack(errNotConnected)
}
return fetchPrevSnapshotManifests(ctx, w.c, &oc), nil
}

View File

@ -859,7 +859,7 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() {
expectedTags := map[string]string{}
for _, k := range baseTagKeys {
tk, tv := makeTagKV(k)
tk, tv := MakeTagKV(k)
expectedTags[tk] = tv
}