fix: save ModTime properly in disk cache (#11522)

fix #11414
This commit is contained in:
Poorna Krishnamoorthy 2021-02-11 19:25:47 -08:00 committed by GitHub
parent 2a7b123895
commit 93fd248b52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 7 deletions

View File

@ -103,7 +103,6 @@ func (m *cacheMeta) ToObjectInfo(bucket, object string) (o ObjectInfo) {
} }
// We set file info only if its valid. // We set file info only if its valid.
o.ModTime = m.Stat.ModTime
o.Size = m.Stat.Size o.Size = m.Stat.Size
o.ETag = extractETag(m.Meta) o.ETag = extractETag(m.Meta)
o.ContentType = m.Meta["content-type"] o.ContentType = m.Meta["content-type"]
@ -122,6 +121,12 @@ func (m *cacheMeta) ToObjectInfo(bucket, object string) (o ObjectInfo) {
o.Expires = t.UTC() o.Expires = t.UTC()
} }
} }
if mtime, ok := m.Meta["last-modified"]; ok {
if t, e = time.Parse(http.TimeFormat, mtime); e == nil {
o.ModTime = t.UTC()
}
}
// etag/md5Sum has already been extracted. We need to // etag/md5Sum has already been extracted. We need to
// remove to avoid it from appearing as part of user-defined metadata // remove to avoid it from appearing as part of user-defined metadata
o.UserDefined = cleanMetadata(m.Meta) o.UserDefined = cleanMetadata(m.Meta)
@ -506,9 +511,7 @@ func (c *diskCache) statCache(ctx context.Context, cacheObjPath string) (meta *c
} }
// get metadata of part.1 if full file has been cached. // get metadata of part.1 if full file has been cached.
partial = true partial = true
fi, err := os.Stat(pathJoin(cacheObjPath, cacheDataFile)) if _, err := os.Stat(pathJoin(cacheObjPath, cacheDataFile)); err == nil {
if err == nil {
meta.Stat.ModTime = atime.Get(fi)
partial = false partial = false
} }
return meta, partial, meta.Hits, nil return meta, partial, meta.Hits, nil
@ -570,7 +573,6 @@ func (c *diskCache) saveMetadata(ctx context.Context, bucket, object string, met
} }
} }
m.Stat.Size = actualSize m.Stat.Size = actualSize
m.Stat.ModTime = UTCNow()
if !incHitsOnly { if !incHitsOnly {
// reset meta // reset meta
m.Meta = meta m.Meta = meta

View File

@ -200,6 +200,7 @@ func getMetadata(objInfo ObjectInfo) map[string]string {
if !objInfo.Expires.Equal(timeSentinel) { if !objInfo.Expires.Equal(timeSentinel) {
metadata["expires"] = objInfo.Expires.Format(http.TimeFormat) metadata["expires"] = objInfo.Expires.Format(http.TimeFormat)
} }
metadata["last-modified"] = objInfo.ModTime.Format(http.TimeFormat)
for k, v := range objInfo.UserDefined { for k, v := range objInfo.UserDefined {
metadata[k] = v metadata[k] = v
} }

View File

@ -18,6 +18,7 @@ package cmd
import ( import (
"testing" "testing"
"time"
) )
// Tests ToObjectInfo function. // Tests ToObjectInfo function.
@ -27,7 +28,7 @@ func TestCacheMetadataObjInfo(t *testing.T) {
if objInfo.Size != 0 { if objInfo.Size != 0 {
t.Fatal("Unexpected object info value for Size", objInfo.Size) t.Fatal("Unexpected object info value for Size", objInfo.Size)
} }
if !objInfo.ModTime.Equal(timeSentinel) { if !objInfo.ModTime.Equal(time.Time{}) {
t.Fatal("Unexpected object info value for ModTime ", objInfo.ModTime) t.Fatal("Unexpected object info value for ModTime ", objInfo.ModTime)
} }
if objInfo.IsDir { if objInfo.IsDir {

View File

@ -258,7 +258,7 @@ func cleanMetadata(metadata map[string]string) map[string]string {
// Remove STANDARD StorageClass // Remove STANDARD StorageClass
metadata = removeStandardStorageClass(metadata) metadata = removeStandardStorageClass(metadata)
// Clean meta etag keys 'md5Sum', 'etag', "expires", "x-amz-tagging". // Clean meta etag keys 'md5Sum', 'etag', "expires", "x-amz-tagging".
return cleanMetadataKeys(metadata, "md5Sum", "etag", "expires", xhttp.AmzObjectTagging) return cleanMetadataKeys(metadata, "md5Sum", "etag", "expires", xhttp.AmzObjectTagging, "last-modified")
} }
// Filter X-Amz-Storage-Class field only if it is set to STANDARD. // Filter X-Amz-Storage-Class field only if it is set to STANDARD.