Files
seaweedfs/weed/mount/weedfs_link_inline_test.go
T
bd1f24a09b mount: carry a file's inline content into the name a hard link adds (#13008)
* mount: carry a file's inline content into the name a hard link adds

A small file the filer keeps inside its entry (-saveToFilerLimit, or
?saveInside=true on the upload) has content and no chunks. To link it a
mount updates the source and creates the new name, and the filer writes
the link's shared record from each of the two. The create carried the
source's attributes, chunks and extended attributes but not its content,
so the record was left with a size and nothing behind it: both names read
zeros from then on, through the mounts and through the filer.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* mount: carry a remote-only file's RemoteEntry into the name a hard link adds

A file under a remote mount that is not cached yet has no chunks and no
content; its RemoteEntry is what sends a read to fetch the object. The
create of the new name left it out, so the hard link's shared record lost
it and both names read zeros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: cache a remote-only file before a hard link gives it a new name

The filer resolves a remote object from the name a read goes through, and
the new name has no object of its own, so a read through it failed until
the source name had been read. The link now has the filer cache the
object first, so every name shares its local chunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: cache a linked remote-only file through its open handle

The cache moved the stored entry off the base of a handle open on the
source, so the handle refused the link change and its next flush failed.
A handle with buffered writes now publishes them first, and the cache
goes through the handle as a read does, which moves its base along.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-11-25 17:28:56 -08:00

107 lines
4.0 KiB
Go

package mount
import (
"bytes"
"context"
"testing"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"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"
)
// A small file the filer keeps inside its entry (-saveToFilerLimit, or
// ?saveInside=true on the upload) has content and no chunks. The filer writes
// a hard link's shared record from each write of one of its names, so the
// create of the new name has to carry the content as the update of the source
// does. Left out, the record lost it, and both names read zeros from then on.
func TestLinkCarriesInlineContentToTheNewName(t *testing.T) {
wfs, _ := newCreateTestWFS(t)
content := []byte("kept inside the entry\n")
source := &filer_pb.Entry{
Name: "inline.txt",
Content: content,
Attributes: &filer_pb.FuseAttributes{
FileMode: 0o644,
FileSize: uint64(len(content)),
Crtime: 1,
Mtime: 1,
Uid: 99,
Gid: 100,
},
}
if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/", source), 0); err != nil {
t.Fatalf("InsertEntry: %v", err)
}
sourcePath := util.FullPath("/inline.txt")
sourceInode := wfs.inodeToPath.Lookup(sourcePath, source.Attributes.Crtime, false, false, 0, true)
doLink(t, wfs, sourceInode, "hardlink.txt")
// The test filer returns no event, so the mount caches each name as it
// sent it, and its cache keeps one record for a hard link's names as the
// filer does: a create without the content empties it for both.
for _, path := range []util.FullPath{sourcePath, "/hardlink.txt"} {
if got := cachedLink(t, wfs, path).Content; !bytes.Equal(got, content) {
t.Fatalf("%s has content %q after the link, want %q", path, got, content)
}
}
}
// A file under a remote mount that is not cached yet has no chunks and no
// content, and its remote object is keyed by its own path, which the new name
// does not resolve to. The link caches it first, so both names share local
// chunks and keep the RemoteEntry.
func TestLinkCachesARemoteOnlyFileForTheNewName(t *testing.T) {
wfs, server := newCreateTestWFS(t)
remote := &filer_pb.RemoteEntry{StorageName: "cloud1", RemoteMtime: 1, RemoteSize: 4096, RemoteETag: "etag"}
attributes := &filer_pb.FuseAttributes{
FileMode: 0o644,
FileSize: uint64(remote.RemoteSize),
Crtime: 1,
Mtime: 1,
Uid: 99,
Gid: 100,
}
source := &filer_pb.Entry{Name: "remote.bin", RemoteEntry: remote, Attributes: attributes}
if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/", source), 0); err != nil {
t.Fatalf("InsertEntry: %v", err)
}
chunk := &filer_pb.FileChunk{FileId: "3,01637037d6", Size: uint64(remote.RemoteSize), ModifiedTsNs: 1}
server.cachedRemote = &filer_pb.Entry{
Name: source.Name,
RemoteEntry: remote,
Attributes: proto.Clone(attributes).(*filer_pb.FuseAttributes),
Chunks: []*filer_pb.FileChunk{chunk},
}
sourcePath := util.FullPath("/remote.bin")
sourceInode := wfs.inodeToPath.Lookup(sourcePath, source.Attributes.Crtime, false, false, 0, true)
doLink(t, wfs, sourceInode, "hardlink.bin")
for _, path := range []util.FullPath{sourcePath, "/hardlink.bin"} {
got := cachedLink(t, wfs, path)
if !proto.Equal(got.Remote, remote) {
t.Fatalf("%s has remote entry %v after the link, want %v", path, got.Remote, remote)
}
if len(got.GetChunks()) != 1 || got.GetChunks()[0].GetFileIdString() != chunk.GetFileIdString() {
t.Fatalf("%s has chunks %v after the link, want the cached %v", path, got.GetChunks(), chunk)
}
}
}
func (s *createEntryTestServer) CacheRemoteObjectToLocalCluster(ctx context.Context, req *filer_pb.CacheRemoteObjectToLocalClusterRequest) (*filer_pb.CacheRemoteObjectToLocalClusterResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.cachedRemote == nil {
return nil, status.Errorf(codes.NotFound, "%s/%s", req.GetDirectory(), req.GetName())
}
return &filer_pb.CacheRemoteObjectToLocalClusterResponse{Entry: proto.Clone(s.cachedRemote).(*filer_pb.Entry)}, nil
}