From 1887c2527968f7e19a5afe1bbd585664f5890b97 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 5 Feb 2021 20:59:08 +0100 Subject: [PATCH] xl: Fix feeding NumVersions & SuccessorModTime to lifecycle (#11462) After recent refactor where lifecycle started to rely on ObjectInfo to make decisions, it turned out there are some issues calculating Successor Modtime and NumVersions, hence the lifecycle is not working as expected in a versioning bucket in some cases. This commit fixes the behavior. --- cmd/xl-storage-format-v2.go | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/cmd/xl-storage-format-v2.go b/cmd/xl-storage-format-v2.go index e5ad9e155..0229d1049 100644 --- a/cmd/xl-storage-format-v2.go +++ b/cmd/xl-storage-format-v2.go @@ -611,12 +611,13 @@ func (z xlMetaV2) TotalSize() int64 { // versions returns error for unexpected entries. // showPendingDeletes is set to true if ListVersions needs to list objects marked deleted // but waiting to be replicated -func (z xlMetaV2) ListVersions(volume, path string) (versions []FileInfo, modTime time.Time, err error) { - var latestModTime time.Time - var latestVersionID string +func (z xlMetaV2) ListVersions(volume, path string) ([]FileInfo, time.Time, error) { + var versions []FileInfo + var err error + for _, version := range z.Versions { if !version.Valid() { - return nil, latestModTime, errFileCorrupt + return nil, time.Time{}, errFileCorrupt } var fi FileInfo switch version.Type { @@ -628,27 +629,22 @@ func (z xlMetaV2) ListVersions(volume, path string) (versions []FileInfo, modTim fi, err = version.ObjectV1.ToFileInfo(volume, path) } if err != nil { - return nil, latestModTime, err - } - if fi.ModTime.After(latestModTime) { - latestModTime = fi.ModTime - latestVersionID = fi.VersionID + return nil, time.Time{}, err } versions = append(versions, fi) } - // We didn't find the version in delete markers so latest version - // is indeed one of the actual version of the object with data. + sort.Sort(versionsSorter(versions)) + for i := range versions { - if versions[i].VersionID != latestVersionID { - continue + versions[i].NumVersions = len(versions) + if i > 0 { + versions[i].SuccessorModTime = versions[i-1].ModTime } - versions[i].IsLatest = true - break } - sort.Sort(versionsSorter(versions)) - return versions, latestModTime, nil + versions[0].IsLatest = true + return versions, versions[0].ModTime, nil } func getModTimeFromVersion(v xlMetaV2Version) time.Time { @@ -744,8 +740,8 @@ findVersion: // A version is found, fill dynamic fields fi.IsLatest = i == 0 fi.NumVersions = len(z.Versions) - if i < len(orderedVersions)-1 { - fi.SuccessorModTime = getModTimeFromVersion(orderedVersions[i+1]) + if i > 0 { + fi.SuccessorModTime = getModTimeFromVersion(orderedVersions[i-1]) } return fi, nil }