2018-05-23 18:08:46 +08:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-28 02:52:26 +08:00
|
|
|
"context"
|
2018-05-23 18:08:46 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-05-28 02:52:26 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-12-30 16:51:44 +08:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
2018-07-22 08:39:10 +08:00
|
|
|
"net/http"
|
2018-05-24 16:22:37 +08:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-12-17 15:20:08 +08:00
|
|
|
"time"
|
2018-05-23 18:08:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileHandle struct {
|
|
|
|
// cache file has been written to
|
2018-05-29 03:30:17 +08:00
|
|
|
dirtyPages *ContinuousDirtyPages
|
2018-07-06 14:16:34 +08:00
|
|
|
contentType string
|
2018-09-22 15:11:46 +08:00
|
|
|
dirtyMetadata bool
|
|
|
|
handle uint64
|
2018-06-06 17:09:57 +08:00
|
|
|
|
2018-05-25 15:57:25 +08:00
|
|
|
f *File
|
|
|
|
RequestId fuse.RequestID // unique ID for request
|
|
|
|
NodeId fuse.NodeID // file or directory the request is about
|
|
|
|
Uid uint32 // user ID of process making request
|
|
|
|
Gid uint32 // group ID of process making request
|
2018-05-23 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
func newFileHandle(file *File, uid, gid uint32) *FileHandle {
|
|
|
|
return &FileHandle{
|
|
|
|
f: file,
|
|
|
|
dirtyPages: newDirtyPages(file),
|
|
|
|
Uid: uid,
|
|
|
|
Gid: gid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:08:46 +08:00
|
|
|
var _ = fs.Handle(&FileHandle{})
|
2018-05-28 02:52:26 +08:00
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
// var _ = fs.HandleReadAller(&FileHandle{})
|
|
|
|
var _ = fs.HandleReader(&FileHandle{})
|
2018-05-23 18:08:46 +08:00
|
|
|
var _ = fs.HandleFlusher(&FileHandle{})
|
|
|
|
var _ = fs.HandleWriter(&FileHandle{})
|
|
|
|
var _ = fs.HandleReleaser(&FileHandle{})
|
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
// this value should come from the filer instead of the old f
|
2018-09-22 15:11:46 +08:00
|
|
|
if len(fh.f.entry.Chunks) == 0 {
|
2018-09-24 01:01:00 +08:00
|
|
|
glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
|
|
|
|
return nil
|
2018-05-23 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
buff := make([]byte, req.Size)
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-12-30 16:51:44 +08:00
|
|
|
if fh.f.entryViewCache == nil {
|
|
|
|
fh.f.entryViewCache = filer2.NonOverlappingVisibleIntervals(fh.f.entry.Chunks)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, req.Offset, req.Size)
|
2018-05-24 16:22:37 +08:00
|
|
|
|
|
|
|
var vids []string
|
|
|
|
for _, chunkView := range chunkViews {
|
|
|
|
vids = append(vids, volumeId(chunkView.FileId))
|
|
|
|
}
|
|
|
|
|
|
|
|
vid2Locations := make(map[string]*filer_pb.Locations)
|
|
|
|
|
2018-05-25 15:57:25 +08:00
|
|
|
err := fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
glog.V(4).Infof("read fh lookup volume id locations: %v", vids)
|
|
|
|
resp, err := client.LookupVolume(ctx, &filer_pb.LookupVolumeRequest{
|
|
|
|
VolumeIds: vids,
|
|
|
|
})
|
2018-05-23 18:08:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
vid2Locations = resp.LocationsMap
|
2018-05-23 18:08:46 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2018-05-24 16:22:37 +08:00
|
|
|
if err != nil {
|
2018-05-29 13:45:52 +08:00
|
|
|
glog.V(4).Infof("%v/%v read fh lookup volume ids: %v", fh.f.dir.Path, fh.f.Name, err)
|
2018-05-24 16:22:37 +08:00
|
|
|
return fmt.Errorf("failed to lookup volume ids %v: %v", vids, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalRead int64
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, chunkView := range chunkViews {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(chunkView *filer2.ChunkView) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2018-05-29 13:45:52 +08:00
|
|
|
glog.V(4).Infof("read fh reading chunk: %+v", chunkView)
|
2018-05-24 16:22:37 +08:00
|
|
|
|
|
|
|
locations := vid2Locations[volumeId(chunkView.FileId)]
|
|
|
|
if locations == nil || len(locations.Locations) == 0 {
|
|
|
|
glog.V(0).Infof("failed to locate %s", chunkView.FileId)
|
|
|
|
err = fmt.Errorf("failed to locate %s", chunkView.FileId)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var n int64
|
|
|
|
n, err = util.ReadUrl(
|
|
|
|
fmt.Sprintf("http://%s/%s", locations.Locations[0].Url, chunkView.FileId),
|
|
|
|
chunkView.Offset,
|
|
|
|
int(chunkView.Size),
|
2018-12-07 17:57:55 +08:00
|
|
|
buff[chunkView.LogicOffset-req.Offset:chunkView.LogicOffset-req.Offset+int64(chunkView.Size)],
|
|
|
|
!chunkView.IsFullChunk)
|
2018-05-24 16:22:37 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
2018-05-25 15:57:25 +08:00
|
|
|
glog.V(0).Infof("%v/%v read http://%s/%v %v bytes: %v", fh.f.dir.Path, fh.f.Name, locations.Locations[0].Url, chunkView.FileId, n, err)
|
2018-05-24 16:22:37 +08:00
|
|
|
|
|
|
|
err = fmt.Errorf("failed to read http://%s/%s: %v",
|
|
|
|
locations.Locations[0].Url, chunkView.FileId, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:45:52 +08:00
|
|
|
glog.V(4).Infof("read fh read %d bytes: %+v", n, chunkView)
|
2018-05-24 16:22:37 +08:00
|
|
|
totalRead += n
|
|
|
|
|
|
|
|
}(chunkView)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
resp.Data = buff[:totalRead]
|
|
|
|
|
|
|
|
return err
|
2018-05-23 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the file handle
|
|
|
|
func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
2018-05-24 11:55:24 +08:00
|
|
|
|
2018-05-23 18:08:46 +08:00
|
|
|
// write the request to volume servers
|
2018-05-24 11:55:24 +08:00
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(4).Infof("%+v/%v write fh %d: [%d,%d)", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)))
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-05-31 13:02:21 +08:00
|
|
|
chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
|
2018-05-23 18:08:46 +08:00
|
|
|
if err != nil {
|
2018-09-08 04:11:43 +08:00
|
|
|
glog.Errorf("%+v/%v write fh %d: [%d,%d): %v", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)), err)
|
2018-05-29 03:30:17 +08:00
|
|
|
return fmt.Errorf("write %s/%s at [%d,%d): %v", fh.f.dir.Path, fh.f.Name, req.Offset, req.Offset+int64(len(req.Data)), err)
|
2018-05-23 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
2018-05-29 03:30:17 +08:00
|
|
|
resp.Size = len(req.Data)
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-05-31 11:24:57 +08:00
|
|
|
if req.Offset == 0 {
|
2018-07-06 14:16:34 +08:00
|
|
|
fh.contentType = http.DetectContentType(req.Data)
|
2018-05-31 11:24:57 +08:00
|
|
|
fh.dirtyMetadata = true
|
|
|
|
}
|
|
|
|
|
2018-05-31 13:02:21 +08:00
|
|
|
for _, chunk := range chunks {
|
2018-09-22 15:11:46 +08:00
|
|
|
fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
|
2018-12-30 16:51:44 +08:00
|
|
|
fh.f.entryViewCache = nil
|
2018-05-29 03:30:17 +08:00
|
|
|
glog.V(1).Infof("uploaded %s/%s to %s [%d,%d)", fh.f.dir.Path, fh.f.Name, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
|
|
|
|
fh.dirtyMetadata = true
|
|
|
|
}
|
2018-05-23 18:08:46 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
|
|
|
|
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
|
|
|
|
|
2018-12-28 19:27:48 +08:00
|
|
|
fh.dirtyPages.releaseResource()
|
|
|
|
|
2018-11-14 15:53:17 +08:00
|
|
|
fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-05-25 16:27:21 +08:00
|
|
|
fh.f.isOpen = false
|
|
|
|
|
2018-05-23 18:08:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush - experimenting with uploading at flush, this slows operations down till it has been
|
|
|
|
// completely flushed
|
|
|
|
func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
|
|
|
|
// fflush works at fh level
|
|
|
|
// send the data to the OS
|
2018-06-06 17:09:57 +08:00
|
|
|
glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-05-29 03:30:17 +08:00
|
|
|
chunk, err := fh.dirtyPages.FlushToStorage(ctx)
|
|
|
|
if err != nil {
|
2018-11-19 03:51:38 +08:00
|
|
|
glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
|
|
|
|
return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
|
|
|
if chunk != nil {
|
2018-09-22 15:11:46 +08:00
|
|
|
fh.f.entry.Chunks = append(fh.f.entry.Chunks, chunk)
|
2018-12-30 16:51:44 +08:00
|
|
|
fh.f.entryViewCache = nil
|
2018-05-29 03:30:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fh.dirtyMetadata {
|
2018-05-23 18:08:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
return fh.f.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-23 18:08:46 +08:00
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
if fh.f.entry.Attributes != nil {
|
|
|
|
fh.f.entry.Attributes.Mime = fh.contentType
|
|
|
|
fh.f.entry.Attributes.Uid = req.Uid
|
|
|
|
fh.f.entry.Attributes.Gid = req.Gid
|
2018-12-17 15:20:08 +08:00
|
|
|
fh.f.entry.Attributes.Mtime = time.Now().Unix()
|
|
|
|
fh.f.entry.Attributes.Crtime = time.Now().Unix()
|
|
|
|
fh.f.entry.Attributes.FileMode = uint32(0770)
|
2018-07-06 14:16:34 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
2018-05-25 15:57:25 +08:00
|
|
|
Directory: fh.f.dir.Path,
|
2018-09-22 15:11:46 +08:00
|
|
|
Entry: fh.f.entry,
|
2018-05-23 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
2018-09-22 15:11:46 +08:00
|
|
|
glog.V(1).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
|
|
|
|
for i, chunk := range fh.f.entry.Chunks {
|
2018-05-25 15:57:25 +08:00
|
|
|
glog.V(1).Infof("%s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
|
2018-05-25 14:20:26 +08:00
|
|
|
}
|
2018-09-22 15:11:46 +08:00
|
|
|
if _, err := client.CreateEntry(ctx, request); err != nil {
|
2018-05-23 18:08:46 +08:00
|
|
|
return fmt.Errorf("update fh: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2018-05-24 16:22:37 +08:00
|
|
|
|
|
|
|
func volumeId(fileId string) string {
|
|
|
|
lastCommaIndex := strings.LastIndex(fileId, ",")
|
|
|
|
if lastCommaIndex > 0 {
|
|
|
|
return fileId[:lastCommaIndex]
|
|
|
|
}
|
|
|
|
return fileId
|
|
|
|
}
|