seaweedfs/weed/server/filer_grpc_server.go

201 lines
6.2 KiB
Go
Raw Normal View History

2018-05-08 16:59:43 +08:00
package weed_server
import (
"context"
"github.com/chrislusf/seaweedfs/weed/operation"
"github.com/chrislusf/seaweedfs/weed/util"
2018-05-10 14:18:02 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2018-05-13 15:49:59 +08:00
"fmt"
2018-05-14 14:56:16 +08:00
"github.com/chrislusf/seaweedfs/weed/filer2"
"path/filepath"
"github.com/chrislusf/seaweedfs/weed/glog"
"time"
"os"
2018-05-08 16:59:43 +08:00
)
2018-05-10 14:18:02 +08:00
func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
2018-05-08 16:59:43 +08:00
2018-05-14 14:56:16 +08:00
found, entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
2018-05-08 16:59:43 +08:00
if err != nil {
return nil, err
}
if !found {
2018-05-13 15:49:59 +08:00
return nil, fmt.Errorf("%s not found under %s", req.Name, req.Directory)
2018-05-08 16:59:43 +08:00
}
2018-05-10 14:18:02 +08:00
return &filer_pb.LookupDirectoryEntryResponse{
Entry: &filer_pb.Entry{
2018-05-08 16:59:43 +08:00
Name: req.Name,
2018-05-14 14:56:16 +08:00
IsDirectory: entry.IsDirectory(),
Chunks: entry.Chunks,
2018-05-08 16:59:43 +08:00
},
}, nil
}
2018-05-10 14:18:02 +08:00
func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
2018-05-08 16:59:43 +08:00
2018-05-14 14:56:16 +08:00
entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
2018-05-08 16:59:43 +08:00
if err != nil {
return nil, err
}
2018-05-10 14:18:02 +08:00
resp := &filer_pb.ListEntriesResponse{}
2018-05-14 14:56:16 +08:00
for _, entry := range entries {
2018-05-10 14:18:02 +08:00
resp.Entries = append(resp.Entries, &filer_pb.Entry{
2018-05-14 14:56:16 +08:00
Name: entry.Name(),
IsDirectory: entry.IsDirectory(),
Chunks: entry.Chunks,
Attributes: &filer_pb.FuseAttributes{
2018-05-21 08:06:09 +08:00
FileSize: filer2.TotalSize(entry.Chunks),
Mtime: entry.Mtime.Unix(),
Gid: entry.Gid,
Uid: entry.Uid,
FileMode: uint32(entry.Mode),
},
2018-05-08 16:59:43 +08:00
})
}
return resp, nil
}
2018-05-21 16:25:30 +08:00
func (fs *FilerServer) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
2018-05-08 16:59:43 +08:00
2018-05-10 14:18:02 +08:00
attributes := &filer_pb.FuseAttributes{}
2018-05-08 16:59:43 +08:00
2018-05-21 16:25:30 +08:00
fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
found, entry, err := fs.filer.FindEntry(fullpath)
2018-05-08 16:59:43 +08:00
if err != nil {
return nil, err
}
if !found {
attributes.FileSize = 0
return nil, fmt.Errorf("file %s not found", fullpath)
2018-05-08 16:59:43 +08:00
}
attributes.FileSize = filer2.TotalSize(entry.Chunks)
attributes.FileMode = uint32(entry.Mode)
attributes.Uid = entry.Uid
attributes.Gid = entry.Gid
attributes.Mtime = entry.Mtime.Unix()
glog.V(0).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
2018-05-21 16:25:30 +08:00
return &filer_pb.GetEntryAttributesResponse{
2018-05-08 16:59:43 +08:00
Attributes: attributes,
Chunks: entry.Chunks,
2018-05-08 16:59:43 +08:00
}, nil
}
2018-05-10 14:18:02 +08:00
func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
2018-05-08 16:59:43 +08:00
server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
if err != nil {
return nil, err
}
content, err := util.Get(server)
if err != nil {
return nil, err
}
2018-05-10 14:18:02 +08:00
return &filer_pb.GetFileContentResponse{
2018-05-08 16:59:43 +08:00
Content: content,
}, nil
}
func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
err = fs.filer.CreateEntry(&filer2.Entry{
FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
Attr: filer2.Attr{
Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
Mode: os.FileMode(req.Entry.Attributes.FileMode),
Uid: req.Entry.Attributes.Uid,
Gid: req.Entry.Attributes.Gid,
},
Chunks: req.Entry.Chunks,
})
if err == nil {
}
return &filer_pb.CreateEntryResponse{}, err
}
func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
fullpath := filepath.Join(req.Directory, req.Entry.Name)
found, entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
if err != nil {
return &filer_pb.UpdateEntryResponse{}, err
}
if !found {
return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("file not found: %s", fullpath)
}
// remove old chunks if not included in the new ones
unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
err = fs.filer.UpdateEntry(&filer2.Entry{
FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
Attr: filer2.Attr{
Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
Mode: os.FileMode(req.Entry.Attributes.FileMode),
Uid: req.Entry.Attributes.Uid,
Gid: req.Entry.Attributes.Gid,
},
Chunks: chunks,
})
if err == nil {
for _, garbage := range unusedChunks {
glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
}
for _, garbage := range garbages {
glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
}
}
return &filer_pb.UpdateEntryResponse{}, err
}
2018-05-10 14:18:02 +08:00
func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
2018-05-14 14:56:16 +08:00
entry, err := fs.filer.DeleteEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
if err == nil {
for _, chunk := range entry.Chunks {
if err = operation.DeleteFile(fs.getMasterNode(), chunk.FileId, fs.jwt(chunk.FileId)); err != nil {
glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
2018-05-14 14:56:16 +08:00
}
2018-05-08 16:59:43 +08:00
}
}
2018-05-14 15:17:22 +08:00
return &filer_pb.DeleteEntryResponse{}, err
2018-05-08 16:59:43 +08:00
}
func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
assignResult, err := operation.Assign(fs.master, &operation.VolumeAssignRequest{
Count: uint64(req.Count),
Replication: req.Replication,
Collection: req.Collection,
})
if err != nil {
return nil, fmt.Errorf("assign volume: %v", err)
}
if assignResult.Error != "" {
return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
}
return &filer_pb.AssignVolumeResponse{
FileId: assignResult.Fid,
Count: int32(assignResult.Count),
Url: assignResult.Url,
PublicUrl: assignResult.PublicUrl,
}, err
}