1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2025-09-09 21:02:46 +02:00
seaweedfs/weed/mount/weedfs_file_lseek.go
Chris Lu 4af182f880
Context cancellation during reading range reading large files (#7093)
* context cancellation during reading range reading large files

* address comments

* cancellation for fuse read

* fix cancellation

* pass in context for each function to avoid racing condition

* Update reader_at_test.go

* remove dead code

* Update weed/filer/reader_at.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/filer/filechunk_group.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/filer/filechunk_group.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* address comments

* Update weed/mount/weedfs_file_read.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/mount/weedfs_file_lseek.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/mount/weedfs_file_read.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/filer/reader_at.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update weed/mount/weedfs_file_lseek.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* test cancellation

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-08-06 10:09:26 -07:00

84 lines
2.2 KiB
Go

package mount
import (
"context"
"syscall"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
// These are non-POSIX extensions
const (
SEEK_DATA uint32 = 3 // seek to next data after the offset
SEEK_HOLE uint32 = 4 // seek to next hole after the offset
ENXIO = fuse.Status(syscall.ENXIO)
)
// Lseek finds next data or hole segments after the specified offset
// See https://man7.org/linux/man-pages/man2/lseek.2.html
func (wfs *WFS) Lseek(cancel <-chan struct{}, in *fuse.LseekIn, out *fuse.LseekOut) fuse.Status {
// not a documented feature
if in.Padding != 0 {
return fuse.EINVAL
}
if in.Whence != SEEK_DATA && in.Whence != SEEK_HOLE {
return fuse.EINVAL
}
// file must exist
fh := wfs.GetHandle(FileHandleId(in.Fh))
if fh == nil {
return fuse.EBADF
}
// lock the file until the proper offset was calculated
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Lseek", fh.fh, util.SharedLock)
defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
fileSize := int64(filer.FileSize(fh.GetEntry().GetEntry()))
offset := max(int64(in.Offset), 0)
glog.V(4).Infof(
"Lseek %s fh %d in [%d,%d], whence %d",
fh.FullPath(), fh.fh, offset, fileSize, in.Whence,
)
// can neither seek beyond file nor seek to a hole at the end of the file with SEEK_DATA
if offset > fileSize {
return ENXIO
} else if in.Whence == SEEK_DATA && offset == fileSize {
return ENXIO
}
// Create a context that will be cancelled when the cancel channel receives a signal
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
select {
case <-cancel:
cancelFunc()
}
}()
// search chunks for the offset
found, offset := fh.entryChunkGroup.SearchChunks(ctx, offset, fileSize, in.Whence)
if found {
out.Offset = uint64(offset)
return fuse.OK
}
// in case we found no exact matches, we return the recommended fallbacks, that is:
// original offset for SEEK_DATA or end of file for an implicit hole
if in.Whence == SEEK_DATA {
out.Offset = in.Offset
} else {
out.Offset = uint64(fileSize)
}
return fuse.OK
}