Files
seaweedfs/weed/sftpd/sftp_file_reader.go
T
c46567084f sftp: read downloads as the file's logical content (#13355)
* 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>
2026-12-03 11:13:40 -08:00

129 lines
4.1 KiB
Go

package sftpd
import (
"context"
"fmt"
"io"
"math"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb"
filer_pb "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
)
// downloads is what all downloads share, as in the FUSE mount: volume
// lookups that can be invalidated, and one memory budget (the mount's
// default -readerCacheSizeMB) for every open file's read buffers.
type downloads struct {
filerClient *wdclient.FilerClient
budget *filer.ReaderCacheBudget
}
func newDownloads(options *SFTPServiceOptions) *downloads {
fc := wdclient.NewFilerClient([]pb.ServerAddress{options.Filer}, options.GrpcDialOption, options.DataCenter, &wdclient.FilerClientOption{
FilerTokenSource: security.FilerAdminToken,
})
return &downloads{fc, filer.NewReaderCacheBudget(256 << 20)}
}
// newFileReaderAt reads FileSize bytes of the file, with zeros where no
// chunk or inline content stores data.
func (fs *SftpServer) newFileReaderAt(ctx context.Context, absPath string, entry *filer_pb.Entry) (io.ReaderAt, error) {
if len(entry.Content) == 0 && entry.IsInRemoteOnly() {
var err error
if entry, err = fs.downloadRemoteEntry(ctx, absPath); err != nil {
return nil, err
}
}
size := int64(entry.GetAttributes().GetFileSize())
if len(entry.GetChunks()) == 0 {
return inlineReaderAt{entry.Content, size}, nil
}
if fs.downloads == nil {
return nil, fmt.Errorf("read %s: downloads are not set up", absPath)
}
// Like a mount's file handle: 64 MiB sections keep each read's chunk
// lookup short however large the file is.
dl := fs.downloads
lookup := dl.filerClient.GetLookupFileIdFunction()
// Resolve manifests here, under the request context and recursively.
chunks, _, err := filer.ResolveChunkManifest(ctx, lookup, entry.GetChunks(), 0, math.MaxInt64, dl.filerClient, true)
if err != nil {
return nil, fmt.Errorf("read %s: %w", absPath, err)
}
group, err := filer.NewChunkGroup(lookup, (*chunk_cache.TieredChunkCache)(nil), chunks, 0, dl.filerClient, nil, dl.budget)
if err != nil {
return nil, fmt.Errorf("read %s: %w", absPath, err)
}
return chunkReaderAt{ctx, group, size}, nil
}
// downloadRemoteEntry has the filer copy content only a remote store holds
// into the cluster, as a mount's read does, and returns the cached entry.
func (fs *SftpServer) downloadRemoteEntry(ctx context.Context, absPath string) (*filer_pb.Entry, error) {
dir, name := util.FullPath(absPath).DirAndName()
var entry *filer_pb.Entry
err := fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
resp, err := client.CacheRemoteObjectToLocalCluster(ctx, &filer_pb.CacheRemoteObjectToLocalClusterRequest{Directory: dir, Name: name})
entry = resp.GetEntry()
return err
})
if err != nil {
return nil, fmt.Errorf("download %s from its remote store: %w", absPath, err)
}
if entry == nil || entry.IsInRemoteOnly() {
return nil, fmt.Errorf("content of %s is not readable", absPath)
}
return entry, nil
}
type chunkReaderAt struct {
ctx context.Context
group *filer.ChunkGroup
size int64
}
func (r chunkReaderAt) ReadAt(p []byte, off int64) (int, error) {
if off < 0 {
return 0, fmt.Errorf("negative offset %d", off)
}
n, _, err := r.group.ReadDataAt(r.ctx, r.size, p, off)
if err == nil && n < len(p) {
if off+int64(n) < r.size {
return n, io.ErrUnexpectedEOF
}
err = io.EOF
}
return n, err
}
func (r chunkReaderAt) Close() error { return r.group.Close() }
// inlineReaderAt reads inline content up to size, then zeros.
type inlineReaderAt struct {
content []byte
size int64
}
func (r inlineReaderAt) ReadAt(p []byte, off int64) (int, error) {
if off < 0 {
return 0, fmt.Errorf("negative offset %d", off)
}
if off >= r.size {
return 0, io.EOF
}
n := int(min(int64(len(p)), r.size-off))
clear(p[:n])
if off < int64(len(r.content)) {
copy(p[:n], r.content[off:])
}
if n < len(p) {
return n, io.EOF
}
return n, nil
}