mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
mount, filer: a content change clears the stored content MD5 (#13238)
* mount: a content change clears the stored content MD5 Attributes.Md5 is the whole content's MD5: the S3 and WebDAV ETags and WebDAV's oc:checksums report it. A write, a size change, a grown fallocate, an O_TRUNC open or a copy_file_range into the file kept it, so those reported the old content's MD5 and an ETag that did not change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: AppendToEntry clears the stored content MD5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * mount: copy_file_range clears the MD5 once it buffers bytes A cancelled copy or a later read error returned before the clear, leaving the bytes already buffered under the old MD5. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: an empty AppendToEntry keeps the content MD5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
484e16e30c
commit
e84c3f9ada
8 files changed
+155
No files matched your search
@@ -208,6 +208,9 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse
|
||||
truncNow := time.Now()
|
||||
entry.Attributes.Mtime = truncNow.Unix()
|
||||
entry.Attributes.MtimeNs = int32(truncNow.Nanosecond())
|
||||
if size != oldFileSize {
|
||||
entry.Attributes.Md5 = nil
|
||||
}
|
||||
entry.Attributes.FileSize = size
|
||||
if size == 0 {
|
||||
clearS3Encryption(entry)
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
//go:build !freebsd
|
||||
|
||||
package mount
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/go-fuse/v2/fuse"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/filer"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
|
||||
var storedContentMd5 = []byte("0123456789abcdef")
|
||||
|
||||
func openHandleWithMd5(t *testing.T, fileSize uint64) (*WFS, *FileHandle) {
|
||||
t.Helper()
|
||||
wfs, fh := newOpenFileHandle(t, fileSize)
|
||||
t.Cleanup(fh.dirtyPages.Destroy)
|
||||
fh.GetEntry().GetEntry().Attributes.Md5 = storedContentMd5
|
||||
return wfs, fh
|
||||
}
|
||||
|
||||
// The filer reports Attributes.Md5 as the content's MD5 (S3 and WebDAV
|
||||
// ETags, oc:checksums): a change of the bytes through the mount drops it.
|
||||
func TestContentChangeClearsMd5(t *testing.T) {
|
||||
t.Run("write", func(t *testing.T) {
|
||||
wfs, fh := openHandleWithMd5(t, 10)
|
||||
in := &fuse.WriteIn{Fh: uint64(fh.fh), Offset: 2, Size: 3}
|
||||
_, status := wfs.Write(nil, in, []byte("abc"))
|
||||
require.Equal(t, fuse.OK, status)
|
||||
require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5)
|
||||
})
|
||||
t.Run("fallocate past the end", func(t *testing.T) {
|
||||
wfs, fh := openHandleWithMd5(t, 10)
|
||||
require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 4096, Length: 4096}))
|
||||
require.Nil(t, fh.GetEntry().GetEntry().Attributes.Md5)
|
||||
})
|
||||
t.Run("fallocate inside the file", func(t *testing.T) {
|
||||
wfs, fh := openHandleWithMd5(t, 10)
|
||||
require.Equal(t, fuse.OK, wfs.Fallocate(nil, &fuse.FallocateIn{Fh: uint64(fh.fh), Offset: 0, Length: 1}))
|
||||
require.Equal(t, storedContentMd5, fh.GetEntry().GetEntry().Attributes.Md5)
|
||||
})
|
||||
t.Run("copy_file_range into the file", func(t *testing.T) {
|
||||
wfs, dst := openHandleWithMd5(t, 10)
|
||||
srcInode := wfs.inodeToPath.Lookup(util.FullPath("/src.txt"), 1, false, false, 0, true)
|
||||
src, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, srcInode, &filer_pb.Entry{
|
||||
Name: "src.txt",
|
||||
Attributes: &filer_pb.FuseAttributes{FileMode: 0100644, FileSize: 5, Inode: srcInode},
|
||||
Content: []byte("hello"),
|
||||
}, 0, 0)
|
||||
_, status := wfs.CopyFileRange(make(chan struct{}), &fuse.CopyFileRangeIn{
|
||||
FhIn: uint64(src.fh), FhOut: uint64(dst.fh), OffIn: 1, OffOut: 0, Len: 4,
|
||||
})
|
||||
require.Equal(t, fuse.OK, status)
|
||||
require.Nil(t, dst.GetEntry().GetEntry().Attributes.Md5)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTruncateClearsMd5(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
size uint64
|
||||
keepMd5 bool
|
||||
oTrunc bool
|
||||
}{
|
||||
{name: "shorter", size: 300},
|
||||
{name: "longer", size: 4096},
|
||||
{name: "same size", size: 768, keepMd5: true},
|
||||
{name: "open with O_TRUNC", size: 0, oTrunc: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
wfs := newInvalidateTestWFS(t)
|
||||
current := attributeAppendEntry(3)
|
||||
current.Attributes.Md5 = storedContentMd5
|
||||
fake := &attributeCASFiler{entry: proto.Clone(current).(*filer_pb.Entry)}
|
||||
startFakeFiler(t, wfs, fake)
|
||||
path := util.FullPath("/dir/file")
|
||||
wfs.inodeToPath.Lookup(util.FullPath("/dir"), 1, true, false, 0, false)
|
||||
wfs.inodeToPath.Lookup(path, 1, false, false, 42, true)
|
||||
wfs.inodeToPath.MarkChildrenCached(util.FullPath("/dir"))
|
||||
require.NoError(t, wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", current), 1))
|
||||
local := proto.Clone(current).(*filer_pb.Entry)
|
||||
wfs.mapPbIdFromFilerToLocal(local)
|
||||
fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, 42, local, 0, 0)
|
||||
fh.baseEntry.Store(proto.Clone(local).(*filer_pb.Entry))
|
||||
fh.RememberPath(path)
|
||||
t.Cleanup(fh.dirtyPages.Destroy)
|
||||
|
||||
if tc.oTrunc {
|
||||
require.Equal(t, fuse.OK, wfs.truncateEntry(context.Background(), path, proto.Clone(current).(*filer_pb.Entry)))
|
||||
} else {
|
||||
in := &fuse.SetAttrIn{}
|
||||
in.NodeId, in.Valid, in.Size = 42, fuse.FATTR_SIZE, tc.size
|
||||
require.Equal(t, fuse.OK, wfs.SetAttr(nil, in, &fuse.AttrOut{}))
|
||||
}
|
||||
|
||||
fake.mu.Lock()
|
||||
defer fake.mu.Unlock()
|
||||
require.Equal(t, tc.size, fake.entry.Attributes.FileSize)
|
||||
if tc.keepMd5 {
|
||||
require.Equal(t, storedContentMd5, fake.entry.Attributes.Md5)
|
||||
} else {
|
||||
require.Nil(t, fake.entry.Attributes.Md5)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -195,6 +195,7 @@ func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn)
|
||||
fhOut.pageWrites.Add(1)
|
||||
totalCopied += buffered
|
||||
fhOut.dirtyContent = true
|
||||
fhOut.entry.Attributes.Md5 = nil
|
||||
}
|
||||
if err != nil {
|
||||
glog.Errorf("AddPage error: %v", err)
|
||||
|
||||
@@ -62,6 +62,7 @@ func (wfs *WFS) Fallocate(cancel <-chan struct{}, in *fuse.FallocateIn) (code fu
|
||||
return writeErrorToFuseStatus(err)
|
||||
}
|
||||
entry.Attributes.FileSize = newFileSize
|
||||
entry.Attributes.Md5 = nil
|
||||
// The writes that fill the range will not grow the file, so they charge
|
||||
// nothing; the growth is counted here or the quota never sees it.
|
||||
wfs.AddUncommittedBytes(int64(newFileSize - oldFileSize))
|
||||
|
||||
@@ -660,6 +660,7 @@ func (wfs *WFS) truncateEntry(ctx context.Context, entryFullPath util.FullPath,
|
||||
next.Content = nil
|
||||
next.Chunks = nil
|
||||
next.Attributes.FileSize = 0
|
||||
next.Attributes.Md5 = nil
|
||||
clearS3Encryption(next)
|
||||
truncNow := time.Now()
|
||||
next.Attributes.Mtime = truncNow.Unix()
|
||||
|
||||
@@ -99,6 +99,7 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wr
|
||||
oldFileSize := int64(entry.Attributes.FileSize)
|
||||
newFileSize := max(offset+int64(len(data)), oldFileSize)
|
||||
entry.Attributes.FileSize = uint64(newFileSize)
|
||||
entry.Attributes.Md5 = nil
|
||||
|
||||
// POSIX: writes update mtime and ctime. Stamp the entry now so the
|
||||
// flush path persists the write time rather than overwriting any
|
||||
|
||||
@@ -1379,6 +1379,9 @@ func (fs *FilerServer) AppendToEntry(ctx context.Context, req *filer_pb.AppendTo
|
||||
}
|
||||
|
||||
entry.Chunks = append(entry.GetChunks(), req.Chunks...)
|
||||
if len(req.Chunks) > 0 {
|
||||
entry.Md5 = nil
|
||||
}
|
||||
so, err := fs.applyStorageDefaultsToEntry(ctx, entry)
|
||||
if err != nil {
|
||||
glog.WarningfCtx(ctx, "applyStorageDefaultsToEntry: %v", err)
|
||||
|
||||
@@ -383,3 +383,36 @@ func TestAppendToEntryAppendsAfterSparseTail(t *testing.T) {
|
||||
require.NoError(t, stream(&got))
|
||||
require.Equal(t, []byte("old-\x00\x00\x00\x00\x00\x00new-"), got.Bytes(), "appended bytes must follow the sparse tail")
|
||||
}
|
||||
|
||||
func TestAppendToEntryClearsContentMd5(t *testing.T) {
|
||||
md5 := []byte("0123456789abcdef")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
req *filer_pb.AppendToEntryRequest
|
||||
keepMd5 bool
|
||||
}{
|
||||
{name: "append", req: appendRepresentationRequest("/data", "append.txt")},
|
||||
{name: "empty append", req: &filer_pb.AppendToEntryRequest{Directory: "/data", EntryName: "append.txt"}, keepMd5: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fs, store := newAppendLockServer(t)
|
||||
close(store.release)
|
||||
entry := newFileEntry(appendRepresentationPath, 1)
|
||||
entry.Chunks = []*filer_pb.FileChunk{{FileId: "1,01637037d6", Size: 4}}
|
||||
entry.FileSize = 4
|
||||
entry.Md5 = md5
|
||||
store.entries[appendRepresentationPath] = entry
|
||||
|
||||
_, err := fs.AppendToEntry(context.Background(), tc.req)
|
||||
require.NoError(t, err)
|
||||
|
||||
stored, err := fs.filer.FindEntry(context.Background(), appendRepresentationPath)
|
||||
require.NoError(t, err)
|
||||
if tc.keepMd5 {
|
||||
require.Equal(t, md5, stored.Md5)
|
||||
} else {
|
||||
require.Nil(t, stored.Md5, "the MD5 of the content before the append")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user