mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* sftp: read downloads as the file's logical content Downloads read through filer.NewFileReader, which stops at the last stored byte: holes between chunks, a tail no chunk stores and the zeros after partial inline content came back missing, so sparse files downloaded short or with later bytes shifted. Read chunked files through filer.ChunkReadAt with one shared ReaderCache, set up as WebDAV's: it serves FileSize bytes, zero-fills what no chunk stores, resolves manifests, prefetches and re-looks-up moved volumes. Closing a handle only releases its stream, as S3 does. Inline content is read up to the file's size, then zeros. This replaces sftpd's own block cache, whose LRU helper is now unused. * sftp: read large downloads in sections, as the FUSE mount does ChunkReadAt walks the file's chunk views from the start on every read, so a sequential download of a file with many chunks costs time quadratic in its chunk count. Read through filer.ChunkGroup instead, the reader of a mount's file handle: it keeps chunks in 64 MiB sections, returns EOF at the file size for any offset, and holds every open file's buffers to one shared budget (the mount's default 256 MiB). * sftp: cache remote-only content on open instead of refusing it A file whose content only a remote store holds is copied into the cluster with CacheRemoteObjectToLocalCluster when it is opened for reading, as a mount's read and the filer's HTTP read do, and then read from the cached chunks. It is still refused when the remote store cannot be read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Denis Maier <denis@dAir.lab> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
6.3 KiB
Go
164 lines
6.3 KiB
Go
package sftpd
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"testing/iotest"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
// A download is FileSize bytes: holes and an unstored tail read as zeros,
|
|
// and inline content counts only up to the size.
|
|
func TestDownloadReadsLogicalContent(t *testing.T) {
|
|
rw := newRWTest(t, PermAll)
|
|
objects := map[string][]byte{"3,01": []byte("AAAAAAAAAA"), "3,02": []byte("BBBBBBBBBB")}
|
|
volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write(objects[r.URL.Path[1:]])
|
|
}))
|
|
t.Cleanup(volume.Close)
|
|
rw.fs.downloads = newDownloads(&SFTPServiceOptions{Filer: rw.fs.filerAddr, GrpcDialOption: rw.fs.grpcDialOption})
|
|
inline := func(content string, size uint64) *filer_pb.Entry {
|
|
return &filer_pb.Entry{Content: []byte(content), Attributes: &filer_pb.FuseAttributes{FileSize: size}}
|
|
}
|
|
z := func(n int) string { return string(make([]byte, n)) }
|
|
rw.fake.mu.Lock()
|
|
rw.fake.volume = volume.Listener.Addr().String()
|
|
files := map[string]*filer_pb.Entry{
|
|
"gap": chunkedEntry(30, fileChunk("3,01", 0, 10), fileChunk("3,02", 20, 10)),
|
|
"tail": chunkedEntry(15, fileChunk("3,01", 0, 10)),
|
|
"cut": inline("abcdef", 4),
|
|
"padded": inline("abc", 6),
|
|
}
|
|
for name, e := range files {
|
|
rw.fake.entries["/buckets/b/"+name] = e
|
|
}
|
|
rw.fake.entries["/buckets/b/remote"] = &filer_pb.Entry{Attributes: &filer_pb.FuseAttributes{FileSize: 5}, RemoteEntry: &filer_pb.RemoteEntry{RemoteSize: 5}}
|
|
rw.fake.mu.Unlock()
|
|
if f, err := rw.client.Open("/b/remote"); err == nil {
|
|
f.Close()
|
|
t.Error("open of content an unreachable remote store holds succeeded")
|
|
}
|
|
for name, want := range map[string]string{
|
|
"gap": "AAAAAAAAAA" + z(10) + "BBBBBBBBBB",
|
|
"tail": "AAAAAAAAAA" + z(5),
|
|
"cut": "abcd",
|
|
"padded": "abc" + z(3),
|
|
} {
|
|
f, err := rw.client.Open("/b/" + name)
|
|
if err != nil {
|
|
t.Fatalf("open %s: %v", name, err)
|
|
}
|
|
// One read of everything, then one-byte reads that start inside
|
|
// holes and inline content too.
|
|
for _, r := range []io.Reader{io.NewSectionReader(f, 0, 1<<20), iotest.OneByteReader(io.NewSectionReader(f, 0, 1<<20))} {
|
|
got, err := io.ReadAll(r)
|
|
if err != nil || !bytes.Equal(got, []byte(want)) {
|
|
t.Errorf("%s: read %q (err %v), want %q", name, got, err, want)
|
|
}
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
t.Errorf("%s: close: %v", name, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reads at or past the end, up to the largest offset, are EOF.
|
|
func TestDownloadEOFPastTheEnd(t *testing.T) {
|
|
rw := newRWTest(t, PermAll)
|
|
rw.fs.downloads = newDownloads(&SFTPServiceOptions{Filer: rw.fs.filerAddr, GrpcDialOption: rw.fs.grpcDialOption})
|
|
ra, err := rw.fs.newFileReaderAt(context.Background(), "/f", chunkedEntry(30, fileChunk("3,01", 0, 10)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ra.(io.Closer).Close()
|
|
for _, off := range []int64{30, 31, math.MaxInt64} {
|
|
if n, err := ra.ReadAt(make([]byte, 1), off); n != 0 || err != io.EOF {
|
|
t.Errorf("read at %d: n=%d err=%v, want EOF", off, n, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A read across the 64 MiB section boundary, after a section with no
|
|
// chunks, joins both sides; the tail past the last chunk is zeros.
|
|
func TestDownloadAcrossSections(t *testing.T) {
|
|
rw := newRWTest(t, PermAll)
|
|
objects := map[string][]byte{"3,01": []byte("AAAAAAAAAA"), "3,02": []byte("BBBBBBBBBB")}
|
|
volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write(objects[r.URL.Path[1:]])
|
|
}))
|
|
t.Cleanup(volume.Close)
|
|
rw.fake.mu.Lock()
|
|
rw.fake.volume = volume.Listener.Addr().String()
|
|
rw.fake.mu.Unlock()
|
|
rw.fs.downloads = newDownloads(&SFTPServiceOptions{Filer: rw.fs.filerAddr, GrpcDialOption: rw.fs.grpcDialOption})
|
|
const boundary = filer.SectionSize * 2 // the first two sections hold no chunk
|
|
ra, err := rw.fs.newFileReaderAt(context.Background(), "/f", chunkedEntry(boundary+15, fileChunk("3,01", boundary-10, 10), fileChunk("3,02", boundary, 10)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ra.(io.Closer).Close()
|
|
got := make([]byte, 30)
|
|
n, err := ra.ReadAt(got, boundary-10)
|
|
if want := "AAAAAAAAAABBBBBBBBBB\x00\x00\x00\x00\x00"; string(got[:n]) != want || err != io.EOF {
|
|
t.Errorf("read across the boundary = %q (err %v), want %q and EOF", got[:n], err, want)
|
|
}
|
|
n, err = ra.ReadAt(got[:10], 5)
|
|
if string(got[:n]) != string(make([]byte, 10)) || err != nil {
|
|
t.Errorf("read in an empty section = %q (err %v), want zeros", got[:n], err)
|
|
}
|
|
}
|
|
|
|
func TestInlineReaderRefusesNegativeOffset(t *testing.T) {
|
|
if _, err := (inlineReaderAt{[]byte("abc"), 3}).ReadAt(make([]byte, 1), -1); err == nil {
|
|
t.Error("read at a negative offset succeeded")
|
|
}
|
|
}
|
|
|
|
func (f *lockTestFiler) CacheRemoteObjectToLocalCluster(_ context.Context, req *filer_pb.CacheRemoteObjectToLocalClusterRequest) (*filer_pb.CacheRemoteObjectToLocalClusterResponse, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
key := req.Directory + "/" + req.Name
|
|
cached, ok := f.remote[key]
|
|
if !ok {
|
|
return nil, status.Error(codes.Unavailable, "remote store unreachable")
|
|
}
|
|
f.entries[key] = cached
|
|
return &filer_pb.CacheRemoteObjectToLocalClusterResponse{Entry: cached}, nil
|
|
}
|
|
|
|
// Content only a remote store holds is cached into the cluster on open, as
|
|
// a mount's read does, then read from the cached chunks.
|
|
func TestDownloadCachesRemoteOnlyContent(t *testing.T) {
|
|
rw := newRWTest(t, PermAll)
|
|
volume := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte("AAAAAAAAAA"))
|
|
}))
|
|
t.Cleanup(volume.Close)
|
|
rw.fs.downloads = newDownloads(&SFTPServiceOptions{Filer: rw.fs.filerAddr, GrpcDialOption: rw.fs.grpcDialOption})
|
|
remote := &filer_pb.RemoteEntry{RemoteSize: 10}
|
|
cached := chunkedEntry(10, fileChunk("3,01", 0, 10))
|
|
cached.RemoteEntry = remote
|
|
rw.fake.mu.Lock()
|
|
rw.fake.volume = volume.Listener.Addr().String()
|
|
rw.fake.entries["/buckets/b/remote"] = &filer_pb.Entry{Name: "remote", Attributes: &filer_pb.FuseAttributes{FileSize: 10, FileMode: 0o644}, RemoteEntry: remote}
|
|
rw.fake.remote = map[string]*filer_pb.Entry{"/buckets/b/remote": cached}
|
|
rw.fake.mu.Unlock()
|
|
f, err := rw.client.Open("/b/remote")
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
defer f.Close()
|
|
if got, err := io.ReadAll(f); err != nil || string(got) != "AAAAAAAAAA" {
|
|
t.Errorf("read %q (err %v), want the cached content", got, err)
|
|
}
|
|
}
|