From 9da4e8656cdc79dfc896a2d8709c3b46d76b5b4e Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Fri, 4 Dec 2026 03:44:48 +0000 Subject: [PATCH] filer: reproduce overwritten bytes lost by server-side copy Signed-off-by: Perfloop Agent --- ...iler_server_handlers_copy_manifest_test.go | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 weed/server/filer_server_handlers_copy_manifest_test.go diff --git a/weed/server/filer_server_handlers_copy_manifest_test.go b/weed/server/filer_server_handlers_copy_manifest_test.go new file mode 100644 index 0000000000..27305f5d27 --- /dev/null +++ b/weed/server/filer_server_handlers_copy_manifest_test.go @@ -0,0 +1,152 @@ +package weed_server + +import ( + "bytes" + "context" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/seaweedfs/seaweedfs/weed/cluster" + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/seaweedfs/weed/wdclient" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/protobuf/proto" +) + +type chunkCopyMaster struct { + master_pb.UnimplementedSeaweedServer + volumeURL string + nextID atomic.Uint64 +} + +func (m *chunkCopyMaster) KeepConnected(stream master_pb.Seaweed_KeepConnectedServer) error { + if _, err := stream.Recv(); err != nil { + return err + } + if err := stream.Send(&master_pb.KeepConnectedResponse{VolumeLocation: &master_pb.VolumeLocation{ + Url: m.volumeURL, PublicUrl: m.volumeURL, NewVids: []uint32{1}, + }}); err != nil { + return err + } + <-stream.Context().Done() + return nil +} + +func (m *chunkCopyMaster) Assign(context.Context, *master_pb.AssignRequest) (*master_pb.AssignResponse, error) { + return &master_pb.AssignResponse{Fid: fmt.Sprintf("1,%x00000001", 100+m.nextID.Add(1)), Location: &master_pb.Location{Url: m.volumeURL}, Count: 1}, nil +} + +func (m *chunkCopyMaster) LookupVolume(_ context.Context, req *master_pb.LookupVolumeRequest) (*master_pb.LookupVolumeResponse, error) { + resp := &master_pb.LookupVolumeResponse{} + for _, id := range req.VolumeOrFileIds { + resp.VolumeIdLocations = append(resp.VolumeIdLocations, &master_pb.LookupVolumeResponse_VolumeIdLocation{ + VolumeOrFileId: id, Locations: []*master_pb.Location{{Url: m.volumeURL, PublicUrl: m.volumeURL}}, + }) + } + return resp, nil +} + +func TestCopyChunkManifestPreservesOverwrites(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + old := &filer_pb.FileChunk{FileId: "1,0100000001", Size: 7, ModifiedTsNs: 1} + filer_pb.BeforeEntrySerialization([]*filer_pb.FileChunk{old}) + manifest, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: []*filer_pb.FileChunk{old}}) + if err != nil { + t.Fatal(err) + } + var mu sync.Mutex + chunks := map[string][]byte{"/1,0100000001": []byte("OLD old"), "/1,0200000001": []byte("NEW"), "/1,0300000001": manifest} + volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + switch r.Method { + case http.MethodGet: + data, ok := chunks[r.URL.Path] + if !ok { + http.NotFound(w, r) + return + } + http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(data)) + case http.MethodPut: + data, err := io.ReadAll(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + chunks[r.URL.Path] = data + w.WriteHeader(http.StatusCreated) + } + })) + t.Cleanup(volume.Close) + master := &chunkCopyMaster{volumeURL: strings.TrimPrefix(volume.URL, "http://")} + lis, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + grpcServer := grpc.NewServer() + master_pb.RegisterSeaweedServer(grpcServer, master) + go grpcServer.Serve(lis) + t.Cleanup(grpcServer.Stop) + _, port, err := net.SplitHostPort(lis.Addr().String()) + if err != nil { + t.Fatal(err) + } + dialOption := grpc.WithTransportCredentials(insecure.NewCredentials()) + mc := wdclient.NewMasterClient(dialOption, "", cluster.FilerType, "", "", "", + *pb.NewServiceDiscoveryFromMap(map[string]pb.ServerAddress{"m": pb.ServerAddress("127.0.0.1:0." + port)})) + go mc.KeepConnectedToMaster(ctx) + mc.WaitUntilConnected(ctx) + if ctx.Err() != nil { + t.Fatal(ctx.Err()) + } + + store := newRenameTestStore() + src := newFileEntry("/src", 1) + src.Chunks = []*filer_pb.FileChunk{ + {FileId: "1,0300000001", Size: old.Size, ModifiedTsNs: 1, IsChunkManifest: true}, + {FileId: "1,0200000001", Size: 3, ModifiedTsNs: 2}, + } + filer_pb.BeforeEntrySerialization(src.Chunks) + filer_pb.AfterEntryDeserialization(src.Chunks) + store.entries["/src"] = src + fs := &FilerServer{filer: newRenameTestFiler(t, store), option: &FilerOption{}, grpcDialOption: dialOption, + entryLockTable: util.NewLockTable[util.FullPath]()} + fs.filer.MasterClient = mc + read := func(entry *filer.Entry) string { + t.Helper() + var buf bytes.Buffer + if err := filer.StreamContent(mc, &buf, entry.Chunks, 0, int64(old.Size)); err != nil { + t.Fatal(err) + } + return buf.String() + } + if got := read(src); got != "NEW old" { + t.Fatalf("source reads %q, want NEW old", got) + } + rec := httptest.NewRecorder() + fs.PostHandler(rec, httptest.NewRequest(http.MethodPost, "/dst?cp.from=/src", http.NoBody).WithContext(ctx), 0) + if rec.Code != http.StatusNoContent { + t.Fatalf("copy: status %d, body %s", rec.Code, rec.Body.String()) + } + dst, err := fs.filer.FindEntry(ctx, "/dst") + if err != nil { + t.Fatal(err) + } + if got := read(dst); got != "NEW old" { + t.Fatalf("copied file reads %q, want NEW old", got) + } +}